backlinks.mjs
39 lines 1.2 KB
Raw
sha256:8915fe406161f95c1681f9469375e7bae5b28c884f00bedbdef65e4b0cd0738d docs(flow): commit FLOW-V0-SPEC.md hygiene for 7A-INT merge Human 13 hours ago
1 /**
2 * Reverse wikilink index (Issue #1 Phase C2).
3 */
4
5 import { listMarkdownFiles, readNote } from './vault.mjs';
6 import { findFirstWikilinkToTargetInBody, vaultBasenameTargetKey } from './wikilink.mjs';
7
8 /**
9 * @param {import('./config.mjs').loadConfig extends () => infer R ? R : never} config
10 * @param {string} vaultRelativePath - target note path
11 * @returns {{ path: string, backlinks: { path: string, title: string|null, context: string }[] }}
12 */
13 export function runBacklinks(config, vaultRelativePath) {
14 const target = vaultRelativePath.replace(/\\/g, '/');
15 const targetKey = vaultBasenameTargetKey(target);
16 const paths = listMarkdownFiles(config.vault_path, { ignore: config.ignore });
17 const backlinks = [];
18
19 for (const p of paths) {
20 const rel = p.replace(/\\/g, '/');
21 if (rel === target) continue;
22 let note;
23 try {
24 note = readNote(config.vault_path, p);
25 } catch (_) {
26 continue;
27 }
28 const body = note.body || '';
29 const context = findFirstWikilinkToTargetInBody(body, targetKey);
30 if (context == null) continue;
31 backlinks.push({
32 path: rel,
33 title: note.frontmatter?.title ?? null,
34 context,
35 });
36 }
37
38 return { path: target, backlinks };
39 }
File History 1 commit
sha256:8915fe406161f95c1681f9469375e7bae5b28c884f00bedbdef65e4b0cd0738d docs(flow): commit FLOW-V0-SPEC.md hygiene for 7A-INT merge Human 13 hours ago