fix: preserve code blocks in embedded markdown files (fixes #106)#122
fix: preserve code blocks in embedded markdown files (fixes #106)#122
Conversation
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughRefactors embed resolution in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
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 |
Previously, embedded content was extracted from rendered HTML which converted code blocks (like mermaid) to their rendered form. This made it impossible to preserve the original code block syntax. Now reads raw markdown directly from source files, preserving all code blocks in their original format. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
7a000fb to
20597d1
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/utils.ts (1)
4-4: Remove unused import.The
htmlToMarkdownimport is no longer used after refactoring to read raw markdown directly instead of parsing HTML DOM.🔎 Proposed fix
-import { TAbstractFile, TFile, TFolder, htmlToMarkdown, CachedMetadata } from "obsidian"; +import { TAbstractFile, TFile, TFolder, CachedMetadata } from "obsidian";
🧹 Nitpick comments (1)
src/utils.ts (1)
515-575: Consider extracting YAML header removal logic.The YAML header removal logic is duplicated in three places (lines 546-548, 559-561, 566-568). While this works correctly, you could extract it to reduce duplication.
🔎 Optional refactor to deduplicate YAML header removal
async function getEmbedContentFromSource( plugin: MarkdownExportPlugin, embedLink: string, currentPath: string ): Promise<string | null> { const parsed = parseEmbedLink(embedLink, currentPath); if (!parsed.filePath) { return null; } try { const file = plugin.app.vault.getAbstractFileByPath(parsed.filePath); if (!(file instanceof TFile)) { return null; } let content = await plugin.app.vault.cachedRead(file); // Extract specific section if needed if (parsed.blockId) { const blockContent = await getBlockContent( plugin, parsed.filePath, parsed.blockId ); - // Remove YAML header if configured (in case block is at start of file) - if (plugin.settings.removeYamlHeader && blockContent) { - return blockContent.replace(EMBED_METADATA_REGEXP, ""); - } - return blockContent; + content = blockContent; } - - if (parsed.heading) { + else if (parsed.heading) { const headingContent = await getHeadingContent( plugin, parsed.filePath, parsed.heading ); - // Remove YAML header if configured (in case heading is at start of file) - if (plugin.settings.removeYamlHeader && headingContent) { - return headingContent.replace(EMBED_METADATA_REGEXP, ""); - } - return headingContent; + content = headingContent; } // Remove YAML header if configured for full file content - if (plugin.settings.removeYamlHeader) { + if (content && plugin.settings.removeYamlHeader) { content = content.replace(EMBED_METADATA_REGEXP, ""); } return content; } catch (error) { console.error("Error getting embed content from source:", error); return null; } }
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/utils.ts
🧰 Additional context used
🧬 Code graph analysis (1)
src/utils.ts (1)
src/config.ts (1)
EMBED_METADATA_REGEXP(8-9)
🪛 GitHub Actions: Lint and build
src/utils.ts
[error] 4-4: ESLint: 'htmlToMarkdown' is defined but never used. (no-unused-vars) | Command: eslint src/ --ext .js,.jsx,.ts,.tsx
🪛 GitHub Check: build_and_test (18)
src/utils.ts
[failure] 597-597:
'embedValue' is never reassigned. Use 'const' instead
🪛 GitHub Check: build_and_test (20)
src/utils.ts
[failure] 597-597:
'embedValue' is never reassigned. Use 'const' instead
🔇 Additional comments (2)
src/utils.ts (2)
452-513: LGTM: Heading extraction logic is sound.The function correctly identifies headings, extracts content up to the next heading of equal or higher level, and handles errors gracefully by returning null.
660-715: The path resolution for root-level files is already correctly handled. The code uses ternary operators that properly evaluate emptycurrentDirstrings (which result fromsubstring(0, -1)when files have no directory separator), falling back to use the link text directly. No issues found.
Summary
getEmbedMap()to read raw markdown directly from source files instead of extracting from rendered DOMChanges
getEmbedMap()function to read raw markdown from source filesgetHeadingContent()helper to extract heading sectionsgetEmbedContentFromSource()helper to unify embed content extraction![[file.md]]- full file embeds![[file.md#heading]]- heading section embeds![[file.md#^blockid]]- block reference embedsremoveYamlHeadersetting behaviorTest plan
![[file.md#heading]])![[file.md#^blockid]])removeYamlHeadersetting still works correctly🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes
Refactor
✏️ Tip: You can customize this high-level summary in your review settings.