fix: skip frontmatter when rewriting wiki links#453
Conversation
|
CodeAnt AI is reviewing your PR. Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
📝 WalkthroughWalkthroughA bug fix modifies Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/ui/file-preview/src/markdown/linking.ts`:
- Around line 222-223: The frontmatter detection using trim() ===
FRONTMATTER_DELIMITER is too permissive (matches indented '---') so update the
checks to only match a delimiter at column 0: replace occurrences where you
compare line.trim() to FRONTMATTER_DELIMITER (the inFrontmatter computation and
the similar check at the later block) with a strict check such as line ===
FRONTMATTER_DELIMITER or a regex anchored to start like /^---\s*$/ to allow
optional trailing spaces but not leading indentation; update the
variables/expressions that compute inFrontmatter and the corresponding second
check (references: inFrontmatter, FRONTMATTER_DELIMITER, lines) accordingly.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0f85b5c2-17bb-41d1-9ace-5a2f96b9d82e
📒 Files selected for processing (2)
src/ui/file-preview/src/markdown/linking.tstest/test-markdown-preview.js
| if (line.trim() === FRONTMATTER_DELIMITER) { | ||
| inFrontmatter = false; |
There was a problem hiding this comment.
Suggestion: The frontmatter close check uses trim(), so an indented line like --- (which is valid content inside YAML block scalars) is treated as a closing delimiter. That flips parsing out of frontmatter too early and starts rewriting wikilinks that are still inside frontmatter. Detect the closing delimiter only on an actual delimiter line (not whitespace-indented content). [logic error]
Severity Level: Major ⚠️
- ❌ Frontmatter wikilinks rewritten in preview for some YAML scalars.
- ⚠️ Markdown preview via renderMarkdown shows incorrect frontmatter links.Steps of Reproduction ✅
1. Create a markdown note on disk whose first lines form YAML frontmatter with a block
scalar containing an indented `---`, for example:
---
title: |
intro text
---
source_talk: "[[Frontmatter Link]]"
---
(this shape is valid YAML frontmatter with the line ` ---` as scalar content).
2. Open this note in the markdown preview pipeline, which calls `renderMarkdown()` in
`src/ui/file-preview/src/components/markdown-renderer.ts:66-68`. That function passes the
full document text to `prepareMarkdownSource()` in
`src/ui/file-preview/src/markdown/parser.ts:53-55`.
3. `prepareMarkdownSource()` calls `rewriteWikiLinks(source)` in
`src/ui/file-preview/src/markdown/linking.ts:219-255`. Inside this function,
`inFrontmatter` is initially `true` because the first line is `'---'` (lines 220–223).
4. When the line ` ---` inside the block scalar is processed, `line.trim()` equals
`'---'`, so the condition at `linking.ts:231` (`if (line.trim() ===
FRONTMATTER_DELIMITER)`) fires, setting `inFrontmatter = false` at line 232. Subsequent
YAML frontmatter lines like `source_talk: "[[Frontmatter Link]]"` are now treated as body
content and passed to `replaceWikiLinksOutsideInlineCode()` (lines 249–253), causing
`[[Frontmatter Link]]` to be rewritten into a markdown link even though it is still inside
frontmatter.Fix in Cursor | Fix in VSCode Claude
(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** src/ui/file-preview/src/markdown/linking.ts
**Line:** 231:232
**Comment:**
*Logic Error: The frontmatter close check uses `trim()`, so an indented line like ` ---` (which is valid content inside YAML block scalars) is treated as a closing delimiter. That flips parsing out of frontmatter too early and starts rewriting wikilinks that are still inside frontmatter. Detect the closing delimiter only on an actual delimiter line (not whitespace-indented content).
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix|
CodeAnt AI finished reviewing your PR. |
|
Hello @Showiix! From what I can see on current
So this looks valid as a defensive correctness fix for Could you clarify the original repro steps for the issue?
|
|
Thanks for checking this locally. You're right, and sorry for overstating the impact in the original description. I re-checked the current The narrower thing I can still verify is at the helper level: when To answer your questions directly:
I updated the PR description to reflect that narrower scope. |
Summary
rewriteWikiLinks()preserve YAML frontmatter when it receives a full markdown document.---inside YAML block scalars.Scope
This is best described as a defensive correctness fix for the markdown rewrite helper.
On current
main, the editor round-trip path strips YAML frontmatter inpreprocessForEditor()before handing the body to Tiptap, then re-attaches it during serialization. Because of that, I do not want to claim confirmed current-main on-disk corruption through the normal editor/autosave path.The issue this PR verifies is narrower: if
rewriteWikiLinks()/prepareMarkdownSource()is called with a complete markdown document that includes YAML frontmatter, wikilinks inside the frontmatter should stay literal metadata rather than being rewritten as body links.Related to #452.
Tests
node test/test-markdown-preview.jsnpm test