Skip to content

Markdown configuration: marked extensions don't work #1874

Description

@dzylikecode

Bug Report

  • OS: windows
  • browser: Google Chrome 104.0.5112.102

In order to customize the parsing rules, I follow the advice of the document about Markdown configuration and read the marked documentation. However, when I wanna reproduce the example to add a custom syntax to generate <dl> description lists, it doesn't work.

Steps to reproduce

  • configure the docsify

    window.$docsify={markdown: function(marked,oldRenderer){letrenderer={// blockcode: function(code,infostring,escaped){returnoldRenderer.code.apply(this,arguments);},blockquote: function(quote){returnoldRenderer.blockquote.apply(this,arguments);},html: function(html){returnoldRenderer.html.apply(this,arguments);},heading: function(text,level,raw,slugger){returnoldRenderer.heading.apply(this,arguments);},hr: function(){returnoldRenderer.hr.apply(this,arguments);},list: function(body,ordered,start){returnoldRenderer.list.apply(this,arguments);},listitem: function(text,task,checked){returnoldRenderer.listitem.apply(this,arguments);},checkbox: function(checked){returnoldRenderer.checkbox.apply(this,arguments);},paragraph: function(text){returnoldRenderer.paragraph.apply(this,arguments);},table: function(header,body){returnoldRenderer.table.apply(this,arguments);},tablerow: function(content){returnoldRenderer.tablerow.apply(this,arguments);},tablecell: function(content,flags){returnoldRenderer.tablecell.apply(this,arguments);},// inlinestrong: function(text){returnoldRenderer.strong.apply(this,arguments);},em: function(text){returnoldRenderer.em.apply(this,arguments);},codespan: function(code){returnoldRenderer.codespan.apply(this,arguments);},br: function(){returnoldRenderer.br.apply(this,arguments);},del: function(text){returnoldRenderer.del.apply(this,arguments);},link: function(href,title,text){returnoldRenderer.link.apply(this,arguments);},image: function(href,title,text){returnoldRenderer.image.apply(this,arguments);},text: function(text){returnoldRenderer.text.apply(this,arguments);},};constdescriptionList={name: "descriptionList",level: "block",// Is this a block-level or inline-level tokenizer?start(src){returnsrc.match(/:[^:\n]/)?.index;},// Hint to Marked.js to stop and check for a matchtokenizer(src,tokens){construle=/^(?::[^:\n]+:[^:\n]*(?:\n|$))+/;// Regex for the complete token, anchor to string startconstmatch=rule.exec(src);if(match){consttoken={// Token to generatetype: "descriptionList",// Should match "name" aboveraw: match[0],// Text to consume from the sourcetext: match[0].trim(),// Additional custom propertiestokens: [],// Array where child inline tokens will be generated};this.lexer.inline(token.text,token.tokens);// Queue this data to be processed for inline tokensreturntoken;}},renderer(token){return`<dl>${this.parser.parseInline(token.tokens)}\n</dl>`;// parseInline to turn child tokens into HTML},};constdescription={name: "description",level: "inline",// Is this a block-level or inline-level tokenizer?start(src){returnsrc.match(/:/)?.index;},// Hint to Marked.js to stop and check for a matchtokenizer(src,tokens){construle=/^:([^:\n]+):([^:\n]*)(?:\n|$)/;// Regex for the complete token, anchor to string startconstmatch=rule.exec(src);if(match){return{// Token to generatetype: "description",// Should match "name" aboveraw: match[0],// Text to consume from the sourcedt: this.lexer.inlineTokens(match[1].trim()),// Additional custom properties, includingdd: this.lexer.inlineTokens(match[2].trim()),// any further-nested inline tokens};}},renderer(token){return`\n<dt>${this.parser.parseInline(token.dt)}</dt><dd>${this.parser.parseInline(token.dd)}</dd>`;},childTokens: ["dt","dd"],// Any child tokens to be visited by walkTokens};functionwalkTokens(token){// Post-processing on the completed token treeif(token.type==="strong"){token.text+=" walked";token.tokens=this.Lexer.lexInline(token.text);}}marked.use({extensions: [descriptionList,description],
    walkTokens,});// EQUIVALENT TO:marked.use({extensions: [descriptionList]});marked.use({extensions: [description]});marked.use({ walkTokens });returnmarked;},};
  • write in README.md file

    A Description List:
    : Topic 1 : Description 1
    : **Topic 2** : _Description 2_
    
  • open chrome to debug

What is current behaviour

  • extension does not work

    The output of the browser is as follows:

    <p>
    A Description List: : Topic 1 : Description 1 : <strong>Topic 2</strong> :
    <em>Description 2</em></p>

What is the expected behaviour

  • marked documentation says that the output will contain elements <dt> and <dd>

    <p>A Description List:</p><dl><dt>Topic 1</dt><dd>Description 1</dd><dt><strong>Topic 2 walked</strong></dt><dd><em>Description 2</em></dd></dl>

Other relevant information

  • Bug does still occur when all/other plugins are disabled?

  • Your OS:

  • Node.js version:

  • npm/yarn version:

  • Browser version:

  • Docsify version:

  • Docsify plugins:

Please create a reproducible sandbox

Edit 307qqv236

Mention the docsify version in which this bug was not present (if any)

Activity

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