From 2d6321f9d4fae61c9def04b48d4203ee40737a3e Mon Sep 17 00:00:00 2001 From: RyotaUshio Date: Tue, 30 Apr 2024 16:58:26 +0900 Subject: [PATCH] release: 0.1.1 --- manifest.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/extract.ts | 38 +++++++++++++++++++++++++++++++------- src/main.ts | 15 ++++++++------- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/manifest.json b/manifest.json index 920c55e..7b5b965 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "better-note-composer", "name": "Better Note Composer", - "version": "0.1.0", + "version": "0.1.1", "minAppVersion": "1.3.5", "description": "", "author": "Ryota Ushio", diff --git a/package-lock.json b/package-lock.json index 3922dc3..5700183 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-better-note-composer", - "version": "0.1.0", + "version": "0.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-better-note-composer", - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "devDependencies": { "@types/node": "^16.18.96", diff --git a/package.json b/package.json index 2cf3bdd..f68544d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-better-note-composer", - "version": "0.1.0", + "version": "0.1.1", "description": "An Obsidian.md plugin", "scripts": { "dev": "node esbuild.config.mjs", diff --git a/src/extract.ts b/src/extract.ts index 7db6f6f..334fb20 100644 --- a/src/extract.ts +++ b/src/extract.ts @@ -1,4 +1,4 @@ -import { BlockSubpathResult, Editor, FrontmatterLinkCache, HeadingSubpathResult, LinkCache, Loc, MarkdownView, PaneType, Reference, ReferenceCache, TFile, parseLinktext, resolveSubpath } from 'obsidian'; +import { BlockSubpathResult, CachedMetadata, Editor, FrontmatterLinkCache, HeadingSubpathResult, LinkCache, Loc, MarkdownView, PaneType, Reference, ReferenceCache, TFile, getLinkpath, parseLinktext, resolveSubpath } from 'obsidian'; import { TransactionSpec, Line } from '@codemirror/state'; import { EditorView } from '@codemirror/view'; @@ -93,7 +93,7 @@ export class ExtractionTask extends BetterNoteComposerComponent { async process(cm: EditorView, paneType: PaneType | boolean) { const extractedContent = await this.getExtractedContent(); - this.updateSrcFile(cm); + await this.updateSrcFile(cm); await this.updateBacklinksInOtherFiles(); await this.openAndAppendToDstFile(extractedContent, paneType); } @@ -128,11 +128,32 @@ export class ExtractionTask extends BetterNoteComposerComponent { return data.slice(this.extraction.srcRange.start.offset, endOffset); } - updateSrcFile(cm: EditorView) { + async updateSrcFile(cm: EditorView) { const sourcePath = this.extraction.srcRange.file.path; const cache = this.app.metadataCache.getCache(sourcePath); if (!cache) throw Error(`${this.plugin.manifest.name}: Cache not found for ${sourcePath}$`); + await this.updateSrcFileFrontmatter(cache); + this.updateSrcFileContent(cm, cache); + } + + async updateSrcFileFrontmatter(cache: CachedMetadata) { + const sourcePath = this.extraction.srcRange.file.path; + const backlinks = (cache.frontmatterLinks ?? []) + .filter((link) => { + const linkpath = getLinkpath(link.link); + const targetFile = this.app.metadataCache.getFirstLinkpathDest(linkpath, sourcePath); + return targetFile?.path === sourcePath; + }); + await this.updateBacklinksInFile(backlinks, sourcePath, { + updateFrontmatter: true, + updateContent: false, + }); + } + + updateSrcFileContent(cm: EditorView, cache: CachedMetadata) { + const sourcePath = this.extraction.srcRange.file.path; + const links = [...cache.links ?? [], ...cache.embeds ?? []]; const shouldBeUpdated = (link: ReferenceCache) => { const isInExtractedRange = contains(this.extraction.srcRange, link.position); @@ -178,6 +199,8 @@ export class ExtractionTask extends BetterNoteComposerComponent { const allBacklinks = this.app.metadataCache.getBacklinksForFile(this.extraction.srcRange.file); const promises: Promise[] = []; for (const sourcePath of allBacklinks.keys()) { + if (sourcePath === this.extraction.srcRange.file.path) continue; + const backlinks: (ReferenceCache | FrontmatterLinkCache)[] = allBacklinks.get(sourcePath) ?? []; const promise = this.updateBacklinksInFile(backlinks, sourcePath); promises.push(promise); @@ -185,7 +208,9 @@ export class ExtractionTask extends BetterNoteComposerComponent { await Promise.all(promises); } - async updateBacklinksInFile(backlinks: (ReferenceCache | FrontmatterLinkCache)[], sourcePath: string) { + async updateBacklinksInFile(backlinks: (ReferenceCache | FrontmatterLinkCache)[], sourcePath: string, options?: { updateFrontmatter: boolean, updateContent: boolean }) { + options = { updateFrontmatter: true, updateContent: true, ...options }; + const linksToBeUpdated: ReferenceCache[] = []; const frontmatterLinksToBeUpdated: FrontmatterLinkCache[] = []; @@ -210,8 +235,7 @@ export class ExtractionTask extends BetterNoteComposerComponent { const file = this.app.vault.getAbstractFileByPath(sourcePath); if (!(file instanceof TFile)) return; - if (linksToBeUpdated.length > 0 - && sourcePath !== this.extraction.srcRange.file.path) { + if (options.updateContent && linksToBeUpdated.length > 0) { await this.app.vault.process(file, (data) => { linksToBeUpdated .sort((a, b) => b.position.start.offset - a.position.start.offset) @@ -223,7 +247,7 @@ export class ExtractionTask extends BetterNoteComposerComponent { }) } - if (frontmatterLinksToBeUpdated.length > 0) { + if (options.updateFrontmatter && frontmatterLinksToBeUpdated.length > 0) { await this.app.fileManager.processFrontMatter(file, (frontmatter) => { frontmatterLinksToBeUpdated .forEach((backlink) => { diff --git a/src/main.ts b/src/main.ts index dbe9d6a..c40b969 100644 --- a/src/main.ts +++ b/src/main.ts @@ -28,14 +28,11 @@ export default class BetterNoteComposerPlugin extends Plugin { } private registerCommands() { - const showModalAndRun = (srcFile: TFile, callback: (dstFile: TFile, evt: MouseEvent | KeyboardEvent) => Promise) => { + const showModalAndRun = (srcFile: TFile, callback: (dstFile: TFile, evt: MouseEvent | KeyboardEvent) => any) => { new MarkdownFileChooserModal(this) .setFilter((file) => file !== srcFile) .suggestFiles() - .then(async (dstFile, evt) => { - await callback(dstFile, evt); - // await this.app.workspace.getLeaf(Keymap.isModEvent(evt)).openFile(dstFile); - }); + .then(callback); }; const commands = [ @@ -50,7 +47,9 @@ export default class BetterNoteComposerPlugin extends Plugin { }, executor: (editor, info) => { const srcFile = info.file!; - showModalAndRun(srcFile, (dstFile, evt) => this.extractor.extractSelection(srcFile, editor, dstFile, Keymap.isModEvent(evt))); + showModalAndRun(srcFile, (dstFile, evt) => { + this.extractor.extractSelection(srcFile, editor, dstFile, Keymap.isModEvent(evt)) + }); } }), new BetterNoteComposerEditorCommand({ @@ -59,7 +58,9 @@ export default class BetterNoteComposerPlugin extends Plugin { checker: (editor, info) => !!info.file, executor: (editor, info) => { const srcFile = info.file!; - showModalAndRun(srcFile, (dstFile, evt) => this.extractor.extractHeading(srcFile, editor, dstFile, Keymap.isModEvent(evt))); + showModalAndRun(srcFile, (dstFile, evt) => { + this.extractor.extractHeading(srcFile, editor, dstFile, Keymap.isModEvent(evt)) + }); } }), ];