You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you just need to hide them, replace the heading renderer with an empty function.
If you want to keep the hashes in the final output, you can modify the source, simply replacing the regex for headings with noop.
But like you said, the hash is removed. Modifying the source is not desirable.
Is it possible to override the lexer/parser so that hash is not recognized as a header?
Reacted by Yann Brelière, Xge, joakim, Kim Tae Wan and Daniil Pankov
You create a lexer, disable the heading regex, lex the source, and then parse the tokens with the default parser. In the end, you get a plain paragraph with the text, including the hash.
Reacted by Alexander Ivashkin, Florian Nagel, Steven, Kiryl, Charles Harris, Yann Brelière, Ben Murden, Maksim Zaborovskiy, Natt Nguyen, Nhannht and 1 more
I had similar issues and I solved it like this. Basically changing all rules for non-whitelisted elements into noop. Maybe this could be added to setOptions in the future...
var loadMarkdown = (whitelist) => {
var marked = require('marked')
var lexer = (marked, whitelist) => {
var list = ['code', 'blockquote', 'html', 'heading', 'hr', 'list', 'listitem', 'paragraph', 'table', 'tablerow', 'tablecell', 'strong', 'em', 'codespan', 'br', 'del', 'link', 'image', 'text']
var blacklist = list.filter((item) => { return whitelist.indexOf(item) < 0 })
var lexer = new marked.Lexer()
for (let i in blacklist) {
var key = blacklist[i]
if (!lexer.rules[key]) continue
lexer.rules[key].exec = () => {} // noop
}
}
marked.lexer = lexer(marked, whitelist)
return marked
}
var marked = loadMarkdown(['paragraph', 'text', 'br', 'em', 'list', 'listitem', 'strong'])
Back to @styfle's initial post, I would like Marked to get out of the header id business altogether: #1043 - not part of Markdown, not really part of the specs, way easier to do as a second pass, comes with all sorts of other issues.
You create a lexer, disable the heading regex, lex the source, and then parse the tokens with the default parser. In the end, you get a plain paragraph with the text, including the hash.
For anyone looking at this now, you need to do this:
You create a lexer, disable the heading regex, lex the source, and then parse the tokens with the default parser. In the end, you get a plain paragraph with the text, including the hash.
For anyone looking at this now, you need to do this:
You create a lexer, disable the heading regex, lex the source, and then parse the tokens with the default parser. In the end, you get a plain paragraph with the text, including the hash.
For anyone looking at this now, you need to do this:
Activity
Feder1co5oave commentedon May 15, 2014
If you just need to hide them, replace the heading renderer with an empty function.
If you want to keep the hashes in the final output, you can modify the source, simply replacing the regex for headings with
noop.styfle commentedon May 23, 2014
@Feder1co5oave I was able to disable headers with the following:
But like you said, the hash is removed. Modifying the source is not desirable.
Is it possible to override the lexer/parser so that hash is not recognized as a header?
khellang commentedon May 29, 2014
👍 for this. I've seen it mentioned in other issues as well, like #21 and maybe #413.
Is there a way to set the
noopwithout modifying the source?Feder1co5oave commentedon May 31, 2014
I think I found a way. You've got to play a bit with a Lexer object:
You create a lexer, disable the
headingregex, lex the source, and then parse the tokens with the default parser. In the end, you get a plain paragraph with the text, including the hash.styfle commentedon Jun 5, 2014
@Feder1co5oave This works very well! Much better than my solution of creating a regex that would never match anything.
Yours will probably perform better because it is effectively a noop.
Thanks!
luketeaford commentedon Dec 21, 2014
I would love the option to turn this off with a configuration option like Kramdown has.
suchipi commentedon Nov 13, 2015
was looking to accomplish the same thing. thanks @Feder1co5oave for a solution. It'd be nice if we could disable each of the rules in the future.
herrmannplatz commentedon Feb 2, 2017
@Feder1co5oave but this only applies to block rules not to inline rules?
joshbruce commentedon Dec 23, 2017
Closing to consolidate.
KostyaTretyak commentedon Jan 6, 2018
Have you tried that? What does not fit?
mortenege commentedon Feb 16, 2018
I had similar issues and I solved it like this. Basically changing all rules for non-whitelisted elements into
noop. Maybe this could be added to setOptions in the future...joshbruce commentedon Feb 16, 2018
Back to @styfle's initial post, I would like Marked to get out of the header id business altogether: #1043 - not part of Markdown, not really part of the specs, way easier to do as a second pass, comes with all sorts of other issues.
joshbruce commentedon Feb 26, 2018
Ps. We also have it documented in our Renderer extension example on how to do it. So, yeah, #1043.
Heath123 commentedon Jul 3, 2020
For anyone looking at this now, you need to do this:
Not sure if this is clean (I should probably use the tokeniser directly?) but it works
witherBattler commentedon Aug 5, 2022
this doesn't affect the result
Heath123 commentedon Aug 10, 2022
Yes it does, I just tested it
UziTech commentedon Aug 10, 2022
You should be able to use the tokenizer for this.
This tells the tokenizer to never find headings.
simon-marcus commentedon Dec 15, 2023
Thanks yes—this seems like the correct approach, but note that you should return
undefinedto avoid type errors if using TypeScript. For example: