Skip to content

Commit 9134c43

Browse files
Added nodeId property for metadataCache pos object
1 parent ce11534 commit 9134c43

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

src/@types/Obsidian.d.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Pos } from "obsidian"
2+
13
export * from "obsidian"
24

35
declare module "obsidian" {
@@ -55,15 +57,8 @@ export interface FileCacheEntry {
5557
size: number
5658
}
5759

58-
interface Position {
59-
line: number
60-
col: number
61-
offset: number
62-
}
63-
64-
interface PositionRange {
65-
start: Position
66-
end: Position
60+
export interface CanvasPos extends Pos {
61+
nodeId: string
6762
}
6863

6964
export interface MetadataCacheMap {
@@ -75,26 +70,26 @@ export interface MetadataCacheEntry {
7570
link: string
7671
original: string
7772
displayText: string
78-
position: PositionRange
73+
position: Pos | CanvasPos
7974
}[]
8075
embeds?: {
8176
link: string
8277
original: string
8378
displayText: string
84-
position: PositionRange
79+
position: Pos | CanvasPos
8580
}[]
8681
headings?: {
8782
heading: string
8883
level: number
89-
position: PositionRange
84+
position: Pos | CanvasPos
9085
}[]
9186
listItems?: {
9287
parent: number
93-
position: PositionRange
88+
position: Pos | CanvasPos
9489
}[]
9590
sections?: {
9691
type: "paragraph" | "list" | "heading"
97-
position: PositionRange
92+
position: Pos | CanvasPos
9893
}[]
9994
v: number
10095
}

src/patchers/metadata-cache-patcher.ts

Lines changed: 35 additions & 21 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 { FileCacheEntry, MetadataCacheEntry, MetadataCacheMap, ResolvedLinks } from "src/@types/Obsidian"
3+
import { CanvasPos, FileCacheEntry, MetadataCacheEntry, MetadataCacheMap, ResolvedLinks } 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,31 +44,51 @@ 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) => node.file)
48-
.map((path: string) => ({
49-
link: path,
50-
original: path,
51-
displayText: path,
52-
position: { start: { line: 0, col: 0, offset: 0 }, end: { line: 0, col: 0, offset: 0 } }
47+
.map((node: CanvasNodeData) => ({
48+
link: node.file,
49+
original: node.file,
50+
displayText: node.file,
51+
position: {
52+
nodeId: node.id,
53+
start: { line: 0, col: 0, offset: 0 },
54+
end: { line: 0, col: 0, offset: 0 }
55+
}
5356
}))
5457

5558
// Extract canvas text node links/embeds
5659
const textEncoder = new TextEncoder()
57-
const textNodesMetadataPromises = content.nodes
60+
const textNodes = content.nodes
5861
.filter((node: CanvasNodeData) => node.type === 'text' && node.text)
59-
.map((node: CanvasNodeData) => node.text)
60-
.map((text: string) => textEncoder.encode(text).buffer)
62+
63+
const textNodesIds = textNodes
64+
.map((node: CanvasNodeData) => node.id)
65+
66+
const textNodesMetadataPromises = textNodes
67+
.map((node: CanvasNodeData) => textEncoder.encode(node.text).buffer)
6168
.map((buffer: ArrayBuffer) => this.computeMetadataAsync(buffer) as Promise<MetadataCacheEntry>)
62-
6369
const textNodesMetadata = await Promise.all(textNodesMetadataPromises) // Wait for all text nodes to be resolved
6470

6571
const textNodesEmbeds = textNodesMetadata
66-
.map((metadata: MetadataCacheEntry) => metadata.embeds || [])
67-
.flat()
72+
.map((metadata: MetadataCacheEntry, index: number) => (
73+
(metadata.embeds || []).map(embed => ({
74+
...embed,
75+
position: {
76+
nodeId: textNodesIds[index],
77+
...embed.position
78+
}
79+
}))
80+
)).flat()
6881

6982
const textNodesLinks = textNodesMetadata
70-
.map((metadata: MetadataCacheEntry) => metadata.links || [])
71-
.flat()
83+
.map((metadata: MetadataCacheEntry, index: number) => (
84+
(metadata.links || []).map(link => ({
85+
...link,
86+
position: {
87+
nodeId: textNodesIds[index],
88+
...link.position
89+
}
90+
}))
91+
)).flat()
7292

7393
// Update metadata cache
7494
;(this.metadataCache as MetadataCacheMap)[fileHash] = {
@@ -169,11 +189,5 @@ export default class MetadataCachePatcher extends Patcher {
169189
if (PathHelper.extension(file.path) !== 'canvas') return
170190
this.plugin.app.metadataCache.computeFileMetadataAsync(file)
171191
}))
172-
173-
// Patch complete - reload graph views and local graph views as soon as the layout is ready
174-
this.plugin.app.workspace.onLayoutReady(() => {
175-
const graphViews = [...this.plugin.app.workspace.getLeavesOfType('graph'), ...this.plugin.app.workspace.getLeavesOfType('localgraph')]
176-
for (const view of graphViews) (view as any).rebuildView()
177-
})
178192
}
179193
}

0 commit comments

Comments
 (0)