@drghaliasri/butex 8.0.3 → 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.
@@ -875,82 +875,85 @@ function refTokenLatex(keys, refCommand) {
875
875
  const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
876
876
  return `${command}{${keys.join(",")}}`;
877
877
  }
878
- function collectDocumentLabels(document2) {
879
- const entries = [];
878
+ function implicitObjectKey(kind, ownerId, unitId) {
879
+ const hex = (value) => Array.from(new TextEncoder().encode(value), (byte) => byte.toString(16).padStart(2, "0")).join("");
880
+ const scope = unitId === void 0 ? "a" : "u" + hex(unitId);
881
+ return kind + ":butex-auto-" + scope + "-" + hex(ownerId);
882
+ }
883
+ function implicitReferenceKey(kind, ownerId, unitId) {
884
+ return implicitObjectKey(kind, ownerId, unitId);
885
+ }
886
+ function implicitHeadingReferenceKey(kind, ownerId, unitId) {
887
+ return implicitObjectKey(kind, ownerId, unitId);
888
+ }
889
+ function supportsSingleEquationNumbering(token) {
890
+ return token.display && (token.opening === "\\[" || token.opening === String.fromCharCode(36, 36) || token.opening === "\\begin{equation}" || token.opening === "\\begin{equation*}");
891
+ }
892
+ function isNumberedEquation(token) {
893
+ if (!supportsSingleEquationNumbering(token)) return false;
894
+ if (typeof token.numbered === "boolean") return token.numbered;
895
+ return token.opening === "\\begin{equation}" || Boolean(token.labelEnabled) && Boolean(token.label?.trim());
896
+ }
897
+ function isNumberedFloat(block) {
898
+ return block.captionEnabled && block.caption.trim().length > 0 || block.labelEnabled && block.label.trim().length > 0;
899
+ }
900
+ function collectDocumentReferenceTargets(document2) {
901
+ const targets = [];
880
902
  const counters = { fig: 0, tab: 0, eq: 0 };
881
- function pushEqFromField(tokens) {
882
- for (const token of tokens) {
883
- if (token.kind !== "math") {
884
- continue;
885
- }
886
- if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
887
- counters.eq += 1;
888
- entries.push({
889
- key: token.label.trim(),
890
- kind: "eq",
891
- caption: "",
892
- ownerId: token.id,
893
- number: counters.eq
894
- });
903
+ function append(kind, ownerId, caption, numbered, manualKey = "") {
904
+ if (numbered) counters[kind] += 1;
905
+ targets.push({
906
+ key: implicitReferenceKey(kind, ownerId),
907
+ kind,
908
+ ownerId,
909
+ caption,
910
+ number: numbered ? counters[kind] : null,
911
+ ...manualKey.trim() ? { manualKey: manualKey.trim() } : {}
912
+ });
913
+ }
914
+ function visitField(field) {
915
+ for (const token of field.tokens) {
916
+ if (token.kind === "math" && supportsSingleEquationNumbering(token)) {
917
+ append("eq", token.id, "", isNumberedEquation(token), token.labelEnabled ? token.label ?? "" : "");
895
918
  }
896
919
  }
897
920
  }
898
921
  function walk(blocks) {
899
922
  for (const block of blocks) {
900
- if (block.kind === "image") {
901
- if (block.labelEnabled && block.label.trim().length > 0) {
902
- counters.fig += 1;
903
- entries.push({
904
- key: block.label.trim(),
905
- kind: "fig",
906
- caption: block.captionEnabled ? block.caption : "",
907
- ownerId: block.id,
908
- number: counters.fig
909
- });
910
- }
911
- if (block.captionEnabled) {
912
- pushEqFromField(block.captionField.tokens);
913
- }
914
- }
915
- if (block.kind === "table") {
916
- if (block.labelEnabled && block.label.trim().length > 0) {
917
- counters.tab += 1;
918
- entries.push({
919
- key: block.label.trim(),
920
- kind: "tab",
921
- caption: block.captionEnabled ? block.caption : "",
922
- ownerId: block.id,
923
- number: counters.tab
924
- });
925
- }
926
- if (block.captionEnabled) {
927
- pushEqFromField(block.captionField.tokens);
928
- }
929
- for (const row of block.rows) {
930
- for (const cell of row) {
931
- pushEqFromField(cell.tokens);
932
- }
923
+ if (block.kind === "image" || block.kind === "table") {
924
+ append(
925
+ block.kind === "image" ? "fig" : "tab",
926
+ block.id,
927
+ block.captionEnabled ? block.caption : "",
928
+ isNumberedFloat(block),
929
+ block.labelEnabled ? block.label : ""
930
+ );
931
+ if (block.captionEnabled) visitField(block.captionField);
932
+ if (block.kind === "table") {
933
+ for (const row of block.rows) for (const cell of row) visitField(cell);
933
934
  }
934
- }
935
- if (block.kind === "textBlock") {
936
- pushEqFromField(block.field.tokens);
937
- }
938
- if (block.kind === "list") {
935
+ } else if (block.kind === "textBlock") {
936
+ visitField(block.field);
937
+ } else if (block.kind === "list") {
939
938
  for (const item of block.items) {
940
- pushEqFromField(item.field.tokens);
939
+ visitField(item.field);
941
940
  walk(item.blocks);
942
941
  }
943
942
  }
944
943
  }
945
944
  }
946
945
  walk(document2.blocks);
947
- return entries;
946
+ return targets;
947
+ }
948
+ function collectDocumentLabels(document2) {
949
+ return collectDocumentReferenceTargets(document2).filter((target) => target.number !== null && Boolean(target.manualKey)).map(({ manualKey, kind, caption, ownerId, number }) => ({ key: manualKey, kind, caption, ownerId, number }));
948
950
  }
949
951
  function labelNumberMap(document2) {
950
952
  const map = /* @__PURE__ */ new Map();
951
- for (const entry of collectDocumentLabels(document2)) {
952
- if (!map.has(entry.key)) {
953
- map.set(entry.key, { kind: entry.kind, number: entry.number });
953
+ for (const target of collectDocumentReferenceTargets(document2)) {
954
+ if (target.number === null) continue;
955
+ for (const key of [target.key, target.manualKey]) {
956
+ if (key && !map.has(key)) map.set(key, { kind: target.kind, number: target.number });
954
957
  }
955
958
  }
956
959
  return map;
@@ -1004,8 +1007,12 @@ function stripDisplayMathDelimiters(source) {
1004
1007
  if (trimmed.startsWith("$$") && trimmed.endsWith("$$")) {
1005
1008
  return trimmed.slice(2, -2).trim();
1006
1009
  }
1007
- if (trimmed.startsWith("\\begin{equation}") && trimmed.endsWith("\\end{equation}")) {
1008
- return trimmed.slice("\\begin{equation}".length, -"\\end{equation}".length).trim();
1010
+ for (const environment of ["equation", "equation*"]) {
1011
+ const opening = `\\begin{${environment}}`;
1012
+ const closing = `\\end{${environment}}`;
1013
+ if (trimmed.startsWith(opening) && trimmed.endsWith(closing)) {
1014
+ return trimmed.slice(opening.length, -closing.length).trim();
1015
+ }
1009
1016
  }
1010
1017
  return trimmed;
1011
1018
  }
@@ -1398,7 +1405,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1398
1405
  editable: true,
1399
1406
  sourceOwner: structuredMathJson.source_owner === "editor" ? "editor" : "imported-structured",
1400
1407
  ...typeof structuredMathJson.label_enabled === "boolean" ? { labelEnabled: structuredMathJson.label_enabled } : {},
1401
- ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {}
1408
+ ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {},
1409
+ ...typeof structuredMathJson.numbered === "boolean" ? { numbered: structuredMathJson.numbered } : {}
1402
1410
  });
1403
1411
  } else {
1404
1412
  tokens.push({
@@ -1413,7 +1421,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1413
1421
  reason: "\u0647\u0630\u0647 \u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629 \u0645\u062D\u0641\u0648\u0638\u0629 \u0643\u0646\u0635 \u062E\u0627\u0645 \u0641\u0642\u0637 \u062D\u0627\u0644\u064A\u0627",
1414
1422
  sourceOwner: "raw",
1415
1423
  ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label_enabled === "boolean" ? { labelEnabled: mathJson.label_enabled } : {},
1416
- ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {}
1424
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {},
1425
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.numbered === "boolean" ? { numbered: mathJson.numbered } : {}
1417
1426
  });
1418
1427
  }
1419
1428
  index = span.end;
@@ -1510,6 +1519,7 @@ function parseTextBlock(json, options, path, diagnostics, identities) {
1510
1519
  identities
1511
1520
  ),
1512
1521
  ...metadataFromJson(json.metadata),
1522
+ ...typeof json.label === "string" && json.command !== "\\paragraph" ? { label: json.label } : {},
1513
1523
  ...json.centered === true ? { centered: true } : {}
1514
1524
  };
1515
1525
  }
@@ -9489,6 +9499,9 @@ function replaceMathTokenFromSession(document2, tokenId, session, opening, closi
9489
9499
  if (current.label !== void 0) {
9490
9500
  replaced.label = current.label;
9491
9501
  }
9502
+ if (current.numbered !== void 0) {
9503
+ replaced.numbered = current.numbered;
9504
+ }
9492
9505
  field.tokens[index] = replaced;
9493
9506
  return true;
9494
9507
  });
@@ -9525,6 +9538,24 @@ function updateDocumentTextBlockCentered(document2, blockId, centered) {
9525
9538
  visit(next.blocks);
9526
9539
  return next;
9527
9540
  }
9541
+ function updateDocumentHeadingLabel(document2, blockId, label) {
9542
+ const next = cloneDocument(document2);
9543
+ function visit(blocks) {
9544
+ for (const block of blocks) {
9545
+ if (block.id === blockId && block.kind === "textBlock" && block.command !== "\\paragraph") {
9546
+ if (label.trim()) block.label = label.trim();
9547
+ else delete block.label;
9548
+ return true;
9549
+ }
9550
+ if (block.kind === "list") {
9551
+ for (const item of block.items) if (visit(item.blocks)) return true;
9552
+ }
9553
+ }
9554
+ return false;
9555
+ }
9556
+ visit(next.blocks);
9557
+ return next;
9558
+ }
9528
9559
  function newListBlock(ordered) {
9529
9560
  const command = ordered ? "\\begin{enumerate}" : "\\begin{itemize}";
9530
9561
  const closing = ordered ? "\\end{enumerate}" : "\\end{itemize}";
@@ -9708,6 +9739,9 @@ function updateMathTokenLabel(document2, tokenId, patch) {
9708
9739
  if (typeof patch.label === "string") {
9709
9740
  token.label = patch.label;
9710
9741
  }
9742
+ if (typeof patch.numbered === "boolean") {
9743
+ token.numbered = patch.numbered;
9744
+ }
9711
9745
  return true;
9712
9746
  });
9713
9747
  return next;
@@ -10025,12 +10059,13 @@ function serializeField(field) {
10025
10059
  }
10026
10060
  value += token.source;
10027
10061
  if (!token.math || token.sourceOwner === "raw") {
10028
- if (token.labelEnabled !== void 0 || token.label !== void 0) {
10062
+ if (token.labelEnabled !== void 0 || token.label !== void 0 || token.numbered !== void 0) {
10029
10063
  hasPersistedMath = true;
10030
10064
  mathObjects.push({
10031
10065
  node_type: "RawMathObject",
10032
10066
  ...token.labelEnabled !== void 0 ? { label_enabled: token.labelEnabled } : {},
10033
- ...token.label !== void 0 ? { label: token.label } : {}
10067
+ ...token.label !== void 0 ? { label: token.label } : {},
10068
+ ...token.numbered !== void 0 ? { numbered: token.numbered } : {}
10034
10069
  });
10035
10070
  } else {
10036
10071
  mathObjects.push(null);
@@ -10043,7 +10078,8 @@ function serializeField(field) {
10043
10078
  ...token.sourceSide ? { source_side: token.sourceSide } : {},
10044
10079
  source_owner: token.sourceOwner,
10045
10080
  ...token.labelEnabled !== void 0 ? { label_enabled: token.labelEnabled } : {},
10046
- ...token.label !== void 0 ? { label: token.label } : {}
10081
+ ...token.label !== void 0 ? { label: token.label } : {},
10082
+ ...token.numbered !== void 0 ? { numbered: token.numbered } : {}
10047
10083
  });
10048
10084
  }
10049
10085
  return { value, formats, mathObjects, hasPersistedMath, inlineIds: inlineIdsFromField(field) };
@@ -10083,6 +10119,7 @@ function blockJson(block) {
10083
10119
  id: block.id,
10084
10120
  command: block.command,
10085
10121
  ...fieldJson(block.field),
10122
+ ...block.command !== "\\paragraph" && block.label ? { label: block.label } : {},
10086
10123
  ...block.metadata ? { metadata: { ...block.metadata } } : {},
10087
10124
  ...block.command === "\\paragraph" ? { centered: block.centered === true } : {}
10088
10125
  };
@@ -10474,7 +10511,7 @@ function refTokenLatexForDocument(token, labelKinds, previousToken) {
10474
10511
  }
10475
10512
  return `${semanticRefPrefix(kind)}~${reference}`;
10476
10513
  }
10477
- function tokenLatex(token, labelKinds, previousToken) {
10514
+ function tokenLatex(token, labelKinds, previousToken, unitId) {
10478
10515
  if (token.kind === "text") {
10479
10516
  return styledTextLatex(token.text, token.style);
10480
10517
  }
@@ -10484,12 +10521,15 @@ function tokenLatex(token, labelKinds, previousToken) {
10484
10521
  if (token.kind === "ref") {
10485
10522
  return refTokenLatexForDocument(token, labelKinds, previousToken);
10486
10523
  }
10487
- if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
10524
+ if (isNumberedEquation(token)) {
10488
10525
  const body = mathBodyLatex(token);
10489
- return `\\begin{equation}
10490
- ${body}
10491
- \\label{${token.label.trim()}}
10492
- \\end{equation}`;
10526
+ const autoKey = implicitReferenceKey("eq", token.id, unitId);
10527
+ const aliases = [autoKey];
10528
+ if (token.labelEnabled && token.label?.trim() && token.label.trim() !== autoKey) aliases.push(token.label.trim());
10529
+ return "\\begin{equation}\n" + body + "\n" + aliases.map((key) => "\\label{" + key + "}").join("\n") + "\n\\end{equation}";
10530
+ }
10531
+ if (token.display && token.numbered === false && token.opening === "\\begin{equation}") {
10532
+ return "\\[" + mathBodyLatex(token) + "\\]";
10493
10533
  }
10494
10534
  if (token.sourceOwner === "editor") {
10495
10535
  return editorMathLatexForCompile(token);
@@ -10499,13 +10539,18 @@ ${body}
10499
10539
  }
10500
10540
  return renderMathNodeLatex(token.math);
10501
10541
  }
10502
- function inlineFieldLatexWithLabelKinds(field, labelKinds) {
10503
- return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
10542
+ function inlineFieldLatexWithLabelKinds(field, labelKinds, unitId) {
10543
+ return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1], unitId)).join("");
10504
10544
  }
10505
- function textBlockLatex(block, labelKinds) {
10506
- const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
10545
+ function textBlockLatex(block, labelKinds, unitId) {
10546
+ const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds, unitId);
10507
10547
  const body = block.command === "\\paragraph" ? `\\par
10508
10548
  ${content}` : `${block.command}{${content}}`;
10549
+ if (block.command !== "\\paragraph") {
10550
+ const keys = unitId ? [implicitHeadingReferenceKey(block.command.slice(1), block.id, unitId)] : [];
10551
+ if (block.label?.trim() && !keys.includes(block.label.trim())) keys.push(block.label.trim());
10552
+ return [body, ...keys.map((key) => `\\label{${key}}`)].join("\n");
10553
+ }
10509
10554
  if (block.command === "\\paragraph" && block.centered) {
10510
10555
  return `\\begin{center}
10511
10556
  ${body}
@@ -10516,10 +10561,10 @@ ${body}
10516
10561
  function indent(value) {
10517
10562
  return value.split("\n").map((line) => ` ${line}`).join("\n");
10518
10563
  }
10519
- function listBlockLatex(block, references, labelKinds, fixedFloats) {
10564
+ function listBlockLatex(block, references, labelKinds, fixedFloats, unitId) {
10520
10565
  const items = block.items.map((item) => {
10521
- const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
10522
- return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
10566
+ const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats, unitId))).join("");
10567
+ return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds, unitId)}${nested}`;
10523
10568
  }).join("\n");
10524
10569
  return `${block.command}
10525
10570
  ${items}
@@ -10533,9 +10578,9 @@ function defaultGridColumnSpec(block) {
10533
10578
  }
10534
10579
  return `|${compact.split("").join("|")}|`;
10535
10580
  }
10536
- function tabularInnerLatex(block, labelKinds) {
10581
+ function tabularInnerLatex(block, labelKinds, unitId) {
10537
10582
  const rows = block.rows.map(
10538
- (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
10583
+ (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds, unitId)).join(" & ")}`
10539
10584
  );
10540
10585
  const gridColumns = defaultGridColumnSpec(block);
10541
10586
  if (gridColumns) {
@@ -10549,29 +10594,30 @@ ${block.closing}`;
10549
10594
  ${rows.join(" \\\\\n")}
10550
10595
  ${block.closing}`;
10551
10596
  }
10552
- function floatCaptionLabelLines(block, counter, labelKinds) {
10597
+ function floatCaptionLabelLines(block, counter, labelKinds, unitId) {
10553
10598
  const lines = [];
10554
- const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds).trim();
10599
+ const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds, unitId).trim();
10555
10600
  const hasCaption = block.captionEnabled && caption.length > 0;
10556
10601
  if (hasCaption) {
10557
10602
  lines.push(` \\caption{${caption}}`);
10558
10603
  }
10559
- if (block.labelEnabled && block.label.trim().length > 0) {
10560
- if (!hasCaption) {
10561
- lines.push(` \\refstepcounter{${counter}}`);
10562
- }
10563
- lines.push(` \\label{${block.label.trim()}}`);
10604
+ const hasManualLabel = block.labelEnabled && block.label.trim().length > 0;
10605
+ if (hasCaption || hasManualLabel) {
10606
+ if (!hasCaption) lines.push(" \\refstepcounter{" + counter + "}");
10607
+ const autoKey = implicitReferenceKey(counter === "figure" ? "fig" : "tab", block.id, unitId);
10608
+ lines.push(" \\label{" + autoKey + "}");
10609
+ if (hasManualLabel && block.label.trim() !== autoKey) lines.push(" \\label{" + block.label.trim() + "}");
10564
10610
  }
10565
10611
  return lines;
10566
10612
  }
10567
- function tableBlockLatex(block, labelKinds, fixedFloats) {
10613
+ function tableBlockLatex(block, labelKinds, fixedFloats, unitId) {
10568
10614
  const placement = fixedFloats ? "H" : "htbp";
10569
10615
  const lines = [`\\begin{table}[${placement}]`];
10570
10616
  if (block.centered) {
10571
10617
  lines.push(" \\centering");
10572
10618
  }
10573
- lines.push(...floatCaptionLabelLines(block, "table", labelKinds));
10574
- lines.push(indent(tabularInnerLatex(block, labelKinds)));
10619
+ lines.push(...floatCaptionLabelLines(block, "table", labelKinds, unitId));
10620
+ lines.push(indent(tabularInnerLatex(block, labelKinds, unitId)));
10575
10621
  lines.push("\\end{table}");
10576
10622
  return lines.join("\n");
10577
10623
  }
@@ -10590,29 +10636,29 @@ function imageOptionsLatex(block, constrainToColumn) {
10590
10636
  }
10591
10637
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
10592
10638
  }
10593
- function imageBlockLatex(block, labelKinds, fixedFloats) {
10639
+ function imageBlockLatex(block, labelKinds, fixedFloats, unitId) {
10594
10640
  const placement = fixedFloats ? "H" : "htbp";
10595
10641
  const lines = [`\\begin{figure}[${placement}]`];
10596
10642
  if (block.centered) {
10597
10643
  lines.push(" \\centering");
10598
10644
  }
10599
10645
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
10600
- lines.push(...floatCaptionLabelLines(block, "figure", labelKinds));
10646
+ lines.push(...floatCaptionLabelLines(block, "figure", labelKinds, unitId));
10601
10647
  lines.push("\\end{figure}");
10602
10648
  return lines.join("\n");
10603
10649
  }
10604
- function blockLatex(block, references, labelKinds, fixedFloats) {
10650
+ function blockLatex(block, references, labelKinds, fixedFloats, unitId) {
10605
10651
  if (block.kind === "textBlock") {
10606
- return textBlockLatex(block, labelKinds);
10652
+ return textBlockLatex(block, labelKinds, unitId);
10607
10653
  }
10608
10654
  if (block.kind === "list") {
10609
- return listBlockLatex(block, references, labelKinds, fixedFloats);
10655
+ return listBlockLatex(block, references, labelKinds, fixedFloats, unitId);
10610
10656
  }
10611
10657
  if (block.kind === "table") {
10612
- return tableBlockLatex(block, labelKinds, fixedFloats);
10658
+ return tableBlockLatex(block, labelKinds, fixedFloats, unitId);
10613
10659
  }
10614
10660
  if (block.kind === "image") {
10615
- return imageBlockLatex(block, labelKinds, fixedFloats);
10661
+ return imageBlockLatex(block, labelKinds, fixedFloats, unitId);
10616
10662
  }
10617
10663
  if (block.kind === "bibliography") {
10618
10664
  const width = String(Math.max(references.length, 9));
@@ -10656,10 +10702,8 @@ ${meta.abstract.trim()}
10656
10702
  }
10657
10703
  function documentLabelKindMap(document2) {
10658
10704
  const map = /* @__PURE__ */ new Map();
10659
- for (const entry of collectDocumentLabels(document2)) {
10660
- if (!map.has(entry.key)) {
10661
- map.set(entry.key, entry.kind);
10662
- }
10705
+ for (const [key, entry] of labelNumberMap(document2)) {
10706
+ map.set(key, entry.kind);
10663
10707
  }
10664
10708
  return map;
10665
10709
  }
@@ -10668,6 +10712,17 @@ function closingHamdalaLatex() {
10668
10712
  \\maghribi{${BUTEX_CLOSING_HAMDALA}}
10669
10713
  \\end{center}`;
10670
10714
  }
10715
+ function documentContentLatex(document2, context) {
10716
+ if (!context?.unitId?.trim()) throw new Error("book-content export requires a stable unitId");
10717
+ const labelKinds = documentLabelKindMap(document2);
10718
+ for (const target of context?.targets ?? []) {
10719
+ if (target.kind === "fig" || target.kind === "tab" || target.kind === "eq") {
10720
+ labelKinds.set(target.key, target.kind);
10721
+ if (target.manualKey) labelKinds.set(target.manualKey, target.kind);
10722
+ }
10723
+ }
10724
+ 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");
10725
+ }
10671
10726
  function documentLatex(document2, options = {}) {
10672
10727
  const meta = document2.meta ?? emptyDocumentMeta();
10673
10728
  const twocolumn = options.twocolumn === true;
@@ -10699,6 +10754,65 @@ ${body}
10699
10754
  `;
10700
10755
  }
10701
10756
 
10757
+ // src/document/bookContent.ts
10758
+ function bookContextReferences(context) {
10759
+ return (context?.references ?? []).map((entry, index) => ({
10760
+ ...referenceFromJson(entry),
10761
+ id: "book-ref-" + String(index)
10762
+ }));
10763
+ }
10764
+ function bookContentTargets(document2, context) {
10765
+ const local = collectDocumentReferenceTargets(document2).map((target) => {
10766
+ const key = implicitReferenceKey(target.kind, target.ownerId, context.unitId);
10767
+ const projection = context.targets?.find((entry) => entry.unitId === context.unitId && (entry.ownerId === target.ownerId || entry.key === key));
10768
+ return {
10769
+ ...target,
10770
+ key,
10771
+ ...projection?.displayLabel ? { displayLabel: projection.displayLabel } : {}
10772
+ };
10773
+ });
10774
+ const headings = document2.blocks.flatMap((block) => {
10775
+ if (block.kind !== "textBlock" || block.command === "\\paragraph") return [];
10776
+ const kind = block.command.slice(1);
10777
+ const caption = block.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("");
10778
+ return [{
10779
+ unitId: context.unitId,
10780
+ ownerId: block.id,
10781
+ kind,
10782
+ key: implicitHeadingReferenceKey(kind, block.id, context.unitId),
10783
+ caption,
10784
+ number: null,
10785
+ displayLabel: context.targets?.find((target) => target.unitId === context.unitId && target.ownerId === block.id)?.displayLabel ?? (caption || kind),
10786
+ ...block.label ? { manualKey: block.label } : {}
10787
+ }];
10788
+ });
10789
+ const external = (context.targets ?? []).filter((target) => target.unitId !== context.unitId).map((target) => ({
10790
+ ...target,
10791
+ caption: target.caption ?? "",
10792
+ number: null
10793
+ }));
10794
+ return [...local, ...headings, ...external];
10795
+ }
10796
+ function formatBookReferenceLabel(keys, document2, command, context, options = {}) {
10797
+ const targets = context.targets ?? [];
10798
+ return keys.map((key) => {
10799
+ const target = targets.find((entry) => entry.key === key || entry.manualKey === key);
10800
+ if (target?.displayLabel) return target.displayLabel;
10801
+ const local = collectDocumentReferenceTargets(document2).find((entry) => entry.manualKey === key || implicitReferenceKey(entry.kind, entry.ownerId, context.unitId) === key);
10802
+ if (local) return formatRefLabel([local.manualKey || local.key], document2, command, options);
10803
+ const heading = document2.blocks.find((block) => block.kind === "textBlock" && block.command !== "\\paragraph" && (block.label === key || implicitHeadingReferenceKey(
10804
+ block.command.slice(1),
10805
+ block.id,
10806
+ context.unitId
10807
+ ) === key));
10808
+ if (heading?.kind === "textBlock") {
10809
+ const caption = heading.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("").trim();
10810
+ return caption || heading.command.slice(1);
10811
+ }
10812
+ return command === "eqref" ? "(?)" : "?";
10813
+ }).join(options.documentDirection === "ltr" ? ", " : "\u060C");
10814
+ }
10815
+
10702
10816
  // src/document/validation.ts
10703
10817
  var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
10704
10818
  function hasXeLatexUnsupportedEmoji(value) {
@@ -10758,10 +10872,17 @@ function validateToken(context, token, path, blockId) {
10758
10872
  return;
10759
10873
  }
10760
10874
  validateTextValue(context, token.source, `${path}.source`, blockId, token.id);
10875
+ if (token.numbered === true && !supportsSingleEquationNumbering(token)) {
10876
+ addIssue(context, "invalid_equation_label", `${path}.numbered`, blockId, token.id);
10877
+ }
10761
10878
  if (!token.labelEnabled) {
10762
10879
  return;
10763
10880
  }
10764
- if (!token.display) {
10881
+ if (!supportsSingleEquationNumbering(token)) {
10882
+ addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
10883
+ return;
10884
+ }
10885
+ if (!isNumberedEquation(token)) {
10765
10886
  addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
10766
10887
  return;
10767
10888
  }
@@ -10775,6 +10896,13 @@ function validateInlineField(context, field, path, blockId) {
10775
10896
  function validateBlock(context, block, path) {
10776
10897
  if (block.kind === "textBlock") {
10777
10898
  validateInlineField(context, block.field, `${path}.field`, block.id);
10899
+ if (block.command !== "\\paragraph") {
10900
+ const kind = block.command.slice(1);
10901
+ if (block.label) registerKey(context, block.label, `${path}.label`, kind, block.id);
10902
+ if (context.bookMode && context.bookContext) {
10903
+ context.labelKinds.set(implicitHeadingReferenceKey(kind, block.id, context.bookContext.unitId), kind);
10904
+ }
10905
+ }
10778
10906
  return;
10779
10907
  }
10780
10908
  if (block.kind === "list") {
@@ -10824,6 +10952,10 @@ function validateBlock(context, block, path) {
10824
10952
  }
10825
10953
  return;
10826
10954
  }
10955
+ if (block.kind === "bibliography" && context.bookMode) {
10956
+ addIssue(context, "book_local_bibliography", path, block.id);
10957
+ return;
10958
+ }
10827
10959
  if (block.kind === "raw") {
10828
10960
  validateTextValue(context, block.value, `${path}.value`, block.id);
10829
10961
  }
@@ -10846,7 +10978,7 @@ function validateDuplicateKeys(context) {
10846
10978
  }
10847
10979
  function validatePendingReferences(context) {
10848
10980
  for (const pending of context.pendingCitations) {
10849
- if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)))) {
10981
+ if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)) || context.citationKeys !== void 0 && !context.citationKeys.has(normalizeDocumentKey(key)))) {
10850
10982
  addIssue(
10851
10983
  context,
10852
10984
  "missing_citation",
@@ -10859,7 +10991,7 @@ function validatePendingReferences(context) {
10859
10991
  for (const pending of context.pendingReferences) {
10860
10992
  for (const key of pending.token.keys) {
10861
10993
  const normalized = normalizeDocumentKey(key);
10862
- if (!isValidDocumentKey(normalized)) {
10994
+ if (!isValidDocumentKey(normalized) || !context.labelKinds.has(normalized)) {
10863
10995
  addIssue(
10864
10996
  context,
10865
10997
  "missing_reference",
@@ -10928,7 +11060,7 @@ function resolveDocument(document2) {
10928
11060
  return null;
10929
11061
  }
10930
11062
  }
10931
- function validateDocument(document2) {
11063
+ function validateDocument(document2, options = {}) {
10932
11064
  const resolved = resolveDocument(document2);
10933
11065
  if (!resolved) {
10934
11066
  return {
@@ -10940,6 +11072,9 @@ function validateDocument(document2) {
10940
11072
  issues: [],
10941
11073
  keyOwners: [],
10942
11074
  labelKinds: /* @__PURE__ */ new Map(),
11075
+ bookMode: options.mode === "book-content",
11076
+ bookContext: options.bookContext,
11077
+ ...options.mode === "book-content" && options.bookContext?.references ? { citationKeys: new Set(options.bookContext.references.map((entry) => normalizeDocumentKey(entry.key))) } : {},
10943
11078
  pendingCitations: [],
10944
11079
  pendingReferences: []
10945
11080
  };
@@ -10958,6 +11093,28 @@ function validateDocument(document2) {
10958
11093
  resolved.blocks.forEach((block, index) => {
10959
11094
  validateBlock(context, block, `$.blocks[${String(index)}]`);
10960
11095
  });
11096
+ for (const target of collectDocumentReferenceTargets(resolved)) {
11097
+ const autoKey = context.bookMode && context.bookContext ? implicitReferenceKey(target.kind, target.ownerId, context.bookContext.unitId) : target.key;
11098
+ if (target.number !== null && target.manualKey !== autoKey) {
11099
+ registerKey(
11100
+ context,
11101
+ autoKey,
11102
+ "$.automatic_labels",
11103
+ target.kind,
11104
+ target.kind === "eq" ? void 0 : target.ownerId,
11105
+ target.kind === "eq" ? target.ownerId : void 0
11106
+ );
11107
+ }
11108
+ }
11109
+ if (context.bookMode) {
11110
+ for (const target of context.bookContext?.targets ?? []) {
11111
+ context.labelKinds.set(normalizeDocumentKey(target.key), target.kind);
11112
+ if (target.manualKey) context.labelKinds.set(normalizeDocumentKey(target.manualKey), target.kind);
11113
+ }
11114
+ }
11115
+ if (context.bookMode && !context.bookContext?.unitId?.trim()) {
11116
+ addIssue(context, "book_context_required", "$.book_context");
11117
+ }
10961
11118
  for (const diagnostic of resolved.diagnostics) {
10962
11119
  addIssue(context, "parser_diagnostic", diagnostic.path);
10963
11120
  }
@@ -10968,9 +11125,6 @@ function validateDocument(document2) {
10968
11125
  issues: context.issues
10969
11126
  };
10970
11127
  }
10971
- function isDocumentValid(document2) {
10972
- return validateDocument(document2).valid;
10973
- }
10974
11128
 
10975
11129
  // src/document/history.ts
10976
11130
  var DEFAULT_DOCUMENT_HISTORY_MAX_DEPTH = 100;
@@ -11080,7 +11234,7 @@ function previewInlines(field, islands, output, equationSide, document2, preview
11080
11234
  kind: "cite",
11081
11235
  id: token.id,
11082
11236
  keys: [...token.keys],
11083
- label: formatCiteLabel(token.keys, document2.references, {
11237
+ label: formatCiteLabel(token.keys, previewOptions.mode === "book-content" ? bookContextReferences(previewOptions.bookContext) : document2.references, {
11084
11238
  documentDirection: previewOptions.documentDirection,
11085
11239
  digitForm: previewOptions.digitForm
11086
11240
  })
@@ -11092,12 +11246,16 @@ function previewInlines(field, islands, output, equationSide, document2, preview
11092
11246
  id: token.id,
11093
11247
  keys: [...token.keys],
11094
11248
  refCommand: token.refCommand,
11095
- label: formatRefLabel(token.keys, document2, token.refCommand, {
11249
+ label: previewOptions.mode === "book-content" && previewOptions.bookContext ? formatBookReferenceLabel(token.keys, document2, token.refCommand, previewOptions.bookContext, {
11250
+ documentDirection: previewOptions.documentDirection,
11251
+ digitForm: previewOptions.digitForm
11252
+ }) : formatRefLabel(token.keys, document2, token.refCommand, {
11096
11253
  documentDirection: previewOptions.documentDirection,
11097
11254
  digitForm: previewOptions.digitForm
11098
11255
  })
11099
11256
  };
11100
11257
  }
11258
+ const number = collectDocumentReferenceTargets(document2).find((target) => target.kind === "eq" && target.ownerId === token.id)?.number;
11101
11259
  const island = {
11102
11260
  id: token.id,
11103
11261
  tex: mathTex(token, equationSide, previewOptions.digitForm),
@@ -11105,8 +11263,12 @@ function previewInlines(field, islands, output, equationSide, document2, preview
11105
11263
  output,
11106
11264
  editable: token.editable
11107
11265
  };
11266
+ const numberLabel = number == null ? void 0 : formatBibliographyNumber(number, {
11267
+ documentDirection: previewOptions.documentDirection,
11268
+ digitForm: previewOptions.digitForm
11269
+ });
11108
11270
  islands.push(island);
11109
- return { kind: "math", ...island };
11271
+ return { kind: "math", ...island, ...numberLabel ? { numberLabel } : {} };
11110
11272
  });
11111
11273
  }
11112
11274
  function textBlockPreview(block, islands, output, equationSide, document2, previewOptions) {
@@ -11129,12 +11291,9 @@ function textBlockPreview(block, islands, output, equationSide, document2, previ
11129
11291
  ...block.centered ? { centered: true } : {}
11130
11292
  };
11131
11293
  }
11132
- function floatNumberLabel(document2, key, previewOptions) {
11133
- if (!key || key.trim().length === 0) {
11134
- return void 0;
11135
- }
11136
- const hit = labelNumberMap(document2).get(key.trim());
11137
- if (!hit || hit.kind !== "fig" && hit.kind !== "tab") {
11294
+ function floatNumberLabel(document2, ownerId, previewOptions) {
11295
+ const hit = collectDocumentReferenceTargets(document2).find((target) => target.ownerId === ownerId);
11296
+ if (!hit || hit.number === null || hit.kind !== "fig" && hit.kind !== "tab") {
11138
11297
  return void 0;
11139
11298
  }
11140
11299
  return formatFloatCaptionTitle(hit.kind, hit.number, {
@@ -11176,7 +11335,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
11176
11335
  id: block.id,
11177
11336
  rows: block.rows.map((row) => row.map((cell) => previewInlines(cell, islands, output, equationSide, document2, previewOptions))),
11178
11337
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
11179
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
11338
+ ...label.length > 0 ? { label } : {},
11339
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
11180
11340
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
11181
11341
  };
11182
11342
  }
@@ -11190,7 +11350,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
11190
11350
  options: block.options,
11191
11351
  centered: block.centered,
11192
11352
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
11193
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
11353
+ ...label.length > 0 ? { label } : {},
11354
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
11194
11355
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
11195
11356
  };
11196
11357
  }
@@ -11244,14 +11405,16 @@ function documentPreview(document2, output = "svg", equationSide = "arabic", pre
11244
11405
  const options = {
11245
11406
  documentDirection: previewOptions.documentDirection ?? "rtl",
11246
11407
  digitForm: previewOptions.digitForm ?? (previewOptions.documentDirection === "ltr" ? "western" : "arabicIndic"),
11247
- uiLocale: previewOptions.uiLocale ?? "ar"
11408
+ uiLocale: previewOptions.uiLocale ?? "ar",
11409
+ mode: previewOptions.mode ?? "article",
11410
+ bookContext: previewOptions.bookContext
11248
11411
  };
11249
- const body = document2.blocks.map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
11412
+ const body = document2.blocks.filter((block) => options.mode !== "book-content" || block.kind !== "bibliography").map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
11250
11413
  return {
11251
11414
  blocks: [
11252
- ...articleChromePreview(document2, options.uiLocale ?? "ar", options),
11415
+ ...options.mode === "book-content" ? [] : articleChromePreview(document2, options.uiLocale ?? "ar", options),
11253
11416
  ...body,
11254
- { kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }
11417
+ ...options.mode === "book-content" ? [] : [{ kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }]
11255
11418
  ],
11256
11419
  mathIslands
11257
11420
  };
@@ -12484,6 +12647,7 @@ var import_jsx_runtime5 = require("react/jsx-runtime");
12484
12647
  function RefChip({
12485
12648
  token,
12486
12649
  document: document2,
12650
+ bookContext,
12487
12651
  documentDirection = "rtl",
12488
12652
  digitForm,
12489
12653
  editable = true,
@@ -12495,7 +12659,7 @@ function RefChip({
12495
12659
  onRequestTextFocus
12496
12660
  }) {
12497
12661
  const messages = documentMessages(uiLocale);
12498
- const label = formatRefLabel(token.keys, document2, token.refCommand, { documentDirection, digitForm });
12662
+ const label = bookContext ? formatBookReferenceLabel(token.keys, document2, token.refCommand, bookContext, { documentDirection, digitForm }) : formatRefLabel(token.keys, document2, token.refCommand, { documentDirection, digitForm });
12499
12663
  const forwardArrowKey = documentDirection === "rtl" ? "ArrowLeft" : "ArrowRight";
12500
12664
  const backwardArrowKey = documentDirection === "rtl" ? "ArrowRight" : "ArrowLeft";
12501
12665
  if (!editable) {
@@ -12573,6 +12737,7 @@ function InlineField({
12573
12737
  blockId,
12574
12738
  references = [],
12575
12739
  documentNode,
12740
+ bookContext,
12576
12741
  documentDirection = "rtl",
12577
12742
  digitForm,
12578
12743
  mathOutput = "svg",
@@ -12809,6 +12974,7 @@ function InlineField({
12809
12974
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
12810
12975
  RefChip,
12811
12976
  {
12977
+ bookContext,
12812
12978
  token,
12813
12979
  document: documentNode,
12814
12980
  documentDirection,
@@ -13291,7 +13457,9 @@ function BlockEditor({
13291
13457
  onParagraphCenteredChange,
13292
13458
  onAddListItem,
13293
13459
  onRemoveListItem,
13294
- onManageReferences
13460
+ onManageReferences,
13461
+ bookContext,
13462
+ onHeadingLabelChange
13295
13463
  }) {
13296
13464
  const messages = documentMessages(uiLocale);
13297
13465
  const labels = documentNode ? collectDocumentLabels(documentNode) : [];
@@ -13306,6 +13474,7 @@ function BlockEditor({
13306
13474
  blockId: block.id,
13307
13475
  references,
13308
13476
  documentNode,
13477
+ bookContext,
13309
13478
  documentDirection,
13310
13479
  digitForm,
13311
13480
  mathOutput,
@@ -13383,6 +13552,18 @@ function BlockEditor({
13383
13552
  return /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("section", { className: chromeClass, "data-butex-block-source": block.metadata?.source, children: [
13384
13553
  header,
13385
13554
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(InlineField, { field: block.field, ...fieldProps }),
13555
+ bookContext && block.command !== "\\paragraph" ? /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("label", { className: "butex-document-widget__refs-field", children: [
13556
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("span", { children: messages.floatLabel }),
13557
+ /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
13558
+ "input",
13559
+ {
13560
+ type: "text",
13561
+ dir: "ltr",
13562
+ value: block.label ?? "",
13563
+ onChange: (event) => onHeadingLabelChange?.(block.id, event.currentTarget.value)
13564
+ }
13565
+ )
13566
+ ] }) : null,
13386
13567
  block.command === "\\paragraph" ? /* @__PURE__ */ (0, import_jsx_runtime7.jsx)("div", { className: "butex-document-widget__float-meta butex-document-widget__float-meta--compact", children: /* @__PURE__ */ (0, import_jsx_runtime7.jsxs)("label", { className: "butex-document-widget__float-meta-toggle", children: [
13387
13568
  /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
13388
13569
  "input",
@@ -13442,7 +13623,9 @@ function BlockEditor({
13442
13623
  onParagraphCenteredChange,
13443
13624
  onAddListItem,
13444
13625
  onRemoveListItem,
13445
- onManageReferences
13626
+ onManageReferences,
13627
+ bookContext,
13628
+ onHeadingLabelChange
13446
13629
  },
13447
13630
  child.id
13448
13631
  ))
@@ -13802,6 +13985,7 @@ function digitFormLabel(digitForm) {
13802
13985
  return "456";
13803
13986
  }
13804
13987
  function DocumentInsertToolbar({
13988
+ mode = "article",
13805
13989
  canUndo,
13806
13990
  canRedo,
13807
13991
  editableEquations = true,
@@ -13991,7 +14175,7 @@ function DocumentInsertToolbar({
13991
14175
  )
13992
14176
  ] }),
13993
14177
  /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)("div", { className: "butex-document-widget__toolbar-group", role: "group", "aria-label": messages.structure, children: [
13994
- /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
14178
+ mode === "article" ? /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(
13995
14179
  "button",
13996
14180
  {
13997
14181
  type: "button",
@@ -14001,7 +14185,7 @@ function DocumentInsertToolbar({
14001
14185
  onClick: onOpenArticleMeta,
14002
14186
  children: "\u2726"
14003
14187
  }
14004
- ),
14188
+ ) : null,
14005
14189
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("button", { type: "button", className: "butex-document-widget__icon-btn butex-document-widget__icon-btn--h1", title: messages.addSection, "aria-label": messages.addSection, onClick: onAddSection, children: messages.sectionGlyph }),
14006
14190
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("button", { type: "button", className: "butex-document-widget__icon-btn butex-document-widget__icon-btn--h2", title: messages.addSubsection, "aria-label": messages.addSubsection, onClick: onAddSubsection, children: messages.subsectionGlyph }),
14007
14191
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("button", { type: "button", className: "butex-document-widget__icon-btn butex-document-widget__icon-btn--h3", title: messages.addSubsubsection, "aria-label": messages.addSubsubsection, onClick: onAddSubsubsection, children: messages.subsubsectionGlyph }),
@@ -14076,14 +14260,14 @@ function DocumentInsertToolbar({
14076
14260
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("span", { "aria-hidden": "true", children: "\xA7" }),
14077
14261
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("span", { children: messages.insertInternalRef })
14078
14262
  ] }),
14079
- /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)("button", { type: "button", role: "menuitem", title: messages.insertBibliography, onClick: () => runReferenceAction(onInsertBibliography), children: [
14263
+ mode === "article" ? /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)("button", { type: "button", role: "menuitem", title: messages.insertBibliography, onClick: () => runReferenceAction(onInsertBibliography), children: [
14080
14264
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("span", { className: "butex-document-widget__icon-bibliography", "aria-hidden": "true", children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("svg", { viewBox: "0 0 16 16", width: "14", height: "14", focusable: "false", children: /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("path", { fill: "currentColor", d: "M2.2 2.4h7.2c.9 0 1.6.7 1.6 1.6v9.2c0 .5-.6.8-1 .5l-2.2-1.6H2.2c-.9 0-1.6-.7-1.6-1.6V4c0-.9.7-1.6 1.6-1.6zm0 1.2c-.2 0-.4.2-.4.4v6.5c0 .2.2.4.4.4h6.1l1.5 1.1V4c0-.2-.2-.4-.4-.4H2.2zM3.4 5.2h5.2v1H3.4zm0 2.1h4.2v1H3.4zm0 2.1h3.4v1H3.4zM11.4 4.1h2.2c.8 0 1.4.6 1.4 1.4v7.1c0 .4-.5.7-.8.4l-1.7-1.3h-1.1V4.1zm1.2 1.2v5.4l.7.5V5.5c0-.1-.1-.2-.2-.2h-.5z" }) }) }),
14081
14265
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("span", { children: messages.insertBibliography })
14082
- ] }),
14083
- /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)("button", { type: "button", role: "menuitem", title: messages.manageReferences, onClick: () => runReferenceAction(onManageReferences), children: [
14266
+ ] }) : null,
14267
+ mode === "article" ? /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)("button", { type: "button", role: "menuitem", title: messages.manageReferences, onClick: () => runReferenceAction(onManageReferences), children: [
14084
14268
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("span", { "aria-hidden": "true", children: "\u2630" }),
14085
14269
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("span", { children: messages.manageReferences })
14086
- ] }),
14270
+ ] }) : null,
14087
14271
  /* @__PURE__ */ (0, import_jsx_runtime10.jsxs)("button", { type: "button", role: "menuitem", title: messages.manageLabels, onClick: () => runReferenceAction(onManageLabels), children: [
14088
14272
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("span", { "aria-hidden": "true", children: "\u2317" }),
14089
14273
  /* @__PURE__ */ (0, import_jsx_runtime10.jsx)("span", { children: messages.manageLabels })
@@ -14115,6 +14299,7 @@ function digitFormLabel2(digitForm) {
14115
14299
  return "456";
14116
14300
  }
14117
14301
  function DocumentMobileToolbar({
14302
+ mode = "article",
14118
14303
  mobileView,
14119
14304
  canUndo,
14120
14305
  canRedo,
@@ -14299,7 +14484,7 @@ function DocumentMobileToolbar({
14299
14484
  /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)("section", { className: "butex-document-widget__command-group", "aria-labelledby": `${titleId}-structure`, children: [
14300
14485
  /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("h3", { id: `${titleId}-structure`, children: messages.structure }),
14301
14486
  /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)("div", { className: "butex-document-widget__command-grid", children: [
14302
- /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onOpenArticleMeta), children: messages.articleMetaTitle }),
14487
+ mode === "article" ? /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onOpenArticleMeta), children: messages.articleMetaTitle }) : null,
14303
14488
  /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onAddSection), children: messages.addSection }),
14304
14489
  /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onAddSubsection), children: messages.addSubsection }),
14305
14490
  /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onAddSubsubsection), children: messages.addSubsubsection }),
@@ -14340,8 +14525,8 @@ function DocumentMobileToolbar({
14340
14525
  /* @__PURE__ */ (0, import_jsx_runtime11.jsxs)("div", { className: "butex-document-widget__command-grid", children: [
14341
14526
  /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onInsertCitation, false), children: messages.insertCitation }),
14342
14527
  /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onInsertInternalRef, false), children: messages.insertInternalRef }),
14343
- /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onInsertBibliography), children: messages.insertBibliography }),
14344
- /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(() => onManageReferences(triggerRef.current), false), children: messages.manageReferences }),
14528
+ mode === "article" ? /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(onInsertBibliography), children: messages.insertBibliography }) : null,
14529
+ mode === "article" ? /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(() => onManageReferences(triggerRef.current), false), children: messages.manageReferences }) : null,
14345
14530
  /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("button", { type: "button", onClick: () => runAction(() => onManageLabels(triggerRef.current), false), children: messages.manageLabels })
14346
14531
  ] })
14347
14532
  ] }),
@@ -14412,7 +14597,14 @@ function PreviewInlines({ inlines, output }) {
14412
14597
  if (inline.kind === "ref") {
14413
14598
  return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)("span", { className: "butex-document-widget__preview-cite butex-document-widget__preview-ref", children: contextualRefLabel(inlines, index, inline.label) }, inline.id);
14414
14599
  }
14415
- return /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }, inline.id);
14600
+ return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)("span", { className: "butex-document-widget__preview-math-numbered", children: [
14601
+ /* @__PURE__ */ (0, import_jsx_runtime12.jsx)(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }),
14602
+ inline.numberLabel ? /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)("span", { className: "butex-document-widget__preview-equation-number", children: [
14603
+ "(",
14604
+ inline.numberLabel,
14605
+ ")"
14606
+ ] }) : null
14607
+ ] }, inline.id);
14416
14608
  }) });
14417
14609
  }
14418
14610
  function isBodyPreviewBlock(block) {
@@ -18468,6 +18660,8 @@ function EquationDrawer({
18468
18660
  responsiveMode = "auto",
18469
18661
  labelEnabled = false,
18470
18662
  label = "",
18663
+ numbered = false,
18664
+ onNumberedChange,
18471
18665
  labels = [],
18472
18666
  references = [],
18473
18667
  ownerId = "",
@@ -18557,6 +18751,10 @@ function EquationDrawer({
18557
18751
  ] })
18558
18752
  ] }),
18559
18753
  mathMode === "display" ? /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)("div", { className: "butex-document-widget__float-meta", children: [
18754
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)("label", { className: "butex-document-widget__float-meta-toggle", children: [
18755
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("input", { type: "checkbox", checked: numbered, onChange: (event) => onNumberedChange?.(event.currentTarget.checked) }),
18756
+ /* @__PURE__ */ (0, import_jsx_runtime15.jsx)("span", { children: uiLocale === "ar" ? "\u062A\u0631\u0642\u064A\u0645 \u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629" : "Number equation" })
18757
+ ] }),
18560
18758
  /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)("label", { className: "butex-document-widget__float-meta-toggle", children: [
18561
18759
  /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(
18562
18760
  "input",
@@ -19080,13 +19278,14 @@ var import_react16 = require("react");
19080
19278
  var import_jsx_runtime18 = require("react/jsx-runtime");
19081
19279
  function RefPickerPopover({
19082
19280
  open,
19083
- labels,
19281
+ targets,
19084
19282
  initialKeys = [],
19085
19283
  initialRefCommand = "ref",
19086
19284
  uiLocale = "ar",
19087
19285
  digitForm = "arabicIndic",
19088
19286
  onClose,
19089
19287
  onConfirm,
19288
+ onNumberEquation,
19090
19289
  onManageLabels
19091
19290
  }) {
19092
19291
  const messages = documentMessages(uiLocale);
@@ -19123,22 +19322,19 @@ function RefPickerPopover({
19123
19322
  /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("strong", { children: messages.refPickerTitle }),
19124
19323
  /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("button", { type: "button", "data-dialog-initial-focus": true, className: "butex-document-widget__icon-btn", title: messages.close, "aria-label": messages.close, onClick: onClose, children: "\xD7" })
19125
19324
  ] }),
19126
- /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("div", { className: "butex-document-widget__dialog-body", children: labels.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("p", { className: "butex-document-widget__cite-picker-empty", children: messages.noLabelsYet }) : /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("ul", { className: "butex-document-widget__cite-picker-list", children: labels.map((entry) => {
19127
- const checked = selected.includes(entry.key);
19128
- const title = formatFloatCaptionTitle(entry.kind, entry.number, {
19325
+ /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("div", { className: "butex-document-widget__dialog-body", children: targets.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("p", { className: "butex-document-widget__cite-picker-empty", children: messages.noLabelsYet }) : /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("ul", { className: "butex-document-widget__cite-picker-list", children: targets.filter((entry) => entry.number !== null || Boolean(entry.displayLabel) || entry.kind === "eq").map((entry) => {
19326
+ const checked = selected.includes(entry.key) || Boolean(entry.manualKey && selected.includes(entry.manualKey));
19327
+ const title = entry.displayLabel ?? (entry.number === null ? uiLocale === "ar" ? "\u0645\u0639\u0627\u062F\u0644\u0629 \u063A\u064A\u0631 \u0645\u0631\u0642\u0645\u0629" : "Unnumbered equation" : formatFloatCaptionTitle(entry.kind, entry.number, {
19129
19328
  uiLocale,
19130
19329
  digitForm,
19131
19330
  documentDirection
19132
- });
19331
+ }));
19133
19332
  return /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("li", { children: /* @__PURE__ */ (0, import_jsx_runtime18.jsxs)("label", { className: "butex-document-widget__cite-picker-item", children: [
19134
- /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("input", { type: "radio", name: "butex-ref-key", checked, onChange: () => selectKey(entry.key, entry.kind) }),
19333
+ /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("input", { type: "radio", name: "butex-ref-key", checked, disabled: entry.number === null && !entry.displayLabel, onChange: () => selectKey(entry.key, entry.kind) }),
19135
19334
  /* @__PURE__ */ (0, import_jsx_runtime18.jsxs)("span", { children: [
19136
- /* @__PURE__ */ (0, import_jsx_runtime18.jsxs)("strong", { children: [
19137
- title,
19138
- " ",
19139
- /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("span", { className: "butex-document-widget__refs-item-key", children: entry.key })
19140
- ] }),
19141
- entry.caption ? /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("span", { className: "butex-document-widget__cite-picker-meta", children: entry.caption }) : null
19335
+ /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("strong", { children: entry.kind === "eq" && entry.number !== null ? title.replace(/(\d+|[٠-٩]+)$/, "($1)") : title }),
19336
+ entry.caption ? /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("span", { className: "butex-document-widget__cite-picker-meta", children: entry.caption }) : null,
19337
+ entry.kind === "eq" && entry.number === null && onNumberEquation ? /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("button", { type: "button", onClick: () => onNumberEquation(entry.ownerId), children: uiLocale === "ar" ? "\u0631\u0642\u0651\u0645 \u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629 \u0623\u0648\u0644\u0627\u064B" : "Number equation first" }) : null
19142
19338
  ] })
19143
19339
  ] }) }, `${entry.kind}:${entry.ownerId}`);
19144
19340
  }) }) }),
@@ -21783,6 +21979,8 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
21783
21979
  function ButexDocumentEditor2({
21784
21980
  initialDocument,
21785
21981
  documentMeta,
21982
+ mode = "article",
21983
+ bookContext,
21786
21984
  className,
21787
21985
  debug = false,
21788
21986
  documentDirection = "rtl",
@@ -21831,6 +22029,27 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
21831
22029
  return setCurrentDigitForm(session, digitForm);
21832
22030
  }
21833
22031
  const historyRef = (0, import_react17.useRef)(createDocumentHistory());
22032
+ const selectedMathRef = (0, import_react17.useRef)(selectedMath);
22033
+ selectedMathRef.current = selectedMath;
22034
+ const imagePickCountRef = (0, import_react17.useRef)(0);
22035
+ const documentGenerationRef = (0, import_react17.useRef)(0);
22036
+ (0, import_react17.useEffect)(() => {
22037
+ documentGenerationRef.current += 1;
22038
+ return () => {
22039
+ documentGenerationRef.current += 1;
22040
+ };
22041
+ }, [initialDocument]);
22042
+ const safeRequestImagePick = (0, import_react17.useCallback)(async (ctx) => {
22043
+ if (!onRequestImagePick) return null;
22044
+ const generation = documentGenerationRef.current;
22045
+ imagePickCountRef.current += 1;
22046
+ try {
22047
+ const picked = await onRequestImagePick(ctx);
22048
+ return generation === documentGenerationRef.current ? picked : null;
22049
+ } finally {
22050
+ imagePickCountRef.current -= 1;
22051
+ }
22052
+ }, [onRequestImagePick]);
21834
22053
  const documentRef = (0, import_react17.useRef)(documentNode);
21835
22054
  const editorFocusRef = (0, import_react17.useRef)(editorFocus);
21836
22055
  const textSnapshotArmedRef = (0, import_react17.useRef)(false);
@@ -21855,16 +22074,26 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
21855
22074
  }
21856
22075
  documentRef.current = documentNode;
21857
22076
  editorFocusRef.current = editorFocus;
21858
- const latex = documentLatex(documentNode, { digitForm });
22077
+ const latex = mode === "book-content" ? documentContentLatex(documentNode, bookContext) : documentLatex(documentNode, { digitForm });
21859
22078
  const documentJson = (0, import_react17.useMemo)(() => toDocumentJson(documentNode), [documentNode]);
21860
22079
  const documentLabels = (0, import_react17.useMemo)(() => collectDocumentLabels(documentNode), [documentNode]);
22080
+ const displayReferences = (0, import_react17.useMemo)(
22081
+ () => mode === "book-content" ? bookContextReferences(bookContext) : documentNode.references,
22082
+ [mode, bookContext, documentNode]
22083
+ );
22084
+ const referenceTargets = (0, import_react17.useMemo)(
22085
+ () => mode === "book-content" && bookContext ? bookContentTargets(documentNode, bookContext) : collectDocumentReferenceTargets(documentNode),
22086
+ [mode, bookContext, documentNode]
22087
+ );
21861
22088
  const preview = (0, import_react17.useMemo)(
21862
22089
  () => documentPreview(documentNode, mathOutput, equationSide, {
21863
22090
  documentDirection,
21864
22091
  digitForm,
21865
- uiLocale
22092
+ uiLocale,
22093
+ mode,
22094
+ bookContext
21866
22095
  }),
21867
- [documentNode, documentDirection, digitForm, equationSide, mathOutput, uiLocale]
22096
+ [documentNode, documentDirection, digitForm, equationSide, mathOutput, uiLocale, mode, bookContext]
21868
22097
  );
21869
22098
  const debugEnabled = (0, import_react17.useMemo)(() => {
21870
22099
  if (debug) {
@@ -21991,11 +22220,11 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
21991
22220
  setHistoryTick((tick) => tick + 1);
21992
22221
  }, []);
21993
22222
  const applyDocument = (0, import_react17.useCallback)(
21994
- (next, mode = "immediate") => {
21995
- if (mode === "immediate") {
22223
+ (next, mode2 = "immediate") => {
22224
+ if (mode2 === "immediate") {
21996
22225
  pushDocumentSnapshot(historyRef.current, documentRef.current);
21997
22226
  bumpHistoryUi();
21998
- } else if (mode === "text") {
22227
+ } else if (mode2 === "text") {
21999
22228
  if (!textSnapshotArmedRef.current) {
22000
22229
  pushDocumentSnapshot(historyRef.current, documentRef.current);
22001
22230
  textSnapshotArmedRef.current = true;
@@ -22034,6 +22263,9 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22034
22263
  },
22035
22264
  getDocumentJson() {
22036
22265
  return toDocumentJson(documentRef.current);
22266
+ },
22267
+ hasPendingChildTransaction() {
22268
+ return selectedMathRef.current !== null || imagePickCountRef.current > 0;
22037
22269
  }
22038
22270
  }),
22039
22271
  [applyDocument, insertImageBlock]
@@ -22171,7 +22403,7 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22171
22403
  function onFieldBlur() {
22172
22404
  textSnapshotArmedRef.current = false;
22173
22405
  }
22174
- function openNewEquation(mode) {
22406
+ function openNewEquation(mode2) {
22175
22407
  if (!editableEquations) {
22176
22408
  return;
22177
22409
  }
@@ -22189,11 +22421,12 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22189
22421
  fieldId: target?.fieldId ?? null,
22190
22422
  textTokenId: target?.textTokenId ?? null,
22191
22423
  insertAt: target?.caretOffset ?? 0,
22192
- mathMode: mode,
22424
+ mathMode: mode2,
22193
22425
  session: equationSessionWithDocumentDigits(createEmptyEquationSession(equationSide)),
22194
22426
  reason: null,
22195
22427
  labelEnabled: false,
22196
- label: ""
22428
+ label: "",
22429
+ numbered: false
22197
22430
  });
22198
22431
  }
22199
22432
  function openMath(token, origin) {
@@ -22207,18 +22440,19 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22207
22440
  editorFocus: { ...editorFocusRef.current }
22208
22441
  };
22209
22442
  const result = mathTokenToEditorSession(token.math, equationSide);
22210
- const mode = token.display ? "display" : "inline";
22443
+ const mode2 = token.display ? "display" : "inline";
22211
22444
  if (!result.editable) {
22212
22445
  setSelectedMath({
22213
22446
  tokenId: token.id,
22214
22447
  fieldId: null,
22215
22448
  insertAt: 0,
22216
22449
  textTokenId: null,
22217
- mathMode: mode,
22450
+ mathMode: mode2,
22218
22451
  session: equationSessionWithDocumentDigits(createEmptyEquationSession(equationSide)),
22219
22452
  reason: result.reason,
22220
22453
  labelEnabled: Boolean(token.labelEnabled),
22221
- label: token.label ?? ""
22454
+ label: token.label ?? "",
22455
+ numbered: isNumberedEquation(token)
22222
22456
  });
22223
22457
  return;
22224
22458
  }
@@ -22227,11 +22461,12 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22227
22461
  fieldId: null,
22228
22462
  insertAt: 0,
22229
22463
  textTokenId: null,
22230
- mathMode: mode,
22464
+ mathMode: mode2,
22231
22465
  session: equationSessionWithDocumentDigits(result.session),
22232
22466
  reason: null,
22233
22467
  labelEnabled: Boolean(token.labelEnabled),
22234
- label: token.label ?? ""
22468
+ label: token.label ?? "",
22469
+ numbered: isNumberedEquation(token)
22235
22470
  });
22236
22471
  }
22237
22472
  function selectMobileView(view) {
@@ -22322,10 +22557,11 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22322
22557
  if (selectedMath.mathMode === "display") {
22323
22558
  next2 = updateMathTokenLabel(next2, selectedMath.tokenId, {
22324
22559
  labelEnabled: selectedMath.labelEnabled,
22325
- label: selectedMath.label
22560
+ label: selectedMath.label,
22561
+ numbered: selectedMath.numbered
22326
22562
  });
22327
22563
  } else {
22328
- next2 = updateMathTokenLabel(next2, selectedMath.tokenId, { labelEnabled: false, label: "" });
22564
+ next2 = updateMathTokenLabel(next2, selectedMath.tokenId, { labelEnabled: false, label: "", numbered: false });
22329
22565
  }
22330
22566
  pendingFocusRef.current = { kind: "math", tokenId: selectedMath.tokenId };
22331
22567
  applyDocument(next2, "immediate");
@@ -22370,10 +22606,11 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22370
22606
  0,
22371
22607
  fieldAfter.tokens.findIndex((token) => token.id === trailingTextTokenId) - 1
22372
22608
  )]?.id : null : fieldAfter?.tokens.filter((token) => token.kind === "math").at(-1)?.id ?? null;
22373
- if (insertedMathId && selectedMath.mathMode === "display" && selectedMath.labelEnabled) {
22609
+ if (insertedMathId && selectedMath.mathMode === "display") {
22374
22610
  next = updateMathTokenLabel(next, insertedMathId, {
22375
- labelEnabled: true,
22376
- label: selectedMath.label
22611
+ labelEnabled: selectedMath.labelEnabled,
22612
+ label: selectedMath.label,
22613
+ numbered: selectedMath.numbered
22377
22614
  });
22378
22615
  }
22379
22616
  if (insertedMathId) {
@@ -22549,6 +22786,7 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22549
22786
  /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
22550
22787
  DocumentInsertToolbar,
22551
22788
  {
22789
+ mode,
22552
22790
  canUndo,
22553
22791
  canRedo,
22554
22792
  editableEquations,
@@ -22587,6 +22825,7 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22587
22825
  /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
22588
22826
  DocumentMobileToolbar,
22589
22827
  {
22828
+ mode,
22590
22829
  mobileView,
22591
22830
  canUndo,
22592
22831
  canRedo,
@@ -22658,7 +22897,7 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22658
22897
  "aria-label": messages.documentEditor,
22659
22898
  "aria-hidden": !showEditorPanel,
22660
22899
  children: /* @__PURE__ */ (0, import_jsx_runtime19.jsxs)("div", { className: "butex-document-widget__blocks", children: [
22661
- /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
22900
+ mode === "article" ? /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
22662
22901
  ArticleMetaPanel,
22663
22902
  {
22664
22903
  meta: documentNode.meta ?? emptyDocumentMeta(),
@@ -22670,13 +22909,16 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22670
22909
  applyDocument(updateDocumentMeta(documentRef.current, patch), "text");
22671
22910
  }
22672
22911
  }
22673
- ),
22912
+ ) : null,
22913
+ mode === "book-content" && documentNode.blocks.some((block) => block.kind === "bibliography") ? /* @__PURE__ */ (0, import_jsx_runtime19.jsx)("p", { role: "alert", className: "butex-document-widget__error", children: uiLocale === "ar" ? "\u062A\u0633\u062A\u062E\u062F\u0645 \u0641\u0635\u0648\u0644 \u0627\u0644\u0643\u062A\u0627\u0628 \u0642\u0627\u0626\u0645\u0629 \u0645\u0631\u0627\u062C\u0639 \u0645\u0634\u062A\u0631\u0643\u0629\u061B \u0623\u0632\u0644 \u0642\u0627\u0626\u0645\u0629 \u0627\u0644\u0645\u0631\u0627\u062C\u0639 \u0627\u0644\u0645\u062D\u0644\u064A\u0629 \u0645\u0646 \u0627\u0644\u0641\u0635\u0644." : "Book chapters use the shared bibliography. Remove the local bibliography block." }) : null,
22674
22914
  documentNode.blocks.map((block, blockIndex) => /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
22675
22915
  BlockEditor,
22676
22916
  {
22677
22917
  block,
22678
- references: documentNode.references,
22918
+ references: displayReferences,
22679
22919
  documentNode,
22920
+ bookContext: mode === "book-content" ? bookContext : void 0,
22921
+ onHeadingLabelChange: (blockId, label) => applyDocument(updateDocumentHeadingLabel(documentRef.current, blockId, label), "text"),
22680
22922
  documentDirection,
22681
22923
  digitForm,
22682
22924
  mathOutput,
@@ -22715,7 +22957,7 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22715
22957
  applyDocument(clearDocumentImageAsset(documentRef.current, blockId), "immediate");
22716
22958
  },
22717
22959
  resolveImageUrl,
22718
- onRequestImagePick,
22960
+ onRequestImagePick: onRequestImagePick ? safeRequestImagePick : void 0,
22719
22961
  listImageAssets,
22720
22962
  renderImageBlockEditor,
22721
22963
  onFloatMetaChange: (blockId, kind, patch) => {
@@ -22737,7 +22979,7 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22737
22979
  clearBlockSelection();
22738
22980
  applyDocument(removeDocumentListItem(documentRef.current, listBlockId, itemId), "immediate");
22739
22981
  },
22740
- onManageReferences: () => openReferencesManager()
22982
+ onManageReferences: mode === "article" ? () => openReferencesManager() : void 0
22741
22983
  },
22742
22984
  block.id
22743
22985
  ))
@@ -22825,12 +23067,14 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22825
23067
  responsiveMode,
22826
23068
  labelEnabled: selectedMath.labelEnabled,
22827
23069
  label: selectedMath.label,
23070
+ numbered: selectedMath.numbered,
23071
+ onNumberedChange: (numbered) => setSelectedMath((current) => current ? { ...current, numbered } : current),
22828
23072
  labels: documentLabels,
22829
- references: documentNode.references,
23073
+ references: displayReferences,
22830
23074
  ownerId: selectedMath.tokenId ?? void 0,
22831
- onLabelEnabledChange: (enabled) => setSelectedMath((current) => current ? { ...current, labelEnabled: enabled } : current),
23075
+ onLabelEnabledChange: (enabled) => setSelectedMath((current) => current ? { ...current, labelEnabled: enabled, numbered: enabled || current.numbered } : current),
22832
23076
  onLabelChange: (nextLabel) => setSelectedMath((current) => current ? { ...current, label: nextLabel } : current),
22833
- onMathModeChange: (mode) => setSelectedMath((current) => current ? { ...current, mathMode: mode } : current),
23077
+ onMathModeChange: (mode2) => setSelectedMath((current) => current ? { ...current, mathMode: mode2 } : current),
22834
23078
  onClose: closeEquationAndRestoreOrigin,
22835
23079
  onSave: saveEquation,
22836
23080
  onDelete: deleteSelectedEquation
@@ -22840,36 +23084,39 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22840
23084
  CitePickerPopover,
22841
23085
  {
22842
23086
  open: citePicker !== null,
22843
- references: documentNode.references,
23087
+ references: displayReferences,
22844
23088
  initialKeys: citePicker?.keys ?? [],
22845
23089
  uiLocale,
22846
23090
  digitForm,
22847
23091
  onClose: () => setCitePicker(null),
22848
23092
  onConfirm: confirmCiteKeys,
22849
- onManageReferences: () => {
23093
+ onManageReferences: mode === "article" ? () => {
22850
23094
  setCitePicker(null);
22851
23095
  openReferencesManager();
22852
- }
23096
+ } : void 0
22853
23097
  }
22854
23098
  ) : null,
22855
23099
  !previewOnly ? /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
22856
23100
  RefPickerPopover,
22857
23101
  {
22858
23102
  open: refPicker !== null,
22859
- labels: documentLabels,
23103
+ targets: referenceTargets,
22860
23104
  initialKeys: refPicker?.keys ?? [],
22861
23105
  initialRefCommand: refPicker?.refCommand ?? "ref",
22862
23106
  uiLocale,
22863
23107
  digitForm,
22864
23108
  onClose: () => setRefPicker(null),
22865
23109
  onConfirm: confirmRefKeys,
23110
+ onNumberEquation: (tokenId) => {
23111
+ applyDocument(updateMathTokenLabel(documentRef.current, tokenId, { numbered: true }), "immediate");
23112
+ },
22866
23113
  onManageLabels: () => {
22867
23114
  setRefPicker(null);
22868
23115
  openLabelsManager();
22869
23116
  }
22870
23117
  }
22871
23118
  ) : null,
22872
- !previewOnly ? /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
23119
+ !previewOnly && mode === "article" ? /* @__PURE__ */ (0, import_jsx_runtime19.jsx)(
22873
23120
  ReferencesPanel,
22874
23121
  {
22875
23122
  open: referencesOpen,
@@ -22890,7 +23137,7 @@ var ButexDocumentEditor = (0, import_react17.forwardRef)(
22890
23137
  {
22891
23138
  open: labelsOpen,
22892
23139
  labels: documentLabels,
22893
- references: documentNode.references,
23140
+ references: displayReferences,
22894
23141
  uiLocale,
22895
23142
  digitForm,
22896
23143
  returnFocusRef: managerFocusOriginRef,
@@ -22927,6 +23174,8 @@ var ButexDocumentEditor3 = (0, import_react18.forwardRef)(
22927
23174
  function ValidatedButexDocumentEditor({
22928
23175
  initialDocument,
22929
23176
  uiLocale = "ar",
23177
+ mode = "article",
23178
+ bookContext,
22930
23179
  onDocumentChange,
22931
23180
  onValidityChange,
22932
23181
  renderImageBlockEditor,
@@ -22955,10 +23204,10 @@ var ButexDocumentEditor3 = (0, import_react18.forwardRef)(
22955
23204
  (document2) => {
22956
23205
  const validationTarget = initialDocument !== void 0 && (malformedInitialDocument || awaitingInitialResolutionRef.current) ? initialDocument : document2;
22957
23206
  awaitingInitialResolutionRef.current = false;
22958
- emitValidity(isDocumentValid(validationTarget));
23207
+ emitValidity(validateDocument(validationTarget, { mode, bookContext }).valid);
22959
23208
  onDocumentChange?.(document2);
22960
23209
  },
22961
- [emitValidity, initialDocument, malformedInitialDocument, onDocumentChange]
23210
+ [emitValidity, initialDocument, malformedInitialDocument, onDocumentChange, mode, bookContext]
22962
23211
  );
22963
23212
  const handleImageBlockEditor = (0, import_react18.useCallback)(
22964
23213
  (ctx) => {
@@ -22995,6 +23244,8 @@ var ButexDocumentEditor3 = (0, import_react18.forwardRef)(
22995
23244
  ...rest,
22996
23245
  initialDocument,
22997
23246
  uiLocale,
23247
+ mode,
23248
+ bookContext,
22998
23249
  onDocumentChange: handleDocumentChange,
22999
23250
  renderImageBlockEditor: handleImageBlockEditor
23000
23251
  }
@@ -23037,6 +23288,8 @@ function resolvePreviewDocument(input, uiLocale = "ar") {
23037
23288
  }
23038
23289
  }
23039
23290
  function ButexDocumentPreview({
23291
+ mode = "article",
23292
+ bookContext,
23040
23293
  document: document2,
23041
23294
  className,
23042
23295
  documentDirection = "rtl",
@@ -23055,7 +23308,9 @@ function ButexDocumentPreview({
23055
23308
  preview: documentPreview(resolved.document, mathOutput, equationSide, {
23056
23309
  documentDirection,
23057
23310
  digitForm: resolvedDigitForm,
23058
- uiLocale
23311
+ uiLocale,
23312
+ mode,
23313
+ bookContext
23059
23314
  }),
23060
23315
  error: resolved.error
23061
23316
  };
@@ -23065,12 +23320,14 @@ function ButexDocumentPreview({
23065
23320
  preview: documentPreview(createEmptyDocument(), mathOutput, equationSide, {
23066
23321
  documentDirection,
23067
23322
  digitForm: resolvedDigitForm,
23068
- uiLocale
23323
+ uiLocale,
23324
+ mode,
23325
+ bookContext
23069
23326
  }),
23070
23327
  error: `${messages.loadDocumentError}: ${detail}`
23071
23328
  };
23072
23329
  }
23073
- }, [documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
23330
+ }, [bookContext, mode, documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
23074
23331
  (0, import_react19.useEffect)(() => {
23075
23332
  injectBuTeXDocumentStyles();
23076
23333
  }, []);