Skip to content

Commit 826335b

Browse files
Added per node metadata cache info. Updated documentation
1 parent 6e97b2b commit 826335b

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ Advanced Canvas enables .canvas files to be indexed by the metadata cache. This
130130
- The metadata cache is located in the `app.metadataCache` object - the same object that is used by Obsidian for markdown files
131131
- e.g. `app.metadataCache.getCache`/`app.metadataCache.getFileCache` now works with .canvas files
132132
- The `position` object which is found inside metadata cache entries now contains a new key `nodeId` for .canvas files
133+
- The metadata cache entry for a .canvas file now contains a new key `nodes` which is an object of type `{ [nodeId: string]: MetadataCacheEntry }` - this allows for other plugins to access the full metadata cache for single nodes. The `MetadataCacheEntry` object is the same as for markdown files (even created with the same function - 1:1 compatibility)
133134
- The resolved links object now has entries for .canvas files
134135
- The `app.metadataCache.resolvedLinks` object values for .canvas files are implemented in the exact same way as for markdown files
135136

src/@types/Obsidian.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ export interface MetadataCacheEntry {
9494
v: number
9595
}
9696

97+
export interface MetadataCacheCanvasEntry extends MetadataCacheEntry {
98+
nodes: {
99+
[nodeId: string]: MetadataCacheEntry
100+
}
101+
}
102+
97103
export interface ResolvedLinks {
98104
[path: string]: {
99105
[link: string]: number

src/patchers/metadata-cache-patcher.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TFile } from "obsidian"
22
import { CanvasData, CanvasNodeData } from "src/@types/Canvas"
3-
import { CanvasPos, FileCacheEntry, MetadataCacheEntry, MetadataCacheMap, ResolvedLinks } from "src/@types/Obsidian"
3+
import { MetadataCacheCanvasEntry, MetadataCacheEntry, MetadataCacheMap } from "src/@types/Obsidian"
44
import HashHelper from "src/utils/hash-helper"
55
import PatchHelper from "src/utils/patch-helper"
66
import PathHelper from "src/utils/path-helper"
@@ -44,12 +44,13 @@ export default class MetadataCachePatcher extends Patcher {
4444
// Extract canvas file node embeds
4545
const fileNodesEmbeds = content.nodes
4646
.filter((node: CanvasNodeData) => node.type === 'file' && node.file)
47-
.map((node: CanvasNodeData) => ({
48-
link: node.file,
49-
original: node.file,
50-
displayText: node.file,
47+
.map((node: CanvasNodeData) => [node.id, node.file] as [string, string])
48+
.map(([nodeId, file]) => ({
49+
link: file,
50+
original: file,
51+
displayText: file,
5152
position: {
52-
nodeId: node.id,
53+
nodeId: nodeId,
5354
start: { line: 0, col: 0, offset: 0 },
5455
end: { line: 0, col: 0, offset: 0 }
5556
}
@@ -99,8 +100,14 @@ export default class MetadataCachePatcher extends Patcher {
99100
],
100101
links: [
101102
...textNodesLinks
102-
]
103-
} as MetadataCacheEntry
103+
],
104+
nodes: {
105+
...textNodesMetadata.reduce((acc, metadata, index) => {
106+
acc[textNodesIds[index]] = metadata
107+
return acc
108+
}, {} as Record<string, MetadataCacheEntry>)
109+
}
110+
} satisfies MetadataCacheCanvasEntry as MetadataCacheEntry
104111

105112
// Trigger metadata cache change event
106113
this.trigger('changed', file, "", this.metadataCache[fileHash])

0 commit comments

Comments
 (0)