Commit 7ce7329
fix(headings): match ATX headings on CRLF lines (fixes empty TOC on Windows files)
Context:
HEADING_RE (wikilink/navigation.logic.ts) is the shared ATX-heading matcher used
by the Table of Contents (toc.logic.extractTocHeadings), wikilink navigation
(findHeadingPosition), wikilink completion, and copy-block-link. These callers
split raw file content on '\n', so a CRLF-authored file leaves a trailing '\r'
on each line.
Problem:
HEADING_RE was /^#{1,6}\s+(.+)$/. In JS regex `.` does not match '\r', and `$`
(no multiline) only matches end-of-string or before a final '\n' -- not before a
'\r'. So `# Heading\r` never matched. For any markdown file with CRLF line
endings the TOC was completely empty ("No headings found"), and wikilink jumps to
`#heading` returned null. The breakage only affected unedited files: once the
user made any edit, CodeMirror rewrote the buffer to LF and headings reappeared.
Solution:
Add an optional trailing carriage return to the capture's terminator:
/^#{1,6}\s+(.+)\r?$/. The greedy `(.+)` still stops before '\r' (so the capture
is unchanged for LF input), `\r?` consumes the optional carriage return, then `$`
matches end-of-line. Callers split on '\n' and compute positions as
`line.length + 1`; since '\r' stays inside line.length, byte offsets remain
correct for CRLF.
Behavior:
- CRLF files: TOC populates, wikilink/heading navigation and completion resolve.
- LF files: capture group and positions byte-identical to before.
Files:
- src/lib/core/markdown-editor/extensions/wikilink/navigation.logic.ts:1-7 —
HEADING_RE tolerates trailing '\r'; expanded doc comment.
- src/tests/.../wikilink/navigation.logic.test.ts — findHeadingPosition over CRLF
content (positions 0 and 28).
- src/tests/lib/plugins/table-of-contents/toc.logic.test.ts — extractTocHeadings
over CRLF content (levels, text, line, pos).
- tasks/todo/bug-hunt-fixes.md — H7 marked done.
Note: separate local heading regexes in live-preview/embed-resolver.logic.ts:2
and kanban.logic.ts:3 have the same latent CRLF gap; left untouched as they are
outside this fix's scope.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent dac9bd0 commit 7ce7329
4 files changed
Lines changed: 26 additions & 3 deletions
File tree
- src
- lib/core/markdown-editor/extensions/wikilink
- tests/lib
- core/markdown-editor/extensions/wikilink
- plugins/table-of-contents
- tasks/todo
Lines changed: 6 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
3 | 7 | | |
4 | 8 | | |
5 | 9 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
61 | 70 | | |
62 | 71 | | |
63 | 72 | | |
| |||
Lines changed: 10 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
41 | 51 | | |
42 | 52 | | |
43 | 53 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
0 commit comments