|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { parse } from '../src/parse' |
| 3 | +import headings from '../src/plugins/headings' |
| 4 | + |
| 5 | +const CONTENT = `# My Page Title |
| 6 | +
|
| 7 | +This is the description paragraph. |
| 8 | +
|
| 9 | +## Section One |
| 10 | +
|
| 11 | +More content here. |
| 12 | +` |
| 13 | + |
| 14 | +describe('headings plugin', () => { |
| 15 | + it('extracts title and description into meta', async () => { |
| 16 | + const tree = await parse(CONTENT, { plugins: [headings()] }) |
| 17 | + |
| 18 | + expect(tree.meta.title).toBe('My Page Title') |
| 19 | + expect(tree.meta.description).toBe('This is the description paragraph.') |
| 20 | + }) |
| 21 | + |
| 22 | + it('keeps extracted nodes in the tree by default (remove: false)', async () => { |
| 23 | + const tree = await parse(CONTENT, { plugins: [headings()] }) |
| 24 | + |
| 25 | + const tags = tree.nodes.filter((n) => Array.isArray(n)).map((n) => (n as any)[0]) |
| 26 | + |
| 27 | + expect(tags).toContain('h1') |
| 28 | + expect(tags).toContain('p') |
| 29 | + }) |
| 30 | + |
| 31 | + it('removes extracted nodes when remove: true', async () => { |
| 32 | + const tree = await parse(CONTENT, { plugins: [headings({ remove: true })] }) |
| 33 | + |
| 34 | + expect(tree.meta.title).toBe('My Page Title') |
| 35 | + expect(tree.meta.description).toBe('This is the description paragraph.') |
| 36 | + |
| 37 | + const tags = tree.nodes.filter((n) => Array.isArray(n)).map((n) => (n as any)[0]) |
| 38 | + |
| 39 | + expect(tags).not.toContain('h1') |
| 40 | + expect(tags).toContain('h2') |
| 41 | + // The first <p> (description) should be removed, but the second section content stays |
| 42 | + const paragraphs = tree.nodes.filter((n) => Array.isArray(n) && (n as any)[0] === 'p') |
| 43 | + expect( |
| 44 | + paragraphs.every((p) => { |
| 45 | + const text = Array.isArray(p) && p.length > 2 ? String(p[2]) : '' |
| 46 | + return text !== 'This is the description paragraph.' |
| 47 | + }) |
| 48 | + ).toBe(true) |
| 49 | + }) |
| 50 | + |
| 51 | + it('does not set meta.title when no matching tag exists', async () => { |
| 52 | + const tree = await parse('Just a paragraph.\n', { plugins: [headings()] }) |
| 53 | + |
| 54 | + expect(tree.meta.title).toBeUndefined() |
| 55 | + expect(tree.meta.description).toBe('Just a paragraph.') |
| 56 | + }) |
| 57 | + |
| 58 | + it('uses custom titleTag and descriptionTag', async () => { |
| 59 | + const md = `## Custom Title |
| 60 | +
|
| 61 | +> Lead-in quote as description. |
| 62 | +
|
| 63 | +More content. |
| 64 | +` |
| 65 | + const tree = await parse(md, { |
| 66 | + plugins: [headings({ titleTag: 'h2', descriptionTag: 'blockquote' })], |
| 67 | + }) |
| 68 | + |
| 69 | + expect(tree.meta.title).toBe('Custom Title') |
| 70 | + expect(tree.meta.description).toBe('Lead-in quote as description.') |
| 71 | + }) |
| 72 | +}) |
0 commit comments