Skip to content

Commit 80ed55f

Browse files
bingryanclaude
andcommitted
fix: use file parameter instead of getActiveFile() for embed processing
Fixes #95 where embedded markdown files were being missed when dealing with large documents. The previous code used `getActiveFile()` which could return undefined or the wrong file during async processing, especially in batch export scenarios. Now uses the `file.path` parameter which is the correct file being exported, ensuring consistent behavior regardless of active file state. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c6259fb commit 80ed55f

1 file changed

Lines changed: 28 additions & 29 deletions

File tree

src/utils.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -932,38 +932,37 @@ export async function tryCopyMarkdownByRead(
932932

933933
// Process embeds BEFORE converting WikiLinks to Markdown
934934
// This ensures block embeds like ![[#^blockid]] are handled correctly
935-
const cfile = plugin.app.workspace.getActiveFile();
936-
if (cfile != undefined) {
937-
const embedMap = await getEmbedMap(plugin, content, cfile.path);
938-
const embeds = await getEmbeds(content);
939-
for (const index in embeds) {
940-
const embedMatch = embeds[index];
941-
const fullMatch = embedMatch[0];
942-
const embedLink = embedMatch[1];
943-
944-
let replacement = embedMap.get(embedLink);
945-
946-
// If not in embedMap and inlineBlockEmbeds is enabled, try to extract block content
947-
if (replacement === undefined && plugin.settings.inlineBlockEmbeds) {
948-
const parsed = parseEmbedLink(embedLink, cfile.path);
949-
if (parsed.blockId && parsed.filePath) {
950-
const blockContent = await getBlockContent(
951-
plugin,
952-
parsed.filePath,
953-
parsed.blockId
954-
);
955-
if (blockContent !== null) {
956-
// Format block content as quote block
957-
replacement = "> " + blockContent.replace(/\n/g, "\n> ");
958-
}
935+
// Use the file being exported instead of getActiveFile() to avoid issues
936+
// when the active file changes during async processing or is undefined
937+
const embedMap = await getEmbedMap(plugin, content, file.path);
938+
const embeds = await getEmbeds(content);
939+
for (const index in embeds) {
940+
const embedMatch = embeds[index];
941+
const fullMatch = embedMatch[0];
942+
const embedLink = embedMatch[1];
943+
944+
let replacement = embedMap.get(embedLink);
945+
946+
// If not in embedMap and inlineBlockEmbeds is enabled, try to extract block content
947+
if (replacement === undefined && plugin.settings.inlineBlockEmbeds) {
948+
const parsed = parseEmbedLink(embedLink, file.path);
949+
if (parsed.blockId && parsed.filePath) {
950+
const blockContent = await getBlockContent(
951+
plugin,
952+
parsed.filePath,
953+
parsed.blockId
954+
);
955+
if (blockContent !== null) {
956+
// Format block content as quote block
957+
replacement = "> " + blockContent.replace(/\n/g, "\n> ");
959958
}
960959
}
960+
}
961961

962-
// Only replace if we found a replacement
963-
// This prevents replacing with "undefined"
964-
if (replacement !== undefined) {
965-
content = content.replace(fullMatch, replacement);
966-
}
962+
// Only replace if we found a replacement
963+
// This prevents replacing with "undefined"
964+
if (replacement !== undefined) {
965+
content = content.replace(fullMatch, replacement);
967966
}
968967
}
969968

0 commit comments

Comments
 (0)