From 58e57d7c0a9d3975919ed78596577585ecda176e Mon Sep 17 00:00:00 2001 From: Trang Doan Date: Fri, 3 Apr 2026 14:24:56 -0400 Subject: [PATCH 1/2] ENG-1580 fix template not applied during node creation from command palette - Switch create-discourse-node command from editorCallback to callback so it's available from any view (canvas, graph, file explorer, etc.) - Fix stale metadata cache race condition in applyTemplate: compute merged frontmatter inside processFrontMatter callback using live fm instead of potentially stale metadataCache for newly created files - Simplify createModifyNodeModalSubmitHandler to accept optional editor, removing duplicate inline handler Co-Authored-By: Claude Sonnet 4.6 --- apps/obsidian/src/utils/registerCommands.ts | 12 ++++++------ apps/obsidian/src/utils/templates.ts | 10 +++------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/apps/obsidian/src/utils/registerCommands.ts b/apps/obsidian/src/utils/registerCommands.ts index 25d8f0619..4a7a5c044 100644 --- a/apps/obsidian/src/utils/registerCommands.ts +++ b/apps/obsidian/src/utils/registerCommands.ts @@ -26,7 +26,7 @@ type ModifyNodeSubmitParams = { const createModifyNodeModalSubmitHandler = ( plugin: DiscourseGraphPlugin, - editor: Editor, + editor?: Editor, ): ((params: ModifyNodeSubmitParams) => Promise) => { return async ({ nodeType, @@ -36,7 +36,7 @@ const createModifyNodeModalSubmitHandler = ( relationshipTargetFile, }: ModifyNodeSubmitParams) => { if (selectedExistingNode) { - editor.replaceSelection(`[[${selectedExistingNode.basename}]]`); + editor?.replaceSelection(`[[${selectedExistingNode.basename}]]`); await addRelationIfRequested(plugin, selectedExistingNode, { relationshipId, relationshipTargetFile, @@ -91,10 +91,10 @@ export const registerCommands = (plugin: DiscourseGraphPlugin) => { plugin.addCommand({ id: "create-discourse-node", name: "Create discourse node", - editorCallback: (editor: Editor) => { - const currentFile = - plugin.app.workspace.getActiveViewOfType(MarkdownView)?.file || - undefined; + callback: () => { + const activeView = plugin.app.workspace.getActiveViewOfType(MarkdownView); + const editor = activeView?.editor; + const currentFile = activeView?.file || undefined; new ModifyNodeModal(plugin.app, { nodeTypes: plugin.settings.nodeTypes, plugin, diff --git a/apps/obsidian/src/utils/templates.ts b/apps/obsidian/src/utils/templates.ts index 6805c0e33..bbc1e67c7 100644 --- a/apps/obsidian/src/utils/templates.ts +++ b/apps/obsidian/src/utils/templates.ts @@ -124,15 +124,11 @@ export const applyTemplate = async ({ const templateFrontmatter = app.metadataCache.getFileCache(templateFile)?.frontmatter || {}; - const currentFrontmatter = - app.metadataCache.getFileCache(targetFile)?.frontmatter || {}; - - const mergedFrontmatter = mergeFrontmatter( - templateFrontmatter, - currentFrontmatter, - ); + // Read the actual current frontmatter inside processFrontMatter to avoid + // stale metadata cache (newly created files may not be indexed yet). await app.fileManager.processFrontMatter(targetFile, (fm) => { + const mergedFrontmatter = mergeFrontmatter(templateFrontmatter, fm); Object.assign(fm, mergedFrontmatter); }); From 524d1345df23e10a9fd8e6b9ea4f3d4d57abb7fa Mon Sep 17 00:00:00 2001 From: Trang Doan Date: Fri, 3 Apr 2026 14:46:01 -0400 Subject: [PATCH 2/2] fix lint --- apps/obsidian/src/utils/templates.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/obsidian/src/utils/templates.ts b/apps/obsidian/src/utils/templates.ts index bbc1e67c7..5e0469dc0 100644 --- a/apps/obsidian/src/utils/templates.ts +++ b/apps/obsidian/src/utils/templates.ts @@ -127,10 +127,13 @@ export const applyTemplate = async ({ // Read the actual current frontmatter inside processFrontMatter to avoid // stale metadata cache (newly created files may not be indexed yet). - await app.fileManager.processFrontMatter(targetFile, (fm) => { - const mergedFrontmatter = mergeFrontmatter(templateFrontmatter, fm); - Object.assign(fm, mergedFrontmatter); - }); + await app.fileManager.processFrontMatter( + targetFile, + (fm: Record) => { + const mergedFrontmatter = mergeFrontmatter(templateFrontmatter, fm); + Object.assign(fm, mergedFrontmatter); + }, + ); const frontmatterInfo = getFrontMatterInfo(templateContent); const templateBody = frontmatterInfo.exists