obsidian-sync-mcp 0.12.0 → 0.13.0
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/README.md +2 -2
- package/dist/chunk-IQ2E4UFT.js +330 -0
- package/dist/chunk-IQ2E4UFT.js.map +1 -0
- package/dist/main.js +241 -52
- package/dist/main.js.map +1 -1
- package/dist/{vault-XZDTCOVA.js → vault-MLW7WLXO.js} +55 -25
- package/dist/{vault-XZDTCOVA.js.map → vault-MLW7WLXO.js.map} +1 -1
- package/dist/{vault-local-MF65KDVB.js → vault-local-VOGFWQI2.js} +129 -40
- package/dist/vault-local-VOGFWQI2.js.map +1 -0
- package/package.json +6 -2
- package/dist/chunk-XZ4HUASL.js +0 -289
- package/dist/chunk-XZ4HUASL.js.map +0 -1
- package/dist/vault-local-MF65KDVB.js.map +0 -1
|
@@ -4,12 +4,14 @@ if(!("navigator" in globalThis)){globalThis.navigator={language:"en"};}
|
|
|
4
4
|
import {
|
|
5
5
|
AttachmentExistsError,
|
|
6
6
|
AttachmentMoveIncompleteError,
|
|
7
|
+
NoteExistsError,
|
|
8
|
+
NoteNotFoundError,
|
|
7
9
|
isValidAttachmentPath,
|
|
8
10
|
isValidNotePath,
|
|
9
11
|
parseFrontmatterAndLinks,
|
|
10
12
|
validateAttachmentPath,
|
|
11
13
|
validateNotePath
|
|
12
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-IQ2E4UFT.js";
|
|
13
15
|
|
|
14
16
|
// src/vault-local.ts
|
|
15
17
|
import { readFile, writeFile, unlink, mkdir, stat, lstat, realpath, rename, link, copyFile } from "fs/promises";
|
|
@@ -19,8 +21,34 @@ import { glob } from "fs/promises";
|
|
|
19
21
|
var NOFOLLOW = constants.O_NOFOLLOW ?? 0;
|
|
20
22
|
var LocalVault = class {
|
|
21
23
|
root;
|
|
22
|
-
|
|
24
|
+
/** fs calls used by moveNoReplace, replaceable in tests to simulate failures. */
|
|
25
|
+
linkFile = link;
|
|
26
|
+
unlinkSource = unlink;
|
|
27
|
+
refuseSymlinkWrites;
|
|
28
|
+
/**
|
|
29
|
+
* `refuseSymlinkWrites`: refuse to write, delete or move through a symlink
|
|
30
|
+
* (file or folder) anywhere on the path. Set when WRITE_FOLDERS is, because
|
|
31
|
+
* the permission check sees the requested path: a symlink MCP/alias.md ->
|
|
32
|
+
* Raw/a.md would otherwise let a write in MCP/ change a create-only note.
|
|
33
|
+
*/
|
|
34
|
+
constructor(vaultPath, { refuseSymlinkWrites = false } = {}) {
|
|
23
35
|
this.root = realpathSync(resolve(vaultPath));
|
|
36
|
+
this.refuseSymlinkWrites = refuseSymlinkWrites;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* safePath for a write, delete or move. With refuseSymlinkWrites, the
|
|
40
|
+
* resolved path must be the requested one; only case and Unicode form may
|
|
41
|
+
* differ (case-insensitive file systems return the on-disk spelling).
|
|
42
|
+
*/
|
|
43
|
+
async writePath(path, kind, options = {}) {
|
|
44
|
+
const real = await this.safePath(path, kind, options);
|
|
45
|
+
if (this.refuseSymlinkWrites) {
|
|
46
|
+
const same = (s) => s.replace(/\\/g, "/").replace(/\/{2,}/g, "/").replace(/^\//, "").normalize("NFC").toLowerCase();
|
|
47
|
+
if (same(relative(this.root, real).split(sep).join("/")) !== same(path)) {
|
|
48
|
+
throw new Error(`Refusing to modify a file through a symlink while WRITE_FOLDERS is set: ${path}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return real;
|
|
24
52
|
}
|
|
25
53
|
async safePath(path, kind = "folder", { noSymlink = false } = {}) {
|
|
26
54
|
if (kind === "note") validateNotePath(path);
|
|
@@ -124,18 +152,21 @@ var LocalVault = class {
|
|
|
124
152
|
return null;
|
|
125
153
|
}
|
|
126
154
|
}
|
|
127
|
-
async writeNote(path, content) {
|
|
128
|
-
const fullPath = await this.
|
|
155
|
+
async writeNote(path, content, { mode = "upsert" } = {}) {
|
|
156
|
+
const fullPath = await this.writePath(path, "note");
|
|
157
|
+
const flag = mode === "create" ? constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL : mode === "update" ? constants.O_WRONLY | constants.O_TRUNC : "w";
|
|
129
158
|
try {
|
|
130
|
-
await mkdir(dirname(fullPath), { recursive: true });
|
|
131
|
-
await writeFile(fullPath, content, "utf-8");
|
|
159
|
+
if (mode !== "update") await mkdir(dirname(fullPath), { recursive: true });
|
|
160
|
+
await writeFile(fullPath, content, { encoding: "utf-8", flag });
|
|
132
161
|
return true;
|
|
133
|
-
} catch {
|
|
162
|
+
} catch (e) {
|
|
163
|
+
if (mode === "create" && e.code === "EEXIST") throw new NoteExistsError(path);
|
|
164
|
+
if (mode === "update" && e.code === "ENOENT") throw new NoteNotFoundError(path);
|
|
134
165
|
return false;
|
|
135
166
|
}
|
|
136
167
|
}
|
|
137
168
|
async deleteNote(path) {
|
|
138
|
-
const fullPath = await this.
|
|
169
|
+
const fullPath = await this.writePath(path, "note");
|
|
139
170
|
try {
|
|
140
171
|
await unlink(fullPath);
|
|
141
172
|
return true;
|
|
@@ -143,9 +174,67 @@ var LocalVault = class {
|
|
|
143
174
|
return false;
|
|
144
175
|
}
|
|
145
176
|
}
|
|
177
|
+
/** True when both paths name the same file (same device and inode). */
|
|
178
|
+
async sameFile(a, b) {
|
|
179
|
+
try {
|
|
180
|
+
const [sa, sb] = await Promise.all([stat(a), stat(b)]);
|
|
181
|
+
return sa.dev === sb.dev && sa.ino === sb.ino;
|
|
182
|
+
} catch {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Move `fromPath` to `toPath` without replacing anything there: hard-link,
|
|
188
|
+
* or copy where links aren't supported, then remove the source. rename()
|
|
189
|
+
* can't be used because it replaces an existing destination.
|
|
190
|
+
*
|
|
191
|
+
* - "moved": done.
|
|
192
|
+
* - "exists": something else is already at the destination.
|
|
193
|
+
* - "failed": nothing changed. If removing the source failed after the link,
|
|
194
|
+
* the link is undone: two names for one file would let a write to the
|
|
195
|
+
* source change the destination.
|
|
196
|
+
* - "copy-left": the copy exists but the source couldn't be removed; the two
|
|
197
|
+
* are independent files.
|
|
198
|
+
*
|
|
199
|
+
* `requested` is the destination as the caller spelled it (absolute).
|
|
200
|
+
*/
|
|
201
|
+
async moveNoReplace(fromPath, toPath, requested) {
|
|
202
|
+
let linked = true;
|
|
203
|
+
try {
|
|
204
|
+
await this.linkFile(fromPath, toPath);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
if (e.code === "EEXIST") {
|
|
207
|
+
if (fromPath.toLowerCase() === toPath.toLowerCase() && await this.sameFile(fromPath, toPath)) {
|
|
208
|
+
try {
|
|
209
|
+
await rename(fromPath, requested);
|
|
210
|
+
return "moved";
|
|
211
|
+
} catch {
|
|
212
|
+
return "failed";
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return "exists";
|
|
216
|
+
}
|
|
217
|
+
if (e.code === "ENOENT") return "failed";
|
|
218
|
+
linked = false;
|
|
219
|
+
try {
|
|
220
|
+
await copyFile(fromPath, toPath, constants.COPYFILE_EXCL);
|
|
221
|
+
} catch (copyErr) {
|
|
222
|
+
return copyErr.code === "EEXIST" ? "exists" : "failed";
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
try {
|
|
226
|
+
await this.unlinkSource(fromPath);
|
|
227
|
+
return "moved";
|
|
228
|
+
} catch {
|
|
229
|
+
if (!linked) return "copy-left";
|
|
230
|
+
if (await this.sameFile(fromPath, toPath)) await unlink(toPath).catch(() => {
|
|
231
|
+
});
|
|
232
|
+
return "failed";
|
|
233
|
+
}
|
|
234
|
+
}
|
|
146
235
|
async moveNote(from, to) {
|
|
147
|
-
const fromPath = await this.
|
|
148
|
-
const toPath = await this.
|
|
236
|
+
const fromPath = await this.writePath(from, "note");
|
|
237
|
+
const toPath = await this.writePath(to, "note");
|
|
149
238
|
if (fromPath === toPath) {
|
|
150
239
|
try {
|
|
151
240
|
await rename(fromPath, resolve(this.root, to));
|
|
@@ -156,18 +245,17 @@ var LocalVault = class {
|
|
|
156
245
|
}
|
|
157
246
|
try {
|
|
158
247
|
await mkdir(dirname(toPath), { recursive: true });
|
|
159
|
-
|
|
160
|
-
return true;
|
|
161
|
-
} catch (e) {
|
|
162
|
-
if (e.code === "EXDEV") {
|
|
163
|
-
const content = await this.readNote(from);
|
|
164
|
-
if (content === null) return false;
|
|
165
|
-
const wrote = await this.writeNote(to, content);
|
|
166
|
-
if (!wrote) return false;
|
|
167
|
-
return await this.deleteNote(from);
|
|
168
|
-
}
|
|
248
|
+
} catch {
|
|
169
249
|
return false;
|
|
170
250
|
}
|
|
251
|
+
const result = await this.moveNoReplace(fromPath, toPath, resolve(this.root, to));
|
|
252
|
+
if (result === "exists") throw new NoteExistsError(to);
|
|
253
|
+
if (result === "copy-left") {
|
|
254
|
+
await unlink(toPath).catch(() => {
|
|
255
|
+
});
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
return result === "moved";
|
|
171
259
|
}
|
|
172
260
|
async getMetadata(path) {
|
|
173
261
|
const fullPath = await this.safePath(path, "note");
|
|
@@ -217,7 +305,7 @@ var LocalVault = class {
|
|
|
217
305
|
return results.sort((a, b) => a.path.localeCompare(b.path));
|
|
218
306
|
}
|
|
219
307
|
async writeAttachment(path, data, { overwrite = false } = {}) {
|
|
220
|
-
const fullPath = await this.
|
|
308
|
+
const fullPath = await this.writePath(path, "attachment", { noSymlink: true });
|
|
221
309
|
if (!await this.mkdirChecked(path, fullPath)) return false;
|
|
222
310
|
try {
|
|
223
311
|
const flag = constants.O_WRONLY | constants.O_CREAT | (overwrite ? constants.O_TRUNC : constants.O_EXCL) | NOFOLLOW;
|
|
@@ -230,7 +318,7 @@ var LocalVault = class {
|
|
|
230
318
|
}
|
|
231
319
|
}
|
|
232
320
|
async deleteAttachment(path) {
|
|
233
|
-
const fullPath = await this.
|
|
321
|
+
const fullPath = await this.writePath(path, "attachment", { noSymlink: true });
|
|
234
322
|
try {
|
|
235
323
|
await unlink(fullPath);
|
|
236
324
|
return true;
|
|
@@ -239,8 +327,8 @@ var LocalVault = class {
|
|
|
239
327
|
}
|
|
240
328
|
}
|
|
241
329
|
async moveAttachment(from, to, { overwrite = false } = {}) {
|
|
242
|
-
const fromPath = await this.
|
|
243
|
-
const toPath = await this.
|
|
330
|
+
const fromPath = await this.writePath(from, "attachment", { noSymlink: true });
|
|
331
|
+
const toPath = await this.writePath(to, "attachment", { noSymlink: true });
|
|
244
332
|
if (fromPath === toPath) {
|
|
245
333
|
try {
|
|
246
334
|
await rename(fromPath, resolve(this.root, to));
|
|
@@ -250,28 +338,29 @@ var LocalVault = class {
|
|
|
250
338
|
}
|
|
251
339
|
}
|
|
252
340
|
if (!await this.mkdirChecked(to, toPath)) return false;
|
|
253
|
-
|
|
254
|
-
|
|
341
|
+
if (overwrite) {
|
|
342
|
+
try {
|
|
255
343
|
await rename(fromPath, toPath);
|
|
256
344
|
return true;
|
|
345
|
+
} catch (e) {
|
|
346
|
+
if (e.code !== "EXDEV") return false;
|
|
257
347
|
}
|
|
258
|
-
await link(fromPath, toPath);
|
|
259
|
-
} catch (e) {
|
|
260
|
-
if (e.code === "EEXIST") throw new AttachmentExistsError(to);
|
|
261
|
-
if (!["EXDEV", "EPERM", "ENOTSUP", "EOPNOTSUPP"].includes(e.code)) return false;
|
|
262
348
|
try {
|
|
263
|
-
await copyFile(fromPath, toPath
|
|
264
|
-
} catch
|
|
265
|
-
if (copyErr.code === "EEXIST") throw new AttachmentExistsError(to);
|
|
349
|
+
await copyFile(fromPath, toPath);
|
|
350
|
+
} catch {
|
|
266
351
|
return false;
|
|
267
352
|
}
|
|
353
|
+
try {
|
|
354
|
+
await unlink(fromPath);
|
|
355
|
+
return true;
|
|
356
|
+
} catch {
|
|
357
|
+
throw new AttachmentMoveIncompleteError(from, to);
|
|
358
|
+
}
|
|
268
359
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
throw new AttachmentMoveIncompleteError(from, to);
|
|
274
|
-
}
|
|
360
|
+
const result = await this.moveNoReplace(fromPath, toPath, resolve(this.root, to));
|
|
361
|
+
if (result === "exists") throw new AttachmentExistsError(to);
|
|
362
|
+
if (result === "copy-left") throw new AttachmentMoveIncompleteError(from, to);
|
|
363
|
+
return result === "moved";
|
|
275
364
|
}
|
|
276
365
|
async getAttachmentInfo(path) {
|
|
277
366
|
const fullPath = await this.safePath(path, "attachment");
|
|
@@ -306,4 +395,4 @@ var LocalVault = class {
|
|
|
306
395
|
export {
|
|
307
396
|
LocalVault
|
|
308
397
|
};
|
|
309
|
-
//# sourceMappingURL=vault-local-
|
|
398
|
+
//# sourceMappingURL=vault-local-VOGFWQI2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/vault-local.ts"],"sourcesContent":["import { readFile, writeFile, unlink, mkdir, stat, lstat, realpath, rename, link, copyFile } from \"fs/promises\";\nimport { basename, dirname, join, relative, resolve, sep } from \"path\";\nimport { realpathSync, constants } from \"fs\";\nimport { glob } from \"fs/promises\";\nimport { parseFrontmatterAndLinks } from \"./parse.js\";\nimport { validateNotePath, isValidNotePath } from \"./note-path.js\";\nimport { validateAttachmentPath, isValidAttachmentPath } from \"./attachment-path.js\";\nimport { AttachmentExistsError, AttachmentMoveIncompleteError, NoteExistsError, NoteNotFoundError, type NoteWriteOptions, type VaultBackend, type NoteInfo, type NoteListing, type AttachmentInfo, type WriteOptions, type MoveOptions } from \"./vault-backend.js\";\n\n// Not defined on Windows, which has no symlinks a non-admin can create anyway.\nconst NOFOLLOW = constants.O_NOFOLLOW ?? 0;\n\nexport class LocalVault implements VaultBackend {\n private root: string;\n /** fs calls used by moveNoReplace, replaceable in tests to simulate failures. */\n protected linkFile = link;\n protected unlinkSource = unlink;\n\n private refuseSymlinkWrites: boolean;\n\n /**\n * `refuseSymlinkWrites`: refuse to write, delete or move through a symlink\n * (file or folder) anywhere on the path. Set when WRITE_FOLDERS is, because\n * the permission check sees the requested path: a symlink MCP/alias.md ->\n * Raw/a.md would otherwise let a write in MCP/ change a create-only note.\n */\n constructor(vaultPath: string, { refuseSymlinkWrites = false }: { refuseSymlinkWrites?: boolean } = {}) {\n this.root = realpathSync(resolve(vaultPath));\n this.refuseSymlinkWrites = refuseSymlinkWrites;\n }\n\n /**\n * safePath for a write, delete or move. With refuseSymlinkWrites, the\n * resolved path must be the requested one; only case and Unicode form may\n * differ (case-insensitive file systems return the on-disk spelling).\n */\n private async writePath(path: string, kind: \"note\" | \"attachment\", options: { noSymlink?: boolean } = {}): Promise<string> {\n const real = await this.safePath(path, kind, options);\n if (this.refuseSymlinkWrites) {\n const same = (s: string) => s.replace(/\\\\/g, \"/\").replace(/\\/{2,}/g, \"/\").replace(/^\\//, \"\").normalize(\"NFC\").toLowerCase();\n if (same(relative(this.root, real).split(sep).join(\"/\")) !== same(path)) {\n throw new Error(`Refusing to modify a file through a symlink while WRITE_FOLDERS is set: ${path}`);\n }\n }\n return real;\n }\n\n private async safePath(path: string, kind: \"note\" | \"attachment\" | \"folder\" = \"folder\", { noSymlink = false } = {}): Promise<string> {\n // Note operations must target a real note (.md, no dot-folders, etc.)\n // and attachment operations an allowed media file; folder listing\n // passes a directory and skips this.\n if (kind === \"note\") validateNotePath(path);\n if (kind === \"attachment\") validateAttachmentPath(path);\n const full = resolve(this.root, path);\n // Lexical check first (catches ../ without hitting disk)\n if (!full.startsWith(this.root + sep)) {\n throw new Error(\"Path traversal blocked\");\n }\n // Resolve symlinks and re-check (catches symlink escapes). A path\n // that doesn't exist yet resolves through its nearest existing\n // ancestor, so a symlinked parent can't redirect a write either.\n const real = await this.resolveExisting(full);\n if (!real.startsWith(this.root + sep)) {\n throw new Error(\"Path traversal blocked\");\n }\n // The name was validated, but a symlink inside the vault can point at\n // a note, a dot-folder or another type, so the target must pass too.\n if (kind !== \"folder\" && !this.isValidKind(relative(this.root, real), kind)) {\n throw new Error(\"Path traversal blocked\");\n }\n // realpath fails with ENOENT on a dangling symlink, so it resolves as\n // a new file, but a write would follow it and create outside the vault.\n if (await this.hasDanglingSymlink(full)) {\n throw new Error(\"Path traversal blocked\");\n }\n // Writing or deleting through a symlink would act on its target, a\n // different path from the one the caller named.\n if (noSymlink && (await this.isSymlink(full))) {\n throw new Error(`Refusing to modify a symlink: ${path}`);\n }\n return real;\n }\n\n /**\n * mkdir for an attachment write to `fullPath` (from safePath), then check\n * the path again: a folder swapped for a symlink since would redirect the\n * write. Returns false when mkdir fails.\n */\n private async mkdirChecked(path: string, fullPath: string): Promise<boolean> {\n try {\n await mkdir(dirname(fullPath), { recursive: true });\n } catch {\n return false;\n }\n if ((await this.safePath(path, \"attachment\", { noSymlink: true })) !== fullPath) {\n throw new Error(\"Path traversal blocked\");\n }\n return true;\n }\n\n private isValidKind(rel: string, kind: \"note\" | \"attachment\"): boolean {\n const path = rel.split(sep).join(\"/\");\n return kind === \"note\" ? isValidNotePath(path) : isValidAttachmentPath(path);\n }\n\n private async isSymlink(full: string): Promise<boolean> {\n try {\n return (await lstat(full)).isSymbolicLink();\n } catch (e: any) {\n if (e.code === \"ENOENT\") return false;\n throw e;\n }\n }\n\n /**\n * realpath of `full`, or for a path that doesn't exist yet the realpath of\n * its nearest existing ancestor joined with the rest.\n */\n private async resolveExisting(full: string): Promise<string> {\n const rest: string[] = [];\n let current = full;\n for (;;) {\n try {\n return join(await realpath(current), ...rest);\n } catch (e: any) {\n if (e.code !== \"ENOENT\") throw e;\n const parent = dirname(current);\n if (parent === current) return full; // reached filesystem root\n rest.unshift(basename(current));\n current = parent;\n }\n }\n }\n\n /** Whether `full` or one of its ancestors below the root is a symlink to nothing. */\n private async hasDanglingSymlink(full: string): Promise<boolean> {\n for (let current = full; current !== this.root; current = dirname(current)) {\n let s;\n try {\n s = await lstat(current);\n } catch (e: any) {\n if (e.code === \"ENOENT\") continue;\n throw e;\n }\n // The nearest existing entry decides: anything but a symlink is\n // real, and a resolvable symlink was checked by resolveExisting.\n if (!s.isSymbolicLink()) return false;\n try {\n await realpath(current);\n return false;\n } catch (e: any) {\n if (e.code === \"ENOENT\") return true;\n throw e;\n }\n }\n return false;\n }\n\n async init(): Promise<void> {}\n\n async close(): Promise<void> {}\n\n async readNote(path: string): Promise<string | null> {\n const fullPath = await this.safePath(path, \"note\");\n try {\n return await readFile(fullPath, \"utf-8\");\n } catch {\n return null;\n }\n }\n\n async writeNote(path: string, content: string, { mode = \"upsert\" }: NoteWriteOptions = {}): Promise<boolean> {\n const fullPath = await this.writePath(path, \"note\");\n // O_EXCL refuses an existing file and a missing O_CREAT refuses a\n // missing one, each in the same step as the write.\n const flag =\n mode === \"create\" ? constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL\n : mode === \"update\" ? constants.O_WRONLY | constants.O_TRUNC\n : \"w\";\n try {\n if (mode !== \"update\") await mkdir(dirname(fullPath), { recursive: true });\n await writeFile(fullPath, content, { encoding: \"utf-8\", flag });\n return true;\n } catch (e: any) {\n if (mode === \"create\" && e.code === \"EEXIST\") throw new NoteExistsError(path);\n if (mode === \"update\" && e.code === \"ENOENT\") throw new NoteNotFoundError(path);\n return false;\n }\n }\n\n async deleteNote(path: string): Promise<boolean> {\n const fullPath = await this.writePath(path, \"note\");\n try {\n await unlink(fullPath);\n return true;\n } catch {\n return false;\n }\n }\n\n /** True when both paths name the same file (same device and inode). */\n private async sameFile(a: string, b: string): Promise<boolean> {\n try {\n const [sa, sb] = await Promise.all([stat(a), stat(b)]);\n return sa.dev === sb.dev && sa.ino === sb.ino;\n } catch {\n return false;\n }\n }\n\n /**\n * Move `fromPath` to `toPath` without replacing anything there: hard-link,\n * or copy where links aren't supported, then remove the source. rename()\n * can't be used because it replaces an existing destination.\n *\n * - \"moved\": done.\n * - \"exists\": something else is already at the destination.\n * - \"failed\": nothing changed. If removing the source failed after the link,\n * the link is undone: two names for one file would let a write to the\n * source change the destination.\n * - \"copy-left\": the copy exists but the source couldn't be removed; the two\n * are independent files.\n *\n * `requested` is the destination as the caller spelled it (absolute).\n */\n private async moveNoReplace(fromPath: string, toPath: string, requested: string): Promise<\"moved\" | \"exists\" | \"failed\" | \"copy-left\"> {\n let linked = true;\n try {\n await this.linkFile(fromPath, toPath);\n } catch (e: any) {\n if (e.code === \"EEXIST\") {\n // A case-insensitive mount whose realpath keeps the requested case\n // (WSL /mnt/c, Docker Desktop bind mounts) makes a case-only rename\n // look like a move onto an existing file: it's the source itself.\n if (fromPath.toLowerCase() === toPath.toLowerCase() && (await this.sameFile(fromPath, toPath))) {\n try {\n await rename(fromPath, requested);\n return \"moved\";\n } catch {\n return \"failed\";\n }\n }\n return \"exists\";\n }\n if (e.code === \"ENOENT\") return \"failed\";\n // Cross-device, or a filesystem without hard links (codes vary: EXDEV,\n // EPERM, ENOTSUP, ENOSYS on some FUSE mounts...): copy, then delete.\n linked = false;\n try {\n await copyFile(fromPath, toPath, constants.COPYFILE_EXCL);\n } catch (copyErr: any) {\n return copyErr.code === \"EEXIST\" ? \"exists\" : \"failed\";\n }\n }\n try {\n await this.unlinkSource(fromPath);\n return \"moved\";\n } catch {\n if (!linked) return \"copy-left\";\n if (await this.sameFile(fromPath, toPath)) await unlink(toPath).catch(() => {});\n return \"failed\";\n }\n }\n\n async moveNote(from: string, to: string): Promise<boolean> {\n const fromPath = await this.writePath(from, \"note\");\n const toPath = await this.writePath(to, \"note\");\n if (fromPath === toPath) {\n // Same file: a no-op, or a case-only rename on a case-insensitive\n // filesystem, where toPath is the realpath and has the old case.\n try {\n await rename(fromPath, resolve(this.root, to));\n return true;\n } catch {\n return false;\n }\n }\n try {\n await mkdir(dirname(toPath), { recursive: true });\n } catch {\n return false;\n }\n const result = await this.moveNoReplace(fromPath, toPath, resolve(this.root, to));\n if (result === \"exists\") throw new NoteExistsError(to);\n if (result === \"copy-left\") {\n // Report a failed move with nothing changed, rather than two copies.\n await unlink(toPath).catch(() => {});\n return false;\n }\n return result === \"moved\";\n }\n\n async getMetadata(path: string): Promise<NoteInfo | null> {\n const fullPath = await this.safePath(path, \"note\");\n try {\n const [content, s] = await Promise.all([\n readFile(fullPath, \"utf-8\"),\n stat(fullPath),\n ]);\n return {\n path,\n size: s.size,\n ctime: s.birthtimeMs,\n mtime: s.mtimeMs,\n ...parseFrontmatterAndLinks(content),\n };\n } catch {\n return null;\n }\n }\n\n async listNotes(folder?: string): Promise<string[]> {\n const notes = await this.listNotesWithMtime(folder);\n return notes.map((n) => n.path);\n }\n\n async listNotesWithMtime(folder?: string): Promise<NoteListing[]> {\n if (folder && !folder.endsWith(\"/\") && !folder.endsWith(\"\\\\\")) folder += \"/\";\n const searchDir = folder ? await this.safePath(folder) : this.root;\n const entries: string[] = [];\n try {\n for await (const entry of glob(\"**/*.md\", { cwd: searchDir })) {\n const full = folder ? `${folder}${entry}` : entry;\n if (!isValidNotePath(full)) continue;\n entries.push(full);\n }\n } catch {\n return [];\n }\n const results = await Promise.all(\n entries.map(async (p) => {\n try {\n const s = await stat(resolve(this.root, p));\n return { path: p, mtime: s.mtimeMs };\n } catch {\n return { path: p, mtime: 0 };\n }\n }),\n );\n return results.sort((a, b) => a.path.localeCompare(b.path));\n }\n\n async writeAttachment(path: string, data: Uint8Array, { overwrite = false }: WriteOptions = {}): Promise<boolean> {\n const fullPath = await this.writePath(path, \"attachment\", { noSymlink: true });\n if (!(await this.mkdirChecked(path, fullPath))) return false;\n try {\n // O_EXCL fails if the file exists, in the same step as the create,\n // so two concurrent creates can't both succeed. O_NOFOLLOW refuses\n // a symlink put in place since safePath checked.\n const flag = constants.O_WRONLY | constants.O_CREAT | (overwrite ? constants.O_TRUNC : constants.O_EXCL) | NOFOLLOW;\n await writeFile(fullPath, data, { flag });\n return true;\n } catch (e: any) {\n if (e.code === \"EEXIST\") throw new AttachmentExistsError(path);\n if (e.code === \"ELOOP\") throw new Error(`Refusing to modify a symlink: ${path}`);\n return false;\n }\n }\n\n async deleteAttachment(path: string): Promise<boolean> {\n const fullPath = await this.writePath(path, \"attachment\", { noSymlink: true });\n try {\n await unlink(fullPath);\n return true;\n } catch {\n return false;\n }\n }\n\n async moveAttachment(from: string, to: string, { overwrite = false }: MoveOptions = {}): Promise<boolean> {\n const fromPath = await this.writePath(from, \"attachment\", { noSymlink: true });\n const toPath = await this.writePath(to, \"attachment\", { noSymlink: true });\n if (fromPath === toPath) {\n // Same file, which link below would refuse with EEXIST. rename is a\n // no-op here, or the case-only rename on a case-insensitive filesystem.\n try {\n await rename(fromPath, resolve(this.root, to));\n return true;\n } catch {\n return false;\n }\n }\n if (!(await this.mkdirChecked(to, toPath))) return false;\n if (overwrite) {\n try {\n await rename(fromPath, toPath);\n return true;\n } catch (e: any) {\n if (e.code !== \"EXDEV\") return false;\n }\n // Cross-device: copy over the destination, then delete the source.\n try {\n await copyFile(fromPath, toPath);\n } catch {\n return false;\n }\n try {\n await unlink(fromPath);\n return true;\n } catch {\n throw new AttachmentMoveIncompleteError(from, to);\n }\n }\n const result = await this.moveNoReplace(fromPath, toPath, resolve(this.root, to));\n if (result === \"exists\") throw new AttachmentExistsError(to);\n if (result === \"copy-left\") throw new AttachmentMoveIncompleteError(from, to);\n return result === \"moved\";\n }\n\n async getAttachmentInfo(path: string): Promise<AttachmentInfo | null> {\n const fullPath = await this.safePath(path, \"attachment\");\n try {\n const s = await stat(fullPath);\n if (!s.isFile()) return null;\n return { path, size: s.size, ctime: s.birthtimeMs, mtime: s.mtimeMs };\n } catch {\n return null;\n }\n }\n\n async listAttachments(): Promise<AttachmentInfo[]> {\n const results: AttachmentInfo[] = [];\n try {\n // Dot-folders (.obsidian, .trash, .git) are never attachments; skip them while walking.\n for await (const entry of glob(\"**/*\", { cwd: this.root, exclude: (p: any) => String(typeof p === \"string\" ? p : p.name).split(/[/\\\\]/).some((seg: string) => seg.startsWith(\".\")) })) {\n const path = entry.replace(/\\\\/g, \"/\");\n if (!isValidAttachmentPath(path)) continue;\n try {\n // A symlink out of the vault would leak the target's name and\n // size, and one to a note or dot-folder isn't an attachment.\n const real = await realpath(resolve(this.root, path));\n if (!real.startsWith(this.root + sep) || !this.isValidKind(relative(this.root, real), \"attachment\")) continue;\n const s = await stat(real);\n if (s.isFile()) results.push({ path, size: s.size, ctime: s.birthtimeMs, mtime: s.mtimeMs });\n } catch {\n // Vanished between listing and stat, or a dangling symlink.\n }\n }\n } catch {\n return [];\n }\n return results.sort((a, b) => a.path.localeCompare(b.path));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA,SAAS,UAAU,WAAW,QAAQ,OAAO,MAAM,OAAO,UAAU,QAAQ,MAAM,gBAAgB;AAClG,SAAS,UAAU,SAAS,MAAM,UAAU,SAAS,WAAW;AAChE,SAAS,cAAc,iBAAiB;AACxC,SAAS,YAAY;AAOrB,IAAM,WAAW,UAAU,cAAc;AAElC,IAAM,aAAN,MAAyC;AAAA,EACpC;AAAA;AAAA,EAEE,WAAW;AAAA,EACX,eAAe;AAAA,EAEjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQR,YAAY,WAAmB,EAAE,sBAAsB,MAAM,IAAuC,CAAC,GAAG;AACpG,SAAK,OAAO,aAAa,QAAQ,SAAS,CAAC;AAC3C,SAAK,sBAAsB;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAc,UAAU,MAAc,MAA6B,UAAmC,CAAC,GAAoB;AACvH,UAAM,OAAO,MAAM,KAAK,SAAS,MAAM,MAAM,OAAO;AACpD,QAAI,KAAK,qBAAqB;AAC1B,YAAM,OAAO,CAAC,MAAc,EAAE,QAAQ,OAAO,GAAG,EAAE,QAAQ,WAAW,GAAG,EAAE,QAAQ,OAAO,EAAE,EAAE,UAAU,KAAK,EAAE,YAAY;AAC1H,UAAI,KAAK,SAAS,KAAK,MAAM,IAAI,EAAE,MAAM,GAAG,EAAE,KAAK,GAAG,CAAC,MAAM,KAAK,IAAI,GAAG;AACrE,cAAM,IAAI,MAAM,2EAA2E,IAAI,EAAE;AAAA,MACrG;AAAA,IACJ;AACA,WAAO;AAAA,EACX;AAAA,EAEA,MAAc,SAAS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EAEA,MAAM,kBAA6C;AAC/C,UAAM,UAA4B,CAAC;AACnC,QAAI;AAEA,uBAAiB,SAAS,KAAK,QAAQ,EAAE,KAAK,KAAK,MAAM,SAAS,CAAC,MAAW,OAAO,OAAO,MAAM,WAAW,IAAI,EAAE,IAAI,EAAE,MAAM,OAAO,EAAE,KAAK,CAAC,QAAgB,IAAI,WAAW,GAAG,CAAC,EAAE,CAAC,GAAG;AACnL,cAAM,OAAO,MAAM,QAAQ,OAAO,GAAG;AACrC,YAAI,CAAC,sBAAsB,IAAI,EAAG;AAClC,YAAI;AAGA,gBAAM,OAAO,MAAM,SAAS,QAAQ,KAAK,MAAM,IAAI,CAAC;AACpD,cAAI,CAAC,KAAK,WAAW,KAAK,OAAO,GAAG,KAAK,CAAC,KAAK,YAAY,SAAS,KAAK,MAAM,IAAI,GAAG,YAAY,EAAG;AACrG,gBAAM,IAAI,MAAM,KAAK,IAAI;AACzB,cAAI,EAAE,OAAO,EAAG,SAAQ,KAAK,EAAE,MAAM,MAAM,EAAE,MAAM,OAAO,EAAE,aAAa,OAAO,EAAE,QAAQ,CAAC;AAAA,QAC/F,QAAQ;AAAA,QAER;AAAA,MACJ;AAAA,IACJ,QAAQ;AACJ,aAAO,CAAC;AAAA,IACZ;AACA,WAAO,QAAQ,KAAK,CAAC,GAAG,MAAM,EAAE,KAAK,cAAc,EAAE,IAAI,CAAC;AAAA,EAC9D;AACJ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "obsidian-sync-mcp",
|
|
3
3
|
"mcpName": "io.github.es617/obsidian-sync-mcp",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.13.0",
|
|
5
5
|
"description": "Give any AI agent access to your Obsidian vault over MCP",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"license": "MIT",
|
|
@@ -36,13 +36,15 @@
|
|
|
36
36
|
"test": "node --import tsx --test 'src/**/*.test.ts'",
|
|
37
37
|
"test:e2e": "node --import tsx --test test/e2e.test.ts",
|
|
38
38
|
"test:couchdb": "tsup test/couchdb-obfuscation.e2e.ts --out-dir .harness-tmp && node .harness-tmp/couchdb-obfuscation.e2e.js",
|
|
39
|
-
"test:smoke": "npm run build && tsup test/smoke.e2e.ts --out-dir .harness-tmp && node .harness-tmp/smoke.e2e.js"
|
|
39
|
+
"test:smoke": "npm run build && tsup test/smoke.e2e.ts --out-dir .harness-tmp && node .harness-tmp/smoke.e2e.js",
|
|
40
|
+
"test:vault-stub": "tsup test/vault-notes.stub.e2e.ts --out-dir .harness-tmp && node --test .harness-tmp/vault-notes.stub.e2e.js"
|
|
40
41
|
},
|
|
41
42
|
"dependencies": {
|
|
42
43
|
"@vrtmrz/value-store": "^0.1.0",
|
|
43
44
|
"diff-match-patch": "^1.0.5",
|
|
44
45
|
"fastmcp": "^3.34.0",
|
|
45
46
|
"fflate": "^0.8.2",
|
|
47
|
+
"markdown-it": "15.0.3",
|
|
46
48
|
"minimatch": "^10",
|
|
47
49
|
"octagonal-wheels": "^0.1.56",
|
|
48
50
|
"pouchdb-adapter-http": "^9.0.0",
|
|
@@ -55,9 +57,11 @@
|
|
|
55
57
|
"pouchdb-utils": "^9.0.0",
|
|
56
58
|
"transform-pouch": "^2.0.0",
|
|
57
59
|
"xxhash-wasm": "^1.0.2",
|
|
60
|
+
"yaml": "2.9.1",
|
|
58
61
|
"zod": "^4.6.7"
|
|
59
62
|
},
|
|
60
63
|
"devDependencies": {
|
|
64
|
+
"@modelcontextprotocol/client": "2.4.0",
|
|
61
65
|
"@types/node": "^22",
|
|
62
66
|
"tsup": "^8",
|
|
63
67
|
"tsx": "^4",
|
package/dist/chunk-XZ4HUASL.js
DELETED
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Node polyfills for livesync-commonlib browser globals
|
|
3
|
-
if(!("navigator" in globalThis)){globalThis.navigator={language:"en"};}
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
-
for (let key of __getOwnPropNames(from))
|
|
11
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
-
}
|
|
14
|
-
return to;
|
|
15
|
-
};
|
|
16
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
17
|
-
|
|
18
|
-
// src/note-path.ts
|
|
19
|
-
var RESERVED_ROOT_FILES = /* @__PURE__ */ new Set([
|
|
20
|
-
"redflag.md",
|
|
21
|
-
"redflag2.md",
|
|
22
|
-
"redflag3.md",
|
|
23
|
-
"flag_rebuild.md",
|
|
24
|
-
"flag_fetch.md"
|
|
25
|
-
]);
|
|
26
|
-
function validateVaultPathShape(path, kind) {
|
|
27
|
-
const problem = vaultPathShapeProblem(path);
|
|
28
|
-
if (problem !== null) throw new Error(`Invalid ${kind} path${problem}`);
|
|
29
|
-
}
|
|
30
|
-
function vaultPathShapeProblem(path) {
|
|
31
|
-
if (!path || path.length > 1e3) return "";
|
|
32
|
-
if (path.startsWith("/") || path.includes("\0") || path.includes("..")) return "";
|
|
33
|
-
if (path.includes(":")) return ": ':' is not allowed";
|
|
34
|
-
for (const segment of path.split(/[/\\]/)) {
|
|
35
|
-
if (segment === "") return ": empty path segment";
|
|
36
|
-
if (segment.startsWith(".")) return ": dot-folders and hidden files (e.g. .obsidian) are not allowed";
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
function validateNotePath(path) {
|
|
41
|
-
validateVaultPathShape(path, "note");
|
|
42
|
-
if (!path.endsWith(".md")) {
|
|
43
|
-
throw new Error("Invalid note path: must be a vault-relative path ending in .md");
|
|
44
|
-
}
|
|
45
|
-
if (!path.includes("/") && !path.includes("\\") && RESERVED_ROOT_FILES.has(path.toLowerCase())) {
|
|
46
|
-
throw new Error("Invalid note path: reserved LiveSync control file");
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
function isValidNotePath(path) {
|
|
50
|
-
try {
|
|
51
|
-
validateNotePath(path);
|
|
52
|
-
return true;
|
|
53
|
-
} catch {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// src/attachment-path.ts
|
|
59
|
-
var SUPPORTED_ATTACHMENT_EXTENSIONS = [
|
|
60
|
-
// images
|
|
61
|
-
"avif",
|
|
62
|
-
"bmp",
|
|
63
|
-
"gif",
|
|
64
|
-
"jpeg",
|
|
65
|
-
"jpg",
|
|
66
|
-
"png",
|
|
67
|
-
"webp",
|
|
68
|
-
// audio
|
|
69
|
-
"flac",
|
|
70
|
-
"m4a",
|
|
71
|
-
"mp3",
|
|
72
|
-
"ogg",
|
|
73
|
-
"wav",
|
|
74
|
-
"3gp",
|
|
75
|
-
// video
|
|
76
|
-
"mkv",
|
|
77
|
-
"mov",
|
|
78
|
-
"mp4",
|
|
79
|
-
"ogv",
|
|
80
|
-
"webm",
|
|
81
|
-
// documents
|
|
82
|
-
"pdf"
|
|
83
|
-
];
|
|
84
|
-
function parseAttachmentExtensions(raw) {
|
|
85
|
-
if (raw === void 0 || raw.trim() === "") return new Set(SUPPORTED_ATTACHMENT_EXTENSIONS);
|
|
86
|
-
const requested = raw.split(",").map((e) => e.trim().toLowerCase().replace(/^\./, "")).filter(Boolean);
|
|
87
|
-
const unsupported = requested.filter((e) => !SUPPORTED_ATTACHMENT_EXTENSIONS.includes(e));
|
|
88
|
-
if (unsupported.length > 0) {
|
|
89
|
-
throw new Error(
|
|
90
|
-
`ATTACHMENT_EXTENSIONS contains unsupported extensions: ${unsupported.join(", ")}. Supported: ${SUPPORTED_ATTACHMENT_EXTENSIONS.join(", ")}.`
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
return new Set(requested);
|
|
94
|
-
}
|
|
95
|
-
function attachmentExtension(path) {
|
|
96
|
-
const name = path.split(/[/\\]/).pop() ?? "";
|
|
97
|
-
const dot = name.lastIndexOf(".");
|
|
98
|
-
return dot <= 0 ? "" : name.slice(dot + 1).toLowerCase();
|
|
99
|
-
}
|
|
100
|
-
var SUPPORTED = new Set(SUPPORTED_ATTACHMENT_EXTENSIONS);
|
|
101
|
-
function validateAttachmentPath(path, allowed = SUPPORTED) {
|
|
102
|
-
validateVaultPathShape(path, "attachment");
|
|
103
|
-
const ext = attachmentExtension(path);
|
|
104
|
-
if (!ext || !allowed.has(ext)) {
|
|
105
|
-
throw new Error(
|
|
106
|
-
`Invalid attachment path: extension must be one of ${[...allowed].sort().join(", ")}`
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
function isValidAttachmentPath(path, allowed = SUPPORTED) {
|
|
111
|
-
const ext = attachmentExtension(path);
|
|
112
|
-
return !!ext && allowed.has(ext) && vaultPathShapeProblem(path) === null;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// src/parse.ts
|
|
116
|
-
var WIKILINK = /\[\[([^[\]|#\n]+)(?:#[^[\]|\n]*)?(?:\|[^[\]\n]*)?\]\]/g;
|
|
117
|
-
var LINK_LABEL = String.raw`[^[\]\n]|\[[^[\]\n]*\]`;
|
|
118
|
-
var NOTE_LINK = new RegExp(String.raw`\[(?:${LINK_LABEL})+\]\(([^()#\n]+\.md)(?:#[^()\n]*)?\)`, "g");
|
|
119
|
-
var ATTACHMENT_LINK = new RegExp(String.raw`\[((?:${LINK_LABEL})*)\]\((<[^<>\n]+>|[^()\s]+)(?:\s+"[^"\n]*")?\)`, "g");
|
|
120
|
-
function parseFrontmatterAndLinks(content) {
|
|
121
|
-
const frontmatter = {};
|
|
122
|
-
const tags = /* @__PURE__ */ new Set();
|
|
123
|
-
const links = [];
|
|
124
|
-
if (content.startsWith("---\n")) {
|
|
125
|
-
const end = content.indexOf("\n---", 4);
|
|
126
|
-
if (end !== -1) {
|
|
127
|
-
const yaml = content.slice(4, end);
|
|
128
|
-
let inTagsList = false;
|
|
129
|
-
for (const line of yaml.split("\n")) {
|
|
130
|
-
if (inTagsList) {
|
|
131
|
-
const listItem = line.match(/^\s+-\s+(.+)/);
|
|
132
|
-
if (listItem) {
|
|
133
|
-
const trimmed = listItem[1].trim();
|
|
134
|
-
if (trimmed) tags.add(trimmed);
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
inTagsList = false;
|
|
138
|
-
}
|
|
139
|
-
const match = line.match(/^([\p{L}\p{N}_][\p{L}\p{M}\p{N}_-]*)\s*:\s*(.+)/u);
|
|
140
|
-
if (match) {
|
|
141
|
-
frontmatter[match[1]] = match[2].trim();
|
|
142
|
-
}
|
|
143
|
-
if (/^tags\s*:/.test(line)) {
|
|
144
|
-
const value = line.replace(/^tags\s*:\s*/, "").trim();
|
|
145
|
-
if (value) {
|
|
146
|
-
const tagValues = value.replace(/[[\]]/g, "");
|
|
147
|
-
for (const t of tagValues.split(",")) {
|
|
148
|
-
const trimmed = t.trim();
|
|
149
|
-
if (trimmed) tags.add(trimmed);
|
|
150
|
-
}
|
|
151
|
-
} else {
|
|
152
|
-
inTagsList = true;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
for (const match of maskCode(content).matchAll(/(^|\s)#([\p{L}\p{N}_/-][\p{L}\p{M}\p{N}_/-]*)/gu)) {
|
|
159
|
-
if (new RegExp("^\\p{Nd}+$", "u").test(match[2])) continue;
|
|
160
|
-
tags.add(match[2]);
|
|
161
|
-
}
|
|
162
|
-
for (const match of content.matchAll(WIKILINK)) links.push(match[1]);
|
|
163
|
-
for (const match of content.matchAll(NOTE_LINK)) links.push(match[1]);
|
|
164
|
-
addAttachmentLinks(content, links);
|
|
165
|
-
return { frontmatter, tags: [...tags], links: [...new Set(links)] };
|
|
166
|
-
}
|
|
167
|
-
function addAttachmentLinks(text, links, nested = false) {
|
|
168
|
-
for (const match of text.matchAll(ATTACHMENT_LINK)) {
|
|
169
|
-
if (!nested && match[1].includes("[")) addAttachmentLinks(match[1], links, true);
|
|
170
|
-
let target = match[2].replace(/^<|>$/g, "").replace(/#.*$/, "");
|
|
171
|
-
if (/^[a-z][a-z0-9+.-]*:/i.test(target) || target.endsWith(".md")) continue;
|
|
172
|
-
try {
|
|
173
|
-
target = decodeURIComponent(target);
|
|
174
|
-
} catch {
|
|
175
|
-
continue;
|
|
176
|
-
}
|
|
177
|
-
target = target.replace(/^(\.\.?\/)+/, "");
|
|
178
|
-
if (isValidAttachmentPath(target)) links.push(target);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
function maskCode(content) {
|
|
182
|
-
const lines = content.split("\n");
|
|
183
|
-
const out = [];
|
|
184
|
-
let fence = null;
|
|
185
|
-
const inline = [];
|
|
186
|
-
const flushInline = () => {
|
|
187
|
-
if (inline.length === 0) return;
|
|
188
|
-
out.push(...maskInlineSpans(inline.join("\n")).split("\n"));
|
|
189
|
-
inline.length = 0;
|
|
190
|
-
};
|
|
191
|
-
for (const line of lines) {
|
|
192
|
-
const open = line.match(/^ {0,3}(`{3,}|~{3,})/);
|
|
193
|
-
if (fence) {
|
|
194
|
-
const close = open && open[1][0] === fence.char && open[1].length >= fence.len && line.trim() === open[1];
|
|
195
|
-
out.push(".".repeat(line.length));
|
|
196
|
-
if (close) fence = null;
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
if (open && (open[1][0] === "~" || !line.slice(open[0].length).includes("`"))) {
|
|
200
|
-
flushInline();
|
|
201
|
-
fence = { char: open[1][0], len: open[1].length };
|
|
202
|
-
out.push(".".repeat(line.length));
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
if (line.trim() === "") {
|
|
206
|
-
flushInline();
|
|
207
|
-
out.push(line);
|
|
208
|
-
continue;
|
|
209
|
-
}
|
|
210
|
-
inline.push(line);
|
|
211
|
-
}
|
|
212
|
-
flushInline();
|
|
213
|
-
return out.join("\n");
|
|
214
|
-
}
|
|
215
|
-
function maskInlineSpans(text) {
|
|
216
|
-
const runs = [];
|
|
217
|
-
for (let i = 0; i < text.length; ) {
|
|
218
|
-
if (text[i] !== "`") {
|
|
219
|
-
i++;
|
|
220
|
-
continue;
|
|
221
|
-
}
|
|
222
|
-
let n = 0;
|
|
223
|
-
while (text[i + n] === "`") n++;
|
|
224
|
-
runs.push({ at: i, len: n });
|
|
225
|
-
i += n;
|
|
226
|
-
}
|
|
227
|
-
const next = [];
|
|
228
|
-
const seen = /* @__PURE__ */ new Map();
|
|
229
|
-
for (let r = runs.length - 1; r >= 0; r--) {
|
|
230
|
-
next[r] = seen.get(runs[r].len) ?? -1;
|
|
231
|
-
seen.set(runs[r].len, r);
|
|
232
|
-
}
|
|
233
|
-
let result = "";
|
|
234
|
-
let from = 0;
|
|
235
|
-
for (let r = 0; r < runs.length; ) {
|
|
236
|
-
const c = next[r];
|
|
237
|
-
if (c === -1) {
|
|
238
|
-
r++;
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
const start = runs[r].at;
|
|
242
|
-
const end = runs[c].at + runs[c].len;
|
|
243
|
-
result += text.slice(from, start) + text.slice(start, end).replace(/[^\n]/g, ".");
|
|
244
|
-
from = end;
|
|
245
|
-
r = c + 1;
|
|
246
|
-
}
|
|
247
|
-
return result + text.slice(from);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// src/vault-backend.ts
|
|
251
|
-
var AttachmentTooLargeError = class extends Error {
|
|
252
|
-
constructor(path, size, limit) {
|
|
253
|
-
super(`Attachment is ${size} bytes, over the ${limit}-byte limit for moving it: ${path}`);
|
|
254
|
-
this.path = path;
|
|
255
|
-
this.size = size;
|
|
256
|
-
this.limit = limit;
|
|
257
|
-
this.name = "AttachmentTooLargeError";
|
|
258
|
-
}
|
|
259
|
-
};
|
|
260
|
-
var AttachmentMoveIncompleteError = class extends Error {
|
|
261
|
-
constructor(from, to) {
|
|
262
|
-
super(`Copied ${from} to ${to}, but could not delete ${from}`);
|
|
263
|
-
this.from = from;
|
|
264
|
-
this.to = to;
|
|
265
|
-
this.name = "AttachmentMoveIncompleteError";
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
var AttachmentExistsError = class extends Error {
|
|
269
|
-
constructor(path) {
|
|
270
|
-
super(`Attachment already exists: ${path}`);
|
|
271
|
-
this.path = path;
|
|
272
|
-
this.name = "AttachmentExistsError";
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
export {
|
|
277
|
-
__reExport,
|
|
278
|
-
validateNotePath,
|
|
279
|
-
isValidNotePath,
|
|
280
|
-
parseAttachmentExtensions,
|
|
281
|
-
attachmentExtension,
|
|
282
|
-
validateAttachmentPath,
|
|
283
|
-
isValidAttachmentPath,
|
|
284
|
-
parseFrontmatterAndLinks,
|
|
285
|
-
AttachmentTooLargeError,
|
|
286
|
-
AttachmentMoveIncompleteError,
|
|
287
|
-
AttachmentExistsError
|
|
288
|
-
};
|
|
289
|
-
//# sourceMappingURL=chunk-XZ4HUASL.js.map
|