Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion src/ui/file-preview/src/markdown/linking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface ParsedWikiLink {

const WIKI_LINK_PATTERN = /\[\[([^\]|#]*)(?:#([^\]|]+))?(?:\|([^\]]+))?\]\]/g;
const FENCE_PATTERN = /^(`{3,}|~{3,})/;
const FRONTMATTER_DELIMITER = '---';

function encodeLinkPath(pathValue: string): string {
return encodeURI(normalizePathSeparators(pathValue));
Expand Down Expand Up @@ -218,8 +219,21 @@ export function restoreWikiLinks(markdown: string): string {
export function rewriteWikiLinks(source: string): string {
const lines = source.split('\n');
let activeFence: string | null = null;
let inFrontmatter = lines[0]?.trim() === FRONTMATTER_DELIMITER
&& lines.slice(1).some((line) => line.trim() === FRONTMATTER_DELIMITER);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

return lines.map((line, index) => {
if (index === 0 && inFrontmatter) {
return line;
}

if (inFrontmatter) {
if (line.trim() === FRONTMATTER_DELIMITER) {
inFrontmatter = false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
👍 | 👎

}
return line;
}

return lines.map((line) => {
const trimmedStart = line.trimStart();
const fenceMatch = trimmedStart.match(FENCE_PATTERN);
if (fenceMatch) {
Expand Down
23 changes: 23 additions & 0 deletions test/test-markdown-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ async function testWikiRewriteAndRendering() {
assert.ok(fencedRewrite.includes('[[Inside Code]]'), 'Long code fences should remain open until a matching-length close fence appears');
assert.ok(fencedRewrite.includes('[Outside Code](./Outside%20Code.md "mcp-wiki:'), 'Wiki links outside closed fences should still rewrite');

const frontmatterRewrite = rewriteWikiLinks([
'---',
'title: My Note',
'source_talk: "[[Other Note]]"',
'tags: [example]',
'---',
'',
'Body text with a [[Body Link]] for comparison.',
].join('\n'));
assert.strictEqual(
frontmatterRewrite,
[
'---',
'title: My Note',
'source_talk: "[[Other Note]]"',
'tags: [example]',
'---',
'',
'Body text with a [Body Link](./Body%20Link.md "mcp-wiki:%5B%5BBody%20Link%5D%5D") for comparison.',
].join('\n'),
'YAML frontmatter wikilinks should stay literal while body wikilinks still render',
);

const html = renderMarkdown([
'# Title',
'## Details',
Expand Down