From 80ed55fecd13f7fa96e7aa1c31e090ba9c3793a3 Mon Sep 17 00:00:00 2001 From: bingryan Date: Tue, 6 Jan 2026 09:33:35 +0800 Subject: [PATCH] fix: use file parameter instead of getActiveFile() for embed processing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/utils.ts | 57 ++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 3702c63..07fcba9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -932,38 +932,37 @@ export async function tryCopyMarkdownByRead( // Process embeds BEFORE converting WikiLinks to Markdown // This ensures block embeds like ![[#^blockid]] are handled correctly - const cfile = plugin.app.workspace.getActiveFile(); - if (cfile != undefined) { - const embedMap = await getEmbedMap(plugin, content, cfile.path); - const embeds = await getEmbeds(content); - for (const index in embeds) { - const embedMatch = embeds[index]; - const fullMatch = embedMatch[0]; - const embedLink = embedMatch[1]; - - let replacement = embedMap.get(embedLink); - - // If not in embedMap and inlineBlockEmbeds is enabled, try to extract block content - if (replacement === undefined && plugin.settings.inlineBlockEmbeds) { - const parsed = parseEmbedLink(embedLink, cfile.path); - if (parsed.blockId && parsed.filePath) { - const blockContent = await getBlockContent( - plugin, - parsed.filePath, - parsed.blockId - ); - if (blockContent !== null) { - // Format block content as quote block - replacement = "> " + blockContent.replace(/\n/g, "\n> "); - } + // Use the file being exported instead of getActiveFile() to avoid issues + // when the active file changes during async processing or is undefined + const embedMap = await getEmbedMap(plugin, content, file.path); + const embeds = await getEmbeds(content); + for (const index in embeds) { + const embedMatch = embeds[index]; + const fullMatch = embedMatch[0]; + const embedLink = embedMatch[1]; + + let replacement = embedMap.get(embedLink); + + // If not in embedMap and inlineBlockEmbeds is enabled, try to extract block content + if (replacement === undefined && plugin.settings.inlineBlockEmbeds) { + const parsed = parseEmbedLink(embedLink, file.path); + if (parsed.blockId && parsed.filePath) { + const blockContent = await getBlockContent( + plugin, + parsed.filePath, + parsed.blockId + ); + if (blockContent !== null) { + // Format block content as quote block + replacement = "> " + blockContent.replace(/\n/g, "\n> "); } } + } - // Only replace if we found a replacement - // This prevents replacing with "undefined" - if (replacement !== undefined) { - content = content.replace(fullMatch, replacement); - } + // Only replace if we found a replacement + // This prevents replacing with "undefined" + if (replacement !== undefined) { + content = content.replace(fullMatch, replacement); } }