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

links replacing # with h1 due to markdown #100

Open
roskelld opened this issue Mar 9, 2019 · 4 comments
Open

links replacing # with h1 due to markdown #100

roskelld opened this issue Mar 9, 2019 · 4 comments

Comments

@roskelld
Copy link

roskelld commented Mar 9, 2019

I was looking for a solution to add blog posts to my GitHub site and here we are.

When testing out my own code and the hosted demo code I noticed that URLs with a # (so, all hosted pages) are replaced by <h1> in the rendered URL.

Example:
Go to the demo post
http://chris-diana.com/cms.js/demo/#/posts/2017-08-20-another-example-post

The link in the body text goes to:
http://chris-diana.com/cms.js/demo/<h1>

I'm guessing that the markdown https://github.com/chrisdiana/cms.js/blob/develop/src/markdown.js is picking up the # as a heading.

// headers - fix link anchor tag regex
{regex: /(#+)(.*)/g, replacement: (text, chars, content) => {
        var level = chars.length;
        return '<h' + level + '>' + content.trim() + '</h' + level + '>';
}},
@roskelld
Copy link
Author

roskelld commented Mar 9, 2019

I just noticed the comment in the code

// headers - fix link anchor tag regex

I guess this is a known issue

@roskelld
Copy link
Author

roskelld commented Mar 9, 2019

EDIT: SCRATCH THAT I THINK I MISUNDERSTOOD HOW THE CODE WORKS WHEN GENERATING HEADERS

I'm no expert on regex, but I tested this out and it works.

// headers - fix link anchor tag regex 
{regex: /(#+)([^\/])(.*)/g, replacement: (text, chars, content) => { 
 var level = chars.length; 
 return '<h' + level + '>' + content.trim() + '</h' + level + '>'; 
}},

It tests that there's no / following the first #

Here's a test case
regexr.com/49sbu

@raygit83
Copy link

Hi roskelld,

I'm no expert on regex, but I tested this out and it works.

// headers - fix link anchor tag regex 
{regex: /(#+)([^\/])(.*)/g, replacement: (text, chars, content) => { 
 var level = chars.length; 
 return '<h' + level + '>' + content.trim() + '</h' + level + '>'; 
}},

It tests that there's no / following the first #

This code works in filtering # in url links, however it also breaks the detection of regular headings on page markdown articles.

Here's an alternative regex, that works in both cases:

regex: /(#+)(?!\/)(.*)/g

This one assumes that a # preceding a url is directly followed by a / which makes sense in the cms.js environment (e.g. /#/pages/somearticle)

@bjalon
Copy link

bjalon commented Dec 13, 2019

Great, it works just find... You may can propose it as pull request ?

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

3 participants