diff --git a/packages/comark/src/core.ts b/packages/comark/src/core.ts index e7cdc772..1e25ee8f 100644 --- a/packages/comark/src/core.ts +++ b/packages/comark/src/core.ts @@ -17,6 +17,7 @@ import { applyUnwrap, resolveUnwrapTags } from './internal/parse/unwrap.ts' import { createSerializedTask } from './utils/helpers.ts' import { noopTracer, withSpan } from './utils/trace.ts' import { tokenListToTree } from './internal/parse/tree.ts' +import { loadEntities, needsEntities } from './internal/entities.ts' // Re-export plugin utilities export { defineComarkPlugin } from './utils/helpers.ts' @@ -135,6 +136,8 @@ export function createMarkdownParser plugin.pre!(state)) } + if (needsEntities(state.markdown)) await loadEntities() + try { state.tokens = withSpan(tracer, 'comark:tokenize', () => md.parse(state.markdown, {})) } catch (e) { diff --git a/packages/comark/src/internal/entities.ts b/packages/comark/src/internal/entities.ts index 7bdbbe3e..6f04967e 100644 --- a/packages/comark/src/internal/entities.ts +++ b/packages/comark/src/internal/entities.ts @@ -1,10 +1,35 @@ -import { characterEntities } from 'character-entities' -import { characterEntitiesLegacy } from 'character-entities-legacy' - -// Same table as markdown-exit, so bundles hold one copy. +import { setNamedEntities } from 'markdown-exit/core' const own = Object.prototype.hasOwnProperty -const legacy = new Set(characterEntitiesLegacy) + +// The named character reference table is about 12 KiB gzip, so it loads only +// when a document has `&` followed by a letter. markdown-exit/core decodes +// with the same table, so bundles hold one copy. +let characterEntities: Record | undefined +let legacy: Set | undefined +let loading: Promise | undefined + +/** Whether `markdown` may hold a named character reference, which needs the table. */ +export function needsEntities(markdown: string): boolean { + return !characterEntities && /&[a-z]/i.test(markdown) +} + +/** Loads the named character reference table for Comark and markdown-exit, once. */ +export function loadEntities(): Promise { + loading ??= Promise.all([import('character-entities'), import('character-entities-legacy')]).then( + ([entities, legacyNames]) => { + characterEntities = entities.characterEntities + legacy = new Set(legacyNames.characterEntitiesLegacy) + setNamedEntities(characterEntities) + }, + (error) => { + // Try again on the next parse. + loading = undefined + throw error + } + ) + return loading +} // HTML maps C1 control code points to their windows-1252 characters. const C1: Record = { @@ -88,7 +113,7 @@ export function decodeHTMLAttribute(input: string): string { decoded = fromNumericReference(value) if (input.charCodeAt(end) === 59 /* ; */) end++ } - } else { + } else if (characterEntities && legacy) { while (end < length && isAlphanumeric(input.charCodeAt(end))) end++ const name = input.slice(amp + 1, end) const next = input.charCodeAt(end) diff --git a/packages/comark/test/parse/html-attribute-references.test.ts b/packages/comark/test/parse/html-attribute-references.test.ts index 0a77289d..366e9d63 100644 --- a/packages/comark/test/parse/html-attribute-references.test.ts +++ b/packages/comark/test/parse/html-attribute-references.test.ts @@ -1,7 +1,10 @@ -import { describe, expect, it } from 'vitest' +import { beforeAll, describe, expect, it } from 'vitest' +import { loadEntities } from '../../src/internal/entities' import { parseHtmlAttributes } from '../../src/internal/parse/utils' describe('character references in HTML attribute values', () => { + beforeAll(loadEntities) + it.each([ ['<b> &amp; ©', ' & ©'], ['ABC 😀', 'ABC 😀'], diff --git a/packages/comark/test/parse/lazy-entities.test.ts b/packages/comark/test/parse/lazy-entities.test.ts new file mode 100644 index 00000000..df53a9bb --- /dev/null +++ b/packages/comark/test/parse/lazy-entities.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, it } from 'vitest' +import { createMarkdownParser } from '../../src/core' +import { needsEntities } from '../../src/internal/entities' + +// Each test file runs in a fresh module graph, so the table starts unloaded. +describe('named character reference table', () => { + const parse = createMarkdownParser() + + it('loads only when a document has `&` followed by a letter', async () => { + expect((await parse('a & b © c')).nodes).toEqual([['p', {}, 'a & b © c']]) + expect(needsEntities('©')).toBe(true) + + expect((await parse('© [a](/b "<")')).nodes).toEqual([ + ['p', {}, '© ', ['a', { href: '/b', title: '<' }, 'a']], + ]) + expect(needsEntities('©')).toBe(false) + }) +}) diff --git a/packages/comark/test/text-content.test.ts b/packages/comark/test/text-content.test.ts index 36ff926d..5ef4f5d6 100644 --- a/packages/comark/test/text-content.test.ts +++ b/packages/comark/test/text-content.test.ts @@ -1,9 +1,11 @@ -import { describe, expect, it } from 'vitest' +import { beforeAll, describe, expect, it } from 'vitest' import type { Node } from 'comark' -import { decodeHTMLAttribute as decodeHTML } from '../src/internal/entities' +import { decodeHTMLAttribute as decodeHTML, loadEntities } from '../src/internal/entities' import { textContent } from '../src/utils' describe('textContent', () => { + beforeAll(loadEntities) + const tree: Node = ['p', {}, 'Tom & ', ['strong', {}, 'Jerry'], ' ©'] it('concatenates text nodes', () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 814a2dc7..140e9346 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -336,8 +336,8 @@ catalogs: specifier: ^6.0.0 version: 6.1.0 markdown-exit: - specifier: 2.1.0 - version: 2.1.0 + specifier: https://pkg.pr.new/onmax/markdown-exit/markdown-exit@345931b + version: https://pkg.pr.new/onmax/markdown-exit/markdown-exit@345931b markdown-it: specifier: ^15.0.3 version: 15.0.3 @@ -497,7 +497,7 @@ importers: version: link:packages/comark markdown-exit: specifier: 'catalog:' - version: 2.1.0 + version: https://pkg.pr.new/onmax/markdown-exit/markdown-exit@345931b markdown-it: specifier: 'catalog:' version: 15.0.3 @@ -1444,7 +1444,7 @@ importers: version: 6.1.0 markdown-exit: specifier: 'catalog:' - version: 2.1.0 + version: https://pkg.pr.new/onmax/markdown-exit/markdown-exit@345931b shiki: specifier: 'catalog:' version: 4.5.0 @@ -7916,8 +7916,9 @@ packages: mark.js@8.11.1: resolution: {integrity: sha512-1I+1qpDt4idfgLQG+BNWmrqku+7/2bi5nLf4YwF8y8zXvmfiTBY3PV3ZibfrjBueCByROpuBjLLFCajqkgYoLQ==} - markdown-exit@2.1.0: - resolution: {integrity: sha512-ffGj82OodjwgZ2tZtXBLXvJsMbvgtp2r+RDQgpAuWSk2Zi2zgNfQPyzVQFHAUV6Z/B/uQwqFHHEKNmeogIMsbw==} + markdown-exit@https://pkg.pr.new/onmax/markdown-exit/markdown-exit@345931b: + resolution: {integrity: sha512-7lADn12wwZz68i8VLj31H1WW3BK3YkYJYjZn7ydspU5g9Qed+BXE6Y+ronMYTROy04x/3dK2R5suPpAaZHpbDg==, tarball: https://pkg.pr.new/onmax/markdown-exit/markdown-exit@345931b} + version: 2.1.3 markdown-it@15.0.3: resolution: {integrity: sha512-+Az7Ojz3iRGu8KYMvJ6xVhvrxP3eE+vEtJX75Ow88XBpcQNV/0lGX5ZynIIBftREo4DC2jsveAMDaeltmm8QvQ==} @@ -16726,7 +16727,7 @@ snapshots: mark.js@8.11.1: {} - markdown-exit@2.1.0: + markdown-exit@https://pkg.pr.new/onmax/markdown-exit/markdown-exit@345931b: dependencies: character-entities: 2.0.2 linkify-it: 6.1.0 diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 7568f3a1..dfbe3531 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -94,7 +94,7 @@ catalog: js-yaml: ^5.5.0 katex: ^0.19.1 linkify-it: ^6.0.0 - markdown-exit: 2.1.0 + markdown-exit: https://pkg.pr.new/onmax/markdown-exit/markdown-exit@345931b "markdown-it": ^15.0.3 markdown-it-math: ^6.1.0 minimark: 0.2.0 diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 01f56c4d..a6135f2b 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -67,7 +67,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "42.9k (83 files)", "@comark/svelte": "50.3k (91 files)", "@comark/vue": "60.2k (87 files)", - "comark": "424k (169 files)", + "comark": "425k (169 files)", } `) })