diff --git a/packages/markdown-exit/package.json b/packages/markdown-exit/package.json index 6006363..b955c36 100644 --- a/packages/markdown-exit/package.json +++ b/packages/markdown-exit/package.json @@ -38,7 +38,7 @@ "prepublishOnly": "pnpm build" }, "dependencies": { - "entities": "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 b0ee109..0ff6f24 100644 --- a/packages/markdown-exit/src/common/utils.ts +++ b/packages/markdown-exit/src/common/utils.ts @@ -1,9 +1,9 @@ // Utilities // -import { decodeHTMLStrict } from 'entities' import * as mdurl from 'mdurl' import * as ucmicro from 'uc.micro' +import { decodeNamedEntity } from './entities' type ClassToWrap = new (...args: any[]) => object @@ -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 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 aa7585c..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 { decodeHTMLStrict } from 'entities' +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 = decodeHTMLStrict(match[0]) - if (decoded !== match[0]) { + 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 e08dd0e..c703ab9 100644 --- a/packages/markdown-exit/tests/misc.test.ts +++ b/packages/markdown-exit/tests/misc.test.ts @@ -228,6 +228,33 @@ describe('misc', () => { assert.strictEqual(md.render(' codeblock\n'), '
codeblock\n\n')
})
+ it('should keep invalid entity references in link destinations and titles', () => {
+ const md = MarkdownExit()
+
+ assert.strictEqual(
+ md.render('[a](/x&foo;yA "t¬it;")'),
+ '\n',
+ )
+ })
+
+ 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() diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 26d4d8e..d2cf4c4 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 + 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: - entities: + character-entities: specifier: catalog:integrations - version: 8.0.0 + version: 2.0.2 linkify-it: specifier: catalog:integrations version: 6.1.0 @@ -1383,10 +1383,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'} @@ -3785,8 +3781,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..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: - entities: ^8.0.0 + character-entities: ^2.0.2 linkify-it: ^6.0.0 mdurl: ^2.1.0 punycode.js: ^2.3.1