fix: preserve zero from named extension renderers (#4199)

This commit is contained in:
lfhwnqe
2026-11-09 08:17:04 -07:00
committed by GitHub
parent 7bfa1f70e8
commit b81a00a2e3
2 changed files with 21 additions and 2 deletions
+2 -2
View File
@@ -52,7 +52,7 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
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', 'checkbox', 'html', 'def', 'paragraph', 'text'].includes(genericToken.type)) {
out += ret || '';
out += ret === false ? '' : ret ?? '';
continue;
}
}
@@ -138,7 +138,7 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
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', 'checkbox', 'strong', 'em', 'codespan', 'br', 'del', 'text'].includes(anyToken.type)) {
out += ret || '';
out += ret === false ? '' : ret ?? '';
continue;
}
}
+19
View File
@@ -344,6 +344,25 @@ describe('marked unit', () => {
assert.strictEqual(html, 'fallbacktest');
});
it('should keep zero from both block and inline extension renderers', () => {
const extension = level => ({
name: level + 'Count',
level,
tokenizer(src) {
if (src.startsWith(':count:')) {
return { type: level + 'Count', raw: ':count:' };
}
},
renderer() {
return 0;
},
});
marked.use({ extensions: [extension('block'), extension('inline')] });
assert.strictEqual(marked.parse(':count:'), '0');
assert.strictEqual(marked.parseInline(':count:'), '0');
});
it('should fall back when tokenizers return false', () => {
const extension = {
name: 'test',