@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.mjs CHANGED
@@ -851,11 +851,17 @@ function refTokenLatex(keys, refCommand) {
851
851
  const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
852
852
  return `${command}{${keys.join(",")}}`;
853
853
  }
854
- function implicitReferenceKey(kind, ownerId, unitId) {
854
+ function implicitObjectKey(kind, ownerId, unitId) {
855
855
  const hex = (value) => Array.from(new TextEncoder().encode(value), (byte) => byte.toString(16).padStart(2, "0")).join("");
856
856
  const scope = unitId === void 0 ? "a" : "u" + hex(unitId);
857
857
  return kind + ":butex-auto-" + scope + "-" + hex(ownerId);
858
858
  }
859
+ function implicitReferenceKey(kind, ownerId, unitId) {
860
+ return implicitObjectKey(kind, ownerId, unitId);
861
+ }
862
+ function implicitHeadingReferenceKey(kind, ownerId, unitId) {
863
+ return implicitObjectKey(kind, ownerId, unitId);
864
+ }
859
865
  function supportsSingleEquationNumbering(token) {
860
866
  return token.display && (token.opening === "\\[" || token.opening === String.fromCharCode(36, 36) || token.opening === "\\begin{equation}" || token.opening === "\\begin{equation*}");
861
867
  }
@@ -1495,6 +1501,7 @@ function parseTextBlock(json, options, path, diagnostics, identities) {
1495
1501
  identities
1496
1502
  ),
1497
1503
  ...metadataFromJson(json.metadata),
1504
+ ...typeof json.label === "string" && json.command !== "\\paragraph" ? { label: json.label } : {},
1498
1505
  ...json.centered === true ? { centered: true } : {}
1499
1506
  };
1500
1507
  }
@@ -5491,6 +5498,24 @@ function updateDocumentTextBlockCentered(document2, blockId, centered) {
5491
5498
  visit(next.blocks);
5492
5499
  return next;
5493
5500
  }
5501
+ function updateDocumentHeadingLabel(document2, blockId, label) {
5502
+ const next = cloneDocument(document2);
5503
+ function visit(blocks) {
5504
+ for (const block of blocks) {
5505
+ if (block.id === blockId && block.kind === "textBlock" && block.command !== "\\paragraph") {
5506
+ if (label.trim()) block.label = label.trim();
5507
+ else delete block.label;
5508
+ return true;
5509
+ }
5510
+ if (block.kind === "list") {
5511
+ for (const item of block.items) if (visit(item.blocks)) return true;
5512
+ }
5513
+ }
5514
+ return false;
5515
+ }
5516
+ visit(next.blocks);
5517
+ return next;
5518
+ }
5494
5519
  function removeDocumentBlockById(document2, blockId) {
5495
5520
  const next = cloneDocument(document2);
5496
5521
  next.blocks = next.blocks.filter((block) => block.id !== blockId);
@@ -7166,6 +7191,7 @@ function blockJson(block) {
7166
7191
  id: block.id,
7167
7192
  command: block.command,
7168
7193
  ...fieldJson(block.field),
7194
+ ...block.command !== "\\paragraph" && block.label ? { label: block.label } : {},
7169
7195
  ...block.metadata ? { metadata: { ...block.metadata } } : {},
7170
7196
  ...block.command === "\\paragraph" ? { centered: block.centered === true } : {}
7171
7197
  };
@@ -7294,6 +7320,13 @@ function parseBasicCommand(value) {
7294
7320
  text: requireString2(value.text, "text")
7295
7321
  };
7296
7322
  }
7323
+ if (value.op === "set_heading_label") {
7324
+ return {
7325
+ op: value.op,
7326
+ block_id: requireString2(value.block_id, "block_id", false).trim(),
7327
+ label: value.label === null ? null : requireString2(value.label, "label")
7328
+ };
7329
+ }
7297
7330
  if (value.op === "remove_block") {
7298
7331
  return { op: value.op, block_id: requireString2(value.block_id, "block_id", false).trim() };
7299
7332
  }
@@ -7386,7 +7419,7 @@ function parseCommand2(value) {
7386
7419
  if (value.op === "update_document_meta" || value.op === "insert_reference" || value.op === "update_reference" || value.op === "remove_reference" || value.op === "move_reference") {
7387
7420
  return parseArticleCommand(value);
7388
7421
  }
7389
- 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") {
7422
+ 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") {
7390
7423
  return parseBasicCommand(value);
7391
7424
  }
7392
7425
  throw new DocumentCommandError("invalid_command", "Unsupported document command");
@@ -7431,6 +7464,20 @@ function applyBasicCommand(document2, command) {
7431
7464
  replacePlainField(document2, block2.field, command.text);
7432
7465
  return;
7433
7466
  }
7467
+ if (command.op === "set_heading_label") {
7468
+ const block2 = document2.blocks.find((entry) => entry.id === command.block_id);
7469
+ if (!block2) throw new DocumentCommandError("block_not_found", "Heading was not found");
7470
+ if (block2.kind !== "textBlock" || block2.command === "\\paragraph") {
7471
+ throw new DocumentCommandError("block_kind_mismatch", "Target is not a heading");
7472
+ }
7473
+ const key = command.label?.trim() ?? "";
7474
+ if (key && documentKeyError(key, { references: document2.references, labels: collectDocumentKeyLabels(document2), excludeOwnerId: block2.id })) {
7475
+ throw new DocumentCommandError("invalid_command", "Heading label is invalid or duplicated");
7476
+ }
7477
+ if (key) block2.label = key;
7478
+ else delete block2.label;
7479
+ return;
7480
+ }
7434
7481
  if (command.op === "remove_block") {
7435
7482
  const index = document2.blocks.findIndex((block2) => block2.id === command.block_id);
7436
7483
  if (index < 0) {
@@ -8158,7 +8205,7 @@ function refTokenLatexForDocument(token, labelKinds, previousToken) {
8158
8205
  }
8159
8206
  return `${semanticRefPrefix(kind)}~${reference}`;
8160
8207
  }
8161
- function tokenLatex(token, labelKinds, previousToken) {
8208
+ function tokenLatex(token, labelKinds, previousToken, unitId) {
8162
8209
  if (token.kind === "text") {
8163
8210
  return styledTextLatex(token.text, token.style);
8164
8211
  }
@@ -8170,7 +8217,7 @@ function tokenLatex(token, labelKinds, previousToken) {
8170
8217
  }
8171
8218
  if (isNumberedEquation(token)) {
8172
8219
  const body = mathBodyLatex(token);
8173
- const autoKey = implicitReferenceKey("eq", token.id);
8220
+ const autoKey = implicitReferenceKey("eq", token.id, unitId);
8174
8221
  const aliases = [autoKey];
8175
8222
  if (token.labelEnabled && token.label?.trim() && token.label.trim() !== autoKey) aliases.push(token.label.trim());
8176
8223
  return "\\begin{equation}\n" + body + "\n" + aliases.map((key) => "\\label{" + key + "}").join("\n") + "\n\\end{equation}";
@@ -8186,16 +8233,21 @@ function tokenLatex(token, labelKinds, previousToken) {
8186
8233
  }
8187
8234
  return renderMathNodeLatex(token.math);
8188
8235
  }
8189
- function inlineFieldLatexWithLabelKinds(field, labelKinds) {
8190
- return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
8236
+ function inlineFieldLatexWithLabelKinds(field, labelKinds, unitId) {
8237
+ return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1], unitId)).join("");
8191
8238
  }
8192
8239
  function inlineFieldLatex(field) {
8193
8240
  return inlineFieldLatexWithLabelKinds(field);
8194
8241
  }
8195
- function textBlockLatex(block, labelKinds) {
8196
- const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
8242
+ function textBlockLatex(block, labelKinds, unitId) {
8243
+ const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds, unitId);
8197
8244
  const body = block.command === "\\paragraph" ? `\\par
8198
8245
  ${content}` : `${block.command}{${content}}`;
8246
+ if (block.command !== "\\paragraph") {
8247
+ const keys = unitId ? [implicitHeadingReferenceKey(block.command.slice(1), block.id, unitId)] : [];
8248
+ if (block.label?.trim() && !keys.includes(block.label.trim())) keys.push(block.label.trim());
8249
+ return [body, ...keys.map((key) => `\\label{${key}}`)].join("\n");
8250
+ }
8199
8251
  if (block.command === "\\paragraph" && block.centered) {
8200
8252
  return `\\begin{center}
8201
8253
  ${body}
@@ -8206,10 +8258,10 @@ ${body}
8206
8258
  function indent(value) {
8207
8259
  return value.split("\n").map((line) => ` ${line}`).join("\n");
8208
8260
  }
8209
- function listBlockLatex(block, references, labelKinds, fixedFloats) {
8261
+ function listBlockLatex(block, references, labelKinds, fixedFloats, unitId) {
8210
8262
  const items = block.items.map((item) => {
8211
- const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
8212
- return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
8263
+ const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats, unitId))).join("");
8264
+ return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds, unitId)}${nested}`;
8213
8265
  }).join("\n");
8214
8266
  return `${block.command}
8215
8267
  ${items}
@@ -8223,9 +8275,9 @@ function defaultGridColumnSpec(block) {
8223
8275
  }
8224
8276
  return `|${compact.split("").join("|")}|`;
8225
8277
  }
8226
- function tabularInnerLatex(block, labelKinds) {
8278
+ function tabularInnerLatex(block, labelKinds, unitId) {
8227
8279
  const rows = block.rows.map(
8228
- (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
8280
+ (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds, unitId)).join(" & ")}`
8229
8281
  );
8230
8282
  const gridColumns = defaultGridColumnSpec(block);
8231
8283
  if (gridColumns) {
@@ -8239,9 +8291,9 @@ ${block.closing}`;
8239
8291
  ${rows.join(" \\\\\n")}
8240
8292
  ${block.closing}`;
8241
8293
  }
8242
- function floatCaptionLabelLines(block, counter, labelKinds) {
8294
+ function floatCaptionLabelLines(block, counter, labelKinds, unitId) {
8243
8295
  const lines = [];
8244
- const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds).trim();
8296
+ const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds, unitId).trim();
8245
8297
  const hasCaption = block.captionEnabled && caption.length > 0;
8246
8298
  if (hasCaption) {
8247
8299
  lines.push(` \\caption{${caption}}`);
@@ -8249,20 +8301,20 @@ function floatCaptionLabelLines(block, counter, labelKinds) {
8249
8301
  const hasManualLabel = block.labelEnabled && block.label.trim().length > 0;
8250
8302
  if (hasCaption || hasManualLabel) {
8251
8303
  if (!hasCaption) lines.push(" \\refstepcounter{" + counter + "}");
8252
- const autoKey = implicitReferenceKey(counter === "figure" ? "fig" : "tab", block.id);
8304
+ const autoKey = implicitReferenceKey(counter === "figure" ? "fig" : "tab", block.id, unitId);
8253
8305
  lines.push(" \\label{" + autoKey + "}");
8254
8306
  if (hasManualLabel && block.label.trim() !== autoKey) lines.push(" \\label{" + block.label.trim() + "}");
8255
8307
  }
8256
8308
  return lines;
8257
8309
  }
8258
- function tableBlockLatex(block, labelKinds, fixedFloats) {
8310
+ function tableBlockLatex(block, labelKinds, fixedFloats, unitId) {
8259
8311
  const placement = fixedFloats ? "H" : "htbp";
8260
8312
  const lines = [`\\begin{table}[${placement}]`];
8261
8313
  if (block.centered) {
8262
8314
  lines.push(" \\centering");
8263
8315
  }
8264
- lines.push(...floatCaptionLabelLines(block, "table", labelKinds));
8265
- lines.push(indent(tabularInnerLatex(block, labelKinds)));
8316
+ lines.push(...floatCaptionLabelLines(block, "table", labelKinds, unitId));
8317
+ lines.push(indent(tabularInnerLatex(block, labelKinds, unitId)));
8266
8318
  lines.push("\\end{table}");
8267
8319
  return lines.join("\n");
8268
8320
  }
@@ -8281,29 +8333,29 @@ function imageOptionsLatex(block, constrainToColumn) {
8281
8333
  }
8282
8334
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
8283
8335
  }
8284
- function imageBlockLatex(block, labelKinds, fixedFloats) {
8336
+ function imageBlockLatex(block, labelKinds, fixedFloats, unitId) {
8285
8337
  const placement = fixedFloats ? "H" : "htbp";
8286
8338
  const lines = [`\\begin{figure}[${placement}]`];
8287
8339
  if (block.centered) {
8288
8340
  lines.push(" \\centering");
8289
8341
  }
8290
8342
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
8291
- lines.push(...floatCaptionLabelLines(block, "figure", labelKinds));
8343
+ lines.push(...floatCaptionLabelLines(block, "figure", labelKinds, unitId));
8292
8344
  lines.push("\\end{figure}");
8293
8345
  return lines.join("\n");
8294
8346
  }
8295
- function blockLatex(block, references, labelKinds, fixedFloats) {
8347
+ function blockLatex(block, references, labelKinds, fixedFloats, unitId) {
8296
8348
  if (block.kind === "textBlock") {
8297
- return textBlockLatex(block, labelKinds);
8349
+ return textBlockLatex(block, labelKinds, unitId);
8298
8350
  }
8299
8351
  if (block.kind === "list") {
8300
- return listBlockLatex(block, references, labelKinds, fixedFloats);
8352
+ return listBlockLatex(block, references, labelKinds, fixedFloats, unitId);
8301
8353
  }
8302
8354
  if (block.kind === "table") {
8303
- return tableBlockLatex(block, labelKinds, fixedFloats);
8355
+ return tableBlockLatex(block, labelKinds, fixedFloats, unitId);
8304
8356
  }
8305
8357
  if (block.kind === "image") {
8306
- return imageBlockLatex(block, labelKinds, fixedFloats);
8358
+ return imageBlockLatex(block, labelKinds, fixedFloats, unitId);
8307
8359
  }
8308
8360
  if (block.kind === "bibliography") {
8309
8361
  const width = String(Math.max(references.length, 9));
@@ -8357,6 +8409,17 @@ function closingHamdalaLatex() {
8357
8409
  \\maghribi{${BUTEX_CLOSING_HAMDALA}}
8358
8410
  \\end{center}`;
8359
8411
  }
8412
+ function documentContentLatex(document2, context) {
8413
+ if (!context?.unitId?.trim()) throw new Error("book-content export requires a stable unitId");
8414
+ const labelKinds = documentLabelKindMap(document2);
8415
+ for (const target of context?.targets ?? []) {
8416
+ if (target.kind === "fig" || target.kind === "tab" || target.kind === "eq") {
8417
+ labelKinds.set(target.key, target.kind);
8418
+ if (target.manualKey) labelKinds.set(target.manualKey, target.kind);
8419
+ }
8420
+ }
8421
+ 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");
8422
+ }
8360
8423
  function documentLatex(document2, options = {}) {
8361
8424
  const meta = document2.meta ?? emptyDocumentMeta();
8362
8425
  const twocolumn = options.twocolumn === true;
@@ -8388,6 +8451,65 @@ ${body}
8388
8451
  `;
8389
8452
  }
8390
8453
 
8454
+ // src/document/bookContent.ts
8455
+ function bookContextReferences(context) {
8456
+ return (context?.references ?? []).map((entry, index) => ({
8457
+ ...referenceFromJson(entry),
8458
+ id: "book-ref-" + String(index)
8459
+ }));
8460
+ }
8461
+ function bookContentTargets(document2, context) {
8462
+ const local = collectDocumentReferenceTargets(document2).map((target) => {
8463
+ const key = implicitReferenceKey(target.kind, target.ownerId, context.unitId);
8464
+ const projection = context.targets?.find((entry) => entry.unitId === context.unitId && (entry.ownerId === target.ownerId || entry.key === key));
8465
+ return {
8466
+ ...target,
8467
+ key,
8468
+ ...projection?.displayLabel ? { displayLabel: projection.displayLabel } : {}
8469
+ };
8470
+ });
8471
+ const headings = document2.blocks.flatMap((block) => {
8472
+ if (block.kind !== "textBlock" || block.command === "\\paragraph") return [];
8473
+ const kind = block.command.slice(1);
8474
+ const caption = block.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("");
8475
+ return [{
8476
+ unitId: context.unitId,
8477
+ ownerId: block.id,
8478
+ kind,
8479
+ key: implicitHeadingReferenceKey(kind, block.id, context.unitId),
8480
+ caption,
8481
+ number: null,
8482
+ displayLabel: context.targets?.find((target) => target.unitId === context.unitId && target.ownerId === block.id)?.displayLabel ?? (caption || kind),
8483
+ ...block.label ? { manualKey: block.label } : {}
8484
+ }];
8485
+ });
8486
+ const external = (context.targets ?? []).filter((target) => target.unitId !== context.unitId).map((target) => ({
8487
+ ...target,
8488
+ caption: target.caption ?? "",
8489
+ number: null
8490
+ }));
8491
+ return [...local, ...headings, ...external];
8492
+ }
8493
+ function formatBookReferenceLabel(keys, document2, command, context, options = {}) {
8494
+ const targets = context.targets ?? [];
8495
+ return keys.map((key) => {
8496
+ const target = targets.find((entry) => entry.key === key || entry.manualKey === key);
8497
+ if (target?.displayLabel) return target.displayLabel;
8498
+ const local = collectDocumentReferenceTargets(document2).find((entry) => entry.manualKey === key || implicitReferenceKey(entry.kind, entry.ownerId, context.unitId) === key);
8499
+ if (local) return formatRefLabel([local.manualKey || local.key], document2, command, options);
8500
+ const heading = document2.blocks.find((block) => block.kind === "textBlock" && block.command !== "\\paragraph" && (block.label === key || implicitHeadingReferenceKey(
8501
+ block.command.slice(1),
8502
+ block.id,
8503
+ context.unitId
8504
+ ) === key));
8505
+ if (heading?.kind === "textBlock") {
8506
+ const caption = heading.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("").trim();
8507
+ return caption || heading.command.slice(1);
8508
+ }
8509
+ return command === "eqref" ? "(?)" : "?";
8510
+ }).join(options.documentDirection === "ltr" ? ", " : "\u060C");
8511
+ }
8512
+
8391
8513
  // src/document/validation.ts
8392
8514
  var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
8393
8515
  function hasXeLatexUnsupportedEmoji(value) {
@@ -8471,6 +8593,13 @@ function validateInlineField(context, field, path, blockId) {
8471
8593
  function validateBlock(context, block, path) {
8472
8594
  if (block.kind === "textBlock") {
8473
8595
  validateInlineField(context, block.field, `${path}.field`, block.id);
8596
+ if (block.command !== "\\paragraph") {
8597
+ const kind = block.command.slice(1);
8598
+ if (block.label) registerKey(context, block.label, `${path}.label`, kind, block.id);
8599
+ if (context.bookMode && context.bookContext) {
8600
+ context.labelKinds.set(implicitHeadingReferenceKey(kind, block.id, context.bookContext.unitId), kind);
8601
+ }
8602
+ }
8474
8603
  return;
8475
8604
  }
8476
8605
  if (block.kind === "list") {
@@ -8520,6 +8649,10 @@ function validateBlock(context, block, path) {
8520
8649
  }
8521
8650
  return;
8522
8651
  }
8652
+ if (block.kind === "bibliography" && context.bookMode) {
8653
+ addIssue(context, "book_local_bibliography", path, block.id);
8654
+ return;
8655
+ }
8523
8656
  if (block.kind === "raw") {
8524
8657
  validateTextValue(context, block.value, `${path}.value`, block.id);
8525
8658
  }
@@ -8542,7 +8675,7 @@ function validateDuplicateKeys(context) {
8542
8675
  }
8543
8676
  function validatePendingReferences(context) {
8544
8677
  for (const pending of context.pendingCitations) {
8545
- if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)))) {
8678
+ if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)) || context.citationKeys !== void 0 && !context.citationKeys.has(normalizeDocumentKey(key)))) {
8546
8679
  addIssue(
8547
8680
  context,
8548
8681
  "missing_citation",
@@ -8624,7 +8757,7 @@ function resolveDocument(document2) {
8624
8757
  return null;
8625
8758
  }
8626
8759
  }
8627
- function validateDocument(document2) {
8760
+ function validateDocument(document2, options = {}) {
8628
8761
  const resolved = resolveDocument(document2);
8629
8762
  if (!resolved) {
8630
8763
  return {
@@ -8636,6 +8769,9 @@ function validateDocument(document2) {
8636
8769
  issues: [],
8637
8770
  keyOwners: [],
8638
8771
  labelKinds: /* @__PURE__ */ new Map(),
8772
+ bookMode: options.mode === "book-content",
8773
+ bookContext: options.bookContext,
8774
+ ...options.mode === "book-content" && options.bookContext?.references ? { citationKeys: new Set(options.bookContext.references.map((entry) => normalizeDocumentKey(entry.key))) } : {},
8639
8775
  pendingCitations: [],
8640
8776
  pendingReferences: []
8641
8777
  };
@@ -8655,10 +8791,11 @@ function validateDocument(document2) {
8655
8791
  validateBlock(context, block, `$.blocks[${String(index)}]`);
8656
8792
  });
8657
8793
  for (const target of collectDocumentReferenceTargets(resolved)) {
8658
- if (target.number !== null && target.manualKey !== target.key) {
8794
+ const autoKey = context.bookMode && context.bookContext ? implicitReferenceKey(target.kind, target.ownerId, context.bookContext.unitId) : target.key;
8795
+ if (target.number !== null && target.manualKey !== autoKey) {
8659
8796
  registerKey(
8660
8797
  context,
8661
- target.key,
8798
+ autoKey,
8662
8799
  "$.automatic_labels",
8663
8800
  target.kind,
8664
8801
  target.kind === "eq" ? void 0 : target.ownerId,
@@ -8666,6 +8803,15 @@ function validateDocument(document2) {
8666
8803
  );
8667
8804
  }
8668
8805
  }
8806
+ if (context.bookMode) {
8807
+ for (const target of context.bookContext?.targets ?? []) {
8808
+ context.labelKinds.set(normalizeDocumentKey(target.key), target.kind);
8809
+ if (target.manualKey) context.labelKinds.set(normalizeDocumentKey(target.manualKey), target.kind);
8810
+ }
8811
+ }
8812
+ if (context.bookMode && !context.bookContext?.unitId?.trim()) {
8813
+ addIssue(context, "book_context_required", "$.book_context");
8814
+ }
8669
8815
  for (const diagnostic of resolved.diagnostics) {
8670
8816
  addIssue(context, "parser_diagnostic", diagnostic.path);
8671
8817
  }
@@ -8796,7 +8942,7 @@ function previewInlines(field, islands, output, equationSide, document2, preview
8796
8942
  kind: "cite",
8797
8943
  id: token.id,
8798
8944
  keys: [...token.keys],
8799
- label: formatCiteLabel(token.keys, document2.references, {
8945
+ label: formatCiteLabel(token.keys, previewOptions.mode === "book-content" ? bookContextReferences(previewOptions.bookContext) : document2.references, {
8800
8946
  documentDirection: previewOptions.documentDirection,
8801
8947
  digitForm: previewOptions.digitForm
8802
8948
  })
@@ -8808,7 +8954,10 @@ function previewInlines(field, islands, output, equationSide, document2, preview
8808
8954
  id: token.id,
8809
8955
  keys: [...token.keys],
8810
8956
  refCommand: token.refCommand,
8811
- label: formatRefLabel(token.keys, document2, token.refCommand, {
8957
+ label: previewOptions.mode === "book-content" && previewOptions.bookContext ? formatBookReferenceLabel(token.keys, document2, token.refCommand, previewOptions.bookContext, {
8958
+ documentDirection: previewOptions.documentDirection,
8959
+ digitForm: previewOptions.digitForm
8960
+ }) : formatRefLabel(token.keys, document2, token.refCommand, {
8812
8961
  documentDirection: previewOptions.documentDirection,
8813
8962
  digitForm: previewOptions.digitForm
8814
8963
  })
@@ -8964,14 +9113,16 @@ function documentPreview(document2, output = "svg", equationSide = "arabic", pre
8964
9113
  const options = {
8965
9114
  documentDirection: previewOptions.documentDirection ?? "rtl",
8966
9115
  digitForm: previewOptions.digitForm ?? (previewOptions.documentDirection === "ltr" ? "western" : "arabicIndic"),
8967
- uiLocale: previewOptions.uiLocale ?? "ar"
9116
+ uiLocale: previewOptions.uiLocale ?? "ar",
9117
+ mode: previewOptions.mode ?? "article",
9118
+ bookContext: previewOptions.bookContext
8968
9119
  };
8969
- const body = document2.blocks.map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
9120
+ const body = document2.blocks.filter((block) => options.mode !== "book-content" || block.kind !== "bibliography").map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
8970
9121
  return {
8971
9122
  blocks: [
8972
- ...articleChromePreview(document2, options.uiLocale ?? "ar", options),
9123
+ ...options.mode === "book-content" ? [] : articleChromePreview(document2, options.uiLocale ?? "ar", options),
8973
9124
  ...body,
8974
- { kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }
9125
+ ...options.mode === "book-content" ? [] : [{ kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }]
8975
9126
  ],
8976
9127
  mathIslands
8977
9128
  };
@@ -8985,6 +9136,11 @@ function documentLatex2(document2, options = {}) {
8985
9136
  }
8986
9137
  return documentLatex(document2, options);
8987
9138
  }
9139
+ function documentContentLatex2(document2, context) {
9140
+ const validation = validateDocument(document2, { mode: "book-content", bookContext: context });
9141
+ if (!validation.valid) throw new DocumentValidationError(validation.issues);
9142
+ return documentContentLatex(document2, context);
9143
+ }
8988
9144
  export {
8989
9145
  BUTEX_BASMALA,
8990
9146
  BUTEX_BASMALA_LINE1,
@@ -9013,6 +9169,8 @@ export {
9013
9169
  arabicXeLatexPreamblePkg,
9014
9170
  bibliographyEntryBody,
9015
9171
  bibliographyEntryLatex,
9172
+ bookContentTargets,
9173
+ bookContextReferences,
9016
9174
  butexBasmalaDisplayLatex,
9017
9175
  butexBasmalaDisplayTex,
9018
9176
  butexBasmalaTitlingPreamble,
@@ -9038,6 +9196,7 @@ export {
9038
9196
  detectInlineSpans,
9039
9197
  detectMathSpans,
9040
9198
  diffDocument,
9199
+ documentContentLatex2 as documentContentLatex,
9041
9200
  documentHasAbstract,
9042
9201
  documentHasMaketitle,
9043
9202
  documentHasTitleStack,
@@ -9054,6 +9213,7 @@ export {
9054
9213
  fillDocumentMetaGaps,
9055
9214
  findFirstInlineField,
9056
9215
  formatBibliographyNumber,
9216
+ formatBookReferenceLabel,
9057
9217
  formatCiteLabel,
9058
9218
  formatFloatCaptionPreview,
9059
9219
  formatFloatCaptionTitle,
@@ -9065,6 +9225,7 @@ export {
9065
9225
  getDocumentReferences,
9066
9226
  hasXeLatexUnsupportedEmoji,
9067
9227
  hijriMonthLabel,
9228
+ implicitHeadingReferenceKey,
9068
9229
  implicitReferenceKey,
9069
9230
  inlineFieldLatex,
9070
9231
  insertCiteTokenAtCaret,
@@ -9119,6 +9280,7 @@ export {
9119
9280
  toggleBlockInSelection,
9120
9281
  toggleTextTokenStyle,
9121
9282
  updateCiteTokenKeys,
9283
+ updateDocumentHeadingLabel,
9122
9284
  updateDocumentImageAsset,
9123
9285
  updateDocumentImageMeta,
9124
9286
  updateDocumentImageValue,