|
| 1 | +import { test } from 'node:test' |
| 2 | +import assert from 'node:assert' |
| 3 | + |
| 4 | +import { extractFirstH1 } from './extract-title-from-md.js' |
| 5 | + |
| 6 | +test.describe('extractFirstH1', () => { |
| 7 | + test('extracts ATX style H1 headings', async () => { |
| 8 | + const tests = [ |
| 9 | + { |
| 10 | + input: '# Simple Heading', |
| 11 | + expect: 'Simple Heading', |
| 12 | + note: 'basic ATX H1' |
| 13 | + }, |
| 14 | + { |
| 15 | + input: '# Extra Spaces ', |
| 16 | + expect: 'Extra Spaces', |
| 17 | + note: 'ATX H1 with extra spaces' |
| 18 | + }, |
| 19 | + { |
| 20 | + input: '# Heading with **bold** and *italic*', |
| 21 | + expect: 'Heading with **bold** and *italic*', |
| 22 | + note: 'ATX H1 with inline formatting' |
| 23 | + }, |
| 24 | + { |
| 25 | + input: 'Some text\n# First Heading\n## Second Heading', |
| 26 | + expect: 'First Heading', |
| 27 | + note: 'ATX H1 after other content' |
| 28 | + }, |
| 29 | + { |
| 30 | + input: '## Not H1\n# Real H1\n### Not H1', |
| 31 | + expect: 'Real H1', |
| 32 | + note: 'ATX H1 between other headings' |
| 33 | + } |
| 34 | + ] |
| 35 | + |
| 36 | + for (const testCase of tests) { |
| 37 | + const result = extractFirstH1(testCase.input) |
| 38 | + assert.equal(result, testCase.expect, testCase.note) |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + test('extracts Setext style H1 headings', async () => { |
| 43 | + const tests = [ |
| 44 | + { |
| 45 | + input: 'Simple Heading\n==============', |
| 46 | + expect: 'Simple Heading', |
| 47 | + note: 'basic Setext H1' |
| 48 | + }, |
| 49 | + { |
| 50 | + input: 'Simple Heading\n===', |
| 51 | + expect: 'Simple Heading', |
| 52 | + note: 'Setext H1 with minimum underline' |
| 53 | + }, |
| 54 | + { |
| 55 | + input: ' Trimmed Heading \n==============', |
| 56 | + expect: 'Trimmed Heading', |
| 57 | + note: 'Setext H1 with spaces to trim' |
| 58 | + }, |
| 59 | + { |
| 60 | + input: 'Heading with **bold** and *italic*\n==================', |
| 61 | + expect: 'Heading with **bold** and *italic*', |
| 62 | + note: 'Setext H1 with inline formatting' |
| 63 | + }, |
| 64 | + { |
| 65 | + input: 'Some text\n\nFirst Heading\n=============\n\nMore text', |
| 66 | + expect: 'First Heading', |
| 67 | + note: 'Setext H1 with surrounding content' |
| 68 | + } |
| 69 | + ] |
| 70 | + |
| 71 | + for (const testCase of tests) { |
| 72 | + const result = extractFirstH1(testCase.input) |
| 73 | + assert.equal(result, testCase.expect, testCase.note) |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + test('handles edge cases correctly', async () => { |
| 78 | + const tests = [ |
| 79 | + { |
| 80 | + input: '', |
| 81 | + expect: null, |
| 82 | + note: 'empty string' |
| 83 | + }, |
| 84 | + { |
| 85 | + input: '## Only H2\n### Only H3', |
| 86 | + expect: null, |
| 87 | + note: 'no H1 present' |
| 88 | + }, |
| 89 | + { |
| 90 | + input: 'Not a heading\n---', |
| 91 | + expect: null, |
| 92 | + note: 'Setext H2 (dashes) should not match' |
| 93 | + }, |
| 94 | + { |
| 95 | + input: 'Not a heading\n--', |
| 96 | + expect: null, |
| 97 | + note: 'Setext H2 (dashes) should not match' |
| 98 | + }, |
| 99 | + { |
| 100 | + input: 'Not a heading\n==', |
| 101 | + expect: 'Not a heading', |
| 102 | + note: 'markdown-it accepts any number of equals for Setext H1' |
| 103 | + }, |
| 104 | + { |
| 105 | + input: '\n========', |
| 106 | + expect: null, |
| 107 | + note: 'empty line before Setext underline' |
| 108 | + }, |
| 109 | + { |
| 110 | + input: ' # Code block heading', |
| 111 | + expect: null, |
| 112 | + note: 'indented code block should not match' |
| 113 | + }, |
| 114 | + { |
| 115 | + input: '```\n# Code fence heading\n```', |
| 116 | + expect: null, |
| 117 | + note: 'fenced code block should not match (simple case)' |
| 118 | + } |
| 119 | + ] |
| 120 | + |
| 121 | + for (const testCase of tests) { |
| 122 | + const result = extractFirstH1(testCase.input) |
| 123 | + assert.equal(result, testCase.expect, testCase.note) |
| 124 | + } |
| 125 | + }) |
| 126 | + |
| 127 | + test('prefers first H1 when multiple exist', async () => { |
| 128 | + const tests = [ |
| 129 | + { |
| 130 | + input: '# First ATX\n# Second ATX', |
| 131 | + expect: 'First ATX', |
| 132 | + note: 'multiple ATX H1s' |
| 133 | + }, |
| 134 | + { |
| 135 | + input: 'First Setext\n============\n\nSecond Setext\n============', |
| 136 | + expect: 'First Setext', |
| 137 | + note: 'multiple Setext H1s' |
| 138 | + }, |
| 139 | + { |
| 140 | + input: '# ATX First\n\nSetext Second\n=============', |
| 141 | + expect: 'ATX First', |
| 142 | + note: 'ATX before Setext' |
| 143 | + }, |
| 144 | + { |
| 145 | + input: 'Setext First\n============\n\n# ATX Second', |
| 146 | + expect: 'Setext First', |
| 147 | + note: 'Setext before ATX' |
| 148 | + } |
| 149 | + ] |
| 150 | + |
| 151 | + for (const testCase of tests) { |
| 152 | + const result = extractFirstH1(testCase.input) |
| 153 | + assert.equal(result, testCase.expect, testCase.note) |
| 154 | + } |
| 155 | + }) |
| 156 | + |
| 157 | + test('handles multiline documents correctly', async () => { |
| 158 | + const markdown = ` |
| 159 | +Some introductory text here |
| 160 | +that spans multiple lines |
| 161 | +
|
| 162 | +# The Real Title |
| 163 | +
|
| 164 | +## A subsection |
| 165 | +
|
| 166 | +More content here |
| 167 | +` |
| 168 | + const result = extractFirstH1(markdown) |
| 169 | + assert.equal(result, 'The Real Title', 'finds H1 in realistic document') |
| 170 | + }) |
| 171 | + |
| 172 | + test('handles frontmatter-like content', async () => { |
| 173 | + const markdown = `--- |
| 174 | +title: Frontmatter Title |
| 175 | +--- |
| 176 | +
|
| 177 | +# Actual H1 Title |
| 178 | +
|
| 179 | +Content here` |
| 180 | + const result = extractFirstH1(markdown) |
| 181 | + assert.equal(result, 'Actual H1 Title', 'ignores frontmatter') |
| 182 | + }) |
| 183 | +}) |
0 commit comments