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:
Sarath Francis
2026-07-23 22:37:40 -06:00
committed by GitHub
parent 1d3229a4cc
commit e1b6139fec
2 changed files with 28 additions and 2 deletions
+2 -2
View File
@@ -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;
}
+26
View File
@@ -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: [{