diff --git a/docs/content/1.getting-started/2.document-model.md b/docs/content/1.getting-started/2.document-model.md index 38035be7..cc696cf1 100644 --- a/docs/content/1.getting-started/2.document-model.md +++ b/docs/content/1.getting-started/2.document-model.md @@ -241,7 +241,7 @@ Use the utilities from `comark/utils` instead of writing traversal logic for com ### Extract text -`textContent()` returns the plain text inside a node: +`textContent()` returns the plain text inside a node, leaving out comments: ```typescript [text-content.ts] import { textContent } from 'comark/utils' diff --git a/packages/comark/src/plugins/title-description.ts b/packages/comark/src/plugins/title-description.ts index 4886cf4d..8f708860 100644 --- a/packages/comark/src/plugins/title-description.ts +++ b/packages/comark/src/plugins/title-description.ts @@ -40,8 +40,8 @@ function flattenNodeText(node: Node): string { if (typeof node === 'string') { return node } - // Leave out the separate link `heading-links` adds next to the heading text - if (Array.isArray(node) && node[1]?.$?.permalink === 'separate') { + // Leave out comments and the separate link `heading-links` adds next to the heading text + if (Array.isArray(node) && (node[0] === null || node[1]?.$?.permalink === 'separate')) { return '' } if (Array.isArray(node)) { diff --git a/packages/comark/src/plugins/toc.ts b/packages/comark/src/plugins/toc.ts index 91aa274d..50c2d349 100644 --- a/packages/comark/src/plugins/toc.ts +++ b/packages/comark/src/plugins/toc.ts @@ -61,8 +61,8 @@ function flattenNodeText(node: Node): string { if (typeof node === 'string') { return node } - // Leave out the separate link `heading-links` adds next to the heading text - if (Array.isArray(node) && node[1]?.$?.permalink === 'separate') { + // Leave out comments and the separate link `heading-links` adds next to the heading text + if (Array.isArray(node) && (node[0] === null || node[1]?.$?.permalink === 'separate')) { return '' } if (Array.isArray(node)) { diff --git a/packages/comark/src/utils/index.ts b/packages/comark/src/utils/index.ts index 3840c064..ffa5bab7 100644 --- a/packages/comark/src/utils/index.ts +++ b/packages/comark/src/utils/index.ts @@ -20,7 +20,7 @@ export interface TextContentOptions { decodeHTML?: (text: string) => string } -/** Return the concatenated text content of a Markdown AST node. */ +/** Return the concatenated text content of a Markdown AST node. Comment nodes add no text. */ export function textContent(node: Node, options?: TextContentOptions): string { return collectText(node, options?.decodeHTML) } @@ -29,6 +29,9 @@ function collectText(node: Node, decode: ((text: string) => string) | undefined) if (typeof node === 'string') { return decode ? decode(node) : node } + if (node[0] === null) { + return '' + } let out = '' const len = node.length for (let i = 2; i < len; i++) { diff --git a/packages/comark/test/heading-ids.test.ts b/packages/comark/test/heading-ids.test.ts index 598ab78b..af6aa3b0 100644 --- a/packages/comark/test/heading-ids.test.ts +++ b/packages/comark/test/heading-ids.test.ts @@ -130,6 +130,14 @@ describe('headingIds option', () => { expect((tree.nodes[0] as any)[1].id).toBe('star-here') }) + + it('does not include HTML comment text in the slug', async () => { + const source = '## Install guide' + const tree = await parseMarkdown(source) + + expect((tree.nodes[0] as any)[1].id).toBe('install-guide') + expect((await renderMarkdown(tree)).trim()).toBe(source) + }) }) describe('non-ASCII headings', () => { diff --git a/packages/comark/test/plugins/toc.test.ts b/packages/comark/test/plugins/toc.test.ts index ea119260..72f6b5c8 100644 --- a/packages/comark/test/plugins/toc.test.ts +++ b/packages/comark/test/plugins/toc.test.ts @@ -25,6 +25,12 @@ describe('toc plugin', () => { expect(tree.meta.toc.links[0].children?.map((l) => l.text)).toEqual(['Subsection']) }) + it('leaves HTML comments out of link text', async () => { + const tree = await parseMarkdown('## Usage \n', { plugins: [toc()] }) + + expect(tree.meta.toc.links).toEqual([{ id: 'usage', depth: 2, text: 'Usage ' }]) + }) + it('narrows tree.meta.toc to Toc at the type level', async () => { const tree = await parseMarkdown(CONTENT, { plugins: [toc()] }) diff --git a/packages/comark/test/text-content.test.ts b/packages/comark/test/text-content.test.ts index 36ff926d..b09c33d0 100644 --- a/packages/comark/test/text-content.test.ts +++ b/packages/comark/test/text-content.test.ts @@ -11,6 +11,11 @@ describe('textContent', () => { expect(textContent(tree)).toBe('Tom & Jerry ©') }) + it('leaves out comment nodes', () => { + expect(textContent(['h1', {}, 'Install ', [null, {}, ' TODO '], ' guide'])).toBe('Install guide') + expect(textContent([null, {}, ' note '])).toBe('') + }) + it('applies decodeHTML to every text node', () => { expect(textContent(tree, { decodeHTML })).toBe('Tom & Jerry ©') expect(textContent('<', { decodeHTML })).toBe('<') diff --git a/packages/comark/test/title-description.test.ts b/packages/comark/test/title-description.test.ts index 5f10c9c8..9450ef0e 100644 --- a/packages/comark/test/title-description.test.ts +++ b/packages/comark/test/title-description.test.ts @@ -79,6 +79,14 @@ describe('title-description plugin', () => { expect(tree.meta.description).toBeUndefined() }) + it('leaves HTML comments out of title and description', async () => { + const md = '# Install guide\n\nRun it now.\n' + const tree = await parseMarkdown(md, { plugins: [titleDescription()] }) + + expect(tree.meta.title).toBe('Install guide') + expect(tree.meta.description).toBe('Run it now.') + }) + it('uses custom titleTag and descriptionTag', async () => { const md = `## Custom Title