wiki-graph-sdk 0.7.0 → 0.7.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/dist/archives.js CHANGED
@@ -6,7 +6,7 @@ globalThis.__WIKIGRAPH_DATA_DIR__ ??= __WIKIGRAPH_RESOLVE__(__WIKIGRAPH_FILE_URL
6
6
  import { randomUUID as randomUUID2 } from "crypto";
7
7
  import { link, mkdtemp, rename as rename2, rm as rm3, stat as stat2 } from "fs/promises";
8
8
  import { tmpdir } from "os";
9
- import { basename as basename2, dirname as dirname2, join as join3 } from "path";
9
+ import { basename as basename3, dirname as dirname2, join as join3 } from "path";
10
10
  import { Readable as Readable2 } from "stream";
11
11
  import {
12
12
  addChapter,
@@ -51,7 +51,10 @@ import {
51
51
  } from "wiki-graph-core";
52
52
 
53
53
  // src/conversions.ts
54
+ import { createHash } from "crypto";
55
+ import { createReadStream } from "fs";
54
56
  import { rm as rm2 } from "fs/promises";
57
+ import { basename as basename2 } from "path";
55
58
  import {
56
59
  WikiGraph
57
60
  } from "wiki-graph-core";
@@ -1280,6 +1283,9 @@ var WikiGraphConversionManager = class {
1280
1283
  if (options.targetStage !== void 0 && options.output.format !== "wikg") {
1281
1284
  throw new Error("targetStage is only supported for wikg output.");
1282
1285
  }
1286
+ if (options.output.format === "pcex") {
1287
+ throw new Error("pcex is supported only as a conversion input.");
1288
+ }
1283
1289
  if (options.targetStage !== void 0 && options.input.format === "wikg") {
1284
1290
  throw new Error(
1285
1291
  "targetStage is only supported when creating a wikg archive."
@@ -1298,7 +1304,13 @@ var WikiGraphConversionManager = class {
1298
1304
  options.digestDirectory,
1299
1305
  requiresDigest
1300
1306
  );
1307
+ let chapterCount;
1301
1308
  const write = async (archive) => {
1309
+ if (requiresDigest) await options.onSourceImported?.();
1310
+ if (typeof archive.readToc === "function") {
1311
+ const toc = await archive.readToc();
1312
+ chapterCount = toc === void 0 ? void 0 : countTocChapters(toc.items);
1313
+ }
1302
1314
  await writeArchive(archive, options.output.path, options.output.format);
1303
1315
  };
1304
1316
  if (options.input.format === "wikg") {
@@ -1331,16 +1343,28 @@ var WikiGraphConversionManager = class {
1331
1343
  };
1332
1344
  if (options.input.format === "epub") {
1333
1345
  await app.digestEpubSession(digestOptions, write);
1346
+ } else if (options.input.format === "pcex") {
1347
+ await app.digestPcexSession(
1348
+ {
1349
+ ...digestOptions,
1350
+ pdfDigest: options.input.sourcePdf.digest,
1351
+ ...options.input.sourcePdf.name === void 0 ? {} : { pdfName: options.input.sourcePdf.name }
1352
+ },
1353
+ write
1354
+ );
1334
1355
  } else if (options.input.format === "markdown") {
1335
1356
  await app.digestMarkdownSession(digestOptions, write);
1336
1357
  } else {
1337
1358
  await app.digestTxtSession(digestOptions, write);
1338
1359
  }
1339
1360
  }
1361
+ const sourceArtifact = await resolveSourceArtifact(options.input);
1340
1362
  return {
1363
+ ...chapterCount === void 0 ? {} : { chapterCount },
1341
1364
  inputFormat: options.input.format,
1342
1365
  outputFormat: options.output.format,
1343
- outputPath: options.output.path
1366
+ outputPath: options.output.path,
1367
+ ...sourceArtifact === void 0 ? {} : { sourceArtifact }
1344
1368
  };
1345
1369
  }
1346
1370
  };
@@ -1356,8 +1380,48 @@ async function writeArchive(archive, path, format) {
1356
1380
  const file = new NodeFile(resolveWikiGraphRuntimePath(path));
1357
1381
  if (format === "epub") await archive.exportEpub(file);
1358
1382
  else if (format === "wikg") await archive.saveAs(file);
1383
+ else if (format === "pcex") throw new Error("pcex output is not supported.");
1359
1384
  else await archive.exportText(file);
1360
1385
  }
1386
+ async function resolveSourceArtifact(input) {
1387
+ if (!("path" in input) || input.format === "wikg") return void 0;
1388
+ if (input.format === "pcex") {
1389
+ return {
1390
+ digest: input.sourcePdf.digest.trim().toLowerCase(),
1391
+ mediaType: "application/pdf",
1392
+ ...input.sourcePdf.name === void 0 ? {} : { name: input.sourcePdf.name }
1393
+ };
1394
+ }
1395
+ if (input.format !== "epub") return void 0;
1396
+ const path = resolveWikiGraphRuntimePath(input.path);
1397
+ const hash = createHash("sha256");
1398
+ try {
1399
+ for await (const chunk of createReadStream(path)) {
1400
+ hash.update(chunk);
1401
+ }
1402
+ } catch (error) {
1403
+ if (isNodeErrorCode(error, "ENOENT")) return void 0;
1404
+ throw error;
1405
+ }
1406
+ return {
1407
+ digest: hash.digest("hex"),
1408
+ mediaType: "application/epub+zip",
1409
+ name: basename2(path)
1410
+ };
1411
+ }
1412
+ function isNodeErrorCode(error, code) {
1413
+ return typeof error === "object" && error !== null && "code" in error && error.code === code;
1414
+ }
1415
+ function countTocChapters(items) {
1416
+ let count = 0;
1417
+ for (const item of items) {
1418
+ if (item.serialId !== void 0) count += 1;
1419
+ count += countTocChapters(
1420
+ item.children
1421
+ );
1422
+ }
1423
+ return count;
1424
+ }
1361
1425
 
1362
1426
  // src/query-runtime.ts
1363
1427
  import {
@@ -2037,7 +2101,7 @@ var WikiGraphArchiveManager = class {
2037
2101
  }
2038
2102
  const outputPath = join3(
2039
2103
  dirname2(path),
2040
- `.${basename2(path)}.${randomUUID2()}.tmp.wikg`
2104
+ `.${basename3(path)}.${randomUUID2()}.tmp.wikg`
2041
2105
  );
2042
2106
  try {
2043
2107
  if (options.importPath === void 0) {