Description
New attempt at custom Tokens, Renderers for Marked.js. Faster than #1872.
May be a possible fix for: #1373, #1693, #1695, #2061,
Users can add a custom Tokenizer and Renderer which will be executed without needing to overwrite existing components, with the extension in the following format:
const myExtension = {
extensions: [{
name: 'underline',
start(src) { return src.match(/:/)?.index; }, // First characters of your token so Marked.js knows to stop and check for a match
level: 'block', // Is this a block-level or inline-level tokenizer?
tokenizer(src, tokens) {
const rule = /^:([^\n]*)(?:\n|$)/; // Regex for the complete token
const match = rule.exec(src);
if (match) {
return { // Token to generate
type: 'underline', // Should match "name" above
raw: match[0], // The text that you want your token to consume from the source
text: match[1].trim() // Any custom properties you want the Renderer to access
};
}
},
renderer (token) {
return `<u>${token.text}</u>\n`;
}]
};
The extension(s) can then be loaded like so:
marked.use(myExtension, extension2, extension3);
\\ EQUIVALENT TO:
marked.use(myExtension);
marked.use(extension2);
marked.use(extension3);
Benchmarks (on my laptop):
Master from Feb 2021
es5 marked completed in 4260ms and passed 82.74%
es6 marked completed in 4289ms and passed 82.74%
es5 marked (gfm) completed in 4518ms and passed 82.13%
es6 marked (gfm) completed in 4504ms and passed 82.13%
es5 marked (pedantic) completed in 4596ms and passed 61.48%
es6 marked (pedantic) completed in 4783ms and passed 61.48%
commonmark completed in 3617ms and passed 100.00%
markdown-it completed in 3646ms and passed 89.21%
Master (current)
es5 marked completed in 4504ms and passed 86.90%
es6 marked completed in 4622ms and passed 86.90%
es5 marked (gfm) completed in 4776ms and passed 86.29%
es6 marked (gfm) completed in 5029ms and passed 86.29%
es5 marked (pedantic) completed in 4674ms and passed 71.03%
es6 marked (pedantic) completed in 5007ms and passed 71.03%
commonmark completed in 3678ms and passed 100.00%
markdown-it completed in 3672ms and passed 89.21%
This PR
es5 marked completed in 4761ms and passed 86.90%
es6 marked completed in 4814ms and passed 86.90%
es5 marked (gfm) completed in 5115ms and passed 86.29%
es6 marked (gfm) completed in 5105ms and passed 86.29%
es5 marked (pedantic) completed in 4972ms and passed 71.03%
es6 marked (pedantic) completed in 5069ms and passed 71.03%
commonmark completed in 3619ms and passed 100.00%
markdown-it completed in 3735ms and passed 89.21%
This PR with all extensions running at the top of the Lexer rather than using "before" to spread them out:
es5 marked completed in 4591ms and passed 86.90%
es6 marked completed in 4644ms and passed 86.90%
es5 marked (gfm) completed in 4891ms and passed 86.29%
es6 marked (gfm) completed in 4804ms and passed 86.29%
es5 marked (pedantic) completed in 4770ms and passed 71.03%
es6 marked (pedantic) completed in 4799ms and passed 71.03%
commonmark completed in 3617ms and passed 100.00%
markdown-it completed in 3587ms and passed 89.21%
Contributor
- [x] Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
- [ ] no tests required for this PR.
- [x] If submitting new feature, it has been documented in the appropriate places.
Committer
In most cases, this should be a different person than the contributor.
released