Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions apps/obsidian/src/utils/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ModifyNodeSubmitParams = {

const createModifyNodeModalSubmitHandler = (
plugin: DiscourseGraphPlugin,
editor: Editor,
editor?: Editor,
): ((params: ModifyNodeSubmitParams) => Promise<void>) => {
return async ({
nodeType,
Expand All @@ -36,7 +36,7 @@ const createModifyNodeModalSubmitHandler = (
relationshipTargetFile,
}: ModifyNodeSubmitParams) => {
if (selectedExistingNode) {
editor.replaceSelection(`[[${selectedExistingNode.basename}]]`);
editor?.replaceSelection(`[[${selectedExistingNode.basename}]]`);
await addRelationIfRequested(plugin, selectedExistingNode, {
relationshipId,
relationshipTargetFile,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 8 additions & 9 deletions apps/obsidian/src/utils/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,17 @@ 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: Record<string, unknown>) => {
const mergedFrontmatter = mergeFrontmatter(templateFrontmatter, fm);
Object.assign(fm, mergedFrontmatter);
},
);

await app.fileManager.processFrontMatter(targetFile, (fm) => {
Object.assign(fm, mergedFrontmatter);
});

const frontmatterInfo = getFrontMatterInfo(templateContent);
const templateBody = frontmatterInfo.exists
? templateContent.slice(frontmatterInfo.contentStart)
Expand Down
Loading