backlinks.mjs
39 lines 1.2 KB
Raw
sha256:8d46372e39d2d5a54fd93a8b1c27922fe0d9b22a72197345f1d2c71701cc4ce2 feat(auth): persistent login system + C7 session introspection Human minor ⚠ breaking 16 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 2 commits
sha256:8d46372e39d2d5a54fd93a8b1c27922fe0d9b22a72197345f1d2c71701cc4ce2 feat(auth): persistent login system + C7 session introspection Human minor 16 days ago