forked from silverbulletmd/silverbullet-plug-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.ts
More file actions
201 lines (175 loc) · 5.1 KB
/
graph.ts
File metadata and controls
201 lines (175 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { syscall } from "@silverbulletmd/silverbullet/syscall";
export interface GraphNode {
id: string;
name: string;
isCurrent: boolean;
isOrphan: boolean;
}
export interface GraphEdge {
source: string;
target: string;
}
export interface GraphData {
nodes: GraphNode[];
edges: GraphEdge[];
}
export async function buildFullGraph(
currentPage: string,
includeOrphans = false,
): Promise<GraphData> {
const nodeMap = new Map<string, GraphNode>();
const edgeSet = new Set<string>();
const edges: GraphEdge[] = [];
// Query ALL links — no where clause
const allLinks = await syscall("index.queryLuaObjects", "link", {
objectVariable: "l",
}, {});
for (const link of allLinks) {
const source = link.page;
const target = link.toPage;
if (!source || !target || !isPageLink(source) || !isPageLink(target)) {
continue;
}
if (isSystemPage(source) || isSystemPage(target)) continue;
// Ensure both nodes exist
if (!nodeMap.has(source)) {
nodeMap.set(source, {
id: source,
name: source,
isCurrent: source === currentPage,
isOrphan: false,
});
}
if (!nodeMap.has(target)) {
nodeMap.set(target, {
id: target,
name: target,
isCurrent: target === currentPage,
isOrphan: false,
});
}
// Deduplicate edges
const edgeKey = `${source}->${target}`;
if (!edgeSet.has(edgeKey)) {
edgeSet.add(edgeKey);
edges.push({ source, target });
}
}
// Ensure current page is always in the graph even if it has no links
if (!nodeMap.has(currentPage)) {
nodeMap.set(currentPage, {
id: currentPage,
name: currentPage,
isCurrent: true,
isOrphan: false,
});
}
// Add orphan pages (pages with zero links)
if (includeOrphans) {
const allPages = await queryAllPages();
for (const pageName of allPages) {
if (!nodeMap.has(pageName)) {
nodeMap.set(pageName, {
id: pageName,
name: pageName,
isCurrent: pageName === currentPage,
isOrphan: true,
});
}
}
}
return {
nodes: Array.from(nodeMap.values()),
edges,
};
}
/** Query all content pages, filtering out system pages and non-page entries */
async function queryAllPages(): Promise<Set<string>> {
const allPages = await syscall("index.queryLuaObjects", "page", {
objectVariable: "p",
}, {});
const pageNames = new Set<string>();
for (const page of allPages) {
const name = page.name ?? page.ref;
if (!name || !isPageLink(name) || isSystemPage(name)) continue;
pageNames.add(name);
}
return pageNames;
}
export async function buildLocalGraph(
currentPage: string,
): Promise<GraphData> {
const nodeMap = new Map<string, GraphNode>();
const edgeSet = new Set<string>();
const edges: GraphEdge[] = [];
// Add current page as center node
nodeMap.set(currentPage, {
id: currentPage,
name: currentPage,
isCurrent: true,
isOrphan: false,
});
// Query outgoing links: pages linked FROM currentPage
const outWhereExpr = await syscall(
"lua.parseExpression",
"l.page == targetPage",
);
const outlinks = await syscall("index.queryLuaObjects", "link", {
objectVariable: "l",
where: outWhereExpr,
}, { targetPage: currentPage });
for (const link of outlinks) {
const target = link.toPage;
if (!target || !isPageLink(target)) continue;
if (!nodeMap.has(target)) {
nodeMap.set(target, { id: target, name: target, isCurrent: false, isOrphan: false });
}
const edgeKey = `${currentPage}->${target}`;
if (!edgeSet.has(edgeKey)) {
edgeSet.add(edgeKey);
edges.push({ source: currentPage, target });
}
}
// Query backlinks: pages that link TO currentPage
const backWhereExpr = await syscall(
"lua.parseExpression",
"l.toPage == targetPage",
);
const backlinks = await syscall("index.queryLuaObjects", "link", {
objectVariable: "l",
where: backWhereExpr,
}, { targetPage: currentPage });
for (const link of backlinks) {
const source = link.page;
if (!source || !isPageLink(source)) continue;
if (!nodeMap.has(source)) {
nodeMap.set(source, { id: source, name: source, isCurrent: false, isOrphan: false });
}
const edgeKey = `${source}->${currentPage}`;
if (!edgeSet.has(edgeKey)) {
edgeSet.add(edgeKey);
edges.push({ source, target: currentPage });
}
}
return {
nodes: Array.from(nodeMap.values()),
edges,
};
}
/** Filter out system/internal pages */
function isSystemPage(name: string): boolean {
if (/^(PLUGS|SETTINGS|CONFIG)$/.test(name)) return true;
if (name.startsWith("Library/")) return true;
if (name.startsWith("_")) return true;
return false;
}
/** Filter out non-page links (images, URLs, attachments) */
function isPageLink(name: string): boolean {
// Skip external URLs
if (name.startsWith("http://") || name.startsWith("https://")) return false;
// Skip attachment-like paths with file extensions for media
if (/\.(png|jpg|jpeg|gif|svg|webp|pdf|mp3|wav|ogg|mp4)$/i.test(name)) {
return false;
}
return true;
}