Skip to content

How to disable a feature of marked? #420

Closed

Description

@styfle

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?

Activity

  1. Feder1co5oave commented on May 15, 2014

    @Feder1co5oave
    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.

  2. styfle commented on May 23, 2014

    @styfle
    MemberAuthor

    @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?

  3. khellang commented on May 29, 2014

    @khellang

    👍 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?

  4. Feder1co5oave commented on May 31, 2014

    @Feder1co5oave
    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.

  5. styfle commented on Jun 5, 2014

    @styfle
    MemberAuthor

    @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!

  6. luketeaford commented on Dec 21, 2014

    @luketeaford

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

  7. suchipi commented on Nov 13, 2015

    @suchipi

    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.

  8. herrmannplatz commented on Feb 2, 2017

    @herrmannplatz

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

  9. joshbruce commented on Dec 23, 2017

    @joshbruce
    Member

    Closing to consolidate.

  10. KostyaTretyak commented on Jan 6, 2018

    @KostyaTretyak
    Contributor

    Have you tried that? What does not fit?

    var renderer = new marked.Renderer();
    renderer.heading = function (text, level) {
      return '#'.repeat(level) + ' ' + text;
    }
  11. mortenege commented on Feb 16, 2018

    @mortenege

    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'])
    
  12. joshbruce commented on Feb 16, 2018

    @joshbruce
    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.

  13. joshbruce commented on Feb 26, 2018

    @joshbruce
    Member

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

  14. Heath123 commented on Jul 3, 2020

    @Heath123

    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

  15. witherBattler commented on Aug 5, 2022

    @witherBattler

    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

  16. Heath123 commented on Aug 10, 2022

    @Heath123

    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

  17. UziTech commented on Aug 10, 2022

    @UziTech
    Member

    You should be able to use the tokenizer for this.

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

    This tells the tokenizer to never find headings.

  18. simon-marcus commented on Dec 15, 2023

    @simon-marcus

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions