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

don't create IDs for headers by default #326

Closed
jonathanong opened this issue Dec 31, 2013 · 14 comments
Closed

don't create IDs for headers by default #326

jonathanong opened this issue Dec 31, 2013 · 14 comments

Comments

@jonathanong
Copy link

since we can create custom renderers now, i don't see the point of including this by default. the default renderer should be minimal and simple, and it would close plenty of issues surrounding this issue.

@Feder1co5oave
Copy link
Contributor

What issues? If you want to avoid id collisions, just use the headerPrefix option.
I find that headers with ID come handy when I need to reference different parts of the same document.

@jonathanong
Copy link
Author

because it's another option and an opinionated feature. i don't want it at all. easier if it wasn't supported.

@kennethormandy
Copy link

I agree with this. It’s nice to have the option, but I think it makes more sense that it’s opt-in.

@ChrisWren
Copy link
Contributor

I agree in 0.4.0 release it should be removed.

@Feder1co5oave
Copy link
Contributor

I'd like to share my implementation of the header renderer with ID creation:

myRenderer.heading = function(text, level, raw, options) {
    return '<h'
     + level
     +' id="'
     + options.headerPrefix
     + raw.toLowerCase().replace(/[^\wàùòèéì]+/g, '-').replace(/^\-+|\-+$/g, '')
     + '">'
     + text
     + '</h'
     + level
     + '>\n';
}

It is useful to keep letters with accents (I included the Italian ones) and removes any leading or trailing dashes:

$ bin/marked 
### `$ cat LICENSE`
<h3 id="cat-license-"><code>$ cat LICENSE</code></h3>

$ bin/marked 
### Definizione di probabilità assiomatica (Kolmogorov)
<h3 id="definizione-di-probabilità-assiomatica-kolmogorov">Definizione di probabilità assiomatica (Kolmogorov)</h3>

@ArmorDarks
Copy link

I also think it should be switched off by default, since it injects unexpected ids into markup and seems like pretty much opinionated option.

Besides, it doesn't work with non-latin headers, which results in quite horrible ids

@the0neWhoKnocks
Copy link

+1

It's far too easy to create issues on a page with dynamically generated ids. At the least, if it's not going to be removed, it should be automatically prefixed with marked- or md-. Something to set it apart from other ids that may already be on the page. If a plugin or framework is asserting it's opinions on a dev, it should at least do so transparently & in a non-conflicting manor.

Aside from that, and not being able to easily add new custom token rules (not just overriding current ones); this is some fine code and has saved me time implementing a new MD editor. So thank you for your efforts, they are appreciated.

@brunowego
Copy link

+1

2 similar comments
@fedesilvaponte
Copy link

+1

@Dafrok
Copy link

Dafrok commented Jun 17, 2017

+1

@luketeaford
Copy link

Would love this feature -- the ID should be optional because there are many scenarios where it is obtrusive.

@ahuglajbclajep
Copy link

This is easy to achieve.
If node >=4.0.0,

const marked = require('marked');

const renderer = new marked.Renderer();
renderer.heading = (text, level) => `<h${level}>${text}</h${level}>`;
marked.setOptions({renderer});

Have a nice Markdown life!

@joshbruce
Copy link
Member

See #956

If you are still interested in this, comment.

@alberto56
Copy link

Just sharing how I obtain IDs in the browser

            const renderer = new marked.Renderer();
            renderer.heading = function(text, level) {
              const id = text.text.toLowerCase().replace(/[^a-zA-Z0-9]+/g, '-');
              return `<h${text.depth} id="${id}">${text.text}</h${text.depth}>`;
            }
            marked.setOptions({renderer});

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