Skip to content

Commit 3b879df

Browse files
committed
fix: accurately handle links with offsets
1 parent 51d58c1 commit 3b879df

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

src/FrontmatterMarkdownLinksCache.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export class FrontmatterMarkdownLinksCache {
5252
this.fileFrontmatterLinkCacheMap.set(filePath, links);
5353
}
5454

55-
filterInPlace(links, (oldLink) => oldLink.key !== link.key);
5655
links.push(link);
5756
this.addStoreAction(FRONTMATTER_LINKS_STORE_NAME, (store) => {
5857
store.put({ filePath, links });
@@ -130,16 +129,7 @@ export class FrontmatterMarkdownLinksCache {
130129
const frontmatterLinksStore = transaction.objectStore(FRONTMATTER_LINKS_STORE_NAME);
131130
const frontmatterLinksEntries = await getResult(frontmatterLinksStore.getAll()) as FrontmatterLinkEntry[];
132131
for (const entry of frontmatterLinksEntries) {
133-
const usedKeys = new Set<string>();
134-
const uniqueLinks = [];
135-
for (const link of entry.links) {
136-
if (usedKeys.has(link.key)) {
137-
continue;
138-
}
139-
usedKeys.add(link.key);
140-
uniqueLinks.push(link);
141-
}
142-
this.fileFrontmatterLinkCacheMap.set(entry.filePath, uniqueLinks);
132+
this.fileFrontmatterLinkCacheMap.set(entry.filePath, entry.links);
143133
}
144134
}
145135

src/Plugin.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -456,42 +456,45 @@ export class Plugin extends PluginBase<PluginTypes> {
456456
processItem: async (note) => {
457457
cachedFilePaths.delete(note.path);
458458
if (this.frontmatterMarkdownLinksCache.isCacheValid(note)) {
459-
const links = this.frontmatterMarkdownLinksCache.getLinks(note);
460-
if (links.length > 0) {
461-
const cache = await getCacheSafe(this.app, note);
462-
if (cache) {
463-
cache.frontmatterLinks ??= [];
464-
const currentLinks = new Map<string, FrontmatterLinkCache>();
465-
466-
for (const link of cache.frontmatterLinks) {
467-
currentLinks.set(link.key, link);
468-
}
459+
const frontmatterMarkdownLinksCacheLinks = this.frontmatterMarkdownLinksCache.getLinks(note);
460+
if (frontmatterMarkdownLinksCacheLinks.length === 0) {
461+
return;
462+
}
463+
const cache = await getCacheSafe(this.app, note);
464+
if (!cache) {
465+
return;
466+
}
467+
cache.frontmatterLinks ??= [];
469468

470-
const linkKeys = new Set(links.map((link) => link.key));
471-
filterInPlace(cache.frontmatterLinks, (link) => !linkKeys.has(link.key));
472-
473-
const newLinks: FrontmatterLinkCache[] = [];
474-
475-
for (const link of links) {
476-
const currentLink = currentLinks.get(link.key);
477-
if (currentLink && currentLink.original !== link.original) {
478-
cache.frontmatterLinks.push(currentLink);
479-
this.frontmatterMarkdownLinksCache.deleteKey(note.path, link.key);
480-
continue;
481-
}
482-
483-
if (cache.frontmatter && getNestedPropertyValue(cache.frontmatter, link.key) instanceof String) {
484-
cache.frontmatterLinks.push(link);
485-
newLinks.push(link);
486-
} else {
487-
this.frontmatterMarkdownLinksCache.deleteKey(note.path, link.key);
488-
}
489-
}
469+
const obsidianLinkMap = new Map<string, FrontmatterLinkCache>();
470+
471+
for (const link of cache.frontmatterLinks) {
472+
obsidianLinkMap.set(link.key, link);
473+
}
490474

491-
for (const link of newLinks) {
492-
this.updateResolvedOrUnresolvedLinksCache(link.link, note.path);
475+
const frontmatterMarkdownLinksCacheKeys = new Set(frontmatterMarkdownLinksCacheLinks.map((link) => link.key));
476+
filterInPlace(cache.frontmatterLinks, (link) => !frontmatterMarkdownLinksCacheKeys.has(link.key));
477+
478+
const newLinks: FrontmatterLinkCache[] = [];
479+
480+
for (const link of frontmatterMarkdownLinksCacheLinks) {
481+
const value = getNestedPropertyValue(cache.frontmatter ?? {}, link.key);
482+
if (value !== link.original) {
483+
this.frontmatterMarkdownLinksCache.deleteKey(note.path, link.key);
484+
const obsidianLink = obsidianLinkMap.get(link.key);
485+
if (obsidianLink) {
486+
cache.frontmatterLinks.push(obsidianLink);
487+
obsidianLinkMap.delete(link.key);
493488
}
489+
continue;
494490
}
491+
492+
cache.frontmatterLinks.push(link);
493+
newLinks.push(link);
494+
}
495+
496+
for (const link of newLinks) {
497+
this.updateResolvedOrUnresolvedLinksCache(link.link, note.path);
495498
}
496499
return;
497500
}

0 commit comments

Comments
 (0)