Marked: Overriding tokenizer bug??

Created on 24 Apr 2020  路  4Comments  路  Source: markedjs/marked

Thank you for your wonderful library.

I wanna override default codespan tokenizer to include LaTeX same as official documents.
Strictly, I wanna change codespan regexp from /^(`+)([^`]|[^`][\s\S]*?[^`])\1(?!`)/ to /^([`$]+)([^`$]|[^`$][\s\S]*\?[^`$])\1(?![`$])/. I did that like below.

// Override function
const tokenizer = {
  codespan(src) {
    const match = src.match(/^([`$]+)([^`$]|[^`$][\s\S]*?[^`$])\1(?![`$])/);
    if (match) {
      return {
        type: 'codespan',
        raw: match[0],
        text: match[2].trim()
      };
    }
    // return nothing not to use original codespan tokenizer
  }
};
marked.use({ tokenizer });

This works well for ` but doesn't work when $ is not at the beginning of the line.
Like this,

> marked('a`b`')
> <p>a<code>b</code></p> 
OK!

> marked('$ab$')
> <p><code>ab</code></p>
OK!

> marked('a$b$')
> <p>a$b$</p>
Not working!

Am I wrong somewhere? Or is this a bug??

For your information I checked the regexp works correctly in the match function.

> '$c*d$'.match(/^([`$]+)([^`$]|[^`$][\s\S]*?[^`$])\1(?![`$])/)
> ["$c*d$", "$", "c*d"] 

> '`c*d`'.match(/^([`$]+)([^`$]|[^`$][\s\S]*?[^`$])\1(?![`$])/)
> ["`c*d`", "`", "c*d"]
question

All 4 comments

You probably have to override the inlineText to stop when it sees a $.

Right now inlineText matches a$b$ but should only be matching a and leaving $b$ for the codespan to check

Thank you very much for your quick response.
I overrode inlineText token. This works fine.
Is this what you mean?? To tell you the truth, I don' t understand how this works...

inlineText(src, inRawBlock, smartypants) {
  const cap = src.match(/^([`$]+|[^`$])(?:[\s\S]*?(?:(?=[\\<!\[`$*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/);
    if (cap) {
      var text;
      if (inRawBlock) {
        text = this.options.sanitize ? this.options.sanitizer ? this.options.sanitizer(cap[0]) : cap[0] : cap[0];
      } else {
        text = (this.options.smartypants ? smartypants(cap[0]) : cap[0]);
      }
      return {
        type: 'text',
        raw: cap[0],
        text: text
      };
   }
}

Ya if it works for you it looks good 馃憤

I cannot say thank you enough.
THANK YOU!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chunhei2008 picture chunhei2008  路  3Comments

FireflyAndStars picture FireflyAndStars  路  3Comments

elennaro picture elennaro  路  4Comments

UziTech picture UziTech  路  4Comments

james4388 picture james4388  路  3Comments