Skip to content

Commit 186f399

Browse files
committed
fix: cache invalidation
1 parent e0bde16 commit 186f399

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/FrontmatterMarkdownLinksCache.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ export class FrontmatterMarkdownLinksCache {
6767
});
6868
}
6969

70+
public deleteKey(filePath: string, key: string): void {
71+
const links = this.fileFrontmatterLinkCacheMap.get(filePath);
72+
if (!links) {
73+
return;
74+
}
75+
const unfilteredCount = links.length;
76+
filterInPlace(links, (link) => link.key !== key);
77+
if (links.length === unfilteredCount) {
78+
return;
79+
}
80+
81+
if (links.length === 0) {
82+
this.delete(filePath);
83+
return;
84+
}
85+
86+
this.addStoreAction(FRONTMATTER_LINKS_STORE_NAME, (store) => {
87+
store.put({ filePath, links });
88+
});
89+
}
90+
7091
public getFilePaths(): string[] {
7192
return Array.from(this.fileFrontmatterLinkCacheMap.keys());
7293
}

src/Plugin.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,29 @@ export class Plugin extends PluginBase<PluginTypes> {
458458
const cache = await getCacheSafe(this.app, note);
459459
if (cache) {
460460
cache.frontmatterLinks ??= [];
461+
const currentLinks = new Map<string, FrontmatterLinkCache>();
462+
463+
for (const link of cache.frontmatterLinks) {
464+
currentLinks.set(link.key, link);
465+
}
466+
461467
const linkKeys = new Set(links.map((link) => link.key));
462468
filterInPlace(cache.frontmatterLinks, (link) => !linkKeys.has(link.key));
463-
cache.frontmatterLinks.push(...links);
469+
470+
const newLinks: FrontmatterLinkCache[] = [];
464471

465472
for (const link of links) {
473+
const currentLink = currentLinks.get(link.key);
474+
if (currentLink && currentLink.original !== link.original) {
475+
cache.frontmatterLinks.push(currentLink);
476+
continue;
477+
}
478+
479+
cache.frontmatterLinks.push(link);
480+
newLinks.push(link);
481+
}
482+
483+
for (const link of newLinks) {
466484
this.updateResolvedOrUnresolvedLinksCache(link.link, note.path);
467485
}
468486
}
@@ -488,6 +506,7 @@ export class Plugin extends PluginBase<PluginTypes> {
488506

489507
private processFrontmatterLinks(value: unknown, key: string, cache: CachedMetadata, filePath: string): boolean {
490508
if (typeof value === 'string') {
509+
this.frontmatterMarkdownLinksCache.deleteKey(filePath, key);
491510
const parseLinkResults = parseLinks(value);
492511
const isSingleLink = parseLinkResults[0]?.raw === value;
493512

0 commit comments

Comments
 (0)