@elonzh/fafa 0.1.0 → 0.2.1
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 +14 -3
- package/dist/bin.js +2 -2
- package/dist/chunk-LKU762XJ.js +2807 -0
- package/dist/{chunk-OBB66GA5.js → chunk-ZCVVJMRN.js} +363 -136
- package/dist/{dist-HLWFT422.js → dist-G3FZO234.js} +1 -1
- package/dist/index.js +2 -2
- package/package.json +3 -2
- package/web/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/web/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/web/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/web/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/web/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/web/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/web/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/web/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/web/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/web/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/web/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/web/assets/index-BT8QLdyk.css +1 -0
- package/web/assets/index-IRxKoB6K.js +25 -0
- package/web/icon.svg +6 -0
- package/web/index.html +31 -0
- package/web/manifest.webmanifest +17 -0
- package/dist/chunk-T24ISKK3.js +0 -849
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// ../../packages/application/dist/index.js
|
|
2
2
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
3
3
|
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
4
|
-
import { dirname as dirname5, join as
|
|
4
|
+
import { dirname as dirname5, join as join6 } from "path";
|
|
5
5
|
|
|
6
6
|
// ../../packages/assets/dist/index.js
|
|
7
7
|
import { createHash } from "crypto";
|
|
@@ -167,56 +167,274 @@ function dataUrl(asset) {
|
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
// ../../packages/config/dist/index.js
|
|
170
|
-
import { access, mkdir, readdir, readFile as
|
|
170
|
+
import { access as access2, mkdir as mkdir2, readdir, readFile as readFile3, rename as rename3, stat as stat2, writeFile as writeFile3 } from "fs/promises";
|
|
171
171
|
import { homedir } from "os";
|
|
172
|
-
import { basename as basename2, dirname, extname as extname2, isAbsolute, join, relative, resolve, sep } from "path";
|
|
172
|
+
import { basename as basename2, dirname, extname as extname2, isAbsolute as isAbsolute2, join as join2, relative as relative2, resolve as resolve3, sep } from "path";
|
|
173
173
|
import matter from "gray-matter";
|
|
174
174
|
import ignore from "ignore";
|
|
175
175
|
import { parse, stringify } from "yaml";
|
|
176
|
+
import { z as z2 } from "zod";
|
|
177
|
+
import { readFile as readFile2, realpath, rename, writeFile } from "fs/promises";
|
|
178
|
+
import { isAbsolute, relative, resolve } from "path";
|
|
179
|
+
import { createHash as createHash2 } from "crypto";
|
|
180
|
+
import { access, mkdir, readFile as readFile22, realpath as realpath2, rename as rename2, stat, writeFile as writeFile2 } from "fs/promises";
|
|
181
|
+
import { basename as basename3, join, resolve as resolve2 } from "path";
|
|
176
182
|
import { z } from "zod";
|
|
183
|
+
function locateFrontmatter(markdown) {
|
|
184
|
+
const opening = markdown.match(/^(?:\uFEFF)?---[ \t]*(?:\r?\n)/);
|
|
185
|
+
if (!opening) {
|
|
186
|
+
throw new FaFaError("CONFIG_INVALID", "Article is missing opening YAML front matter (---)");
|
|
187
|
+
}
|
|
188
|
+
const bodyStart = opening[0].length;
|
|
189
|
+
const closingPattern = /^---[ \t]*(?:\r?\n|$)/gm;
|
|
190
|
+
closingPattern.lastIndex = bodyStart;
|
|
191
|
+
const closing = closingPattern.exec(markdown);
|
|
192
|
+
if (!closing) {
|
|
193
|
+
throw new FaFaError("CONFIG_INVALID", "Article YAML front matter is missing closing delimiter (---)");
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
bodyStart,
|
|
197
|
+
closingIndex: closing.index,
|
|
198
|
+
eol: markdown.includes("\r\n") ? "\r\n" : "\n",
|
|
199
|
+
frontmatter: markdown.slice(bodyStart, closing.index)
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function setArticleFrontmatterField(markdown, field, value) {
|
|
203
|
+
const normalized = value.trim();
|
|
204
|
+
if (!normalized) {
|
|
205
|
+
throw new FaFaError("CONFIG_INVALID", `Front matter field "${field}" cannot be empty`);
|
|
206
|
+
}
|
|
207
|
+
const { bodyStart, closingIndex, eol, frontmatter } = locateFrontmatter(markdown);
|
|
208
|
+
const encoded = JSON.stringify(normalized);
|
|
209
|
+
const fieldLine = new RegExp(`^${field}[ \\t]*:[^\\r\\n]*(?:\\r?\\n|$)`, "m");
|
|
210
|
+
const updated = fieldLine.test(frontmatter) ? frontmatter.replace(fieldLine, (line) => `${field}: ${encoded}${line.endsWith(eol) ? eol : ""}`) : `${frontmatter}${frontmatter && !frontmatter.endsWith(eol) ? eol : ""}${field}: ${encoded}${eol}`;
|
|
211
|
+
return `${markdown.slice(0, bodyStart)}${updated}${markdown.slice(closingIndex)}`;
|
|
212
|
+
}
|
|
213
|
+
function clearArticleFrontmatterField(markdown, field) {
|
|
214
|
+
const { bodyStart, closingIndex, frontmatter } = locateFrontmatter(markdown);
|
|
215
|
+
const fieldLine = new RegExp(`^${field}[ \\t]*:[^\\r\\n]*(?:\\r?\\n|$)`, "gm");
|
|
216
|
+
if (!fieldLine.test(frontmatter)) return markdown;
|
|
217
|
+
const updated = frontmatter.replace(fieldLine, "");
|
|
218
|
+
return `${markdown.slice(0, bodyStart)}${updated}${markdown.slice(closingIndex)}`;
|
|
219
|
+
}
|
|
220
|
+
async function updateArticleFrontmatterFile(projectRoot, articlePath, field, value) {
|
|
221
|
+
const fullPath = resolveInsideProject(projectRoot, articlePath);
|
|
222
|
+
const [canonicalRoot, canonicalArticle] = await Promise.all([
|
|
223
|
+
realpath(resolve(projectRoot)),
|
|
224
|
+
realpath(fullPath)
|
|
225
|
+
]);
|
|
226
|
+
const rel = relative(canonicalRoot, canonicalArticle);
|
|
227
|
+
if (rel.startsWith("..") || isAbsolute(rel)) {
|
|
228
|
+
throw new FaFaError("CONFIG_INVALID", `Path escapes project root: ${articlePath}`);
|
|
229
|
+
}
|
|
230
|
+
const current = await readFile2(canonicalArticle, "utf8");
|
|
231
|
+
const updated = value === null ? clearArticleFrontmatterField(current, field) : setArticleFrontmatterField(current, field, value);
|
|
232
|
+
if (updated === current) {
|
|
233
|
+
return { fullPath: canonicalArticle, changed: false };
|
|
234
|
+
}
|
|
235
|
+
const tempPath = `${canonicalArticle}.${process.pid}.${Date.now()}.tmp`;
|
|
236
|
+
await writeFile(tempPath, updated, "utf8");
|
|
237
|
+
await rename(tempPath, canonicalArticle);
|
|
238
|
+
return { fullPath: canonicalArticle, changed: true };
|
|
239
|
+
}
|
|
240
|
+
var LIBRARIES_FILENAME = "libraries.json";
|
|
241
|
+
var libraryEntrySchema = z.object({
|
|
242
|
+
id: z.string().min(1).regex(/^[a-z0-9][a-z0-9-]*$/),
|
|
243
|
+
name: z.string().min(1),
|
|
244
|
+
root: z.string().min(1),
|
|
245
|
+
addedAt: z.string().min(1),
|
|
246
|
+
lastOpenedAt: z.string().min(1).optional()
|
|
247
|
+
});
|
|
248
|
+
var libraryRegistrySchema = z.object({
|
|
249
|
+
version: z.literal(1),
|
|
250
|
+
lastActiveLibraryId: z.string().min(1).optional(),
|
|
251
|
+
libraries: z.array(libraryEntrySchema).default([])
|
|
252
|
+
});
|
|
253
|
+
async function pathExists(path) {
|
|
254
|
+
try {
|
|
255
|
+
await access(path);
|
|
256
|
+
return true;
|
|
257
|
+
} catch {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async function isDirectory(path) {
|
|
262
|
+
try {
|
|
263
|
+
return (await stat(path)).isDirectory();
|
|
264
|
+
} catch {
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function buildLibraryId(root) {
|
|
269
|
+
const hash = createHash2("sha256").update(root).digest("hex").slice(0, 8);
|
|
270
|
+
const rawSlug = basename3(root).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 24);
|
|
271
|
+
return rawSlug ? `${rawSlug}-${hash}` : `lib-${hash}`;
|
|
272
|
+
}
|
|
273
|
+
async function summarizeLibrary(entry) {
|
|
274
|
+
const exists2 = await isDirectory(entry.root);
|
|
275
|
+
const hasConfig = exists2 && await pathExists(join(entry.root, CONFIG_FILENAME));
|
|
276
|
+
return { ...entry, exists: exists2, hasConfig };
|
|
277
|
+
}
|
|
278
|
+
async function loadLibraryRegistry(home = getFaFaHome()) {
|
|
279
|
+
const path = join(home, LIBRARIES_FILENAME);
|
|
280
|
+
if (!await pathExists(path)) return { version: 1, libraries: [] };
|
|
281
|
+
let parsedJson;
|
|
282
|
+
try {
|
|
283
|
+
parsedJson = JSON.parse(await readFile22(path, "utf8"));
|
|
284
|
+
} catch {
|
|
285
|
+
throw new FaFaError("CONFIG_INVALID", `Invalid library registry ${path}`);
|
|
286
|
+
}
|
|
287
|
+
const result2 = libraryRegistrySchema.safeParse(parsedJson);
|
|
288
|
+
if (!result2.success) {
|
|
289
|
+
throw new FaFaError("CONFIG_INVALID", `Invalid library registry ${path}`, {
|
|
290
|
+
issues: result2.error.issues
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
return result2.data;
|
|
294
|
+
}
|
|
295
|
+
async function saveLibraryRegistry(registry, home = getFaFaHome()) {
|
|
296
|
+
const parsed = libraryRegistrySchema.parse(registry);
|
|
297
|
+
await mkdir(home, { recursive: true, mode: 448 });
|
|
298
|
+
const path = join(home, LIBRARIES_FILENAME);
|
|
299
|
+
const temporaryPath = `${path}.${process.pid}.${Date.now()}.tmp`;
|
|
300
|
+
await writeFile2(temporaryPath, `${JSON.stringify(parsed, null, 2)}
|
|
301
|
+
`, { mode: 384 });
|
|
302
|
+
await rename2(temporaryPath, path);
|
|
303
|
+
}
|
|
304
|
+
async function listRegisteredLibraries(home = getFaFaHome()) {
|
|
305
|
+
const registry = await loadLibraryRegistry(home);
|
|
306
|
+
const libraries = await Promise.all(registry.libraries.map((entry) => summarizeLibrary(entry)));
|
|
307
|
+
return {
|
|
308
|
+
...registry.lastActiveLibraryId ? { lastActiveLibraryId: registry.lastActiveLibraryId } : {},
|
|
309
|
+
libraries
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
async function registerLibrary(input, home = getFaFaHome()) {
|
|
313
|
+
const resolved = resolve2(input.root);
|
|
314
|
+
if (!await isDirectory(resolved)) {
|
|
315
|
+
throw new FaFaError("CONFIG_INVALID", `Library directory does not exist: ${input.root}`);
|
|
316
|
+
}
|
|
317
|
+
const canonicalRoot = await realpath2(resolved);
|
|
318
|
+
const configPath = join(canonicalRoot, CONFIG_FILENAME);
|
|
319
|
+
if (!await pathExists(configPath)) {
|
|
320
|
+
if (input.initialize) {
|
|
321
|
+
await createProject(canonicalRoot);
|
|
322
|
+
} else {
|
|
323
|
+
throw new FaFaError("PROJECT_NOT_FOUND", `No ${CONFIG_FILENAME} found in ${canonicalRoot}`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
327
|
+
const registry = await loadLibraryRegistry(home);
|
|
328
|
+
const existing = registry.libraries.find((entry2) => entry2.root === canonicalRoot);
|
|
329
|
+
const displayName = input.name?.trim() || existing?.name || basename3(canonicalRoot) || canonicalRoot;
|
|
330
|
+
let entry;
|
|
331
|
+
let libraries;
|
|
332
|
+
if (existing) {
|
|
333
|
+
entry = {
|
|
334
|
+
...existing,
|
|
335
|
+
name: displayName,
|
|
336
|
+
lastOpenedAt: now
|
|
337
|
+
};
|
|
338
|
+
libraries = registry.libraries.map((item) => item.id === existing.id ? entry : item);
|
|
339
|
+
} else {
|
|
340
|
+
let id = buildLibraryId(canonicalRoot);
|
|
341
|
+
let suffix = 1;
|
|
342
|
+
while (registry.libraries.some((item) => item.id === id)) {
|
|
343
|
+
suffix += 1;
|
|
344
|
+
id = `${buildLibraryId(canonicalRoot)}-${suffix}`;
|
|
345
|
+
}
|
|
346
|
+
entry = {
|
|
347
|
+
id,
|
|
348
|
+
name: displayName,
|
|
349
|
+
root: canonicalRoot,
|
|
350
|
+
addedAt: now,
|
|
351
|
+
lastOpenedAt: now
|
|
352
|
+
};
|
|
353
|
+
libraries = [...registry.libraries, entry];
|
|
354
|
+
}
|
|
355
|
+
const updated = {
|
|
356
|
+
version: 1,
|
|
357
|
+
lastActiveLibraryId: entry.id,
|
|
358
|
+
libraries
|
|
359
|
+
};
|
|
360
|
+
await saveLibraryRegistry(updated, home);
|
|
361
|
+
return {
|
|
362
|
+
library: await summarizeLibrary(entry),
|
|
363
|
+
registry: updated
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
async function touchLibrary(libraryId, home = getFaFaHome()) {
|
|
367
|
+
const registry = await loadLibraryRegistry(home);
|
|
368
|
+
const exists2 = registry.libraries.some((entry) => entry.id === libraryId);
|
|
369
|
+
if (!exists2) {
|
|
370
|
+
throw new FaFaError("PROJECT_NOT_FOUND", `Library ${libraryId} is not registered`);
|
|
371
|
+
}
|
|
372
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
373
|
+
const updated = {
|
|
374
|
+
version: 1,
|
|
375
|
+
lastActiveLibraryId: libraryId,
|
|
376
|
+
libraries: registry.libraries.map(
|
|
377
|
+
(entry) => entry.id === libraryId ? { ...entry, lastOpenedAt: now } : entry
|
|
378
|
+
)
|
|
379
|
+
};
|
|
380
|
+
await saveLibraryRegistry(updated, home);
|
|
381
|
+
return updated;
|
|
382
|
+
}
|
|
383
|
+
async function removeRegisteredLibrary(libraryId, home = getFaFaHome()) {
|
|
384
|
+
const registry = await loadLibraryRegistry(home);
|
|
385
|
+
const libraries = registry.libraries.filter((entry) => entry.id !== libraryId);
|
|
386
|
+
const lastActiveLibraryId = registry.lastActiveLibraryId && registry.lastActiveLibraryId !== libraryId ? registry.lastActiveLibraryId : libraries[0]?.id;
|
|
387
|
+
const updated = {
|
|
388
|
+
version: 1,
|
|
389
|
+
...lastActiveLibraryId ? { lastActiveLibraryId } : {},
|
|
390
|
+
libraries
|
|
391
|
+
};
|
|
392
|
+
await saveLibraryRegistry(updated, home);
|
|
393
|
+
return updated;
|
|
394
|
+
}
|
|
177
395
|
var CONFIG_FILENAME = "fafa.config.yaml";
|
|
178
396
|
var IGNORED_CONTENT_DIRECTORIES = /* @__PURE__ */ new Set(["node_modules", "dist", "out", "release", "coverage"]);
|
|
179
|
-
var projectConfigSchema =
|
|
180
|
-
version:
|
|
181
|
-
contentDirs:
|
|
182
|
-
defaults:
|
|
183
|
-
theme:
|
|
184
|
-
codeTheme:
|
|
185
|
-
imageCaption:
|
|
186
|
-
author:
|
|
397
|
+
var projectConfigSchema = z2.object({
|
|
398
|
+
version: z2.literal(1),
|
|
399
|
+
contentDirs: z2.array(z2.string().min(1)).min(1).default(["."]),
|
|
400
|
+
defaults: z2.object({
|
|
401
|
+
theme: z2.string().min(1).default("default"),
|
|
402
|
+
codeTheme: z2.string().min(1).default("github-light"),
|
|
403
|
+
imageCaption: z2.enum(IMAGE_CAPTION_MODES).default("none"),
|
|
404
|
+
author: z2.string().min(1).optional()
|
|
187
405
|
}).default({ theme: "default", codeTheme: "github-light", imageCaption: "none" })
|
|
188
406
|
}).strict();
|
|
189
|
-
var articleMetadataSchema =
|
|
190
|
-
title:
|
|
191
|
-
author:
|
|
192
|
-
cover:
|
|
193
|
-
summary:
|
|
194
|
-
sourceUrl:
|
|
195
|
-
comments:
|
|
196
|
-
open:
|
|
197
|
-
fansOnly:
|
|
407
|
+
var articleMetadataSchema = z2.object({
|
|
408
|
+
title: z2.string().min(1).max(64),
|
|
409
|
+
author: z2.string().max(16).optional(),
|
|
410
|
+
cover: z2.string().min(1).optional(),
|
|
411
|
+
summary: z2.string().max(120).optional(),
|
|
412
|
+
sourceUrl: z2.url().optional(),
|
|
413
|
+
comments: z2.object({
|
|
414
|
+
open: z2.boolean().default(false),
|
|
415
|
+
fansOnly: z2.boolean().default(false)
|
|
198
416
|
}).default({ open: false, fansOnly: false }),
|
|
199
|
-
theme:
|
|
200
|
-
codeTheme:
|
|
201
|
-
imageCaption:
|
|
417
|
+
theme: z2.string().min(1).optional(),
|
|
418
|
+
codeTheme: z2.string().min(1).optional(),
|
|
419
|
+
imageCaption: z2.enum(IMAGE_CAPTION_MODES).optional()
|
|
202
420
|
});
|
|
203
|
-
var storedAccountSchema =
|
|
204
|
-
id:
|
|
205
|
-
platform:
|
|
206
|
-
name:
|
|
207
|
-
appId:
|
|
208
|
-
appSecret:
|
|
209
|
-
test:
|
|
421
|
+
var storedAccountSchema = z2.object({
|
|
422
|
+
id: z2.string().min(1).regex(/^[a-z0-9][a-z0-9-]*$/),
|
|
423
|
+
platform: z2.literal("wechat"),
|
|
424
|
+
name: z2.string().min(1),
|
|
425
|
+
appId: z2.string().min(1),
|
|
426
|
+
appSecret: z2.string().min(1),
|
|
427
|
+
test: z2.boolean().default(false)
|
|
210
428
|
});
|
|
211
|
-
var accountInputSchema = storedAccountSchema.extend({ appSecret:
|
|
212
|
-
var userConfigSchema =
|
|
213
|
-
version:
|
|
214
|
-
defaultAccount:
|
|
215
|
-
accounts:
|
|
429
|
+
var accountInputSchema = storedAccountSchema.extend({ appSecret: z2.string().min(1).optional() });
|
|
430
|
+
var userConfigSchema = z2.object({
|
|
431
|
+
version: z2.literal(1),
|
|
432
|
+
defaultAccount: z2.string().optional(),
|
|
433
|
+
accounts: z2.array(storedAccountSchema).default([])
|
|
216
434
|
});
|
|
217
|
-
async function
|
|
435
|
+
async function pathExists2(path) {
|
|
218
436
|
try {
|
|
219
|
-
await
|
|
437
|
+
await access2(path);
|
|
220
438
|
return true;
|
|
221
439
|
} catch {
|
|
222
440
|
return false;
|
|
@@ -226,12 +444,12 @@ function configError(message, details) {
|
|
|
226
444
|
return new FaFaError("CONFIG_INVALID", message, details);
|
|
227
445
|
}
|
|
228
446
|
async function findProjectRoot(startPath = process.cwd()) {
|
|
229
|
-
let current =
|
|
230
|
-
if (!await
|
|
447
|
+
let current = resolve3(startPath);
|
|
448
|
+
if (!await pathExists2(join2(current, CONFIG_FILENAME)) && basename2(current).includes(".")) {
|
|
231
449
|
current = dirname(current);
|
|
232
450
|
}
|
|
233
451
|
while (true) {
|
|
234
|
-
if (await
|
|
452
|
+
if (await pathExists2(join2(current, CONFIG_FILENAME))) return current;
|
|
235
453
|
const parent = dirname(current);
|
|
236
454
|
if (parent === current) {
|
|
237
455
|
throw new FaFaError(
|
|
@@ -244,8 +462,8 @@ async function findProjectRoot(startPath = process.cwd()) {
|
|
|
244
462
|
}
|
|
245
463
|
async function loadProject(startPath = process.cwd()) {
|
|
246
464
|
const root = await findProjectRoot(startPath);
|
|
247
|
-
const configPath =
|
|
248
|
-
const raw = parse(await
|
|
465
|
+
const configPath = join2(root, CONFIG_FILENAME);
|
|
466
|
+
const raw = parse(await readFile3(configPath, "utf8"));
|
|
249
467
|
const result2 = projectConfigSchema.safeParse(raw);
|
|
250
468
|
if (!result2.success) {
|
|
251
469
|
throw configError(`Invalid ${CONFIG_FILENAME}`, { issues: result2.error.issues });
|
|
@@ -258,15 +476,15 @@ async function loadProject(startPath = process.cwd()) {
|
|
|
258
476
|
};
|
|
259
477
|
}
|
|
260
478
|
async function createProject(rootPath) {
|
|
261
|
-
const root =
|
|
262
|
-
const configPath =
|
|
263
|
-
if (await
|
|
479
|
+
const root = resolve3(rootPath);
|
|
480
|
+
const configPath = join2(root, CONFIG_FILENAME);
|
|
481
|
+
if (await pathExists2(configPath)) {
|
|
264
482
|
throw configError(`${CONFIG_FILENAME} already exists at ${configPath}`);
|
|
265
483
|
}
|
|
266
484
|
const config = projectConfigSchema.parse({ version: 1 });
|
|
267
|
-
await
|
|
485
|
+
await mkdir2(root, { recursive: true });
|
|
268
486
|
const { contentDirs: _contentDirs, ...storedConfig } = config;
|
|
269
|
-
await
|
|
487
|
+
await writeFile3(configPath, stringify(storedConfig), "utf8");
|
|
270
488
|
return { root, configPath, config, contentRoots: [root] };
|
|
271
489
|
}
|
|
272
490
|
async function setProjectDefaultTheme(startPath, theme) {
|
|
@@ -276,8 +494,8 @@ async function setProjectDefaultTheme(startPath, theme) {
|
|
|
276
494
|
defaults: { ...project.config.defaults, theme }
|
|
277
495
|
});
|
|
278
496
|
const temporaryPath = `${project.configPath}.${process.pid}.tmp`;
|
|
279
|
-
await
|
|
280
|
-
await
|
|
497
|
+
await writeFile3(temporaryPath, stringify(config), "utf8");
|
|
498
|
+
await rename3(temporaryPath, project.configPath);
|
|
281
499
|
return { ...project, config };
|
|
282
500
|
}
|
|
283
501
|
async function setProjectDefaultCodeTheme(startPath, codeTheme) {
|
|
@@ -287,8 +505,8 @@ async function setProjectDefaultCodeTheme(startPath, codeTheme) {
|
|
|
287
505
|
defaults: { ...project.config.defaults, codeTheme }
|
|
288
506
|
});
|
|
289
507
|
const temporaryPath = `${project.configPath}.${process.pid}.tmp`;
|
|
290
|
-
await
|
|
291
|
-
await
|
|
508
|
+
await writeFile3(temporaryPath, stringify(config), "utf8");
|
|
509
|
+
await rename3(temporaryPath, project.configPath);
|
|
292
510
|
return { ...project, config };
|
|
293
511
|
}
|
|
294
512
|
async function setProjectDefaultImageCaption(startPath, imageCaption2) {
|
|
@@ -298,25 +516,25 @@ async function setProjectDefaultImageCaption(startPath, imageCaption2) {
|
|
|
298
516
|
defaults: { ...project.config.defaults, imageCaption: imageCaption2 }
|
|
299
517
|
});
|
|
300
518
|
const temporaryPath = `${project.configPath}.${process.pid}.tmp`;
|
|
301
|
-
await
|
|
302
|
-
await
|
|
519
|
+
await writeFile3(temporaryPath, stringify(config), "utf8");
|
|
520
|
+
await rename3(temporaryPath, project.configPath);
|
|
303
521
|
return { ...project, config };
|
|
304
522
|
}
|
|
305
523
|
function resolveInsideProject(projectRoot, candidate) {
|
|
306
|
-
const resolved =
|
|
307
|
-
const normalizedRoot = `${
|
|
308
|
-
if (resolved !==
|
|
524
|
+
const resolved = resolve3(projectRoot, candidate);
|
|
525
|
+
const normalizedRoot = `${resolve3(projectRoot)}${sep}`;
|
|
526
|
+
if (resolved !== resolve3(projectRoot) && !resolved.startsWith(normalizedRoot)) {
|
|
309
527
|
throw configError(`Path escapes project root: ${candidate}`);
|
|
310
528
|
}
|
|
311
529
|
return resolved;
|
|
312
530
|
}
|
|
313
531
|
function pathIsInside(root, candidate) {
|
|
314
|
-
const path =
|
|
315
|
-
return path === "" || !
|
|
532
|
+
const path = relative2(resolve3(root), resolve3(candidate));
|
|
533
|
+
return path === "" || !isAbsolute2(path) && path !== ".." && !path.startsWith(`..${sep}`);
|
|
316
534
|
}
|
|
317
535
|
async function resolveContentRoots(projectRoot, contentDirs) {
|
|
318
536
|
const contentRoots = contentDirs.map((contentDirectory) => {
|
|
319
|
-
if (
|
|
537
|
+
if (isAbsolute2(contentDirectory)) {
|
|
320
538
|
throw configError(`Content directory must be relative to the project root: ${contentDirectory}`, {
|
|
321
539
|
contentDirectory
|
|
322
540
|
});
|
|
@@ -343,7 +561,7 @@ async function resolveContentRoots(projectRoot, contentDirs) {
|
|
|
343
561
|
await Promise.all(
|
|
344
562
|
contentRoots.map(async (contentRoot, index) => {
|
|
345
563
|
try {
|
|
346
|
-
if (!(await
|
|
564
|
+
if (!(await stat2(contentRoot)).isDirectory()) {
|
|
347
565
|
throw configError(`Content directory is not a directory: ${contentDirs[index]}`, {
|
|
348
566
|
contentDirectory: contentDirs[index]
|
|
349
567
|
});
|
|
@@ -370,7 +588,7 @@ function portableProjectPath(inputPath) {
|
|
|
370
588
|
async function createContentDiscoveryFilter(projectRoot) {
|
|
371
589
|
let rules = "";
|
|
372
590
|
try {
|
|
373
|
-
rules = await
|
|
591
|
+
rules = await readFile3(join2(projectRoot, ".gitignore"), "utf8");
|
|
374
592
|
} catch (error) {
|
|
375
593
|
if (error.code !== "ENOENT") throw error;
|
|
376
594
|
}
|
|
@@ -388,7 +606,7 @@ async function createContentDiscoveryFilter(projectRoot) {
|
|
|
388
606
|
};
|
|
389
607
|
}
|
|
390
608
|
function articleSource(project, sourcePath) {
|
|
391
|
-
const relativeSource =
|
|
609
|
+
const relativeSource = relative2(project.root, sourcePath);
|
|
392
610
|
return {
|
|
393
611
|
articleDir: dirname(sourcePath),
|
|
394
612
|
sourcePath,
|
|
@@ -402,30 +620,30 @@ async function discoverArticles(project) {
|
|
|
402
620
|
const entries = await readdir(directory, { withFileTypes: true });
|
|
403
621
|
for (const entry of entries) {
|
|
404
622
|
if (!entry.isFile() || !entry.name.endsWith(".md") || entry.name.startsWith(".")) continue;
|
|
405
|
-
const sourcePath =
|
|
406
|
-
if (filter.ignores(
|
|
407
|
-
const frontmatter = matter(await
|
|
623
|
+
const sourcePath = join2(directory, entry.name);
|
|
624
|
+
if (filter.ignores(relative2(project.root, sourcePath))) continue;
|
|
625
|
+
const frontmatter = matter(await readFile3(sourcePath, "utf8")).data;
|
|
408
626
|
if (typeof frontmatter.title === "string" && frontmatter.title.trim()) {
|
|
409
627
|
found.push(articleSource(project, sourcePath));
|
|
410
628
|
}
|
|
411
629
|
}
|
|
412
630
|
await Promise.all(
|
|
413
631
|
entries.filter(
|
|
414
|
-
(entry) => entry.isDirectory() && !entry.name.startsWith(".") && !filter.ignores(
|
|
415
|
-
).map((entry) => walk3(
|
|
632
|
+
(entry) => entry.isDirectory() && !entry.name.startsWith(".") && !filter.ignores(relative2(project.root, join2(directory, entry.name)), true)
|
|
633
|
+
).map((entry) => walk3(join2(directory, entry.name)))
|
|
416
634
|
);
|
|
417
635
|
}
|
|
418
636
|
await Promise.all(project.contentRoots.map((contentRoot) => walk3(contentRoot)));
|
|
419
637
|
return found.sort((left, right) => left.sourcePath.localeCompare(right.sourcePath));
|
|
420
638
|
}
|
|
421
639
|
async function resolveArticleSource(project, input) {
|
|
422
|
-
const candidate =
|
|
640
|
+
const candidate = isAbsolute2(input) ? resolve3(input) : resolve3(project.root, input);
|
|
423
641
|
resolveInsideProject(project.root, candidate);
|
|
424
642
|
const sourcePath = candidate;
|
|
425
643
|
if (!isInsideContentDirectories(project, sourcePath)) {
|
|
426
644
|
throw configError(`Article is outside contentDirs: ${input}`);
|
|
427
645
|
}
|
|
428
|
-
if (!await
|
|
646
|
+
if (!await pathExists2(sourcePath) || extname2(sourcePath).toLowerCase() !== ".md" || !(await stat2(sourcePath)).isFile()) {
|
|
429
647
|
throw new FaFaError("ARTICLE_NOT_FOUND", `No Markdown article found for ${input}`);
|
|
430
648
|
}
|
|
431
649
|
return articleSource(project, sourcePath);
|
|
@@ -454,25 +672,25 @@ function parseArticleMetadata(frontmatter, sourcePath) {
|
|
|
454
672
|
};
|
|
455
673
|
}
|
|
456
674
|
function getFaFaHome(environment = process.env) {
|
|
457
|
-
if (environment.FAFA_HOME) return
|
|
458
|
-
if (process.platform === "darwin") return
|
|
459
|
-
return
|
|
675
|
+
if (environment.FAFA_HOME) return resolve3(environment.FAFA_HOME);
|
|
676
|
+
if (process.platform === "darwin") return join2(homedir(), "Library", "Application Support", "fafa");
|
|
677
|
+
return join2(homedir(), ".config", "fafa");
|
|
460
678
|
}
|
|
461
679
|
async function loadUserConfig(home = getFaFaHome()) {
|
|
462
|
-
const path =
|
|
463
|
-
if (!await
|
|
464
|
-
const result2 = userConfigSchema.safeParse(JSON.parse(await
|
|
680
|
+
const path = join2(home, "config.json");
|
|
681
|
+
if (!await pathExists2(path)) return { version: 1, accounts: [] };
|
|
682
|
+
const result2 = userConfigSchema.safeParse(JSON.parse(await readFile3(path, "utf8")));
|
|
465
683
|
if (!result2.success) throw configError(`Invalid user config ${path}`, { issues: result2.error.issues });
|
|
466
684
|
return result2.data;
|
|
467
685
|
}
|
|
468
686
|
async function saveUserConfig(config, home = getFaFaHome()) {
|
|
469
687
|
const parsed = userConfigSchema.parse(config);
|
|
470
|
-
await
|
|
471
|
-
const path =
|
|
688
|
+
await mkdir2(home, { recursive: true, mode: 448 });
|
|
689
|
+
const path = join2(home, "config.json");
|
|
472
690
|
const temporaryPath = `${path}.${process.pid}.tmp`;
|
|
473
|
-
await
|
|
691
|
+
await writeFile3(temporaryPath, `${JSON.stringify(parsed, null, 2)}
|
|
474
692
|
`, { mode: 384 });
|
|
475
|
-
await
|
|
693
|
+
await rename3(temporaryPath, path);
|
|
476
694
|
}
|
|
477
695
|
async function upsertAccount(account, home = getFaFaHome()) {
|
|
478
696
|
const parsed = accountInputSchema.parse(account);
|
|
@@ -524,9 +742,9 @@ async function resolveWechatCredential(accountId, home = getFaFaHome()) {
|
|
|
524
742
|
import { execFile } from "child_process";
|
|
525
743
|
import { randomUUID } from "crypto";
|
|
526
744
|
import { constants } from "fs";
|
|
527
|
-
import { access as
|
|
745
|
+
import { access as access3, mkdir as mkdir3, readFile as readFile4, rename as rename4, stat as stat3, writeFile as writeFile4 } from "fs/promises";
|
|
528
746
|
import { createRequire } from "module";
|
|
529
|
-
import { dirname as dirname2, join as
|
|
747
|
+
import { dirname as dirname2, join as join3 } from "path";
|
|
530
748
|
import { pathToFileURL } from "url";
|
|
531
749
|
import { promisify } from "util";
|
|
532
750
|
var require2 = createRequire(import.meta.url);
|
|
@@ -544,8 +762,8 @@ function runtimeIdentity(options) {
|
|
|
544
762
|
}
|
|
545
763
|
async function isExecutable(path) {
|
|
546
764
|
try {
|
|
547
|
-
if (!(await
|
|
548
|
-
if (process.platform !== "win32") await
|
|
765
|
+
if (!(await stat3(path)).isFile()) return false;
|
|
766
|
+
if (process.platform !== "win32") await access3(path, constants.X_OK);
|
|
549
767
|
return true;
|
|
550
768
|
} catch {
|
|
551
769
|
return false;
|
|
@@ -558,7 +776,7 @@ function isManifest(value) {
|
|
|
558
776
|
}
|
|
559
777
|
async function readManagedRuntime(home, identity) {
|
|
560
778
|
try {
|
|
561
|
-
const value = JSON.parse(await
|
|
779
|
+
const value = JSON.parse(await readFile4(join3(home, "browsers", "manifest.json"), "utf8"));
|
|
562
780
|
if (!isManifest(value) || value.playwrightVersion !== identity.playwrightVersion || value.platform !== identity.platform || value.arch !== identity.arch || !await isExecutable(value.executablePath)) {
|
|
563
781
|
return void 0;
|
|
564
782
|
}
|
|
@@ -627,14 +845,14 @@ async function resolveHeadlessShellExecutable(browserDirectory, environment = {
|
|
|
627
845
|
}
|
|
628
846
|
async function writeManifestAtomically(path, manifest) {
|
|
629
847
|
const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
630
|
-
await
|
|
848
|
+
await writeFile4(temporaryPath, `${JSON.stringify(manifest, null, 2)}
|
|
631
849
|
`, { mode: 384 });
|
|
632
|
-
await
|
|
850
|
+
await rename4(temporaryPath, path);
|
|
633
851
|
}
|
|
634
852
|
async function installBrowserRuntime(options) {
|
|
635
853
|
try {
|
|
636
|
-
const browserDirectory =
|
|
637
|
-
await
|
|
854
|
+
const browserDirectory = join3(options.home, "browsers");
|
|
855
|
+
await mkdir3(browserDirectory, { recursive: true });
|
|
638
856
|
const environment = {
|
|
639
857
|
...process.env,
|
|
640
858
|
PLAYWRIGHT_BROWSERS_PATH: browserDirectory
|
|
@@ -642,13 +860,13 @@ async function installBrowserRuntime(options) {
|
|
|
642
860
|
const runInstaller = options.runInstaller ?? defaultRunInstaller;
|
|
643
861
|
const executablePath = await runInstaller({
|
|
644
862
|
browserDirectory,
|
|
645
|
-
cliPath:
|
|
863
|
+
cliPath: join3(dirname2(require2.resolve("playwright-core/package.json")), "cli.js"),
|
|
646
864
|
environment
|
|
647
865
|
});
|
|
648
866
|
if (!await isExecutable(executablePath)) {
|
|
649
867
|
throw new Error(`Installed browser executable was not found: ${executablePath}`);
|
|
650
868
|
}
|
|
651
|
-
await writeManifestAtomically(
|
|
869
|
+
await writeManifestAtomically(join3(browserDirectory, "manifest.json"), {
|
|
652
870
|
...runtimeIdentity(options),
|
|
653
871
|
executablePath
|
|
654
872
|
});
|
|
@@ -857,29 +1075,29 @@ async function exportDocument(options, dependencies = {}) {
|
|
|
857
1075
|
}
|
|
858
1076
|
|
|
859
1077
|
// ../../packages/markdown/dist/index.js
|
|
860
|
-
import { createHash as
|
|
861
|
-
import { access as
|
|
862
|
-
import { basename as
|
|
1078
|
+
import { createHash as createHash3 } from "crypto";
|
|
1079
|
+
import { access as access4, readdir as readdir2, readFile as readFile5 } from "fs/promises";
|
|
1080
|
+
import { basename as basename4, dirname as dirname3, extname as extname3, join as join4, relative as relative3, resolve as resolve4, sep as sep2 } from "path";
|
|
863
1081
|
import matter2 from "gray-matter";
|
|
864
1082
|
import remarkGfm from "remark-gfm";
|
|
865
1083
|
import remarkMath from "remark-math";
|
|
866
1084
|
import remarkParse from "remark-parse";
|
|
867
1085
|
import { unified } from "unified";
|
|
868
1086
|
function sha256(value) {
|
|
869
|
-
return
|
|
1087
|
+
return createHash3("sha256").update(value).digest("hex");
|
|
870
1088
|
}
|
|
871
1089
|
function articleId(projectRoot, sourcePath, frontmatter) {
|
|
872
1090
|
if (typeof frontmatter.id === "string" && frontmatter.id.trim()) return frontmatter.id.trim();
|
|
873
|
-
return
|
|
1091
|
+
return relative3(projectRoot, sourcePath).slice(0, -extname3(sourcePath).length).split(sep2).join("/");
|
|
874
1092
|
}
|
|
875
1093
|
function isInside(root, candidate) {
|
|
876
|
-
const normalizedRoot = `${
|
|
877
|
-
const normalizedCandidate =
|
|
878
|
-
return normalizedCandidate ===
|
|
1094
|
+
const normalizedRoot = `${resolve4(root)}${sep2}`;
|
|
1095
|
+
const normalizedCandidate = resolve4(candidate);
|
|
1096
|
+
return normalizedCandidate === resolve4(root) || normalizedCandidate.startsWith(normalizedRoot);
|
|
879
1097
|
}
|
|
880
1098
|
async function exists(path) {
|
|
881
1099
|
try {
|
|
882
|
-
await
|
|
1100
|
+
await access4(path);
|
|
883
1101
|
return true;
|
|
884
1102
|
} catch {
|
|
885
1103
|
return false;
|
|
@@ -926,7 +1144,7 @@ async function listVaultFiles(root) {
|
|
|
926
1144
|
const entries = await readdir2(directory, { withFileTypes: true });
|
|
927
1145
|
await Promise.all(
|
|
928
1146
|
entries.map(async (entry) => {
|
|
929
|
-
const path =
|
|
1147
|
+
const path = join4(directory, entry.name);
|
|
930
1148
|
if (entry.isDirectory() && !IGNORED_VAULT_DIRECTORIES.has(entry.name)) await visit(path);
|
|
931
1149
|
else if (entry.isFile()) files.push(path);
|
|
932
1150
|
})
|
|
@@ -939,16 +1157,16 @@ async function resolveVaultFile(context, currentSource, target, defaultExtension
|
|
|
939
1157
|
const decoded = decodeURIComponent(target).replaceAll("\\", "/");
|
|
940
1158
|
const withExtension = extname3(decoded) || !defaultExtension ? decoded : `${decoded}${defaultExtension}`;
|
|
941
1159
|
const candidates = [
|
|
942
|
-
|
|
943
|
-
|
|
1160
|
+
resolve4(dirname3(currentSource), withExtension),
|
|
1161
|
+
resolve4(context.projectRoot, withExtension.replace(/^\/+/, ""))
|
|
944
1162
|
];
|
|
945
1163
|
for (const candidate of candidates) {
|
|
946
1164
|
if (isInside(context.projectRoot, candidate) && await exists(candidate)) return candidate;
|
|
947
1165
|
}
|
|
948
1166
|
context.files ??= listVaultFiles(context.projectRoot);
|
|
949
|
-
const expectedName =
|
|
1167
|
+
const expectedName = basename4(withExtension).toLocaleLowerCase();
|
|
950
1168
|
const matches = (await context.files).filter(
|
|
951
|
-
(candidate) =>
|
|
1169
|
+
(candidate) => basename4(candidate).toLocaleLowerCase() === expectedName
|
|
952
1170
|
);
|
|
953
1171
|
if (matches.length === 1) return matches[0];
|
|
954
1172
|
if (matches.length > 1) {
|
|
@@ -1006,13 +1224,13 @@ function extractSelection(markdown, selector) {
|
|
|
1006
1224
|
return selected;
|
|
1007
1225
|
}
|
|
1008
1226
|
function relativeMarkdownPath(from, target) {
|
|
1009
|
-
const value =
|
|
1227
|
+
const value = relative3(from, target).split(sep2).join("/");
|
|
1010
1228
|
return encodeURI(value.startsWith(".") ? value : `./${value}`);
|
|
1011
1229
|
}
|
|
1012
1230
|
function rewriteRelativeImages(markdown, sourceDir, articleDir) {
|
|
1013
1231
|
return markdown.replace(/(!\[[^\]]*\]\()([^\s)]+)(\))/g, (match, open, rawPath, close) => {
|
|
1014
1232
|
if (/^(?:https?:|data:|asset:)/i.test(rawPath)) return match;
|
|
1015
|
-
const target =
|
|
1233
|
+
const target = resolve4(sourceDir, decodeURIComponent(rawPath.split(/[?#]/, 1)[0] ?? rawPath));
|
|
1016
1234
|
return `${open}${relativeMarkdownPath(articleDir, target)}${close}`;
|
|
1017
1235
|
});
|
|
1018
1236
|
}
|
|
@@ -1026,7 +1244,7 @@ async function expandObsidianEmbeds(context, currentSource, markdown, stack) {
|
|
|
1026
1244
|
const targetExtension = extname3(target.path).toLocaleLowerCase();
|
|
1027
1245
|
if (targetExtension && targetExtension !== ".md") {
|
|
1028
1246
|
const assetPath = await resolveVaultFile(context, currentSource, target.path);
|
|
1029
|
-
const replacement2 = `})`;
|
|
1030
1248
|
content = content.replace(raw, replacement2);
|
|
1031
1249
|
continue;
|
|
1032
1250
|
}
|
|
@@ -1035,14 +1253,14 @@ async function expandObsidianEmbeds(context, currentSource, markdown, stack) {
|
|
|
1035
1253
|
if (stack.includes(stackKey) || stack.length >= 12) {
|
|
1036
1254
|
throw new FaFaError("CHECK_FAILED", `Circular or too-deep Obsidian embed: ${raw}`, { stack });
|
|
1037
1255
|
}
|
|
1038
|
-
const targetMarkdown = targetSource === currentSource && !target.path ? markdown : matter2(await
|
|
1256
|
+
const targetMarkdown = targetSource === currentSource && !target.path ? markdown : matter2(await readFile5(targetSource, "utf8")).content;
|
|
1039
1257
|
const selected = extractSelection(targetMarkdown, target.selector);
|
|
1040
1258
|
const replacement = await expandObsidianEmbeds(context, targetSource, selected, [...stack, stackKey]);
|
|
1041
1259
|
content = content.replace(raw, replacement);
|
|
1042
1260
|
}
|
|
1043
1261
|
return content.replace(/(?<!!)\[\[([^\]]+)\]\]/g, (_match, rawTarget) => {
|
|
1044
1262
|
const target = parseObsidianTarget(rawTarget);
|
|
1045
|
-
return target.alias ?? target.selector?.replace(/^\^/, "") ??
|
|
1263
|
+
return target.alias ?? target.selector?.replace(/^\^/, "") ?? basename4(target.path, extname3(target.path));
|
|
1046
1264
|
}).replace(/^[ \t]*\^[a-zA-Z0-9-]+[ \t]*$/gm, "").replace(/[ \t]+\^[a-zA-Z0-9-]+(?=[ \t]*$)/gm, "");
|
|
1047
1265
|
}
|
|
1048
1266
|
async function parseArticle(projectRoot, source) {
|
|
@@ -1050,10 +1268,10 @@ async function parseArticle(projectRoot, source) {
|
|
|
1050
1268
|
throw new FaFaError("ARTICLE_NOT_FOUND", `Expected a Markdown note path: ${source}`);
|
|
1051
1269
|
}
|
|
1052
1270
|
const articleDir = dirname3(source);
|
|
1053
|
-
const raw = await
|
|
1271
|
+
const raw = await readFile5(source, "utf8");
|
|
1054
1272
|
return parseArticleContent(projectRoot, articleDir, raw, source);
|
|
1055
1273
|
}
|
|
1056
|
-
async function parseArticleContent(projectRoot, articleDir, raw, sourcePath =
|
|
1274
|
+
async function parseArticleContent(projectRoot, articleDir, raw, sourcePath = join4(articleDir, "stdin.md")) {
|
|
1057
1275
|
const parsedMatter = matter2(raw);
|
|
1058
1276
|
const frontmatter = parsedMatter.data;
|
|
1059
1277
|
const sourceLineOffset = (raw.slice(0, Math.max(0, raw.length - parsedMatter.content.length)).match(/\n/g) ?? []).length;
|
|
@@ -1112,7 +1330,7 @@ async function parseArticleContent(projectRoot, articleDir, raw, sourcePath = jo
|
|
|
1112
1330
|
});
|
|
1113
1331
|
continue;
|
|
1114
1332
|
}
|
|
1115
|
-
const resolvedPath =
|
|
1333
|
+
const resolvedPath = resolve4(articleDir, decodeURIComponent(source.split(/[?#]/, 1)[0] ?? source));
|
|
1116
1334
|
if (!isInside(projectRoot, resolvedPath)) {
|
|
1117
1335
|
diagnostics.push({
|
|
1118
1336
|
code: "ASSET_OUTSIDE_PROJECT",
|
|
@@ -1134,7 +1352,7 @@ async function parseArticleContent(projectRoot, articleDir, raw, sourcePath = jo
|
|
|
1134
1352
|
assets.push({
|
|
1135
1353
|
id: id2,
|
|
1136
1354
|
source,
|
|
1137
|
-
origin: resolvedPath.startsWith(`${
|
|
1355
|
+
origin: resolvedPath.startsWith(`${resolve4(articleDir)}${sep2}`) ? "article" : "shared",
|
|
1138
1356
|
purpose: "inline",
|
|
1139
1357
|
resolvedPath,
|
|
1140
1358
|
...image.alt ? { alt: image.alt } : {},
|
|
@@ -1167,20 +1385,20 @@ function createCoverReference(projectRoot, articleDir, source) {
|
|
|
1167
1385
|
if (/^[a-z][a-z0-9+.-]*:/i.test(source)) {
|
|
1168
1386
|
throw new FaFaError("ASSET_INVALID", `Only project-local paths and HTTPS covers are allowed: ${source}`);
|
|
1169
1387
|
}
|
|
1170
|
-
const resolvedPath =
|
|
1388
|
+
const resolvedPath = resolve4(articleDir, decodeURIComponent(source.split(/[?#]/, 1)[0] ?? source));
|
|
1171
1389
|
if (!isInside(projectRoot, resolvedPath)) {
|
|
1172
1390
|
throw new FaFaError("ASSET_INVALID", `Cover escapes project root: ${source}`);
|
|
1173
1391
|
}
|
|
1174
1392
|
return {
|
|
1175
1393
|
id,
|
|
1176
1394
|
source,
|
|
1177
|
-
origin: resolvedPath.startsWith(`${
|
|
1395
|
+
origin: resolvedPath.startsWith(`${resolve4(articleDir)}${sep2}`) ? "article" : "shared",
|
|
1178
1396
|
purpose: "cover",
|
|
1179
1397
|
resolvedPath
|
|
1180
1398
|
};
|
|
1181
1399
|
}
|
|
1182
1400
|
function manuscriptDisplayName(article) {
|
|
1183
|
-
return typeof article.manuscript.frontmatter.title === "string" ? article.manuscript.frontmatter.title :
|
|
1401
|
+
return typeof article.manuscript.frontmatter.title === "string" ? article.manuscript.frontmatter.title : basename4(article.manuscript.sourcePath, extname3(article.manuscript.sourcePath));
|
|
1184
1402
|
}
|
|
1185
1403
|
|
|
1186
1404
|
// ../../packages/renderer-wechat/dist/index.js
|
|
@@ -1210,7 +1428,7 @@ function createTranslator(locale, catalog) {
|
|
|
1210
1428
|
}
|
|
1211
1429
|
|
|
1212
1430
|
// ../../packages/renderer-wechat/dist/index.js
|
|
1213
|
-
import { createHash as
|
|
1431
|
+
import { createHash as createHash4 } from "crypto";
|
|
1214
1432
|
import { fromShikiTheme, renderMermaidSVG } from "beautiful-mermaid";
|
|
1215
1433
|
import sharp2 from "sharp";
|
|
1216
1434
|
import { createBundledHighlighter } from "shiki/core";
|
|
@@ -1219,8 +1437,8 @@ import { bundledLanguages, bundledLanguagesAlias } from "shiki/langs";
|
|
|
1219
1437
|
import { bundledThemes } from "shiki/themes";
|
|
1220
1438
|
import MathJaxLoader from "mathjax";
|
|
1221
1439
|
import sharp3 from "sharp";
|
|
1222
|
-
import { readFile as
|
|
1223
|
-
import { basename as
|
|
1440
|
+
import { readFile as readFile6, stat as stat4 } from "fs/promises";
|
|
1441
|
+
import { basename as basename5, extname as extname4, isAbsolute as isAbsolute3, relative as relative4, resolve as resolve5, sep as sep3 } from "path";
|
|
1224
1442
|
import { fileURLToPath } from "url";
|
|
1225
1443
|
import juice from "juice";
|
|
1226
1444
|
var mathJaxPackage = MathJaxLoader;
|
|
@@ -1380,7 +1598,7 @@ var highlighterPromise = createHighlighter({
|
|
|
1380
1598
|
themes: [...codeThemeIds]
|
|
1381
1599
|
});
|
|
1382
1600
|
function sha2562(value) {
|
|
1383
|
-
return
|
|
1601
|
+
return createHash4("sha256").update(value).digest("hex");
|
|
1384
1602
|
}
|
|
1385
1603
|
function walk2(node, visitor) {
|
|
1386
1604
|
visitor(node);
|
|
@@ -1828,18 +2046,18 @@ function themeError(message, details) {
|
|
|
1828
2046
|
function themeAssetPath(filename) {
|
|
1829
2047
|
const packagedThemesDirectory = process.env.FAFA_INTERNAL_THEMES_DIR;
|
|
1830
2048
|
if (packagedThemesDirectory) {
|
|
1831
|
-
if (!
|
|
2049
|
+
if (!isAbsolute3(packagedThemesDirectory)) {
|
|
1832
2050
|
throw themeError("FAFA_INTERNAL_THEMES_DIR must be an absolute path");
|
|
1833
2051
|
}
|
|
1834
|
-
return
|
|
2052
|
+
return resolve5(packagedThemesDirectory, filename);
|
|
1835
2053
|
}
|
|
1836
2054
|
return fileURLToPath(new URL(`../themes/${filename}`, import.meta.url));
|
|
1837
2055
|
}
|
|
1838
2056
|
async function readCss(path, reference) {
|
|
1839
2057
|
let css;
|
|
1840
2058
|
try {
|
|
1841
|
-
if (!(await
|
|
1842
|
-
css = await
|
|
2059
|
+
if (!(await stat4(path)).isFile()) throw themeError(`Theme path is not a file: ${reference}`);
|
|
2060
|
+
css = await readFile6(path, "utf8");
|
|
1843
2061
|
} catch (error) {
|
|
1844
2062
|
if (error instanceof FaFaError) throw error;
|
|
1845
2063
|
if (error.code === "ENOENT") {
|
|
@@ -1900,8 +2118,8 @@ ${preset}`;
|
|
|
1900
2118
|
validateThemeCss(css, reference);
|
|
1901
2119
|
return { ...metadata, source: "preset", reference, css };
|
|
1902
2120
|
}
|
|
1903
|
-
const root =
|
|
1904
|
-
const path =
|
|
2121
|
+
const root = resolve5(projectRoot);
|
|
2122
|
+
const path = isAbsolute3(reference) ? resolve5(reference) : resolve5(root, reference);
|
|
1905
2123
|
const normalizedRoot = `${root}${sep3}`;
|
|
1906
2124
|
if (path !== root && !path.startsWith(normalizedRoot)) {
|
|
1907
2125
|
throw themeError(`Theme file escapes project root: ${reference}`);
|
|
@@ -1911,11 +2129,11 @@ ${preset}`;
|
|
|
1911
2129
|
}
|
|
1912
2130
|
const custom = await readCss(path, reference);
|
|
1913
2131
|
validateThemeCss(custom, reference);
|
|
1914
|
-
const relativePath =
|
|
2132
|
+
const relativePath = relative4(root, path).split(sep3).join("/");
|
|
1915
2133
|
const t = createTranslator(locale, themeMessages);
|
|
1916
2134
|
return {
|
|
1917
2135
|
id: `file:${relativePath}`,
|
|
1918
|
-
name:
|
|
2136
|
+
name: basename5(relativePath, extname4(relativePath)),
|
|
1919
2137
|
description: t("custom.description", { path: relativePath }),
|
|
1920
2138
|
source: "file",
|
|
1921
2139
|
reference: relativePath,
|
|
@@ -2297,8 +2515,8 @@ function wrapWechatPreview(title, body) {
|
|
|
2297
2515
|
}
|
|
2298
2516
|
|
|
2299
2517
|
// ../../packages/wechat-adapter/dist/index.js
|
|
2300
|
-
import { mkdir as
|
|
2301
|
-
import { dirname as dirname4, join as
|
|
2518
|
+
import { mkdir as mkdir4, readFile as readFile7, rename as rename5, writeFile as writeFile5 } from "fs/promises";
|
|
2519
|
+
import { dirname as dirname4, join as join5 } from "path";
|
|
2302
2520
|
|
|
2303
2521
|
// ../../packages/wechat-client/dist/index.js
|
|
2304
2522
|
var WechatApiError = class extends Error {
|
|
@@ -2520,11 +2738,11 @@ function toWechatArticle(article) {
|
|
|
2520
2738
|
var JsonTokenStore = class {
|
|
2521
2739
|
path;
|
|
2522
2740
|
constructor(home) {
|
|
2523
|
-
this.path =
|
|
2741
|
+
this.path = join5(home, "tokens.json");
|
|
2524
2742
|
}
|
|
2525
2743
|
async read() {
|
|
2526
2744
|
try {
|
|
2527
|
-
return JSON.parse(await
|
|
2745
|
+
return JSON.parse(await readFile7(this.path, "utf8"));
|
|
2528
2746
|
} catch (error) {
|
|
2529
2747
|
if (error.code === "ENOENT") return {};
|
|
2530
2748
|
throw error;
|
|
@@ -2532,11 +2750,11 @@ var JsonTokenStore = class {
|
|
|
2532
2750
|
}
|
|
2533
2751
|
async write(tokens) {
|
|
2534
2752
|
const directory = dirname4(this.path);
|
|
2535
|
-
await
|
|
2753
|
+
await mkdir4(directory, { recursive: true, mode: 448 });
|
|
2536
2754
|
const temporary = `${this.path}.${process.pid}.tmp`;
|
|
2537
|
-
await
|
|
2755
|
+
await writeFile5(temporary, `${JSON.stringify(tokens, null, 2)}
|
|
2538
2756
|
`, { mode: 384 });
|
|
2539
|
-
await
|
|
2757
|
+
await rename5(temporary, this.path);
|
|
2540
2758
|
}
|
|
2541
2759
|
async get(appId) {
|
|
2542
2760
|
return (await this.read())[appId];
|
|
@@ -2749,7 +2967,7 @@ var FaFaService = class _FaFaService {
|
|
|
2749
2967
|
}
|
|
2750
2968
|
static async open(startPath = process.cwd(), options = {}) {
|
|
2751
2969
|
const project = await loadProject(startPath);
|
|
2752
|
-
const statePath =
|
|
2970
|
+
const statePath = join6(project.root, ".fafa", "state.json");
|
|
2753
2971
|
return new _FaFaService(project, new FaFaStateStore(statePath), options);
|
|
2754
2972
|
}
|
|
2755
2973
|
close() {
|
|
@@ -3055,11 +3273,20 @@ export {
|
|
|
3055
3273
|
createTranslator,
|
|
3056
3274
|
FaFaError,
|
|
3057
3275
|
hasErrors,
|
|
3276
|
+
updateArticleFrontmatterFile,
|
|
3277
|
+
loadLibraryRegistry,
|
|
3278
|
+
listRegisteredLibraries,
|
|
3279
|
+
registerLibrary,
|
|
3280
|
+
touchLibrary,
|
|
3281
|
+
removeRegisteredLibrary,
|
|
3282
|
+
CONFIG_FILENAME,
|
|
3058
3283
|
loadProject,
|
|
3059
3284
|
createProject,
|
|
3060
3285
|
setProjectDefaultTheme,
|
|
3061
3286
|
setProjectDefaultCodeTheme,
|
|
3062
3287
|
setProjectDefaultImageCaption,
|
|
3288
|
+
resolveInsideProject,
|
|
3289
|
+
createContentDiscoveryFilter,
|
|
3063
3290
|
getFaFaHome,
|
|
3064
3291
|
loadUserConfig,
|
|
3065
3292
|
upsertAccount,
|