Skip to content

Commit

Permalink
release: 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
RyotaUshio committed Apr 30, 2024
1 parent 7063524 commit 2d6321f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
38 changes: 31 additions & 7 deletions src/extract.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -178,14 +199,18 @@ export class ExtractionTask extends BetterNoteComposerComponent {
const allBacklinks = this.app.metadataCache.getBacklinksForFile(this.extraction.srcRange.file);
const promises: Promise<void>[] = [];
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);
}
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[] = [];

Expand All @@ -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)
Expand All @@ -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) => {
Expand Down
15 changes: 8 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ export default class BetterNoteComposerPlugin extends Plugin {
}

private registerCommands() {
const showModalAndRun = (srcFile: TFile, callback: (dstFile: TFile, evt: MouseEvent | KeyboardEvent) => Promise<any>) => {
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 = [
Expand All @@ -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({
Expand All @@ -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))
});
}
}),
];
Expand Down

0 comments on commit 2d6321f

Please sign in to comment.