@rubytech/create-maxy-code 0.1.138 → 0.1.142

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.
Files changed (108) hide show
  1. package/package.json +1 -1
  2. package/payload/platform/config/brand.json +1 -1
  3. package/payload/platform/lib/obsidian-parser/dist/index.d.ts +98 -0
  4. package/payload/platform/lib/obsidian-parser/dist/index.d.ts.map +1 -0
  5. package/payload/platform/lib/obsidian-parser/dist/index.js +480 -0
  6. package/payload/platform/lib/obsidian-parser/dist/index.js.map +1 -0
  7. package/payload/platform/lib/obsidian-parser/src/index.ts +572 -0
  8. package/payload/platform/lib/obsidian-parser/tsconfig.json +8 -0
  9. package/payload/platform/neo4j/schema.cypher +48 -67
  10. package/payload/platform/package.json +2 -2
  11. package/payload/platform/plugins/.claude-plugin/marketplace.json +15 -0
  12. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-erase.d.ts.map +1 -1
  13. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-erase.js +14 -8
  14. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-erase.js.map +1 -1
  15. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-export.d.ts +1 -1
  16. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-export.d.ts.map +1 -1
  17. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-export.js +10 -7
  18. package/payload/platform/plugins/contacts/mcp/dist/tools/contact-export.js.map +1 -1
  19. package/payload/platform/plugins/docs/references/internals.md +1 -1
  20. package/payload/platform/plugins/docs/references/neo4j.md +3 -3
  21. package/payload/platform/plugins/docs/references/plugins-guide.md +3 -0
  22. package/payload/platform/plugins/email/.claude-plugin/plugin.json +1 -1
  23. package/payload/platform/plugins/email/PLUGIN.md +3 -7
  24. package/payload/platform/plugins/email/mcp/dist/index.js +7 -46
  25. package/payload/platform/plugins/email/mcp/dist/index.js.map +1 -1
  26. package/payload/platform/plugins/email/mcp/dist/lib/credentials.d.ts.map +1 -1
  27. package/payload/platform/plugins/email/mcp/dist/lib/credentials.js +4 -15
  28. package/payload/platform/plugins/email/mcp/dist/lib/credentials.js.map +1 -1
  29. package/payload/platform/plugins/email/mcp/dist/lib/graph.d.ts +51 -43
  30. package/payload/platform/plugins/email/mcp/dist/lib/graph.d.ts.map +1 -1
  31. package/payload/platform/plugins/email/mcp/dist/lib/graph.js +223 -229
  32. package/payload/platform/plugins/email/mcp/dist/lib/graph.js.map +1 -1
  33. package/payload/platform/plugins/email/mcp/dist/scripts/email-fetch.js +7 -4
  34. package/payload/platform/plugins/email/mcp/dist/scripts/email-fetch.js.map +1 -1
  35. package/payload/platform/plugins/email/mcp/dist/tools/email-graph-query.d.ts +14 -4
  36. package/payload/platform/plugins/email/mcp/dist/tools/email-graph-query.d.ts.map +1 -1
  37. package/payload/platform/plugins/email/mcp/dist/tools/email-graph-query.js +69 -84
  38. package/payload/platform/plugins/email/mcp/dist/tools/email-graph-query.js.map +1 -1
  39. package/payload/platform/plugins/email/mcp/dist/tools/email-reply.d.ts.map +1 -1
  40. package/payload/platform/plugins/email/mcp/dist/tools/email-reply.js +49 -64
  41. package/payload/platform/plugins/email/mcp/dist/tools/email-reply.js.map +1 -1
  42. package/payload/platform/plugins/email/mcp/dist/tools/email-setup.d.ts.map +1 -1
  43. package/payload/platform/plugins/email/mcp/dist/tools/email-setup.js +1 -2
  44. package/payload/platform/plugins/email/mcp/dist/tools/email-setup.js.map +1 -1
  45. package/payload/platform/plugins/memory/PLUGIN.md +3 -0
  46. package/payload/platform/plugins/memory/mcp/dist/index.js +89 -2
  47. package/payload/platform/plugins/memory/mcp/dist/index.js.map +1 -1
  48. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/index.d.ts +1 -0
  49. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/index.d.ts.map +1 -1
  50. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/index.js +3 -0
  51. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/index.js.map +1 -1
  52. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/types.d.ts +1 -1
  53. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/types.d.ts.map +1 -1
  54. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/types.js +1 -0
  55. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/types.js.map +1 -1
  56. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/x-dm.d.ts +3 -0
  57. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/x-dm.d.ts.map +1 -0
  58. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/x-dm.js +101 -0
  59. package/payload/platform/plugins/memory/mcp/dist/lib/conversation-normalisers/x-dm.js.map +1 -0
  60. package/payload/platform/plugins/memory/mcp/dist/lib/typed-edge-schema.d.ts.map +1 -1
  61. package/payload/platform/plugins/memory/mcp/dist/lib/typed-edge-schema.js +7 -4
  62. package/payload/platform/plugins/memory/mcp/dist/lib/typed-edge-schema.js.map +1 -1
  63. package/payload/platform/plugins/memory/mcp/dist/tools/memory-archive-write.d.ts +122 -1
  64. package/payload/platform/plugins/memory/mcp/dist/tools/memory-archive-write.d.ts.map +1 -1
  65. package/payload/platform/plugins/memory/mcp/dist/tools/memory-archive-write.js +580 -0
  66. package/payload/platform/plugins/memory/mcp/dist/tools/memory-archive-write.js.map +1 -1
  67. package/payload/platform/plugins/memory/mcp/dist/tools/obsidian-vault-import.d.ts +127 -0
  68. package/payload/platform/plugins/memory/mcp/dist/tools/obsidian-vault-import.d.ts.map +1 -0
  69. package/payload/platform/plugins/memory/mcp/dist/tools/obsidian-vault-import.js +477 -0
  70. package/payload/platform/plugins/memory/mcp/dist/tools/obsidian-vault-import.js.map +1 -0
  71. package/payload/platform/plugins/memory/references/schema-base.md +4 -4
  72. package/payload/platform/plugins/memory/skills/conversation-archive/SKILL.md +2 -1
  73. package/payload/platform/plugins/memory/skills/document-ingest/SKILL.md +1 -0
  74. package/payload/platform/plugins/notion-import/.claude-plugin/plugin.json +8 -0
  75. package/payload/platform/plugins/notion-import/PLUGIN.md +27 -0
  76. package/payload/platform/plugins/notion-import/skills/notion-import/SKILL.md +114 -0
  77. package/payload/platform/plugins/notion-import/skills/notion-import/references/attachments.md +55 -0
  78. package/payload/platform/plugins/notion-import/skills/notion-import/references/databases.md +83 -0
  79. package/payload/platform/plugins/notion-import/skills/notion-import/references/page-tree.md +61 -0
  80. package/payload/platform/plugins/notion-import/skills/notion-import/references/workspace-export.md +41 -0
  81. package/payload/platform/plugins/obsidian-import/.claude-plugin/plugin.json +8 -0
  82. package/payload/platform/plugins/obsidian-import/PLUGIN.md +39 -0
  83. package/payload/platform/plugins/obsidian-import/skills/obsidian-import/SKILL.md +92 -0
  84. package/payload/platform/plugins/obsidian-import/skills/obsidian-import/references/attachments.md +80 -0
  85. package/payload/platform/plugins/obsidian-import/skills/obsidian-import/references/daily-notes.md +31 -0
  86. package/payload/platform/plugins/obsidian-import/skills/obsidian-import/references/vault-structure.md +46 -0
  87. package/payload/platform/plugins/obsidian-import/skills/obsidian-import/references/wikilinks.md +70 -0
  88. package/payload/platform/plugins/scheduling/PLUGIN.md +18 -0
  89. package/payload/platform/plugins/scheduling/mcp/dist/index.js +93 -0
  90. package/payload/platform/plugins/scheduling/mcp/dist/index.js.map +1 -1
  91. package/payload/platform/plugins/scheduling/mcp/dist/lib/ics-graph-ingest.d.ts +80 -0
  92. package/payload/platform/plugins/scheduling/mcp/dist/lib/ics-graph-ingest.d.ts.map +1 -0
  93. package/payload/platform/plugins/scheduling/mcp/dist/lib/ics-graph-ingest.js +395 -0
  94. package/payload/platform/plugins/scheduling/mcp/dist/lib/ics-graph-ingest.js.map +1 -0
  95. package/payload/platform/plugins/scheduling/mcp/dist/lib/ics.d.ts +16 -0
  96. package/payload/platform/plugins/scheduling/mcp/dist/lib/ics.d.ts.map +1 -1
  97. package/payload/platform/plugins/scheduling/mcp/dist/lib/ics.js +111 -2
  98. package/payload/platform/plugins/scheduling/mcp/dist/lib/ics.js.map +1 -1
  99. package/payload/platform/plugins/scheduling/mcp/dist/tools/schedule-archive-ics.d.ts +20 -0
  100. package/payload/platform/plugins/scheduling/mcp/dist/tools/schedule-archive-ics.d.ts.map +1 -0
  101. package/payload/platform/plugins/scheduling/mcp/dist/tools/schedule-archive-ics.js +63 -0
  102. package/payload/platform/plugins/scheduling/mcp/dist/tools/schedule-archive-ics.js.map +1 -0
  103. package/payload/platform/plugins/x-import/.claude-plugin/plugin.json +8 -0
  104. package/payload/platform/plugins/x-import/PLUGIN.md +33 -0
  105. package/payload/platform/plugins/x-import/references/archive-shape.md +124 -0
  106. package/payload/platform/plugins/x-import/references/transcript-shape.md +121 -0
  107. package/payload/platform/plugins/x-import/skills/x-import/SKILL.md +123 -0
  108. package/payload/platform/templates/specialists/agents/database-operator.md +18 -2
@@ -0,0 +1,572 @@
1
+ /**
2
+ * obsidian-parser — pure functions for parsing an Obsidian vault on disk.
3
+ *
4
+ * No DB access, no MCP, no I/O side effects other than fs reads. Composed by
5
+ * the `obsidian-vault-import` MCP tool in the memory plugin.
6
+ *
7
+ * Doctrine: every regex is linear-time (no catastrophic backtracking), every
8
+ * function is deterministic. The agent never calls this code; the server does.
9
+ */
10
+
11
+ import { createHash } from "node:crypto";
12
+ import { promises as fs } from "node:fs";
13
+ import * as path from "node:path";
14
+
15
+ // ---------------------------------------------------------------------------
16
+ // Types
17
+ // ---------------------------------------------------------------------------
18
+
19
+ export interface ParsedPage {
20
+ /** Path relative to the vault root, with forward slashes. The MERGE natural key. */
21
+ obsidianPath: string;
22
+ /** Filename without `.md` — the default wikilink target form. */
23
+ title: string;
24
+ /** Aliases declared in frontmatter (`aliases:` array). */
25
+ aliases: string[];
26
+ /** Tag tokens (without `#`), unioned from frontmatter `tags:` and inline `#tag` body matches. */
27
+ tags: string[];
28
+ /** Body text (frontmatter stripped). Stored verbatim. */
29
+ body: string;
30
+ /** SHA-256 of the on-disk file bytes — idempotency key. */
31
+ contentHash: string;
32
+ /** Detected daily-note date (YYYY-MM-DD) or null. */
33
+ noteDate: string | null;
34
+ /** Wikilinks emitted by this page. */
35
+ wikilinks: WikilinkOccurrence[];
36
+ /** Embedded attachments declared via `![[file.png]]` or `![alt](file.png)`. */
37
+ attachments: AttachmentOccurrence[];
38
+ /** Last-modified timestamp from fs stat — used for modified-since filtering. */
39
+ mtimeMs: number;
40
+ }
41
+
42
+ export interface WikilinkOccurrence {
43
+ /** The exact text inside `[[…]]` before any alias `|`. May contain `#anchor`. */
44
+ rawTarget: string;
45
+ /** Target after stripping `#anchor`. */
46
+ target: string;
47
+ /** Anchor portion (after `#`) or null. */
48
+ anchor: string | null;
49
+ /** Alias text after `|`, or null when absent. The display text. */
50
+ alias: string | null;
51
+ }
52
+
53
+ export interface AttachmentOccurrence {
54
+ /** Path as written in the markdown. May be relative to the page or to the vault root. */
55
+ rawPath: string;
56
+ /** Resolved path relative to vault root, normalised. May not exist on disk yet. */
57
+ resolvedPath: string;
58
+ /** Display alt text (from `![alt](path)` form) or null for embed-syntax `![[file]]`. */
59
+ alt: string | null;
60
+ }
61
+
62
+ export interface VaultWalkOptions {
63
+ /** Filter: only pages whose path starts with one of these folder prefixes. */
64
+ folderPrefixes?: string[];
65
+ /** Filter: only pages whose tags intersect with this set. */
66
+ tagFilter?: string[];
67
+ /** Filter: only pages modified at or after this epoch-ms. */
68
+ modifiedSinceMs?: number;
69
+ }
70
+
71
+ // ---------------------------------------------------------------------------
72
+ // Vault walk
73
+ // ---------------------------------------------------------------------------
74
+
75
+ /**
76
+ * Walk the vault directory and return one ParsedPage per `.md` file that
77
+ * survives the optional filter set.
78
+ *
79
+ * Path-traversal defence: paths containing `..` segments are rejected at
80
+ * entry. The `vaultRoot` passed in is assumed to have already been verified
81
+ * by the caller as living under the account's archive directory.
82
+ */
83
+ export async function walkVault(
84
+ vaultRoot: string,
85
+ opts: VaultWalkOptions = {},
86
+ ): Promise<ParsedPage[]> {
87
+ const pages: ParsedPage[] = [];
88
+ const queue: string[] = [vaultRoot];
89
+
90
+ while (queue.length > 0) {
91
+ const dir = queue.shift()!;
92
+ let entries;
93
+ try {
94
+ entries = await fs.readdir(dir, { withFileTypes: true });
95
+ } catch (err) {
96
+ throw new Error(
97
+ `obsidian-parser: cannot read directory ${dir}: ${
98
+ err instanceof Error ? err.message : String(err)
99
+ }`,
100
+ );
101
+ }
102
+ for (const entry of entries) {
103
+ if (entry.name.startsWith(".")) continue;
104
+ const abs = path.join(dir, entry.name);
105
+ if (entry.isDirectory()) {
106
+ queue.push(abs);
107
+ continue;
108
+ }
109
+ if (!entry.isFile()) continue;
110
+ if (!entry.name.toLowerCase().endsWith(".md")) continue;
111
+
112
+ const rel = toPosixRelative(vaultRoot, abs);
113
+ if (rel.split("/").includes("..")) continue;
114
+
115
+ if (opts.folderPrefixes && opts.folderPrefixes.length > 0) {
116
+ const matchesPrefix = opts.folderPrefixes.some((p) =>
117
+ rel === p || rel.startsWith(p.replace(/\/?$/, "/")),
118
+ );
119
+ if (!matchesPrefix) continue;
120
+ }
121
+
122
+ let stat;
123
+ let bytes;
124
+ try {
125
+ stat = await fs.stat(abs);
126
+ bytes = await fs.readFile(abs);
127
+ } catch (err) {
128
+ // Per-file isolation — log and skip, don't abort the walk.
129
+ process.stderr.write(
130
+ `[obsidian-parser] event=read-failed file=${rel} reason=${
131
+ err instanceof Error ? err.message : String(err)
132
+ }\n`,
133
+ );
134
+ continue;
135
+ }
136
+
137
+ if (opts.modifiedSinceMs && stat.mtimeMs < opts.modifiedSinceMs) continue;
138
+
139
+ let parsed: ParsedPage;
140
+ try {
141
+ parsed = parsePage(rel, bytes, stat.mtimeMs);
142
+ } catch (err) {
143
+ process.stderr.write(
144
+ `[obsidian-parser] event=parse-failed file=${rel} reason=${
145
+ err instanceof Error ? err.message : String(err)
146
+ }\n`,
147
+ );
148
+ continue;
149
+ }
150
+
151
+ if (opts.tagFilter && opts.tagFilter.length > 0) {
152
+ const wanted = new Set(opts.tagFilter.map(normalizeTag));
153
+ const hit = parsed.tags.some((t) => wanted.has(normalizeTag(t)));
154
+ if (!hit) continue;
155
+ }
156
+
157
+ pages.push(parsed);
158
+ }
159
+ }
160
+
161
+ pages.sort((a, b) => a.obsidianPath.localeCompare(b.obsidianPath));
162
+ return pages;
163
+ }
164
+
165
+ function toPosixRelative(root: string, abs: string): string {
166
+ const rel = path.relative(root, abs);
167
+ return rel.split(path.sep).join("/");
168
+ }
169
+
170
+ // ---------------------------------------------------------------------------
171
+ // Page parser
172
+ // ---------------------------------------------------------------------------
173
+
174
+ const DAILY_NOTE_FILENAME_RE = /^(\d{4})-(\d{2})-(\d{2})(?:\.md)?$/;
175
+ const DAILY_NOTE_FOLDER_NAMES = new Set([
176
+ "daily",
177
+ "daily-notes",
178
+ "daily notes",
179
+ "journal",
180
+ "journals",
181
+ ]);
182
+
183
+ export function parsePage(
184
+ obsidianPath: string,
185
+ bytes: Buffer,
186
+ mtimeMs: number,
187
+ ): ParsedPage {
188
+ const contentHash = createHash("sha256").update(bytes).digest("hex");
189
+ const text = bytes.toString("utf8");
190
+
191
+ const { frontmatter, body } = splitFrontmatter(text);
192
+
193
+ const title = path
194
+ .basename(obsidianPath)
195
+ .replace(/\.md$/i, "")
196
+ .normalize("NFC");
197
+
198
+ const aliases = extractAliases(frontmatter);
199
+ const frontmatterTags = extractFrontmatterTags(frontmatter);
200
+ const { bodyTags, wikilinks, attachments } = scanBody(body, obsidianPath);
201
+ const tags = dedupeNormalized([...frontmatterTags, ...bodyTags]);
202
+ const noteDate = detectDailyNote(obsidianPath, frontmatter);
203
+
204
+ return {
205
+ obsidianPath,
206
+ title,
207
+ aliases: aliases.map((a) => a.normalize("NFC")),
208
+ tags,
209
+ body,
210
+ contentHash,
211
+ noteDate,
212
+ wikilinks,
213
+ attachments,
214
+ mtimeMs,
215
+ };
216
+ }
217
+
218
+ // ---------------------------------------------------------------------------
219
+ // Frontmatter
220
+ // ---------------------------------------------------------------------------
221
+
222
+ interface ParsedFrontmatter {
223
+ raw: string;
224
+ /** Map of top-level key → raw value string. Arrays are returned as the
225
+ * block following the key (caller parses per-key). */
226
+ fields: Map<string, string>;
227
+ }
228
+
229
+ const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/;
230
+
231
+ export function splitFrontmatter(
232
+ text: string,
233
+ ): { frontmatter: ParsedFrontmatter; body: string } {
234
+ const match = text.match(FRONTMATTER_RE);
235
+ if (!match) {
236
+ return { frontmatter: { raw: "", fields: new Map() }, body: text };
237
+ }
238
+ const raw = match[1];
239
+ const body = text.slice(match[0].length);
240
+
241
+ // Minimal YAML-subset parser: top-level `key: value` and `key:` followed by
242
+ // ` - item` lines. Not a full YAML — we explicitly limit the surface so a
243
+ // malformed frontmatter degrades to "no fields found" rather than throwing.
244
+ const fields = new Map<string, string>();
245
+ const lines = raw.split(/\r?\n/);
246
+ let currentKey: string | null = null;
247
+ let currentBlock: string[] = [];
248
+
249
+ for (const line of lines) {
250
+ if (/^[#]/.test(line.trim())) continue; // comment
251
+ const flush = () => {
252
+ if (currentKey !== null) {
253
+ const existing = fields.get(currentKey);
254
+ const next = currentBlock.join("\n");
255
+ fields.set(currentKey, existing ? existing + "\n" + next : next);
256
+ }
257
+ currentKey = null;
258
+ currentBlock = [];
259
+ };
260
+
261
+ const topMatch = line.match(/^([A-Za-z_][\w-]*)\s*:\s*(.*)$/);
262
+ if (topMatch && !line.startsWith(" ") && !line.startsWith("\t")) {
263
+ flush();
264
+ currentKey = topMatch[1];
265
+ const value = topMatch[2];
266
+ if (value.trim() === "") {
267
+ currentBlock = [];
268
+ } else {
269
+ currentBlock = [value];
270
+ flush();
271
+ }
272
+ continue;
273
+ }
274
+ if (currentKey !== null) {
275
+ currentBlock.push(line);
276
+ }
277
+ }
278
+ if (currentKey !== null) {
279
+ const existing = fields.get(currentKey);
280
+ const next = currentBlock.join("\n");
281
+ fields.set(currentKey, existing ? existing + "\n" + next : next);
282
+ }
283
+
284
+ return { frontmatter: { raw, fields }, body };
285
+ }
286
+
287
+ function extractAliases(fm: ParsedFrontmatter): string[] {
288
+ return parseYamlList(fm.fields.get("aliases"));
289
+ }
290
+
291
+ function extractFrontmatterTags(fm: ParsedFrontmatter): string[] {
292
+ return parseYamlList(fm.fields.get("tags")).map(normalizeTag);
293
+ }
294
+
295
+ function parseYamlList(value: string | undefined): string[] {
296
+ if (!value) return [];
297
+ const trimmed = value.trim();
298
+ if (!trimmed) return [];
299
+ // Flow-style: [a, b, c]
300
+ if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
301
+ return trimmed
302
+ .slice(1, -1)
303
+ .split(",")
304
+ .map((s) => s.trim().replace(/^["']|["']$/g, ""))
305
+ .filter(Boolean);
306
+ }
307
+ // Block-style: - item per line, or single scalar
308
+ const lines = trimmed.split(/\r?\n/);
309
+ if (lines.length === 1 && !lines[0].startsWith("-")) {
310
+ return [lines[0].replace(/^["']|["']$/g, "").trim()].filter(Boolean);
311
+ }
312
+ const out: string[] = [];
313
+ for (const line of lines) {
314
+ const m = line.match(/^\s*-\s*(.*?)\s*$/);
315
+ if (m) {
316
+ const v = m[1].replace(/^["']|["']$/g, "").trim();
317
+ if (v) out.push(v);
318
+ }
319
+ }
320
+ return out;
321
+ }
322
+
323
+ // ---------------------------------------------------------------------------
324
+ // Body scan: wikilinks, tags, attachments — all code-fence-aware
325
+ // ---------------------------------------------------------------------------
326
+
327
+ const WIKILINK_RE = /\[\[([^\]\r\n]+)\]\]/g;
328
+ const EMBED_RE = /!\[\[([^\]\r\n]+)\]\]/g;
329
+ const IMAGE_RE = /!\[([^\]\r\n]*)\]\(([^)\r\n]+)\)/g;
330
+ const INLINE_TAG_RE = /(^|[\s(])#([A-Za-z][\w/-]*)/g;
331
+ const ATTACHMENT_EXT_RE = /\.(png|jpe?g|gif|webp|svg|pdf|mp3|mp4|mov|wav|m4a|webm)$/i;
332
+
333
+ interface BodyScan {
334
+ bodyTags: string[];
335
+ wikilinks: WikilinkOccurrence[];
336
+ attachments: AttachmentOccurrence[];
337
+ }
338
+
339
+ function scanBody(body: string, obsidianPath: string): BodyScan {
340
+ const bodyTags: string[] = [];
341
+ const wikilinks: WikilinkOccurrence[] = [];
342
+ const attachments: AttachmentOccurrence[] = [];
343
+
344
+ for (const segment of splitOnCodeFences(body)) {
345
+ if (segment.isCode) continue;
346
+ const text = segment.text;
347
+
348
+ // Embeds first — they share `[[…]]` with wikilinks but the `!` prefix
349
+ // routes them to attachments.
350
+ const embedSpans: Array<[number, number]> = [];
351
+ for (const m of text.matchAll(EMBED_RE)) {
352
+ const inner = m[1].trim();
353
+ const resolved = resolveAttachmentPath(inner, obsidianPath);
354
+ if (resolved && ATTACHMENT_EXT_RE.test(resolved)) {
355
+ attachments.push({ rawPath: inner, resolvedPath: resolved, alt: null });
356
+ } else {
357
+ // Non-attachment embed (e.g. another note transclusion). Treat as a
358
+ // wikilink so the cross-reference is preserved.
359
+ wikilinks.push(parseWikilinkInner(inner));
360
+ }
361
+ embedSpans.push([m.index ?? 0, (m.index ?? 0) + m[0].length]);
362
+ }
363
+
364
+ for (const m of text.matchAll(WIKILINK_RE)) {
365
+ const start = m.index ?? 0;
366
+ if (embedSpans.some(([s, e]) => start >= s - 1 && start < e)) continue;
367
+ wikilinks.push(parseWikilinkInner(m[1]));
368
+ }
369
+
370
+ for (const m of text.matchAll(IMAGE_RE)) {
371
+ const rawPath = m[2].trim();
372
+ const alt = m[1].trim() || null;
373
+ if (/^https?:\/\//i.test(rawPath)) continue; // remote
374
+ const resolved = resolveAttachmentPath(rawPath, obsidianPath);
375
+ if (resolved && ATTACHMENT_EXT_RE.test(resolved)) {
376
+ attachments.push({ rawPath, resolvedPath: resolved, alt });
377
+ }
378
+ }
379
+
380
+ for (const m of text.matchAll(INLINE_TAG_RE)) {
381
+ bodyTags.push(normalizeTag(m[2]));
382
+ }
383
+ }
384
+
385
+ return {
386
+ bodyTags: dedupeNormalized(bodyTags),
387
+ wikilinks,
388
+ attachments,
389
+ };
390
+ }
391
+
392
+ function parseWikilinkInner(inner: string): WikilinkOccurrence {
393
+ const trimmed = inner.trim();
394
+ const aliasIdx = trimmed.indexOf("|");
395
+ let raw = trimmed;
396
+ let alias: string | null = null;
397
+ if (aliasIdx >= 0) {
398
+ raw = trimmed.slice(0, aliasIdx).trim();
399
+ alias = trimmed.slice(aliasIdx + 1).trim() || null;
400
+ }
401
+ const anchorIdx = raw.indexOf("#");
402
+ let target = raw;
403
+ let anchor: string | null = null;
404
+ if (anchorIdx >= 0) {
405
+ target = raw.slice(0, anchorIdx).trim();
406
+ anchor = raw.slice(anchorIdx + 1).trim() || null;
407
+ }
408
+ return { rawTarget: raw, target: target.normalize("NFC"), anchor, alias };
409
+ }
410
+
411
+ function resolveAttachmentPath(
412
+ rawPath: string,
413
+ obsidianPath: string,
414
+ ): string | null {
415
+ if (!rawPath) return null;
416
+ if (rawPath.startsWith("/") || /^[A-Za-z]:[\\/]/.test(rawPath)) return null;
417
+ if (rawPath.split("/").includes("..")) return null;
418
+ // Obsidian's default: vault-root relative. We also accept page-relative.
419
+ // For the resolved form we prefer vault-root-relative (more deterministic
420
+ // for MERGE), normalising path separators.
421
+ const norm = rawPath.replace(/\\/g, "/");
422
+ if (norm.includes("/")) return norm;
423
+ // Bare filename — could be in any attachments folder. Caller's index does
424
+ // the lookup; we record the bare form.
425
+ return norm;
426
+ }
427
+
428
+ function splitOnCodeFences(text: string): Array<{ text: string; isCode: boolean }> {
429
+ const out: Array<{ text: string; isCode: boolean }> = [];
430
+ const fenceRe = /(^|\n)(```|~~~)[^\n]*\n([\s\S]*?)\n(```|~~~)/g;
431
+ let lastIdx = 0;
432
+ for (const m of text.matchAll(fenceRe)) {
433
+ const start = (m.index ?? 0) + (m[1] ? m[1].length : 0);
434
+ out.push({ text: text.slice(lastIdx, start), isCode: false });
435
+ out.push({ text: m[0], isCode: true });
436
+ lastIdx = start + m[0].length - (m[1] ? m[1].length : 0);
437
+ }
438
+ out.push({ text: text.slice(lastIdx), isCode: false });
439
+ return out;
440
+ }
441
+
442
+ // ---------------------------------------------------------------------------
443
+ // Tag normalization
444
+ // ---------------------------------------------------------------------------
445
+
446
+ export function normalizeTag(tag: string): string {
447
+ return tag
448
+ .replace(/^#/, "")
449
+ .normalize("NFC")
450
+ .trim()
451
+ .toLowerCase();
452
+ }
453
+
454
+ function dedupeNormalized(values: string[]): string[] {
455
+ const seen = new Set<string>();
456
+ const out: string[] = [];
457
+ for (const v of values) {
458
+ const k = v.normalize("NFC").trim().toLowerCase();
459
+ if (!k) continue;
460
+ if (seen.has(k)) continue;
461
+ seen.add(k);
462
+ out.push(v);
463
+ }
464
+ return out;
465
+ }
466
+
467
+ // ---------------------------------------------------------------------------
468
+ // Daily-notes detection
469
+ // ---------------------------------------------------------------------------
470
+
471
+ export function detectDailyNote(
472
+ obsidianPath: string,
473
+ fm: ParsedFrontmatter,
474
+ ): string | null {
475
+ // 1. Frontmatter `date:` or `created:` wins if it's an ISO date.
476
+ for (const key of ["date", "created"]) {
477
+ const v = fm.fields.get(key);
478
+ if (v) {
479
+ const m = v.trim().match(/^(\d{4}-\d{2}-\d{2})/);
480
+ if (m) return m[1];
481
+ }
482
+ }
483
+ // 2. Filename matches YYYY-MM-DD.md.
484
+ const basename = path.basename(obsidianPath);
485
+ const fileMatch = basename.match(DAILY_NOTE_FILENAME_RE);
486
+ if (fileMatch) return `${fileMatch[1]}-${fileMatch[2]}-${fileMatch[3]}`;
487
+
488
+ // 3. Filename is YYYY-MM-DD inside a known daily-notes folder.
489
+ const parts = obsidianPath.split("/");
490
+ const inDailyFolder = parts
491
+ .slice(0, -1)
492
+ .some((p) => DAILY_NOTE_FOLDER_NAMES.has(p.toLowerCase()));
493
+ if (inDailyFolder) {
494
+ const stem = basename.replace(/\.md$/i, "");
495
+ const m = stem.match(/^(\d{4})-(\d{2})-(\d{2})$/);
496
+ if (m) return `${m[1]}-${m[2]}-${m[3]}`;
497
+ }
498
+ return null;
499
+ }
500
+
501
+ // ---------------------------------------------------------------------------
502
+ // Wikilink resolution helpers
503
+ // ---------------------------------------------------------------------------
504
+
505
+ export interface VaultIndex {
506
+ /** Lowercased title (without `.md`) → set of `obsidianPath` candidates. */
507
+ byTitle: Map<string, string[]>;
508
+ /** Lowercased alias → set of `obsidianPath` candidates. */
509
+ byAlias: Map<string, string[]>;
510
+ }
511
+
512
+ export function buildVaultIndex(pages: ParsedPage[]): VaultIndex {
513
+ const byTitle = new Map<string, string[]>();
514
+ const byAlias = new Map<string, string[]>();
515
+ for (const p of pages) {
516
+ pushKey(byTitle, p.title.toLowerCase(), p.obsidianPath);
517
+ for (const a of p.aliases) {
518
+ pushKey(byAlias, a.toLowerCase(), p.obsidianPath);
519
+ }
520
+ }
521
+ return { byTitle, byAlias };
522
+ }
523
+
524
+ function pushKey(m: Map<string, string[]>, k: string, v: string): void {
525
+ const arr = m.get(k);
526
+ if (arr) {
527
+ if (!arr.includes(v)) arr.push(v);
528
+ } else {
529
+ m.set(k, [v]);
530
+ }
531
+ }
532
+
533
+ /**
534
+ * Look up a wikilink target inside the vault, returning candidate page paths.
535
+ *
536
+ * The target is matched against (a) page titles and (b) declared aliases.
537
+ * Path-form targets (`Folder/Page`) match directly. Returns an empty array
538
+ * when no intra-vault candidate exists — the caller falls through to entity
539
+ * resolution.
540
+ */
541
+ export function resolveWikilinkInVault(
542
+ target: string,
543
+ index: VaultIndex,
544
+ knownPaths: Set<string>,
545
+ ): string[] {
546
+ const t = target.normalize("NFC").trim();
547
+ if (!t) return [];
548
+ // Path-form: contains a slash or ends with `.md`.
549
+ if (t.includes("/") || t.toLowerCase().endsWith(".md")) {
550
+ const norm = t.replace(/\\/g, "/").replace(/\.md$/i, "") + ".md";
551
+ if (knownPaths.has(norm)) return [norm];
552
+ return [];
553
+ }
554
+ const titleHits = index.byTitle.get(t.toLowerCase()) ?? [];
555
+ const aliasHits = index.byAlias.get(t.toLowerCase()) ?? [];
556
+ const seen = new Set<string>();
557
+ const out: string[] = [];
558
+ for (const p of [...titleHits, ...aliasHits]) {
559
+ if (seen.has(p)) continue;
560
+ seen.add(p);
561
+ out.push(p);
562
+ }
563
+ return out;
564
+ }
565
+
566
+ // ---------------------------------------------------------------------------
567
+ // Import-id helper
568
+ // ---------------------------------------------------------------------------
569
+
570
+ export function makeImportId(): string {
571
+ return `obs-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
572
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src"
6
+ },
7
+ "include": ["src"]
8
+ }