Skip to content
\n

When using the current HTML tokenizer, it submits two tokens here for the HTML renderer: <cite>, </cite>
\nI want to be able to recognize <cite>marked</cite> as a single token.

\n

My current approach is this

\n
export class CiteTokenizer extends Tokenizer {\n  constructor(options?: MarkedOptions) {\n    super(options);\n  }\n\n  override html(src: string): Tokens.HTML | undefined {\n    const match = /^<cite>([\\s\\S]+?)<\\/cite>/.exec(src);\n\n    if (match) {\n      return {\n        type: 'html',\n        pre: false,\n        text: match[0],\n        block: false,\n        raw: match[0],\n      };\n    }\n\n    return super.html(src);\n  }\n}
\n

This does work for single lines of cite elements, but not if other text is in front of it. No matter what I tried so far, I did not succeed in implementing such behavior.

\n

Is this even possible by implementing a tokenizer, or is a different way a better approach?

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

You will need to write a custom extension.

\n

Something like:

\n
const citeExtension = {\n  name: 'cite',\n  level: 'inline',\n  start(src) { return src.indexOf(\"<cite\"); },\n  tokenizer(src, tokens) {\n    const rule = /^<cite>([\\s\\S]+?)<\\/cite>/;\n    const match = rule.exec(src);\n    if (match) {\n      return {\n        type: 'html',\n        pre: false,\n        text: match[0],\n        block: false,\n        raw: match[0],\n      };\n    }\n  }\n};
","upvoteCount":2,"url":"https://github.com/markedjs/marked/discussions/3306#discussioncomment-9606718"}}}
Discussion options

You must be logged in to vote

You will need to write a custom extension.

Something like:

const citeExtension = {
  name: 'cite',
  level: 'inline',
  start(src) { return src.indexOf("<cite"); },
  tokenizer(src, tokens) {
    const rule = /^<cite>([\s\S]+?)<\/cite>/;
    const match = rule.exec(src);
    if (match) {
      return {
        type: 'html',
        pre: false,
        text: match[0],
        block: false,
        raw: match[0],
      };
    }
  }
};

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@starsbit
Comment options

@UziTech
Comment options

Answer selected by starsbit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants