Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/comark/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -135,6 +136,8 @@ export function createMarkdownParser<const TPlugins extends readonly ComarkPlugi
await withSpan(tracer, `comark:pre:${plugin.name}`, () => plugin.pre!(state))
}

if (needsEntities(state.markdown)) await loadEntities()

try {
state.tokens = withSpan(tracer, 'comark:tokenize', () => md.parse(state.markdown, {}))
} catch (e) {
Expand Down
37 changes: 31 additions & 6 deletions packages/comark/src/internal/entities.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> | undefined
let legacy: Set<string> | undefined
let loading: Promise<void> | 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<void> {
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<number, number> = {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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([
['&lt;b&gt; &amp;amp; &copy;', '<b> &amp; ©'],
['&#65;&#x42;&#X43 &#x1F600;', 'ABC 😀'],
Expand Down
18 changes: 18 additions & 0 deletions packages/comark/test/parse/lazy-entities.test.ts
Original file line number Diff line number Diff line change
@@ -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 &#169; c')).nodes).toEqual([['p', {}, 'a & b © c']])
expect(needsEntities('&copy;')).toBe(true)

expect((await parse('&copy; [a](/b "&lt;")')).nodes).toEqual([
['p', {}, '© ', ['a', { href: '/b', title: '<' }, 'a']],
])
expect(needsEntities('&copy;')).toBe(false)
})
})
6 changes: 4 additions & 2 deletions packages/comark/test/text-content.test.ts
Original file line number Diff line number Diff line change
@@ -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 &amp; ', ['strong', {}, 'Jerry'], ' &copy;']

it('concatenates text nodes', () => {
Expand Down
15 changes: 8 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
}
`)
})
Expand Down
Loading