From 8e7ce937919aa0495dd06183240cb9ee1205d3d0 Mon Sep 17 00:00:00 2001 From: onmax Date: Mon, 2 Nov 2026 13:20:49 +0100 Subject: [PATCH 1/3] perf: decode named entities with decode-named-character-reference entities' decodeHTML ships a packed decode trie that gzip barely shrinks. decode-named-character-reference ships the same names as an object, and decodes through the DOM in browsers. It also only decodes complete names, so link destinations and titles keep unknown names like `&foo;` literal, as CommonMark requires. --- packages/markdown-exit/package.json | 2 +- packages/markdown-exit/src/common/utils.ts | 9 ++------ .../src/parser/inline/rules/entity.ts | 6 ++--- packages/markdown-exit/tests/misc.test.ts | 9 ++++++++ pnpm-lock.yaml | 23 ++++++++++--------- pnpm-workspace.yaml | 2 +- 6 files changed, 28 insertions(+), 23 deletions(-) diff --git a/packages/markdown-exit/package.json b/packages/markdown-exit/package.json index 6006363..4431bc1 100644 --- a/packages/markdown-exit/package.json +++ b/packages/markdown-exit/package.json @@ -38,7 +38,7 @@ "prepublishOnly": "pnpm build" }, "dependencies": { - "entities": "catalog:integrations", + "decode-named-character-reference": "catalog:integrations", "linkify-it": "catalog:integrations", "mdurl": "catalog:integrations", "punycode.js": "catalog:integrations", diff --git a/packages/markdown-exit/src/common/utils.ts b/packages/markdown-exit/src/common/utils.ts index b0ee109..72e87a1 100644 --- a/packages/markdown-exit/src/common/utils.ts +++ b/packages/markdown-exit/src/common/utils.ts @@ -1,7 +1,7 @@ // Utilities // -import { decodeHTMLStrict } from 'entities' +import { decodeNamedCharacterReference } from 'decode-named-character-reference' import * as mdurl from 'mdurl' import * as ucmicro from 'uc.micro' @@ -90,12 +90,7 @@ function replaceEntityPattern(match: string, name: string): string { return match } - const decoded = decodeHTMLStrict(match) - if (decoded !== match) { - return decoded - } - - return match + return decodeNamedCharacterReference(name) || match } export function unescapeMd(str: string): string { diff --git a/packages/markdown-exit/src/parser/inline/rules/entity.ts b/packages/markdown-exit/src/parser/inline/rules/entity.ts index aa7585c..2e529c8 100644 --- a/packages/markdown-exit/src/parser/inline/rules/entity.ts +++ b/packages/markdown-exit/src/parser/inline/rules/entity.ts @@ -1,7 +1,7 @@ // Process html entity - {, ¯, ", ... import type StateInline from '../state_inline' -import { decodeHTMLStrict } from 'entities' +import { decodeNamedCharacterReference } from 'decode-named-character-reference' import { fromCodePoint, isValidEntityCode } from '../../../common/utils' const DIGITAL_RE = /^&#(x[a-f0-9]{1,6}|\d{1,7});/i @@ -36,8 +36,8 @@ export default function entity(state: StateInline, silent: boolean) { } else { const match = state.src.slice(pos).match(NAMED_RE) if (match) { - const decoded = decodeHTMLStrict(match[0]) - if (decoded !== match[0]) { + const decoded = decodeNamedCharacterReference(match[1]) + if (decoded !== false) { if (!silent) { const token = state.push('text_special', '', 0) token.content = decoded diff --git a/packages/markdown-exit/tests/misc.test.ts b/packages/markdown-exit/tests/misc.test.ts index e08dd0e..c4aac30 100644 --- a/packages/markdown-exit/tests/misc.test.ts +++ b/packages/markdown-exit/tests/misc.test.ts @@ -228,6 +228,15 @@ describe('misc', () => { assert.strictEqual(md.render(' codeblock\n'), '
codeblock\n
\n') }) + it('should keep unknown entity names in link destinations and titles', () => { + const md = MarkdownExit() + + assert.strictEqual( + md.render('[a](/x&foo;y "t¬it;")'), + '

a

\n', + ) + }) + it('should quickly exit on empty string', () => { const md = MarkdownExit() diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 26d4d8e..817f788 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -60,9 +60,9 @@ catalogs: specifier: ^3.5.26 version: 3.5.26 integrations: - entities: - specifier: ^8.0.0 - version: 8.0.0 + decode-named-character-reference: + specifier: ^1.3.0 + version: 1.3.0 linkify-it: specifier: ^6.0.0 version: 6.1.0 @@ -185,9 +185,9 @@ importers: packages/markdown-exit: dependencies: - entities: + decode-named-character-reference: specifier: catalog:integrations - version: 8.0.0 + version: 1.3.0 linkify-it: specifier: catalog:integrations version: 6.1.0 @@ -1315,6 +1315,9 @@ packages: decode-named-character-reference@1.2.0: resolution: {integrity: sha512-c6fcElNV6ShtZXmsgNgFFV5tVX2PaV4g+MOAkb8eXHvn6sryJBrZa9r0zV6+dtTyoCKxtDy5tyQ5ZwQuidtd+Q==} + decode-named-character-reference@1.3.0: + resolution: {integrity: sha512-GtpQYB283KrPp6nRw50q3U9/VfOutZOe103qlN7BPP6Ad27xYnOIWv4lPzo8HCAL+mMZofJ9KEy30fq6MfaK6Q==} + deep-eql@5.0.2: resolution: {integrity: sha512-h5k/5U50IJJFpzfL6nO9jaaumfjO/f2NjK/oYB2Djzm4p9L+3T9qWpZqZ2hAbLPuuYq9wrU08WQyBTL5GbPk5Q==} engines: {node: '>=6'} @@ -1383,10 +1386,6 @@ packages: resolution: {integrity: sha512-FDWG5cmEYf2Z00IkYRhbFrwIwvdFKH07uV8dvNy0omp/Qb1xcyCWp2UDtcwJF4QZZvk0sLudP6/hAu42TaqVhQ==} engines: {node: '>=0.12'} - entities@8.0.0: - resolution: {integrity: sha512-zwfzJecQ/Uej6tusMqwAqU/6KL2XaB2VZ2Jg54Je6ahNBGNH6Ek6g3jjNCF0fG9EWQKGZNddNjU5F1ZQn/sBnA==} - engines: {node: '>=20.19.0'} - es-define-property@1.0.1: resolution: {integrity: sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==} engines: {node: '>= 0.4'} @@ -3742,6 +3741,10 @@ snapshots: dependencies: character-entities: 2.0.2 + decode-named-character-reference@1.3.0: + dependencies: + character-entities: 2.0.2 + deep-eql@5.0.2: {} deep-is@0.1.4: {} @@ -3785,8 +3788,6 @@ snapshots: entities@7.0.0: {} - entities@8.0.0: {} - es-define-property@1.0.1: {} es-errors@1.3.0: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 3802cd2..d9a8199 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -29,7 +29,7 @@ catalogs: vitepress: 2.0.0-alpha.12 vue: ^3.5.26 integrations: - entities: ^8.0.0 + decode-named-character-reference: ^1.3.0 linkify-it: ^6.0.0 mdurl: ^2.1.0 punycode.js: ^2.3.1 From 154b427a69e070d0836dbbfda9a9aabdf94a1c50 Mon Sep 17 00:00:00 2001 From: onmax Date: Mon, 2 Nov 2026 13:46:26 +0100 Subject: [PATCH 2/3] fix: look up named entities in character-entities without the DOM decode-named-character-reference's browser export decodes through innerHTML, which fails in workers and under Trusted Types. A direct character-entities lookup behaves the same everywhere. --- packages/markdown-exit/package.json | 2 +- packages/markdown-exit/src/common/entities.ts | 7 +++++++ packages/markdown-exit/src/common/utils.ts | 4 ++-- .../src/parser/inline/rules/entity.ts | 6 +++--- packages/markdown-exit/tests/misc.test.ts | 6 +++--- pnpm-lock.yaml | 17 +++++------------ pnpm-workspace.yaml | 2 +- 7 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 packages/markdown-exit/src/common/entities.ts diff --git a/packages/markdown-exit/package.json b/packages/markdown-exit/package.json index 4431bc1..b955c36 100644 --- a/packages/markdown-exit/package.json +++ b/packages/markdown-exit/package.json @@ -38,7 +38,7 @@ "prepublishOnly": "pnpm build" }, "dependencies": { - "decode-named-character-reference": "catalog:integrations", + "character-entities": "catalog:integrations", "linkify-it": "catalog:integrations", "mdurl": "catalog:integrations", "punycode.js": "catalog:integrations", diff --git a/packages/markdown-exit/src/common/entities.ts b/packages/markdown-exit/src/common/entities.ts new file mode 100644 index 0000000..4ce98de --- /dev/null +++ b/packages/markdown-exit/src/common/entities.ts @@ -0,0 +1,7 @@ +import { characterEntities } from 'character-entities' + +const own = Object.prototype.hasOwnProperty + +export function decodeNamedEntity(name: string): string | undefined { + return own.call(characterEntities, name) ? characterEntities[name] : undefined +} diff --git a/packages/markdown-exit/src/common/utils.ts b/packages/markdown-exit/src/common/utils.ts index 72e87a1..0ff6f24 100644 --- a/packages/markdown-exit/src/common/utils.ts +++ b/packages/markdown-exit/src/common/utils.ts @@ -1,9 +1,9 @@ // Utilities // -import { decodeNamedCharacterReference } from 'decode-named-character-reference' import * as mdurl from 'mdurl' import * as ucmicro from 'uc.micro' +import { decodeNamedEntity } from './entities' type ClassToWrap = new (...args: any[]) => object @@ -90,7 +90,7 @@ function replaceEntityPattern(match: string, name: string): string { return match } - return decodeNamedCharacterReference(name) || match + return decodeNamedEntity(name) ?? match } export function unescapeMd(str: string): string { diff --git a/packages/markdown-exit/src/parser/inline/rules/entity.ts b/packages/markdown-exit/src/parser/inline/rules/entity.ts index 2e529c8..a73d8d6 100644 --- a/packages/markdown-exit/src/parser/inline/rules/entity.ts +++ b/packages/markdown-exit/src/parser/inline/rules/entity.ts @@ -1,7 +1,7 @@ // Process html entity - {, ¯, ", ... import type StateInline from '../state_inline' -import { decodeNamedCharacterReference } from 'decode-named-character-reference' +import { decodeNamedEntity } from '../../../common/entities' import { fromCodePoint, isValidEntityCode } from '../../../common/utils' const DIGITAL_RE = /^&#(x[a-f0-9]{1,6}|\d{1,7});/i @@ -36,8 +36,8 @@ export default function entity(state: StateInline, silent: boolean) { } else { const match = state.src.slice(pos).match(NAMED_RE) if (match) { - const decoded = decodeNamedCharacterReference(match[1]) - if (decoded !== false) { + const decoded = decodeNamedEntity(match[1]) + if (decoded !== undefined) { if (!silent) { const token = state.push('text_special', '', 0) token.content = decoded diff --git a/packages/markdown-exit/tests/misc.test.ts b/packages/markdown-exit/tests/misc.test.ts index c4aac30..6661e26 100644 --- a/packages/markdown-exit/tests/misc.test.ts +++ b/packages/markdown-exit/tests/misc.test.ts @@ -228,12 +228,12 @@ describe('misc', () => { assert.strictEqual(md.render(' codeblock\n'), '
codeblock\n
\n') }) - it('should keep unknown entity names in link destinations and titles', () => { + it('should keep invalid entity references in link destinations and titles', () => { const md = MarkdownExit() assert.strictEqual( - md.render('[a](/x&foo;y "t¬it;")'), - '

a

\n', + md.render('[a](/x&foo;yA "t¬it;")'), + '

a

\n', ) }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 817f788..d2cf4c4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -60,9 +60,9 @@ catalogs: specifier: ^3.5.26 version: 3.5.26 integrations: - decode-named-character-reference: - specifier: ^1.3.0 - version: 1.3.0 + character-entities: + specifier: ^2.0.2 + version: 2.0.2 linkify-it: specifier: ^6.0.0 version: 6.1.0 @@ -185,9 +185,9 @@ importers: packages/markdown-exit: dependencies: - decode-named-character-reference: + character-entities: specifier: catalog:integrations - version: 1.3.0 + version: 2.0.2 linkify-it: specifier: catalog:integrations version: 6.1.0 @@ -1315,9 +1315,6 @@ packages: decode-named-character-reference@1.2.0: resolution: {integrity: sha512-c6fcElNV6ShtZXmsgNgFFV5tVX2PaV4g+MOAkb8eXHvn6sryJBrZa9r0zV6+dtTyoCKxtDy5tyQ5ZwQuidtd+Q==} - decode-named-character-reference@1.3.0: - resolution: {integrity: sha512-GtpQYB283KrPp6nRw50q3U9/VfOutZOe103qlN7BPP6Ad27xYnOIWv4lPzo8HCAL+mMZofJ9KEy30fq6MfaK6Q==} - deep-eql@5.0.2: resolution: {integrity: sha512-h5k/5U50IJJFpzfL6nO9jaaumfjO/f2NjK/oYB2Djzm4p9L+3T9qWpZqZ2hAbLPuuYq9wrU08WQyBTL5GbPk5Q==} engines: {node: '>=6'} @@ -3741,10 +3738,6 @@ snapshots: dependencies: character-entities: 2.0.2 - decode-named-character-reference@1.3.0: - dependencies: - character-entities: 2.0.2 - deep-eql@5.0.2: {} deep-is@0.1.4: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index d9a8199..ba0be33 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -29,7 +29,7 @@ catalogs: vitepress: 2.0.0-alpha.12 vue: ^3.5.26 integrations: - decode-named-character-reference: ^1.3.0 + character-entities: ^2.0.2 linkify-it: ^6.0.0 mdurl: ^2.1.0 punycode.js: ^2.3.1 From ec04f09ad5f429289940627a32fecc8b3d18111a Mon Sep 17 00:00:00 2001 From: SerKo Date: Tue, 3 Nov 2026 15:55:44 +0800 Subject: [PATCH 3/3] test: cover unknown and prototype entity names --- packages/markdown-exit/tests/misc.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/markdown-exit/tests/misc.test.ts b/packages/markdown-exit/tests/misc.test.ts index 6661e26..c703ab9 100644 --- a/packages/markdown-exit/tests/misc.test.ts +++ b/packages/markdown-exit/tests/misc.test.ts @@ -237,6 +237,24 @@ describe('misc', () => { ) }) + it('should keep unknown entity names literal', () => { + const md = MarkdownExit() + + assert.strictEqual( + md.render('&pm1; &acy1; [a](/x&pm1;y "t&acy1;")'), + '

&pm1; &acy1; a

\n', + ) + }) + + it('should not decode Object.prototype keys as entities', () => { + const md = MarkdownExit() + + assert.strictEqual( + md.render('&constructor; &toString; [a](/&hasOwnProperty; "&valueOf;")'), + '

&constructor; &toString; a

\n', + ) + }) + it('should quickly exit on empty string', () => { const md = MarkdownExit()