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

How to disable a feature of marked? #420

Closed
styfle opened this issue May 13, 2014 · 18 comments
Closed

How to disable a feature of marked? #420

styfle opened this issue May 13, 2014 · 18 comments

Comments

@styfle
Copy link
Member

styfle commented May 13, 2014

I would like to disable the hash headers.

# Header Name => <h1>Header Name</h1>

Is this possible to turn off this feature using the current API or does it requiring modifying the source?

@Feder1co5oave
Copy link
Contributor

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

styfle commented May 23, 2014

@Feder1co5oave I was able to disable headers with the following:

var renderer = new marked.Renderer();
renderer.heading = function (text, level) {
  return text;
}

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

👍 for this. I've seen it mentioned in other issues as well, like #21 and maybe #413.

Is there a way to set the noop without modifying the source?

@Feder1co5oave
Copy link
Contributor

I think I found a way. You've got to play a bit with a Lexer object:

> var marked = require('marked'),
... options = { /* ... */ },
... lexer = new marked.Lexer(options);
undefined
> lexer.rules.heading = { exec: function() {} };
{ exec: [Function] }
> marked.parser(lexer.lex('# heading?'), options)
'<p># heading?</p>\n'

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.

@styfle
Copy link
Member Author

styfle commented Jun 5, 2014

@Feder1co5oave This works very well! Much better than my solution of creating a regex that would never match anything.

lexer.rules.heading = /(a^) (a^) (a^)/;

Yours will probably perform better because it is effectively a noop.
Thanks!

@luketeaford
Copy link

I would love the option to turn this off with a configuration option like Kramdown has.

@suchipi
Copy link

suchipi commented 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
Copy link

@Feder1co5oave but this only applies to block rules not to inline rules?

@joshbruce
Copy link
Member

Closing to consolidate.

@KostyaTretyak
Copy link
Contributor

Have you tried that? What does not fit?

var renderer = new marked.Renderer();
renderer.heading = function (text, level) {
  return '#'.repeat(level) + ' ' + text;
}

@mortenege
Copy link

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'])

@joshbruce
Copy link
Member

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

Ps. We also have it documented in our Renderer extension example on how to do it. So, yeah, #1043.

@Heath123
Copy link

Heath123 commented Jul 3, 2020

I think I found a way. You've got to play a bit with a Lexer object:

> var marked = require('marked'),
... options = { /* ... */ },
... lexer = new marked.Lexer(options);
undefined
> lexer.rules.heading = { exec: function() {} };
{ exec: [Function] }
> marked.parser(lexer.lex('# heading?'), options)
'<p># heading?</p>\n'

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:

var marked = require('marked'),
  options = { /* ... */ },
  lexer = new marked.Lexer(options);

console.log(lexer)
lexer.tokenizer.rules.block.heading = { exec: function() {} }

marked.parser(lexer.lex('# heading?'), options)

Not sure if this is clean (I should probably use the tokeniser directly?) but it works

@witherBattler
Copy link

I think I found a way. You've got to play a bit with a Lexer object:

> var marked = require('marked'),
... options = { /* ... */ },
... lexer = new marked.Lexer(options);
undefined
> lexer.rules.heading = { exec: function() {} };
{ exec: [Function] }
> marked.parser(lexer.lex('# heading?'), options)
'<p># heading?</p>\n'

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:

var marked = require('marked'),
  options = { /* ... */ },
  lexer = new marked.Lexer(options);

console.log(lexer)
lexer.tokenizer.rules.block.heading = { exec: function() {} }

marked.parser(lexer.lex('# heading?'), options)

Not sure if this is clean (I should probably use the tokeniser directly?) but it works

this doesn't affect the result

@Heath123
Copy link

Heath123 commented Aug 10, 2022

I think I found a way. You've got to play a bit with a Lexer object:

> var marked = require('marked'),
... options = { /* ... */ },
... lexer = new marked.Lexer(options);
undefined
> lexer.rules.heading = { exec: function() {} };
{ exec: [Function] }
> marked.parser(lexer.lex('# heading?'), options)
'<p># heading?</p>\n'

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:

var marked = require('marked'),
  options = { /* ... */ },
  lexer = new marked.Lexer(options);

console.log(lexer)
lexer.tokenizer.rules.block.heading = { exec: function() {} }

marked.parser(lexer.lex('# heading?'), options)

Not sure if this is clean (I should probably use the tokeniser directly?) but it works

this doesn't affect the result

Yes it does, I just tested it

image

image

@UziTech
Copy link
Member

UziTech commented Aug 10, 2022

You should be able to use the tokenizer for this.

marked.use({
  tokenizer: {
    heading() {}
  }
});

This tells the tokenizer to never find headings.

@simon-marcus
Copy link

You should be able to use the tokenizer for this.

marked.use({
  tokenizer: {
    heading() {}
  }
});

This tells the tokenizer to never find headings.

Thanks yes—this seems like the correct approach, but note that you should return undefined to avoid type errors if using TypeScript. For example:

marked.use({
        tokenizer: {
            url() { return undefined },
            autolink() { return undefined },
            html() { return undefined },
            link() { return undefined },
            reflink() { return undefined },
        }
    })

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