yakivmospan / github-wikito-converter

Generate HTML & PDF documentation from Github wiki or any other markdown-based wiki.
Other
293 stars 52 forks source link

Problems with anchors between .md files #42

Open pbaret opened 5 years ago

pbaret commented 5 years ago

I just found a bug in this package with anchors.

Let's say I have two md files in my wiki that I want to convert to html.

File1.md

# Title 1 of file 1

... content 1 ...

# Title 2 of file 1

... content 2 ...

GitHub automatically sets up anchors for the titles that I can reference in other wiki files to have a direct link.

File2.md

Here I can put a [Reference](File1#title-1-of-file-1) linked to title2 of file 1.

The thing is that when generating the html, because there is only one html page, this doesn't work. The reference put in the link in the html in my example is #File1title-1-of-file-1 instead of the expected #title-1-of-file-1

pbaret commented 5 years ago

Suggested quick fix:

in helpers.js

  _createClass(Helpers, null, [{
    key: 'getPageIdFromFilenameOrLink',
    value: function getPageIdFromFilenameOrLink(filename) {
      var base = path.basename(filename);
      if (base.substr(-3) === '.md') {
        base = base.substr(0, base.length - 3);
      }
      var sp = base.split('#') // the problem comes from #PageId#AnchorId
      if (sp.length == 2)
      {
          base = sp[1]
      }
      return base.replace(/([^a-z0-9\-_~.]+)/gi, '');
    }
  }]);

Special case for TOC links in mardown.js line 105

key: 'convertTocMarkdownString',
    value: function convertTocMarkdownString(markdown) {        
        markdown = markdown.replace(/ *\([^)]*\) */g, function (allPattern, link) {
            var sp = allPattern.split('#');
            if (sp.length == 2)
            {
                allPattern = '(#' + sp[1]
            }

            return allPattern;
        });

      return {
        tocHtml: this.convertMarkdownString(markdown, this.tocRenderer),
        tocItems: this.tocItems
      };

I precise I am not a JS developper nor familiar with node.js so this is really a quick tuning fix that worked for after digging into the sources.