-
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.
Replies: 1 comment 2 replies
-
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: