backlinks.mjs
39 lines 1.2 KB
Raw
sha256:fbe982a22c05c6fe2e93876f250deecdb43d883f648b4e1810c7546caa9f17db docs: activate KNOWTATION- board identity and preserve livi… Human minor ⚠ breaking 2 days 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:fbe982a22c05c6fe2e93876f250deecdb43d883f648b4e1810c7546caa9f17db docs: activate KNOWTATION- board identity and preserve livi… Human minor 2 days ago