@acanthis-dec/cli 0.3.0 → 0.3.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/index.mjs CHANGED
@@ -11,7 +11,7 @@ import { stripVTControlCharacters } from "node:util";
11
11
  import fs$1 from "node:fs";
12
12
  import readline from "node:readline";
13
13
  // package.json
14
- var version = "0.3.0";
14
+ var version = "0.3.1";
15
15
  // src/client.ts
16
16
  var ApiClientError = class extends Error {
17
17
  status;
@@ -4220,6 +4220,14 @@ const HEADING_TAG_NAMES = /* @__PURE__ */ new Set([
4220
4220
  "h2",
4221
4221
  "h3"
4222
4222
  ]);
4223
+ const ALL_HEADING_TAG_NAMES = /* @__PURE__ */ new Set([
4224
+ "h1",
4225
+ "h2",
4226
+ "h3",
4227
+ "h4",
4228
+ "h5",
4229
+ "h6"
4230
+ ]);
4223
4231
  const URI_ATTRIBUTE_NAMES = /* @__PURE__ */ new Set([
4224
4232
  "href",
4225
4233
  "src",
@@ -4446,18 +4454,122 @@ function escapeNodesForXmlSerialization(nodes) {
4446
4454
  }
4447
4455
  }
4448
4456
  }
4449
- function sanitizeChapterHtmlToXhtml(options) {
4450
- const normalizedChildren = normalizeLeadingHeading(sanitizeNodeList(parseDocument(options.chapter.html, { decodeEntities: true }).children, options.imageHrefBySourceUrl), options.chapter.title);
4457
+ function hasSingleImageChild(element) {
4458
+ let foundImg = false;
4459
+ for (const child of element.children) {
4460
+ if (isText$1(child)) {
4461
+ if (child.data.trim().length > 0) return false;
4462
+ continue;
4463
+ }
4464
+ if (isTag$1(child)) {
4465
+ if (child.name.toLowerCase() === "img" && !foundImg) {
4466
+ foundImg = true;
4467
+ continue;
4468
+ }
4469
+ return false;
4470
+ }
4471
+ if (!isComment$1(child)) return false;
4472
+ }
4473
+ return foundImg;
4474
+ }
4475
+ function annotateChapterBlocks(nodes) {
4476
+ const result = [];
4477
+ let blockIndex = 0;
4478
+ for (const node of nodes) {
4479
+ if (isText$1(node)) {
4480
+ const trimmed = node.data.trim();
4481
+ if (!trimmed) {
4482
+ result.push(node);
4483
+ continue;
4484
+ }
4485
+ const wrapped = new Element$1("p", { id: `b${blockIndex}` }, [new Text$1(trimmed)]);
4486
+ wrapped.parent = node.parent;
4487
+ result.push(wrapped);
4488
+ blockIndex += 1;
4489
+ continue;
4490
+ }
4491
+ if (!isTag$1(node)) {
4492
+ result.push(node);
4493
+ continue;
4494
+ }
4495
+ const tag = node.name.toLowerCase();
4496
+ if (tag === "img") {
4497
+ node.attribs = {
4498
+ ...node.attribs,
4499
+ id: `b${blockIndex}`
4500
+ };
4501
+ result.push(node);
4502
+ blockIndex += 1;
4503
+ continue;
4504
+ }
4505
+ if (tag === "p") {
4506
+ if (hasSingleImageChild(node)) {
4507
+ node.attribs = {
4508
+ ...node.attribs,
4509
+ id: `b${blockIndex}`
4510
+ };
4511
+ result.push(node);
4512
+ blockIndex += 1;
4513
+ continue;
4514
+ }
4515
+ const hasText = collectTextContent(node).trim().length > 0;
4516
+ const hasElementChild = node.children.some((child) => isTag$1(child));
4517
+ if (!hasText && !hasElementChild) {
4518
+ result.push(node);
4519
+ continue;
4520
+ }
4521
+ node.attribs = {
4522
+ ...node.attribs,
4523
+ id: `b${blockIndex}`
4524
+ };
4525
+ result.push(node);
4526
+ blockIndex += 1;
4527
+ continue;
4528
+ }
4529
+ if (ALL_HEADING_TAG_NAMES.has(tag)) {
4530
+ node.attribs = {
4531
+ ...node.attribs,
4532
+ id: `b${blockIndex}`
4533
+ };
4534
+ result.push(node);
4535
+ blockIndex += 1;
4536
+ continue;
4537
+ }
4538
+ node.attribs = {
4539
+ ...node.attribs,
4540
+ id: `b${blockIndex}`
4541
+ };
4542
+ result.push(node);
4543
+ blockIndex += 1;
4544
+ }
4545
+ return {
4546
+ nodes: result,
4547
+ totalParagraphs: blockIndex
4548
+ };
4549
+ }
4550
+ function sanitizeChapterHtmlToXhtmlWithStats(options) {
4551
+ const sanitizedChildren = sanitizeNodeList(parseDocument(options.chapter.html, { decodeEntities: true }).children, options.imageHrefBySourceUrl);
4552
+ let childrenForHeading = sanitizedChildren;
4553
+ let totalParagraphs = 0;
4554
+ if (options.enableBlockAnchors) {
4555
+ const annotated = annotateChapterBlocks(sanitizedChildren);
4556
+ childrenForHeading = annotated.nodes;
4557
+ totalParagraphs = annotated.totalParagraphs;
4558
+ }
4559
+ const normalizedChildren = normalizeLeadingHeading(childrenForHeading, options.chapter.title);
4451
4560
  escapeNodesForXmlSerialization(normalizedChildren);
4452
4561
  const bodyHtml = render(normalizedChildren, {
4453
4562
  xmlMode: true,
4454
4563
  encodeEntities: false
4455
4564
  });
4456
- return wrapXhtmlDocument({
4457
- title: options.chapter.title,
4458
- language: options.language,
4459
- bodyHtml
4460
- });
4565
+ return {
4566
+ xhtml: wrapXhtmlDocument({
4567
+ title: options.chapter.title,
4568
+ language: options.language,
4569
+ bodyHtml
4570
+ }),
4571
+ totalParagraphs
4572
+ };
4461
4573
  }
4462
4574
  // ../../packages/epub/src/nav.ts
4463
4575
  function renderNavList(entries, indentLevel) {
@@ -4862,10 +4974,11 @@ function appendChapterEntry(options) {
4862
4974
  const manifestId = `ch-${paddedIndex}`;
4863
4975
  const manifestHref = `text/ch-${paddedIndex}.xhtml`;
4864
4976
  const zipPath = `OEBPS/${manifestHref}`;
4865
- const xhtml = sanitizeChapterHtmlToXhtml({
4977
+ const { xhtml, totalParagraphs } = sanitizeChapterHtmlToXhtmlWithStats({
4866
4978
  chapter: options.chapter,
4867
4979
  imageHrefBySourceUrl: options.imageHrefBySourceUrl,
4868
- language: options.language
4980
+ language: options.language,
4981
+ enableBlockAnchors: options.enableBlockAnchors
4869
4982
  });
4870
4983
  options.manifest.push({
4871
4984
  id: manifestId,
@@ -4878,8 +4991,11 @@ function appendChapterEntry(options) {
4878
4991
  content: xhtml
4879
4992
  });
4880
4993
  return {
4881
- title: options.chapter.title,
4882
- href: manifestHref
4994
+ navEntry: {
4995
+ title: options.chapter.title,
4996
+ href: manifestHref
4997
+ },
4998
+ totalParagraphs
4883
4999
  };
4884
5000
  }
4885
5001
  function buildVolumeEpub(input) {
@@ -4902,19 +5018,46 @@ function buildVolumeEpub(input) {
4902
5018
  binaryEntries
4903
5019
  }).coverImageId;
4904
5020
  const { imageHrefBySourceUrl, registeredImages } = registerIllustrations(input.illustrations);
5021
+ const enableBlockAnchors = input.syncMetadata !== void 0;
5022
+ const manifestChapters = [];
4905
5023
  let chapterCounter = 0;
4906
5024
  for (const chapter of input.chapters) {
4907
5025
  chapterCounter += 1;
4908
- const navEntry = appendChapterEntry({
5026
+ const { navEntry, totalParagraphs } = appendChapterEntry({
4909
5027
  chapter,
4910
5028
  chapterIndex: chapterCounter,
4911
5029
  language,
4912
5030
  imageHrefBySourceUrl,
4913
5031
  manifest,
4914
5032
  spine,
4915
- textEntries
5033
+ textEntries,
5034
+ enableBlockAnchors
4916
5035
  });
4917
5036
  navEntries.push(navEntry);
5037
+ if (enableBlockAnchors) manifestChapters.push({
5038
+ index: chapterCounter,
5039
+ href: navEntry.href,
5040
+ chapterId: chapter.id,
5041
+ title: chapter.title,
5042
+ totalParagraphs
5043
+ });
5044
+ }
5045
+ if (input.syncMetadata) {
5046
+ const acanthisManifest = {
5047
+ schemaVersion: 1,
5048
+ platform: input.syncMetadata.platform,
5049
+ novelId: input.syncMetadata.novelId,
5050
+ volumeId: input.syncMetadata.volumeId,
5051
+ volumeTitle: input.volumeTitle,
5052
+ profile: input.syncMetadata.profile,
5053
+ contentFingerprint: input.syncMetadata.contentFingerprint,
5054
+ generatedAt: modifiedAt.toISOString(),
5055
+ chapters: manifestChapters
5056
+ };
5057
+ textEntries.push({
5058
+ zipPath: "OEBPS/acanthis-manifest.json",
5059
+ content: `${JSON.stringify(acanthisManifest, null, 2)}\n`
5060
+ });
4918
5061
  }
4919
5062
  for (const image of registeredImages) {
4920
5063
  manifest.push({
@@ -5013,7 +5156,7 @@ function buildNovelEpub(input) {
5013
5156
  const volumeNavChildren = [];
5014
5157
  for (const chapter of volume.chapters) {
5015
5158
  chapterCounter += 1;
5016
- const chapterNavEntry = appendChapterEntry({
5159
+ const { navEntry: chapterNavEntry } = appendChapterEntry({
5017
5160
  chapter,
5018
5161
  chapterIndex: chapterCounter,
5019
5162
  language,