Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with anchors between .md files #42

Open
pbaret opened this issue Jan 10, 2019 · 1 comment
Open

Problems with anchors between .md files #42

pbaret opened this issue Jan 10, 2019 · 1 comment

Comments

@pbaret
Copy link

pbaret commented Jan 10, 2019

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
Copy link
Author

pbaret commented Jan 10, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant