mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: fall back to default checkbox renderer when extension returns false (#4023)
A renderer extension named after a built-in token can return false to fall back to the default renderer. The Parser decides that from a list of built-in token type names, and 'checkbox' was never added to either the block or the inline list when the checkbox token was introduced. The result is that a `checkbox` renderer extension returning false emits nothing instead of falling back, so the checkbox silently disappears from the task item.
This commit is contained in:
+2
-2
@@ -50,7 +50,7 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
|
||||
if (this.options.extensions?.renderers?.[anyToken.type]) {
|
||||
const genericToken = anyToken as Tokens.Generic;
|
||||
const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
|
||||
if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'def', 'paragraph', 'text'].includes(genericToken.type)) {
|
||||
if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'checkbox', 'html', 'def', 'paragraph', 'text'].includes(genericToken.type)) {
|
||||
out += ret || '';
|
||||
continue;
|
||||
}
|
||||
@@ -136,7 +136,7 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
|
||||
// Run any renderer extensions
|
||||
if (this.options.extensions?.renderers?.[anyToken.type]) {
|
||||
const ret = this.options.extensions.renderers[anyToken.type].call({ parser: this }, anyToken);
|
||||
if (ret !== false || !['escape', 'html', 'link', 'image', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
|
||||
if (ret !== false || !['escape', 'html', 'link', 'image', 'checkbox', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
|
||||
out += ret || '';
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -396,6 +396,32 @@ describe('marked unit', () => {
|
||||
assert.strictEqual(html, '<h1>extension1 RENDERER EXTENSION</h1>\n<pre><code>extension2 TOKENIZER EXTENSION\n</code></pre>\n');
|
||||
});
|
||||
|
||||
it('should fall back to the default checkbox renderer if the extension returns false', () => {
|
||||
marked.use({
|
||||
extensions: [{
|
||||
name: 'checkbox',
|
||||
renderer(token) {
|
||||
return token.checked ? false : '<span class="todo"></span> ';
|
||||
},
|
||||
}],
|
||||
});
|
||||
const html = marked.parse('- [x] one\n- [ ] two\n');
|
||||
assert.strictEqual(html, '<ul>\n<li><input checked="" disabled="" type="checkbox"> one</li>\n<li><span class="todo"></span> two</li>\n</ul>\n');
|
||||
});
|
||||
|
||||
it('should fall back to the default checkbox renderer in a loose list if the extension returns false', () => {
|
||||
marked.use({
|
||||
extensions: [{
|
||||
name: 'checkbox',
|
||||
renderer() {
|
||||
return false;
|
||||
},
|
||||
}],
|
||||
});
|
||||
const html = marked.parse('- [x] one\n\n- [ ] two\n');
|
||||
assert.strictEqual(html, '<ul>\n<li><p><input checked="" disabled="" type="checkbox"> one</p>\n</li>\n<li><p><input disabled="" type="checkbox"> two</p>\n</li>\n</ul>\n');
|
||||
});
|
||||
|
||||
it('should walk only specified child tokens', () => {
|
||||
const walkableDescription = {
|
||||
extensions: [{
|
||||
|
||||
Reference in New Issue
Block a user