Skip to content

Commit

Permalink
fix: wait index file to be cached by obsidian
Browse files Browse the repository at this point in the history
  • Loading branch information
linyibing committed Mar 7, 2024
1 parent afa6891 commit 4b11208
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
26 changes: 16 additions & 10 deletions src/periodic/File.ts
Expand Up @@ -114,19 +114,25 @@ export class File {
}

tags(filePath: string) {
let {
frontmatter: { tags },
} = this.dataview.page(filePath)?.file || { frontmatter: {} };
const file = this.app.vault.getAbstractFileByPath(filePath);

if (!tags) {
return [];
}
if (file instanceof TFile) {
const { frontmatter } = this.app.metadataCache.getFileCache(file) || {
frontmatter: {},
};

if (typeof tags === 'string') {
tags = [tags];
}
let tags = frontmatter?.tags;

if (!tags) {
return [];
}

return tags.map((tag: string) => tag.replace(/^#(.*)$/, '$1'));
if (typeof tags === 'string') {
tags = [tags];
}

return tags.map((tag: string) => tag.replace(/^#(.*)$/, '$1'));
}
}

listByTag = async (
Expand Down
25 changes: 14 additions & 11 deletions src/util.ts
Expand Up @@ -14,6 +14,10 @@ import {
} from './constant';
import { I18N_MAP } from './i18n';

export function sleep(milliseconds: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

export function renderError(
app: App,
msg: string,
Expand Down Expand Up @@ -67,17 +71,16 @@ export async function createFile(

const fileCreated = await app.vault.create(file, templateContent);

await Promise.all([
app.fileManager.processFrontMatter(fileCreated, (frontMatter) => {
if (!tag) {
return;
}

frontMatter.tags = frontMatter.tags || [];
frontMatter.tags.push(tag.replace(/^#/, ''));
}),
app.workspace.getLeaf().openFile(fileCreated),
]);
await app.fileManager.processFrontMatter(fileCreated, (frontMatter) => {
if (!tag) {
return;
}

frontMatter.tags = frontMatter.tags || [];
frontMatter.tags.push(tag.replace(/^#/, ''));
});
await sleep(30); // 等待被索引,否则读取不到 frontmatter:this.app.metadataCache.getFileCache(file)
await app.workspace.getLeaf().openFile(fileCreated);
}
}

Expand Down

0 comments on commit 4b11208

Please sign in to comment.