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.
My current approach is this
\nexport 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}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.
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.
\nSomething like:
\nconst 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};-
|
Hello, I am trying to build a custom tokenizer because I want to achieve the following behavior: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce erat quam, ultrices at diam a, ullamcorper commodo sapien. Curabitur vestibulum auctor massa sed placerat. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus sed consequat orci. <cite>marked</cite>When using the current HTML tokenizer, it submits two tokens here for the HTML renderer: My current approach is this export class CiteTokenizer extends Tokenizer {
constructor(options?: MarkedOptions) {
super(options);
}
override html(src: string): Tokens.HTML | undefined {
const match = /^<cite>([\s\S]+?)<\/cite>/.exec(src);
if (match) {
return {
type: 'html',
pre: false,
text: match[0],
block: false,
raw: match[0],
};
}
return super.html(src);
}
}This does work for single lines of Is this even possible by implementing a tokenizer, or is a different way a better approach? |
Beta Was this translation helpful? Give feedback.
-
|
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],
};
}
}
}; |
Beta Was this translation helpful? Give feedback.
You will need to write a custom extension.
Something like: