@drghaliasri/butex 8.1.0 → 8.1.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/document.js CHANGED
@@ -47,6 +47,8 @@ __export(public_exports, {
47
47
  arabicXeLatexPreamblePkg: () => arabicXeLatexPreamblePkg,
48
48
  bibliographyEntryBody: () => bibliographyEntryBody,
49
49
  bibliographyEntryLatex: () => bibliographyEntryLatex,
50
+ bookContentTargets: () => bookContentTargets,
51
+ bookContextReferences: () => bookContextReferences,
50
52
  butexBasmalaDisplayLatex: () => butexBasmalaDisplayLatex,
51
53
  butexBasmalaDisplayTex: () => butexBasmalaDisplayTex,
52
54
  butexBasmalaTitlingPreamble: () => butexBasmalaTitlingPreamble,
@@ -72,6 +74,7 @@ __export(public_exports, {
72
74
  detectInlineSpans: () => detectInlineSpans,
73
75
  detectMathSpans: () => detectMathSpans,
74
76
  diffDocument: () => diffDocument,
77
+ documentContentLatex: () => documentContentLatex2,
75
78
  documentHasAbstract: () => documentHasAbstract,
76
79
  documentHasMaketitle: () => documentHasMaketitle,
77
80
  documentHasTitleStack: () => documentHasTitleStack,
@@ -88,6 +91,7 @@ __export(public_exports, {
88
91
  fillDocumentMetaGaps: () => fillDocumentMetaGaps,
89
92
  findFirstInlineField: () => findFirstInlineField,
90
93
  formatBibliographyNumber: () => formatBibliographyNumber,
94
+ formatBookReferenceLabel: () => formatBookReferenceLabel,
91
95
  formatCiteLabel: () => formatCiteLabel,
92
96
  formatFloatCaptionPreview: () => formatFloatCaptionPreview,
93
97
  formatFloatCaptionTitle: () => formatFloatCaptionTitle,
@@ -99,6 +103,7 @@ __export(public_exports, {
99
103
  getDocumentReferences: () => getDocumentReferences,
100
104
  hasXeLatexUnsupportedEmoji: () => hasXeLatexUnsupportedEmoji,
101
105
  hijriMonthLabel: () => hijriMonthLabel,
106
+ implicitHeadingReferenceKey: () => implicitHeadingReferenceKey,
102
107
  implicitReferenceKey: () => implicitReferenceKey,
103
108
  inlineFieldLatex: () => inlineFieldLatex,
104
109
  insertCiteTokenAtCaret: () => insertCiteTokenAtCaret,
@@ -153,6 +158,7 @@ __export(public_exports, {
153
158
  toggleBlockInSelection: () => toggleBlockInSelection,
154
159
  toggleTextTokenStyle: () => toggleTextTokenStyle,
155
160
  updateCiteTokenKeys: () => updateCiteTokenKeys,
161
+ updateDocumentHeadingLabel: () => updateDocumentHeadingLabel,
156
162
  updateDocumentImageAsset: () => updateDocumentImageAsset,
157
163
  updateDocumentImageMeta: () => updateDocumentImageMeta,
158
164
  updateDocumentImageValue: () => updateDocumentImageValue,
@@ -1020,11 +1026,17 @@ function refTokenLatex(keys, refCommand) {
1020
1026
  const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
1021
1027
  return `${command}{${keys.join(",")}}`;
1022
1028
  }
1023
- function implicitReferenceKey(kind, ownerId, unitId) {
1029
+ function implicitObjectKey(kind, ownerId, unitId) {
1024
1030
  const hex = (value) => Array.from(new TextEncoder().encode(value), (byte) => byte.toString(16).padStart(2, "0")).join("");
1025
1031
  const scope = unitId === void 0 ? "a" : "u" + hex(unitId);
1026
1032
  return kind + ":butex-auto-" + scope + "-" + hex(ownerId);
1027
1033
  }
1034
+ function implicitReferenceKey(kind, ownerId, unitId) {
1035
+ return implicitObjectKey(kind, ownerId, unitId);
1036
+ }
1037
+ function implicitHeadingReferenceKey(kind, ownerId, unitId) {
1038
+ return implicitObjectKey(kind, ownerId, unitId);
1039
+ }
1028
1040
  function supportsSingleEquationNumbering(token) {
1029
1041
  return token.display && (token.opening === "\\[" || token.opening === String.fromCharCode(36, 36) || token.opening === "\\begin{equation}" || token.opening === "\\begin{equation*}");
1030
1042
  }
@@ -1664,6 +1676,7 @@ function parseTextBlock(json, options, path, diagnostics, identities) {
1664
1676
  identities
1665
1677
  ),
1666
1678
  ...metadataFromJson(json.metadata),
1679
+ ...typeof json.label === "string" && json.command !== "\\paragraph" ? { label: json.label } : {},
1667
1680
  ...json.centered === true ? { centered: true } : {}
1668
1681
  };
1669
1682
  }
@@ -5660,6 +5673,24 @@ function updateDocumentTextBlockCentered(document2, blockId, centered) {
5660
5673
  visit(next.blocks);
5661
5674
  return next;
5662
5675
  }
5676
+ function updateDocumentHeadingLabel(document2, blockId, label) {
5677
+ const next = cloneDocument(document2);
5678
+ function visit(blocks) {
5679
+ for (const block of blocks) {
5680
+ if (block.id === blockId && block.kind === "textBlock" && block.command !== "\\paragraph") {
5681
+ if (label.trim()) block.label = label.trim();
5682
+ else delete block.label;
5683
+ return true;
5684
+ }
5685
+ if (block.kind === "list") {
5686
+ for (const item of block.items) if (visit(item.blocks)) return true;
5687
+ }
5688
+ }
5689
+ return false;
5690
+ }
5691
+ visit(next.blocks);
5692
+ return next;
5693
+ }
5663
5694
  function removeDocumentBlockById(document2, blockId) {
5664
5695
  const next = cloneDocument(document2);
5665
5696
  next.blocks = next.blocks.filter((block) => block.id !== blockId);
@@ -7335,6 +7366,7 @@ function blockJson(block) {
7335
7366
  id: block.id,
7336
7367
  command: block.command,
7337
7368
  ...fieldJson(block.field),
7369
+ ...block.command !== "\\paragraph" && block.label ? { label: block.label } : {},
7338
7370
  ...block.metadata ? { metadata: { ...block.metadata } } : {},
7339
7371
  ...block.command === "\\paragraph" ? { centered: block.centered === true } : {}
7340
7372
  };
@@ -7463,6 +7495,13 @@ function parseBasicCommand(value) {
7463
7495
  text: requireString2(value.text, "text")
7464
7496
  };
7465
7497
  }
7498
+ if (value.op === "set_heading_label") {
7499
+ return {
7500
+ op: value.op,
7501
+ block_id: requireString2(value.block_id, "block_id", false).trim(),
7502
+ label: value.label === null ? null : requireString2(value.label, "label")
7503
+ };
7504
+ }
7466
7505
  if (value.op === "remove_block") {
7467
7506
  return { op: value.op, block_id: requireString2(value.block_id, "block_id", false).trim() };
7468
7507
  }
@@ -7555,7 +7594,7 @@ function parseCommand2(value) {
7555
7594
  if (value.op === "update_document_meta" || value.op === "insert_reference" || value.op === "update_reference" || value.op === "remove_reference" || value.op === "move_reference") {
7556
7595
  return parseArticleCommand(value);
7557
7596
  }
7558
- if (value.op === "insert_text_block" || value.op === "replace_text_block" || value.op === "remove_block" || value.op === "move_block" || value.op === "insert_figure" || value.op === "update_figure" || value.op === "update_float_meta" || value.op === "insert_bibliography") {
7597
+ if (value.op === "insert_text_block" || value.op === "replace_text_block" || value.op === "set_heading_label" || value.op === "remove_block" || value.op === "move_block" || value.op === "insert_figure" || value.op === "update_figure" || value.op === "update_float_meta" || value.op === "insert_bibliography") {
7559
7598
  return parseBasicCommand(value);
7560
7599
  }
7561
7600
  throw new DocumentCommandError("invalid_command", "Unsupported document command");
@@ -7600,6 +7639,20 @@ function applyBasicCommand(document2, command) {
7600
7639
  replacePlainField(document2, block2.field, command.text);
7601
7640
  return;
7602
7641
  }
7642
+ if (command.op === "set_heading_label") {
7643
+ const block2 = document2.blocks.find((entry) => entry.id === command.block_id);
7644
+ if (!block2) throw new DocumentCommandError("block_not_found", "Heading was not found");
7645
+ if (block2.kind !== "textBlock" || block2.command === "\\paragraph") {
7646
+ throw new DocumentCommandError("block_kind_mismatch", "Target is not a heading");
7647
+ }
7648
+ const key = command.label?.trim() ?? "";
7649
+ if (key && documentKeyError(key, { references: document2.references, labels: collectDocumentKeyLabels(document2), excludeOwnerId: block2.id })) {
7650
+ throw new DocumentCommandError("invalid_command", "Heading label is invalid or duplicated");
7651
+ }
7652
+ if (key) block2.label = key;
7653
+ else delete block2.label;
7654
+ return;
7655
+ }
7603
7656
  if (command.op === "remove_block") {
7604
7657
  const index = document2.blocks.findIndex((block2) => block2.id === command.block_id);
7605
7658
  if (index < 0) {
@@ -8327,7 +8380,7 @@ function refTokenLatexForDocument(token, labelKinds, previousToken) {
8327
8380
  }
8328
8381
  return `${semanticRefPrefix(kind)}~${reference}`;
8329
8382
  }
8330
- function tokenLatex(token, labelKinds, previousToken) {
8383
+ function tokenLatex(token, labelKinds, previousToken, unitId) {
8331
8384
  if (token.kind === "text") {
8332
8385
  return styledTextLatex(token.text, token.style);
8333
8386
  }
@@ -8339,7 +8392,7 @@ function tokenLatex(token, labelKinds, previousToken) {
8339
8392
  }
8340
8393
  if (isNumberedEquation(token)) {
8341
8394
  const body = mathBodyLatex(token);
8342
- const autoKey = implicitReferenceKey("eq", token.id);
8395
+ const autoKey = implicitReferenceKey("eq", token.id, unitId);
8343
8396
  const aliases = [autoKey];
8344
8397
  if (token.labelEnabled && token.label?.trim() && token.label.trim() !== autoKey) aliases.push(token.label.trim());
8345
8398
  return "\\begin{equation}\n" + body + "\n" + aliases.map((key) => "\\label{" + key + "}").join("\n") + "\n\\end{equation}";
@@ -8355,16 +8408,21 @@ function tokenLatex(token, labelKinds, previousToken) {
8355
8408
  }
8356
8409
  return renderMathNodeLatex(token.math);
8357
8410
  }
8358
- function inlineFieldLatexWithLabelKinds(field, labelKinds) {
8359
- return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
8411
+ function inlineFieldLatexWithLabelKinds(field, labelKinds, unitId) {
8412
+ return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1], unitId)).join("");
8360
8413
  }
8361
8414
  function inlineFieldLatex(field) {
8362
8415
  return inlineFieldLatexWithLabelKinds(field);
8363
8416
  }
8364
- function textBlockLatex(block, labelKinds) {
8365
- const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
8417
+ function textBlockLatex(block, labelKinds, unitId) {
8418
+ const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds, unitId);
8366
8419
  const body = block.command === "\\paragraph" ? `\\par
8367
8420
  ${content}` : `${block.command}{${content}}`;
8421
+ if (block.command !== "\\paragraph") {
8422
+ const keys = unitId ? [implicitHeadingReferenceKey(block.command.slice(1), block.id, unitId)] : [];
8423
+ if (block.label?.trim() && !keys.includes(block.label.trim())) keys.push(block.label.trim());
8424
+ return [body, ...keys.map((key) => `\\label{${key}}`)].join("\n");
8425
+ }
8368
8426
  if (block.command === "\\paragraph" && block.centered) {
8369
8427
  return `\\begin{center}
8370
8428
  ${body}
@@ -8375,10 +8433,10 @@ ${body}
8375
8433
  function indent(value) {
8376
8434
  return value.split("\n").map((line) => ` ${line}`).join("\n");
8377
8435
  }
8378
- function listBlockLatex(block, references, labelKinds, fixedFloats) {
8436
+ function listBlockLatex(block, references, labelKinds, fixedFloats, unitId) {
8379
8437
  const items = block.items.map((item) => {
8380
- const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
8381
- return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
8438
+ const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats, unitId))).join("");
8439
+ return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds, unitId)}${nested}`;
8382
8440
  }).join("\n");
8383
8441
  return `${block.command}
8384
8442
  ${items}
@@ -8392,9 +8450,9 @@ function defaultGridColumnSpec(block) {
8392
8450
  }
8393
8451
  return `|${compact.split("").join("|")}|`;
8394
8452
  }
8395
- function tabularInnerLatex(block, labelKinds) {
8453
+ function tabularInnerLatex(block, labelKinds, unitId) {
8396
8454
  const rows = block.rows.map(
8397
- (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
8455
+ (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds, unitId)).join(" & ")}`
8398
8456
  );
8399
8457
  const gridColumns = defaultGridColumnSpec(block);
8400
8458
  if (gridColumns) {
@@ -8408,9 +8466,9 @@ ${block.closing}`;
8408
8466
  ${rows.join(" \\\\\n")}
8409
8467
  ${block.closing}`;
8410
8468
  }
8411
- function floatCaptionLabelLines(block, counter, labelKinds) {
8469
+ function floatCaptionLabelLines(block, counter, labelKinds, unitId) {
8412
8470
  const lines = [];
8413
- const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds).trim();
8471
+ const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds, unitId).trim();
8414
8472
  const hasCaption = block.captionEnabled && caption.length > 0;
8415
8473
  if (hasCaption) {
8416
8474
  lines.push(` \\caption{${caption}}`);
@@ -8418,20 +8476,20 @@ function floatCaptionLabelLines(block, counter, labelKinds) {
8418
8476
  const hasManualLabel = block.labelEnabled && block.label.trim().length > 0;
8419
8477
  if (hasCaption || hasManualLabel) {
8420
8478
  if (!hasCaption) lines.push(" \\refstepcounter{" + counter + "}");
8421
- const autoKey = implicitReferenceKey(counter === "figure" ? "fig" : "tab", block.id);
8479
+ const autoKey = implicitReferenceKey(counter === "figure" ? "fig" : "tab", block.id, unitId);
8422
8480
  lines.push(" \\label{" + autoKey + "}");
8423
8481
  if (hasManualLabel && block.label.trim() !== autoKey) lines.push(" \\label{" + block.label.trim() + "}");
8424
8482
  }
8425
8483
  return lines;
8426
8484
  }
8427
- function tableBlockLatex(block, labelKinds, fixedFloats) {
8485
+ function tableBlockLatex(block, labelKinds, fixedFloats, unitId) {
8428
8486
  const placement = fixedFloats ? "H" : "htbp";
8429
8487
  const lines = [`\\begin{table}[${placement}]`];
8430
8488
  if (block.centered) {
8431
8489
  lines.push(" \\centering");
8432
8490
  }
8433
- lines.push(...floatCaptionLabelLines(block, "table", labelKinds));
8434
- lines.push(indent(tabularInnerLatex(block, labelKinds)));
8491
+ lines.push(...floatCaptionLabelLines(block, "table", labelKinds, unitId));
8492
+ lines.push(indent(tabularInnerLatex(block, labelKinds, unitId)));
8435
8493
  lines.push("\\end{table}");
8436
8494
  return lines.join("\n");
8437
8495
  }
@@ -8450,29 +8508,29 @@ function imageOptionsLatex(block, constrainToColumn) {
8450
8508
  }
8451
8509
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
8452
8510
  }
8453
- function imageBlockLatex(block, labelKinds, fixedFloats) {
8511
+ function imageBlockLatex(block, labelKinds, fixedFloats, unitId) {
8454
8512
  const placement = fixedFloats ? "H" : "htbp";
8455
8513
  const lines = [`\\begin{figure}[${placement}]`];
8456
8514
  if (block.centered) {
8457
8515
  lines.push(" \\centering");
8458
8516
  }
8459
8517
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
8460
- lines.push(...floatCaptionLabelLines(block, "figure", labelKinds));
8518
+ lines.push(...floatCaptionLabelLines(block, "figure", labelKinds, unitId));
8461
8519
  lines.push("\\end{figure}");
8462
8520
  return lines.join("\n");
8463
8521
  }
8464
- function blockLatex(block, references, labelKinds, fixedFloats) {
8522
+ function blockLatex(block, references, labelKinds, fixedFloats, unitId) {
8465
8523
  if (block.kind === "textBlock") {
8466
- return textBlockLatex(block, labelKinds);
8524
+ return textBlockLatex(block, labelKinds, unitId);
8467
8525
  }
8468
8526
  if (block.kind === "list") {
8469
- return listBlockLatex(block, references, labelKinds, fixedFloats);
8527
+ return listBlockLatex(block, references, labelKinds, fixedFloats, unitId);
8470
8528
  }
8471
8529
  if (block.kind === "table") {
8472
- return tableBlockLatex(block, labelKinds, fixedFloats);
8530
+ return tableBlockLatex(block, labelKinds, fixedFloats, unitId);
8473
8531
  }
8474
8532
  if (block.kind === "image") {
8475
- return imageBlockLatex(block, labelKinds, fixedFloats);
8533
+ return imageBlockLatex(block, labelKinds, fixedFloats, unitId);
8476
8534
  }
8477
8535
  if (block.kind === "bibliography") {
8478
8536
  const width = String(Math.max(references.length, 9));
@@ -8526,6 +8584,17 @@ function closingHamdalaLatex() {
8526
8584
  \\maghribi{${BUTEX_CLOSING_HAMDALA}}
8527
8585
  \\end{center}`;
8528
8586
  }
8587
+ function documentContentLatex(document2, context) {
8588
+ if (!context?.unitId?.trim()) throw new Error("book-content export requires a stable unitId");
8589
+ const labelKinds = documentLabelKindMap(document2);
8590
+ for (const target of context?.targets ?? []) {
8591
+ if (target.kind === "fig" || target.kind === "tab" || target.kind === "eq") {
8592
+ labelKinds.set(target.key, target.kind);
8593
+ if (target.manualKey) labelKinds.set(target.manualKey, target.kind);
8594
+ }
8595
+ }
8596
+ return document2.blocks.filter((block) => block.kind !== "bibliography").map((block) => blockLatex(block, document2.references, labelKinds, false, context?.unitId)).filter((part) => part.length > 0).join("\n\n");
8597
+ }
8529
8598
  function documentLatex(document2, options = {}) {
8530
8599
  const meta = document2.meta ?? emptyDocumentMeta();
8531
8600
  const twocolumn = options.twocolumn === true;
@@ -8557,6 +8626,65 @@ ${body}
8557
8626
  `;
8558
8627
  }
8559
8628
 
8629
+ // src/document/bookContent.ts
8630
+ function bookContextReferences(context) {
8631
+ return (context?.references ?? []).map((entry, index) => ({
8632
+ ...referenceFromJson(entry),
8633
+ id: "book-ref-" + String(index)
8634
+ }));
8635
+ }
8636
+ function bookContentTargets(document2, context) {
8637
+ const local = collectDocumentReferenceTargets(document2).map((target) => {
8638
+ const key = implicitReferenceKey(target.kind, target.ownerId, context.unitId);
8639
+ const projection = context.targets?.find((entry) => entry.unitId === context.unitId && (entry.ownerId === target.ownerId || entry.key === key));
8640
+ return {
8641
+ ...target,
8642
+ key,
8643
+ ...projection?.displayLabel ? { displayLabel: projection.displayLabel } : {}
8644
+ };
8645
+ });
8646
+ const headings = document2.blocks.flatMap((block) => {
8647
+ if (block.kind !== "textBlock" || block.command === "\\paragraph") return [];
8648
+ const kind = block.command.slice(1);
8649
+ const caption = block.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("");
8650
+ return [{
8651
+ unitId: context.unitId,
8652
+ ownerId: block.id,
8653
+ kind,
8654
+ key: implicitHeadingReferenceKey(kind, block.id, context.unitId),
8655
+ caption,
8656
+ number: null,
8657
+ displayLabel: context.targets?.find((target) => target.unitId === context.unitId && target.ownerId === block.id)?.displayLabel ?? (caption || kind),
8658
+ ...block.label ? { manualKey: block.label } : {}
8659
+ }];
8660
+ });
8661
+ const external = (context.targets ?? []).filter((target) => target.unitId !== context.unitId).map((target) => ({
8662
+ ...target,
8663
+ caption: target.caption ?? "",
8664
+ number: null
8665
+ }));
8666
+ return [...local, ...headings, ...external];
8667
+ }
8668
+ function formatBookReferenceLabel(keys, document2, command, context, options = {}) {
8669
+ const targets = context.targets ?? [];
8670
+ return keys.map((key) => {
8671
+ const target = targets.find((entry) => entry.key === key || entry.manualKey === key);
8672
+ if (target?.displayLabel) return target.displayLabel;
8673
+ const local = collectDocumentReferenceTargets(document2).find((entry) => entry.manualKey === key || implicitReferenceKey(entry.kind, entry.ownerId, context.unitId) === key);
8674
+ if (local) return formatRefLabel([local.manualKey || local.key], document2, command, options);
8675
+ const heading = document2.blocks.find((block) => block.kind === "textBlock" && block.command !== "\\paragraph" && (block.label === key || implicitHeadingReferenceKey(
8676
+ block.command.slice(1),
8677
+ block.id,
8678
+ context.unitId
8679
+ ) === key));
8680
+ if (heading?.kind === "textBlock") {
8681
+ const caption = heading.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("").trim();
8682
+ return caption || heading.command.slice(1);
8683
+ }
8684
+ return command === "eqref" ? "(?)" : "?";
8685
+ }).join(options.documentDirection === "ltr" ? ", " : "\u060C");
8686
+ }
8687
+
8560
8688
  // src/document/validation.ts
8561
8689
  var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
8562
8690
  function hasXeLatexUnsupportedEmoji(value) {
@@ -8640,6 +8768,13 @@ function validateInlineField(context, field, path, blockId) {
8640
8768
  function validateBlock(context, block, path) {
8641
8769
  if (block.kind === "textBlock") {
8642
8770
  validateInlineField(context, block.field, `${path}.field`, block.id);
8771
+ if (block.command !== "\\paragraph") {
8772
+ const kind = block.command.slice(1);
8773
+ if (block.label) registerKey(context, block.label, `${path}.label`, kind, block.id);
8774
+ if (context.bookMode && context.bookContext) {
8775
+ context.labelKinds.set(implicitHeadingReferenceKey(kind, block.id, context.bookContext.unitId), kind);
8776
+ }
8777
+ }
8643
8778
  return;
8644
8779
  }
8645
8780
  if (block.kind === "list") {
@@ -8689,6 +8824,10 @@ function validateBlock(context, block, path) {
8689
8824
  }
8690
8825
  return;
8691
8826
  }
8827
+ if (block.kind === "bibliography" && context.bookMode) {
8828
+ addIssue(context, "book_local_bibliography", path, block.id);
8829
+ return;
8830
+ }
8692
8831
  if (block.kind === "raw") {
8693
8832
  validateTextValue(context, block.value, `${path}.value`, block.id);
8694
8833
  }
@@ -8711,7 +8850,7 @@ function validateDuplicateKeys(context) {
8711
8850
  }
8712
8851
  function validatePendingReferences(context) {
8713
8852
  for (const pending of context.pendingCitations) {
8714
- if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)))) {
8853
+ if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)) || context.citationKeys !== void 0 && !context.citationKeys.has(normalizeDocumentKey(key)))) {
8715
8854
  addIssue(
8716
8855
  context,
8717
8856
  "missing_citation",
@@ -8793,7 +8932,7 @@ function resolveDocument(document2) {
8793
8932
  return null;
8794
8933
  }
8795
8934
  }
8796
- function validateDocument(document2) {
8935
+ function validateDocument(document2, options = {}) {
8797
8936
  const resolved = resolveDocument(document2);
8798
8937
  if (!resolved) {
8799
8938
  return {
@@ -8805,6 +8944,9 @@ function validateDocument(document2) {
8805
8944
  issues: [],
8806
8945
  keyOwners: [],
8807
8946
  labelKinds: /* @__PURE__ */ new Map(),
8947
+ bookMode: options.mode === "book-content",
8948
+ bookContext: options.bookContext,
8949
+ ...options.mode === "book-content" && options.bookContext?.references ? { citationKeys: new Set(options.bookContext.references.map((entry) => normalizeDocumentKey(entry.key))) } : {},
8808
8950
  pendingCitations: [],
8809
8951
  pendingReferences: []
8810
8952
  };
@@ -8824,10 +8966,11 @@ function validateDocument(document2) {
8824
8966
  validateBlock(context, block, `$.blocks[${String(index)}]`);
8825
8967
  });
8826
8968
  for (const target of collectDocumentReferenceTargets(resolved)) {
8827
- if (target.number !== null && target.manualKey !== target.key) {
8969
+ const autoKey = context.bookMode && context.bookContext ? implicitReferenceKey(target.kind, target.ownerId, context.bookContext.unitId) : target.key;
8970
+ if (target.number !== null && target.manualKey !== autoKey) {
8828
8971
  registerKey(
8829
8972
  context,
8830
- target.key,
8973
+ autoKey,
8831
8974
  "$.automatic_labels",
8832
8975
  target.kind,
8833
8976
  target.kind === "eq" ? void 0 : target.ownerId,
@@ -8835,6 +8978,15 @@ function validateDocument(document2) {
8835
8978
  );
8836
8979
  }
8837
8980
  }
8981
+ if (context.bookMode) {
8982
+ for (const target of context.bookContext?.targets ?? []) {
8983
+ context.labelKinds.set(normalizeDocumentKey(target.key), target.kind);
8984
+ if (target.manualKey) context.labelKinds.set(normalizeDocumentKey(target.manualKey), target.kind);
8985
+ }
8986
+ }
8987
+ if (context.bookMode && !context.bookContext?.unitId?.trim()) {
8988
+ addIssue(context, "book_context_required", "$.book_context");
8989
+ }
8838
8990
  for (const diagnostic of resolved.diagnostics) {
8839
8991
  addIssue(context, "parser_diagnostic", diagnostic.path);
8840
8992
  }
@@ -8965,7 +9117,7 @@ function previewInlines(field, islands, output, equationSide, document2, preview
8965
9117
  kind: "cite",
8966
9118
  id: token.id,
8967
9119
  keys: [...token.keys],
8968
- label: formatCiteLabel(token.keys, document2.references, {
9120
+ label: formatCiteLabel(token.keys, previewOptions.mode === "book-content" ? bookContextReferences(previewOptions.bookContext) : document2.references, {
8969
9121
  documentDirection: previewOptions.documentDirection,
8970
9122
  digitForm: previewOptions.digitForm
8971
9123
  })
@@ -8977,7 +9129,10 @@ function previewInlines(field, islands, output, equationSide, document2, preview
8977
9129
  id: token.id,
8978
9130
  keys: [...token.keys],
8979
9131
  refCommand: token.refCommand,
8980
- label: formatRefLabel(token.keys, document2, token.refCommand, {
9132
+ label: previewOptions.mode === "book-content" && previewOptions.bookContext ? formatBookReferenceLabel(token.keys, document2, token.refCommand, previewOptions.bookContext, {
9133
+ documentDirection: previewOptions.documentDirection,
9134
+ digitForm: previewOptions.digitForm
9135
+ }) : formatRefLabel(token.keys, document2, token.refCommand, {
8981
9136
  documentDirection: previewOptions.documentDirection,
8982
9137
  digitForm: previewOptions.digitForm
8983
9138
  })
@@ -9133,14 +9288,16 @@ function documentPreview(document2, output = "svg", equationSide = "arabic", pre
9133
9288
  const options = {
9134
9289
  documentDirection: previewOptions.documentDirection ?? "rtl",
9135
9290
  digitForm: previewOptions.digitForm ?? (previewOptions.documentDirection === "ltr" ? "western" : "arabicIndic"),
9136
- uiLocale: previewOptions.uiLocale ?? "ar"
9291
+ uiLocale: previewOptions.uiLocale ?? "ar",
9292
+ mode: previewOptions.mode ?? "article",
9293
+ bookContext: previewOptions.bookContext
9137
9294
  };
9138
- const body = document2.blocks.map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
9295
+ const body = document2.blocks.filter((block) => options.mode !== "book-content" || block.kind !== "bibliography").map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
9139
9296
  return {
9140
9297
  blocks: [
9141
- ...articleChromePreview(document2, options.uiLocale ?? "ar", options),
9298
+ ...options.mode === "book-content" ? [] : articleChromePreview(document2, options.uiLocale ?? "ar", options),
9142
9299
  ...body,
9143
- { kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }
9300
+ ...options.mode === "book-content" ? [] : [{ kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }]
9144
9301
  ],
9145
9302
  mathIslands
9146
9303
  };
@@ -9154,6 +9311,11 @@ function documentLatex2(document2, options = {}) {
9154
9311
  }
9155
9312
  return documentLatex(document2, options);
9156
9313
  }
9314
+ function documentContentLatex2(document2, context) {
9315
+ const validation = validateDocument(document2, { mode: "book-content", bookContext: context });
9316
+ if (!validation.valid) throw new DocumentValidationError(validation.issues);
9317
+ return documentContentLatex(document2, context);
9318
+ }
9157
9319
  // Annotate the CommonJS export names for ESM import in node:
9158
9320
  0 && (module.exports = {
9159
9321
  BUTEX_BASMALA,
@@ -9183,6 +9345,8 @@ function documentLatex2(document2, options = {}) {
9183
9345
  arabicXeLatexPreamblePkg,
9184
9346
  bibliographyEntryBody,
9185
9347
  bibliographyEntryLatex,
9348
+ bookContentTargets,
9349
+ bookContextReferences,
9186
9350
  butexBasmalaDisplayLatex,
9187
9351
  butexBasmalaDisplayTex,
9188
9352
  butexBasmalaTitlingPreamble,
@@ -9208,6 +9372,7 @@ function documentLatex2(document2, options = {}) {
9208
9372
  detectInlineSpans,
9209
9373
  detectMathSpans,
9210
9374
  diffDocument,
9375
+ documentContentLatex,
9211
9376
  documentHasAbstract,
9212
9377
  documentHasMaketitle,
9213
9378
  documentHasTitleStack,
@@ -9224,6 +9389,7 @@ function documentLatex2(document2, options = {}) {
9224
9389
  fillDocumentMetaGaps,
9225
9390
  findFirstInlineField,
9226
9391
  formatBibliographyNumber,
9392
+ formatBookReferenceLabel,
9227
9393
  formatCiteLabel,
9228
9394
  formatFloatCaptionPreview,
9229
9395
  formatFloatCaptionTitle,
@@ -9235,6 +9401,7 @@ function documentLatex2(document2, options = {}) {
9235
9401
  getDocumentReferences,
9236
9402
  hasXeLatexUnsupportedEmoji,
9237
9403
  hijriMonthLabel,
9404
+ implicitHeadingReferenceKey,
9238
9405
  implicitReferenceKey,
9239
9406
  inlineFieldLatex,
9240
9407
  insertCiteTokenAtCaret,
@@ -9289,6 +9456,7 @@ function documentLatex2(document2, options = {}) {
9289
9456
  toggleBlockInSelection,
9290
9457
  toggleTextTokenStyle,
9291
9458
  updateCiteTokenKeys,
9459
+ updateDocumentHeadingLabel,
9292
9460
  updateDocumentImageAsset,
9293
9461
  updateDocumentImageMeta,
9294
9462
  updateDocumentImageValue,