Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/content/1.getting-started/2.document-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions packages/comark/src/plugins/title-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/comark/src/plugins/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
5 changes: 4 additions & 1 deletion packages/comark/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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++) {
Expand Down
8 changes: 8 additions & 0 deletions packages/comark/test/heading-ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <!-- TODO: link --> 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', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/comark/test/plugins/toc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <!-- v2 -->\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()] })

Expand Down
5 changes: 5 additions & 0 deletions packages/comark/test/text-content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ describe('textContent', () => {
expect(textContent(tree)).toBe('Tom &amp; Jerry &copy;')
})

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('&lt;', { decodeHTML })).toBe('<')
Expand Down
8 changes: 8 additions & 0 deletions packages/comark/test/title-description.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <!-- TODO: link --> guide\n\nRun it <!-- internal --> 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

Expand Down
Loading