clawvault 2.6.4 → 2.6.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-ECRZL5XR.js → chunk-2GKPENIR.js} +21 -5
- package/dist/{chunk-ZZA73MFY.js → chunk-33DOSHTA.js} +176 -36
- package/dist/{chunk-4VQTUVH7.js → chunk-7YZWHM36.js} +52 -26
- package/dist/{chunk-YNIPYN4F.js → chunk-A4EAUO7T.js} +1 -1
- package/dist/{chunk-6NYYDNNG.js → chunk-BV5KWZKR.js} +1 -1
- package/dist/chunk-FBITHIZF.js +351 -0
- package/dist/{chunk-OQGYFZ4A.js → chunk-FUSLEY6L.js} +82 -1
- package/dist/{chunk-42MXU7A6.js → chunk-K4GFGKFD.js} +51 -47
- package/dist/{chunk-LUBZXECN.js → chunk-KSZROBFH.js} +1 -1
- package/dist/{chunk-MFL6EEPF.js → chunk-LMKQ7NIF.js} +5 -5
- package/dist/{chunk-MPOSMDMU.js → chunk-M5O6FQ66.js} +4 -4
- package/dist/{chunk-P5EPF6MB.js → chunk-MW5C6ZQA.js} +110 -13
- package/dist/{chunk-IIOU45CK.js → chunk-QSRRMEYM.js} +1 -1
- package/dist/{chunk-P7SY3D4E.js → chunk-SV7T4HRE.js} +3 -3
- package/dist/{chunk-627Q3QWK.js → chunk-T3FKSZSN.js} +1 -1
- package/dist/{chunk-LIGHWOH6.js → chunk-TS6NDVOU.js} +2 -2
- package/dist/{chunk-YDWHS4LJ.js → chunk-YD7SVXTF.js} +20 -3
- package/dist/cli/index.js +11 -11
- package/dist/commands/canvas.js +1 -1
- package/dist/commands/context.js +4 -4
- package/dist/commands/doctor.js +7 -7
- package/dist/commands/embed.js +2 -2
- package/dist/commands/graph.js +2 -2
- package/dist/commands/inject.js +2 -2
- package/dist/commands/link.js +3 -3
- package/dist/commands/observe.js +4 -4
- package/dist/commands/rebuild.js +2 -2
- package/dist/commands/replay.js +2 -2
- package/dist/commands/setup.js +1 -1
- package/dist/commands/sleep.js +5 -5
- package/dist/commands/status.js +6 -6
- package/dist/commands/wake.js +3 -3
- package/dist/index.d.ts +7 -0
- package/dist/index.js +17 -17
- package/dist/lib/auto-linker.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-WIOLLGAD.js +0 -190
- /package/dist/{chunk-NJYJL5AA.js → chunk-5UM4PMMM.js} +0 -0
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
-
loadVaultQmdConfig
|
|
3
|
-
|
|
2
|
+
loadVaultQmdConfig,
|
|
3
|
+
recoverQmdEmbeddingIfNeeded,
|
|
4
|
+
runCrashSafeQmdEmbed
|
|
5
|
+
} from "./chunk-FBITHIZF.js";
|
|
4
6
|
import {
|
|
5
7
|
QmdUnavailableError,
|
|
6
|
-
hasQmd
|
|
7
|
-
qmdEmbed
|
|
8
|
+
hasQmd
|
|
8
9
|
} from "./chunk-5PJ4STIC.js";
|
|
9
10
|
import {
|
|
10
11
|
resolveVaultPath
|
|
@@ -18,12 +19,27 @@ async function embedCommand(options = {}) {
|
|
|
18
19
|
const vaultPath = resolveVaultPath({ explicitPath: options.vaultPath });
|
|
19
20
|
const qmdConfig = loadVaultQmdConfig(vaultPath);
|
|
20
21
|
const startedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
22
|
+
const recovery = recoverQmdEmbeddingIfNeeded({
|
|
23
|
+
vaultPath,
|
|
24
|
+
collection: qmdConfig.qmdCollection,
|
|
25
|
+
rootPath: qmdConfig.qmdRoot,
|
|
26
|
+
mode: "marker-or-empty",
|
|
27
|
+
onLog: options.quiet ? void 0 : (message) => console.log(message)
|
|
28
|
+
});
|
|
29
|
+
if (!options.quiet && recovery.recovered) {
|
|
30
|
+
const reasonLabel = recovery.reason === "interrupted_wal" ? "interrupted run" : "empty vector state";
|
|
31
|
+
console.log(`\u2713 Automatic qmd recovery completed (${reasonLabel}).`);
|
|
32
|
+
}
|
|
21
33
|
if (!options.quiet) {
|
|
22
34
|
console.log(
|
|
23
35
|
`Embedding pending documents for collection "${qmdConfig.qmdCollection}" (root: ${qmdConfig.qmdRoot})...`
|
|
24
36
|
);
|
|
25
37
|
}
|
|
26
|
-
|
|
38
|
+
runCrashSafeQmdEmbed({
|
|
39
|
+
vaultPath,
|
|
40
|
+
collection: qmdConfig.qmdCollection,
|
|
41
|
+
rootPath: qmdConfig.qmdRoot
|
|
42
|
+
});
|
|
27
43
|
const finishedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
28
44
|
if (!options.quiet) {
|
|
29
45
|
console.log(`\u2713 Embedding complete for "${qmdConfig.qmdCollection}"`);
|
|
@@ -1,11 +1,123 @@
|
|
|
1
1
|
// src/lib/memory-graph.ts
|
|
2
2
|
import * as fs from "fs";
|
|
3
|
-
import * as
|
|
3
|
+
import * as path2 from "path";
|
|
4
4
|
import matter from "gray-matter";
|
|
5
5
|
import { glob } from "glob";
|
|
6
|
+
|
|
7
|
+
// src/lib/wiki-links.ts
|
|
8
|
+
import * as path from "path";
|
|
9
|
+
function stripInlineCode(line) {
|
|
10
|
+
let result = "";
|
|
11
|
+
let cursor = 0;
|
|
12
|
+
while (cursor < line.length) {
|
|
13
|
+
if (line[cursor] !== "`") {
|
|
14
|
+
result += line[cursor];
|
|
15
|
+
cursor += 1;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
let fenceLength = 1;
|
|
19
|
+
while (cursor + fenceLength < line.length && line[cursor + fenceLength] === "`") {
|
|
20
|
+
fenceLength += 1;
|
|
21
|
+
}
|
|
22
|
+
const fence = "`".repeat(fenceLength);
|
|
23
|
+
const closeIndex = line.indexOf(fence, cursor + fenceLength);
|
|
24
|
+
if (closeIndex === -1) {
|
|
25
|
+
result += fence;
|
|
26
|
+
cursor += fenceLength;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
cursor = closeIndex + fenceLength;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
function parseFence(line) {
|
|
34
|
+
const match = line.match(/^[ \t]{0,3}(`{3,}|~{3,})/);
|
|
35
|
+
if (!match) return null;
|
|
36
|
+
const marker = match[1][0];
|
|
37
|
+
if (marker !== "`" && marker !== "~") return null;
|
|
38
|
+
return { marker, length: match[1].length };
|
|
39
|
+
}
|
|
40
|
+
function isFenceClose(line, fence) {
|
|
41
|
+
const re = new RegExp(`^[ \\t]{0,3}${fence.marker}{${fence.length},}[ \\t]*$`);
|
|
42
|
+
return re.test(line);
|
|
43
|
+
}
|
|
44
|
+
function stripMarkdownCode(markdownContent) {
|
|
45
|
+
const lines = markdownContent.split("\n");
|
|
46
|
+
const visibleLines = [];
|
|
47
|
+
let activeFence = null;
|
|
48
|
+
for (const line of lines) {
|
|
49
|
+
if (activeFence) {
|
|
50
|
+
if (isFenceClose(line, activeFence)) {
|
|
51
|
+
activeFence = null;
|
|
52
|
+
}
|
|
53
|
+
visibleLines.push("");
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const openingFence = parseFence(line);
|
|
57
|
+
if (openingFence) {
|
|
58
|
+
activeFence = openingFence;
|
|
59
|
+
visibleLines.push("");
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (/^(?: {4}|\t)/.test(line)) {
|
|
63
|
+
visibleLines.push("");
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
visibleLines.push(stripInlineCode(line));
|
|
67
|
+
}
|
|
68
|
+
return visibleLines.join("\n");
|
|
69
|
+
}
|
|
70
|
+
function extractRawWikiLinks(markdownContent) {
|
|
71
|
+
const content = stripMarkdownCode(markdownContent);
|
|
72
|
+
const links = [];
|
|
73
|
+
let cursor = 0;
|
|
74
|
+
while (cursor < content.length) {
|
|
75
|
+
const start = content.indexOf("[[", cursor);
|
|
76
|
+
if (start === -1) break;
|
|
77
|
+
const end = content.indexOf("]]", start + 2);
|
|
78
|
+
if (end === -1) break;
|
|
79
|
+
const candidate = content.slice(start + 2, end).trim();
|
|
80
|
+
if (candidate) {
|
|
81
|
+
links.push(candidate);
|
|
82
|
+
}
|
|
83
|
+
cursor = end + 2;
|
|
84
|
+
}
|
|
85
|
+
return links;
|
|
86
|
+
}
|
|
87
|
+
function normalizeWikiLinkTarget(rawTarget) {
|
|
88
|
+
let value = rawTarget.trim();
|
|
89
|
+
if (!value) return "";
|
|
90
|
+
if (value.startsWith("[[") && value.endsWith("]]")) {
|
|
91
|
+
value = value.slice(2, -2).trim();
|
|
92
|
+
}
|
|
93
|
+
const pipeIndex = value.indexOf("|");
|
|
94
|
+
if (pipeIndex >= 0) {
|
|
95
|
+
value = value.slice(0, pipeIndex);
|
|
96
|
+
}
|
|
97
|
+
if (value.startsWith("#")) {
|
|
98
|
+
return "";
|
|
99
|
+
}
|
|
100
|
+
const hashIndex = value.indexOf("#");
|
|
101
|
+
if (hashIndex >= 0) {
|
|
102
|
+
value = value.slice(0, hashIndex);
|
|
103
|
+
}
|
|
104
|
+
value = value.trim().replace(/\\/g, "/").replace(/^\/+/, "");
|
|
105
|
+
if (!value) return "";
|
|
106
|
+
value = value.split("/").map((segment) => segment.trim()).join("/");
|
|
107
|
+
const normalizedPath = path.posix.normalize(value);
|
|
108
|
+
if (!normalizedPath || normalizedPath === ".") {
|
|
109
|
+
return "";
|
|
110
|
+
}
|
|
111
|
+
value = normalizedPath;
|
|
112
|
+
if (value.toLowerCase().endsWith(".md")) {
|
|
113
|
+
value = value.slice(0, -3);
|
|
114
|
+
}
|
|
115
|
+
return value.trim();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/lib/memory-graph.ts
|
|
6
119
|
var MEMORY_GRAPH_SCHEMA_VERSION = 1;
|
|
7
|
-
var GRAPH_INDEX_RELATIVE_PATH =
|
|
8
|
-
var WIKI_LINK_RE = /\[\[([^\]]+)\]\]/g;
|
|
120
|
+
var GRAPH_INDEX_RELATIVE_PATH = path2.join(".clawvault", "graph-index.json");
|
|
9
121
|
var HASH_TAG_RE = /(^|\s)#([\w-]+)/g;
|
|
10
122
|
var FRONTMATTER_RELATION_FIELDS = [
|
|
11
123
|
"related",
|
|
@@ -19,7 +131,7 @@ var FRONTMATTER_RELATION_FIELDS = [
|
|
|
19
131
|
"links"
|
|
20
132
|
];
|
|
21
133
|
function normalizeRelativePath(value) {
|
|
22
|
-
return value.split(
|
|
134
|
+
return value.split(path2.sep).join("/").replace(/^\.\//, "").replace(/^\/+/, "");
|
|
23
135
|
}
|
|
24
136
|
function toNoteKey(relativePath) {
|
|
25
137
|
const normalized = normalizeRelativePath(relativePath);
|
|
@@ -57,31 +169,14 @@ function inferNodeType(relativePath, frontmatter) {
|
|
|
57
169
|
return "note";
|
|
58
170
|
}
|
|
59
171
|
function ensureClawvaultDir(vaultPath) {
|
|
60
|
-
const dirPath =
|
|
172
|
+
const dirPath = path2.join(vaultPath, ".clawvault");
|
|
61
173
|
if (!fs.existsSync(dirPath)) {
|
|
62
174
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
63
175
|
}
|
|
64
176
|
return dirPath;
|
|
65
177
|
}
|
|
66
178
|
function getGraphIndexPath(vaultPath) {
|
|
67
|
-
return
|
|
68
|
-
}
|
|
69
|
-
function normalizeWikiTarget(target) {
|
|
70
|
-
let value = target.trim();
|
|
71
|
-
if (!value) return "";
|
|
72
|
-
const pipeIndex = value.indexOf("|");
|
|
73
|
-
if (pipeIndex >= 0) {
|
|
74
|
-
value = value.slice(0, pipeIndex);
|
|
75
|
-
}
|
|
76
|
-
const hashIndex = value.indexOf("#");
|
|
77
|
-
if (hashIndex >= 0) {
|
|
78
|
-
value = value.slice(0, hashIndex);
|
|
79
|
-
}
|
|
80
|
-
value = value.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "");
|
|
81
|
-
if (value.toLowerCase().endsWith(".md")) {
|
|
82
|
-
value = value.slice(0, -3);
|
|
83
|
-
}
|
|
84
|
-
return value.trim();
|
|
179
|
+
return path2.join(vaultPath, GRAPH_INDEX_RELATIVE_PATH);
|
|
85
180
|
}
|
|
86
181
|
function collectTags(frontmatter, markdownContent) {
|
|
87
182
|
const tags = /* @__PURE__ */ new Set();
|
|
@@ -105,14 +200,15 @@ function collectTags(frontmatter, markdownContent) {
|
|
|
105
200
|
}
|
|
106
201
|
function extractWikiTargets(markdownContent) {
|
|
107
202
|
const targets = /* @__PURE__ */ new Set();
|
|
108
|
-
for (const
|
|
109
|
-
const candidate = match[1];
|
|
110
|
-
if (!candidate) continue;
|
|
203
|
+
for (const candidate of extractRawWikiLinks(markdownContent)) {
|
|
111
204
|
const normalized = normalizeWikiTarget(candidate);
|
|
112
205
|
if (normalized) targets.add(normalized);
|
|
113
206
|
}
|
|
114
207
|
return [...targets];
|
|
115
208
|
}
|
|
209
|
+
function normalizeWikiTarget(target) {
|
|
210
|
+
return normalizeWikiLinkTarget(target);
|
|
211
|
+
}
|
|
116
212
|
function toStringArray(value) {
|
|
117
213
|
if (typeof value === "string") {
|
|
118
214
|
return value.split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
@@ -150,15 +246,57 @@ function buildNoteRegistry(relativePaths) {
|
|
|
150
246
|
}
|
|
151
247
|
return { byLowerPath, byLowerBasename };
|
|
152
248
|
}
|
|
153
|
-
function
|
|
249
|
+
function normalizeLookupPath(candidate) {
|
|
250
|
+
const normalized = normalizeWikiTarget(candidate);
|
|
251
|
+
if (!normalized) return "";
|
|
252
|
+
const resolved = path2.posix.normalize(normalized).replace(/^\/+/, "");
|
|
253
|
+
if (!resolved || resolved === "." || resolved.startsWith("../")) {
|
|
254
|
+
return "";
|
|
255
|
+
}
|
|
256
|
+
return resolved;
|
|
257
|
+
}
|
|
258
|
+
function buildTargetLookupCandidates(target, sourceNoteKey) {
|
|
259
|
+
const candidates = [];
|
|
260
|
+
const sourceDir = path2.posix.dirname(sourceNoteKey);
|
|
261
|
+
const hasSourceDir = sourceDir !== ".";
|
|
262
|
+
const isRelativeTarget = target.startsWith("./") || target.startsWith("../");
|
|
263
|
+
const addCandidate = (candidate) => {
|
|
264
|
+
const normalized = normalizeLookupPath(candidate);
|
|
265
|
+
if (!normalized || candidates.includes(normalized)) return;
|
|
266
|
+
candidates.push(normalized);
|
|
267
|
+
};
|
|
268
|
+
if (isRelativeTarget) {
|
|
269
|
+
if (hasSourceDir) {
|
|
270
|
+
addCandidate(path2.posix.join(sourceDir, target));
|
|
271
|
+
} else {
|
|
272
|
+
addCandidate(target);
|
|
273
|
+
}
|
|
274
|
+
if (target.startsWith("./")) {
|
|
275
|
+
addCandidate(target.slice(2));
|
|
276
|
+
}
|
|
277
|
+
return candidates;
|
|
278
|
+
}
|
|
279
|
+
if (!target.includes("/")) {
|
|
280
|
+
if (hasSourceDir) {
|
|
281
|
+
addCandidate(`${sourceDir}/${target}`);
|
|
282
|
+
}
|
|
283
|
+
addCandidate(target);
|
|
284
|
+
return candidates;
|
|
285
|
+
}
|
|
286
|
+
addCandidate(target);
|
|
287
|
+
return candidates;
|
|
288
|
+
}
|
|
289
|
+
function resolveTargetNodeId(rawTarget, registry, sourceNoteKey) {
|
|
154
290
|
const normalized = normalizeWikiTarget(rawTarget);
|
|
155
291
|
if (!normalized) {
|
|
156
292
|
return toUnresolvedNodeId(rawTarget);
|
|
157
293
|
}
|
|
158
294
|
const lowerTarget = normalized.toLowerCase();
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
295
|
+
for (const candidate of buildTargetLookupCandidates(normalized, sourceNoteKey)) {
|
|
296
|
+
const direct = registry.byLowerPath.get(candidate.toLowerCase());
|
|
297
|
+
if (direct) {
|
|
298
|
+
return toNoteNodeId(direct);
|
|
299
|
+
}
|
|
162
300
|
}
|
|
163
301
|
if (!normalized.includes("/")) {
|
|
164
302
|
const basenameMatches = registry.byLowerBasename.get(lowerTarget) ?? [];
|
|
@@ -186,7 +324,7 @@ function buildFragmentNode(id, title, type, category, pathValue, tags, missing,
|
|
|
186
324
|
};
|
|
187
325
|
}
|
|
188
326
|
function parseFileFragment(vaultPath, relativePath, mtimeMs, registry) {
|
|
189
|
-
const absolutePath =
|
|
327
|
+
const absolutePath = path2.join(vaultPath, relativePath);
|
|
190
328
|
const raw = fs.readFileSync(absolutePath, "utf-8");
|
|
191
329
|
const parsed = matter(raw);
|
|
192
330
|
const frontmatter = parsed.data ?? {};
|
|
@@ -225,7 +363,7 @@ function parseFileFragment(vaultPath, relativePath, mtimeMs, registry) {
|
|
|
225
363
|
}
|
|
226
364
|
const wikiTargets = extractWikiTargets(parsed.content);
|
|
227
365
|
for (const target of wikiTargets) {
|
|
228
|
-
const targetNodeId = resolveTargetNodeId(target, registry);
|
|
366
|
+
const targetNodeId = resolveTargetNodeId(target, registry, noteKey);
|
|
229
367
|
if (targetNodeId.startsWith("unresolved:") && !nodes.has(targetNodeId)) {
|
|
230
368
|
nodes.set(
|
|
231
369
|
targetNodeId,
|
|
@@ -241,7 +379,7 @@ function parseFileFragment(vaultPath, relativePath, mtimeMs, registry) {
|
|
|
241
379
|
});
|
|
242
380
|
}
|
|
243
381
|
for (const relation of extractFrontmatterRelations(frontmatter)) {
|
|
244
|
-
const targetNodeId = resolveTargetNodeId(relation.target, registry);
|
|
382
|
+
const targetNodeId = resolveTargetNodeId(relation.target, registry, noteKey);
|
|
245
383
|
if (targetNodeId.startsWith("unresolved:") && !nodes.has(targetNodeId)) {
|
|
246
384
|
nodes.set(
|
|
247
385
|
targetNodeId,
|
|
@@ -326,7 +464,7 @@ function isValidIndex(index) {
|
|
|
326
464
|
return typed.schemaVersion === MEMORY_GRAPH_SCHEMA_VERSION && typeof typed.vaultPath === "string" && typeof typed.generatedAt === "string" && Boolean(typed.files && typeof typed.files === "object") && Boolean(typed.graph && typeof typed.graph === "object");
|
|
327
465
|
}
|
|
328
466
|
function loadMemoryGraphIndex(vaultPath) {
|
|
329
|
-
const indexPath = getGraphIndexPath(
|
|
467
|
+
const indexPath = getGraphIndexPath(path2.resolve(vaultPath));
|
|
330
468
|
if (!fs.existsSync(indexPath)) {
|
|
331
469
|
return null;
|
|
332
470
|
}
|
|
@@ -341,7 +479,7 @@ function loadMemoryGraphIndex(vaultPath) {
|
|
|
341
479
|
}
|
|
342
480
|
}
|
|
343
481
|
async function buildOrUpdateMemoryGraphIndex(vaultPathInput, options = {}) {
|
|
344
|
-
const vaultPath =
|
|
482
|
+
const vaultPath = path2.resolve(vaultPathInput);
|
|
345
483
|
ensureClawvaultDir(vaultPath);
|
|
346
484
|
const existing = options.forceFull ? null : loadMemoryGraphIndex(vaultPath);
|
|
347
485
|
const markdownFiles = await glob("**/*.md", {
|
|
@@ -354,7 +492,7 @@ async function buildOrUpdateMemoryGraphIndex(vaultPathInput, options = {}) {
|
|
|
354
492
|
const existingFragments = existing?.files ?? {};
|
|
355
493
|
const currentFileSet = new Set(normalizedFiles);
|
|
356
494
|
for (const relativePath of normalizedFiles) {
|
|
357
|
-
const absolutePath =
|
|
495
|
+
const absolutePath = path2.join(vaultPath, relativePath);
|
|
358
496
|
const stat = fs.statSync(absolutePath);
|
|
359
497
|
const existingFragment = existingFragments[relativePath];
|
|
360
498
|
if (!options.forceFull && existingFragment && existingFragment.mtimeMs === stat.mtimeMs) {
|
|
@@ -391,6 +529,8 @@ async function getMemoryGraph(vaultPath, options = {}) {
|
|
|
391
529
|
}
|
|
392
530
|
|
|
393
531
|
export {
|
|
532
|
+
extractRawWikiLinks,
|
|
533
|
+
normalizeWikiLinkTarget,
|
|
394
534
|
MEMORY_GRAPH_SCHEMA_VERSION,
|
|
395
535
|
loadMemoryGraphIndex,
|
|
396
536
|
buildOrUpdateMemoryGraphIndex,
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildEntityIndex
|
|
3
3
|
} from "./chunk-J7ZWCI2C.js";
|
|
4
|
+
import {
|
|
5
|
+
extractRawWikiLinks,
|
|
6
|
+
normalizeWikiLinkTarget
|
|
7
|
+
} from "./chunk-33DOSHTA.js";
|
|
4
8
|
|
|
5
9
|
// src/lib/backlinks.ts
|
|
6
10
|
import * as fs from "fs";
|
|
7
11
|
import * as path from "path";
|
|
8
12
|
var CLAWVAULT_DIR = ".clawvault";
|
|
9
13
|
var BACKLINKS_FILE = "backlinks.json";
|
|
10
|
-
var WIKI_LINK_REGEX = /\[\[([^\]]+)\]\]/g;
|
|
11
14
|
function ensureClawvaultDir(vaultPath) {
|
|
12
15
|
const dir = path.join(vaultPath, CLAWVAULT_DIR);
|
|
13
16
|
if (!fs.existsSync(dir)) {
|
|
@@ -20,29 +23,47 @@ function toVaultId(vaultPath, filePath) {
|
|
|
20
23
|
return relative2.split(path.sep).join("/");
|
|
21
24
|
}
|
|
22
25
|
function normalizeLinkTarget(raw) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
if (
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
if (target.startsWith("#")) return "";
|
|
33
|
-
const hashIndex = target.indexOf("#");
|
|
34
|
-
if (hashIndex !== -1) {
|
|
35
|
-
target = target.slice(0, hashIndex);
|
|
26
|
+
return normalizeWikiLinkTarget(raw);
|
|
27
|
+
}
|
|
28
|
+
function normalizeLookupCandidate(value) {
|
|
29
|
+
const normalized = normalizeLinkTarget(value);
|
|
30
|
+
if (!normalized) return "";
|
|
31
|
+
const resolved = path.posix.normalize(normalized).replace(/^\/+/, "");
|
|
32
|
+
if (!resolved || resolved === "." || resolved.startsWith("../")) {
|
|
33
|
+
return "";
|
|
36
34
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
return resolved;
|
|
36
|
+
}
|
|
37
|
+
function buildLookupCandidates(target, sourceId) {
|
|
38
|
+
const candidates = [];
|
|
39
|
+
const sourceDir = path.posix.dirname(sourceId);
|
|
40
|
+
const hasSourceDir = sourceDir !== ".";
|
|
41
|
+
const isRelativeTarget = target.startsWith("./") || target.startsWith("../");
|
|
42
|
+
const addCandidate = (candidate) => {
|
|
43
|
+
const normalized = normalizeLookupCandidate(candidate);
|
|
44
|
+
if (!normalized || candidates.includes(normalized)) return;
|
|
45
|
+
candidates.push(normalized);
|
|
46
|
+
};
|
|
47
|
+
if (isRelativeTarget) {
|
|
48
|
+
if (hasSourceDir) {
|
|
49
|
+
addCandidate(path.posix.join(sourceDir, target));
|
|
50
|
+
} else {
|
|
51
|
+
addCandidate(target);
|
|
52
|
+
}
|
|
53
|
+
if (target.startsWith("./")) {
|
|
54
|
+
addCandidate(target.slice(2));
|
|
55
|
+
}
|
|
56
|
+
return candidates;
|
|
41
57
|
}
|
|
42
|
-
if (target.
|
|
43
|
-
|
|
58
|
+
if (!target.includes("/")) {
|
|
59
|
+
if (hasSourceDir) {
|
|
60
|
+
addCandidate(`${sourceDir}/${target}`);
|
|
61
|
+
}
|
|
62
|
+
addCandidate(target);
|
|
63
|
+
return candidates;
|
|
44
64
|
}
|
|
45
|
-
|
|
65
|
+
addCandidate(target);
|
|
66
|
+
return candidates;
|
|
46
67
|
}
|
|
47
68
|
function listMarkdownFiles(vaultPath) {
|
|
48
69
|
const files = [];
|
|
@@ -75,11 +96,16 @@ function buildKnownIds(vaultPath, files) {
|
|
|
75
96
|
}
|
|
76
97
|
return { ids, idsLower };
|
|
77
98
|
}
|
|
78
|
-
function resolveTarget(target, known, entityIndex) {
|
|
99
|
+
function resolveTarget(target, sourceId, known, entityIndex) {
|
|
79
100
|
if (!target) return null;
|
|
80
|
-
|
|
101
|
+
for (const candidate of buildLookupCandidates(target, sourceId)) {
|
|
102
|
+
if (known.ids.has(candidate)) return candidate;
|
|
103
|
+
const lowerCandidate = candidate.toLowerCase();
|
|
104
|
+
if (known.idsLower.has(lowerCandidate)) {
|
|
105
|
+
return known.idsLower.get(lowerCandidate);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
81
108
|
const lower = target.toLowerCase();
|
|
82
|
-
if (known.idsLower.has(lower)) return known.idsLower.get(lower);
|
|
83
109
|
if (entityIndex?.entries.has(lower)) return entityIndex.entries.get(lower);
|
|
84
110
|
return null;
|
|
85
111
|
}
|
|
@@ -93,12 +119,12 @@ function scanVaultLinks(vaultPath, options = {}) {
|
|
|
93
119
|
for (const file of files) {
|
|
94
120
|
const sourceId = toVaultId(vaultPath, file);
|
|
95
121
|
const content = fs.readFileSync(file, "utf-8");
|
|
96
|
-
const matches = content
|
|
122
|
+
const matches = extractRawWikiLinks(content);
|
|
97
123
|
linkCount += matches.length;
|
|
98
124
|
for (const match of matches) {
|
|
99
125
|
const target = normalizeLinkTarget(match);
|
|
100
126
|
if (!target) continue;
|
|
101
|
-
const resolved = resolveTarget(target, known, entityIndex);
|
|
127
|
+
const resolved = resolveTarget(target, sourceId, known, entityIndex);
|
|
102
128
|
if (!resolved) {
|
|
103
129
|
orphans.push({ source: sourceId, target });
|
|
104
130
|
continue;
|