@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.
@@ -638,82 +638,82 @@ function refTokenLatex(keys, refCommand) {
638
638
  const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
639
639
  return `${command}{${keys.join(",")}}`;
640
640
  }
641
- function collectDocumentLabels(document2) {
642
- const entries = [];
641
+ function implicitObjectKey(kind, ownerId, unitId) {
642
+ const hex = (value) => Array.from(new TextEncoder().encode(value), (byte) => byte.toString(16).padStart(2, "0")).join("");
643
+ const scope = unitId === void 0 ? "a" : "u" + hex(unitId);
644
+ return kind + ":butex-auto-" + scope + "-" + hex(ownerId);
645
+ }
646
+ function implicitReferenceKey(kind, ownerId, unitId) {
647
+ return implicitObjectKey(kind, ownerId, unitId);
648
+ }
649
+ function implicitHeadingReferenceKey(kind, ownerId, unitId) {
650
+ return implicitObjectKey(kind, ownerId, unitId);
651
+ }
652
+ function supportsSingleEquationNumbering(token) {
653
+ return token.display && (token.opening === "\\[" || token.opening === String.fromCharCode(36, 36) || token.opening === "\\begin{equation}" || token.opening === "\\begin{equation*}");
654
+ }
655
+ function isNumberedEquation(token) {
656
+ if (!supportsSingleEquationNumbering(token)) return false;
657
+ if (typeof token.numbered === "boolean") return token.numbered;
658
+ return token.opening === "\\begin{equation}" || Boolean(token.labelEnabled) && Boolean(token.label?.trim());
659
+ }
660
+ function isNumberedFloat(block) {
661
+ return block.captionEnabled && block.caption.trim().length > 0 || block.labelEnabled && block.label.trim().length > 0;
662
+ }
663
+ function collectDocumentReferenceTargets(document2) {
664
+ const targets = [];
643
665
  const counters = { fig: 0, tab: 0, eq: 0 };
644
- function pushEqFromField(tokens) {
645
- for (const token of tokens) {
646
- if (token.kind !== "math") {
647
- continue;
648
- }
649
- if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
650
- counters.eq += 1;
651
- entries.push({
652
- key: token.label.trim(),
653
- kind: "eq",
654
- caption: "",
655
- ownerId: token.id,
656
- number: counters.eq
657
- });
666
+ function append(kind, ownerId, caption, numbered, manualKey = "") {
667
+ if (numbered) counters[kind] += 1;
668
+ targets.push({
669
+ key: implicitReferenceKey(kind, ownerId),
670
+ kind,
671
+ ownerId,
672
+ caption,
673
+ number: numbered ? counters[kind] : null,
674
+ ...manualKey.trim() ? { manualKey: manualKey.trim() } : {}
675
+ });
676
+ }
677
+ function visitField(field) {
678
+ for (const token of field.tokens) {
679
+ if (token.kind === "math" && supportsSingleEquationNumbering(token)) {
680
+ append("eq", token.id, "", isNumberedEquation(token), token.labelEnabled ? token.label ?? "" : "");
658
681
  }
659
682
  }
660
683
  }
661
684
  function walk(blocks) {
662
685
  for (const block of blocks) {
663
- if (block.kind === "image") {
664
- if (block.labelEnabled && block.label.trim().length > 0) {
665
- counters.fig += 1;
666
- entries.push({
667
- key: block.label.trim(),
668
- kind: "fig",
669
- caption: block.captionEnabled ? block.caption : "",
670
- ownerId: block.id,
671
- number: counters.fig
672
- });
673
- }
674
- if (block.captionEnabled) {
675
- pushEqFromField(block.captionField.tokens);
676
- }
677
- }
678
- if (block.kind === "table") {
679
- if (block.labelEnabled && block.label.trim().length > 0) {
680
- counters.tab += 1;
681
- entries.push({
682
- key: block.label.trim(),
683
- kind: "tab",
684
- caption: block.captionEnabled ? block.caption : "",
685
- ownerId: block.id,
686
- number: counters.tab
687
- });
688
- }
689
- if (block.captionEnabled) {
690
- pushEqFromField(block.captionField.tokens);
691
- }
692
- for (const row of block.rows) {
693
- for (const cell of row) {
694
- pushEqFromField(cell.tokens);
695
- }
686
+ if (block.kind === "image" || block.kind === "table") {
687
+ append(
688
+ block.kind === "image" ? "fig" : "tab",
689
+ block.id,
690
+ block.captionEnabled ? block.caption : "",
691
+ isNumberedFloat(block),
692
+ block.labelEnabled ? block.label : ""
693
+ );
694
+ if (block.captionEnabled) visitField(block.captionField);
695
+ if (block.kind === "table") {
696
+ for (const row of block.rows) for (const cell of row) visitField(cell);
696
697
  }
697
- }
698
- if (block.kind === "textBlock") {
699
- pushEqFromField(block.field.tokens);
700
- }
701
- if (block.kind === "list") {
698
+ } else if (block.kind === "textBlock") {
699
+ visitField(block.field);
700
+ } else if (block.kind === "list") {
702
701
  for (const item of block.items) {
703
- pushEqFromField(item.field.tokens);
702
+ visitField(item.field);
704
703
  walk(item.blocks);
705
704
  }
706
705
  }
707
706
  }
708
707
  }
709
708
  walk(document2.blocks);
710
- return entries;
709
+ return targets;
711
710
  }
712
711
  function labelNumberMap(document2) {
713
712
  const map = /* @__PURE__ */ new Map();
714
- for (const entry of collectDocumentLabels(document2)) {
715
- if (!map.has(entry.key)) {
716
- map.set(entry.key, { kind: entry.kind, number: entry.number });
713
+ for (const target of collectDocumentReferenceTargets(document2)) {
714
+ if (target.number === null) continue;
715
+ for (const key of [target.key, target.manualKey]) {
716
+ if (key && !map.has(key)) map.set(key, { kind: target.kind, number: target.number });
717
717
  }
718
718
  }
719
719
  return map;
@@ -1115,7 +1115,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1115
1115
  editable: true,
1116
1116
  sourceOwner: structuredMathJson.source_owner === "editor" ? "editor" : "imported-structured",
1117
1117
  ...typeof structuredMathJson.label_enabled === "boolean" ? { labelEnabled: structuredMathJson.label_enabled } : {},
1118
- ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {}
1118
+ ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {},
1119
+ ...typeof structuredMathJson.numbered === "boolean" ? { numbered: structuredMathJson.numbered } : {}
1119
1120
  });
1120
1121
  } else {
1121
1122
  tokens.push({
@@ -1130,7 +1131,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1130
1131
  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",
1131
1132
  sourceOwner: "raw",
1132
1133
  ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label_enabled === "boolean" ? { labelEnabled: mathJson.label_enabled } : {},
1133
- ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {}
1134
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {},
1135
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.numbered === "boolean" ? { numbered: mathJson.numbered } : {}
1134
1136
  });
1135
1137
  }
1136
1138
  index = span.end;
@@ -1227,6 +1229,7 @@ function parseTextBlock(json, options, path, diagnostics, identities) {
1227
1229
  identities
1228
1230
  ),
1229
1231
  ...metadataFromJson(json.metadata),
1232
+ ...typeof json.label === "string" && json.command !== "\\paragraph" ? { label: json.label } : {},
1230
1233
  ...json.centered === true ? { centered: true } : {}
1231
1234
  };
1232
1235
  }
@@ -1406,6 +1409,33 @@ function createEmptyDocument() {
1406
1409
  };
1407
1410
  }
1408
1411
 
1412
+ // src/document/bookContent.ts
1413
+ function bookContextReferences(context) {
1414
+ return (context?.references ?? []).map((entry, index) => ({
1415
+ ...referenceFromJson(entry),
1416
+ id: "book-ref-" + String(index)
1417
+ }));
1418
+ }
1419
+ function formatBookReferenceLabel(keys, document2, command, context, options = {}) {
1420
+ const targets = context.targets ?? [];
1421
+ return keys.map((key) => {
1422
+ const target = targets.find((entry) => entry.key === key || entry.manualKey === key);
1423
+ if (target?.displayLabel) return target.displayLabel;
1424
+ const local = collectDocumentReferenceTargets(document2).find((entry) => entry.manualKey === key || implicitReferenceKey(entry.kind, entry.ownerId, context.unitId) === key);
1425
+ if (local) return formatRefLabel([local.manualKey || local.key], document2, command, options);
1426
+ const heading = document2.blocks.find((block) => block.kind === "textBlock" && block.command !== "\\paragraph" && (block.label === key || implicitHeadingReferenceKey(
1427
+ block.command.slice(1),
1428
+ block.id,
1429
+ context.unitId
1430
+ ) === key));
1431
+ if (heading?.kind === "textBlock") {
1432
+ const caption = heading.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("").trim();
1433
+ return caption || heading.command.slice(1);
1434
+ }
1435
+ return command === "eqref" ? "(?)" : "?";
1436
+ }).join(options.documentDirection === "ltr" ? ", " : "\u060C");
1437
+ }
1438
+
1409
1439
  // src/editor/accentCommands.ts
1410
1440
  var ACCENT_COMMANDS = {
1411
1441
  vec: {
@@ -4569,7 +4599,7 @@ function previewInlines(field, islands, output, equationSide, document2, preview
4569
4599
  kind: "cite",
4570
4600
  id: token.id,
4571
4601
  keys: [...token.keys],
4572
- label: formatCiteLabel(token.keys, document2.references, {
4602
+ label: formatCiteLabel(token.keys, previewOptions.mode === "book-content" ? bookContextReferences(previewOptions.bookContext) : document2.references, {
4573
4603
  documentDirection: previewOptions.documentDirection,
4574
4604
  digitForm: previewOptions.digitForm
4575
4605
  })
@@ -4581,12 +4611,16 @@ function previewInlines(field, islands, output, equationSide, document2, preview
4581
4611
  id: token.id,
4582
4612
  keys: [...token.keys],
4583
4613
  refCommand: token.refCommand,
4584
- label: formatRefLabel(token.keys, document2, token.refCommand, {
4614
+ label: previewOptions.mode === "book-content" && previewOptions.bookContext ? formatBookReferenceLabel(token.keys, document2, token.refCommand, previewOptions.bookContext, {
4615
+ documentDirection: previewOptions.documentDirection,
4616
+ digitForm: previewOptions.digitForm
4617
+ }) : formatRefLabel(token.keys, document2, token.refCommand, {
4585
4618
  documentDirection: previewOptions.documentDirection,
4586
4619
  digitForm: previewOptions.digitForm
4587
4620
  })
4588
4621
  };
4589
4622
  }
4623
+ const number = collectDocumentReferenceTargets(document2).find((target) => target.kind === "eq" && target.ownerId === token.id)?.number;
4590
4624
  const island = {
4591
4625
  id: token.id,
4592
4626
  tex: mathTex(token, equationSide, previewOptions.digitForm),
@@ -4594,8 +4628,12 @@ function previewInlines(field, islands, output, equationSide, document2, preview
4594
4628
  output,
4595
4629
  editable: token.editable
4596
4630
  };
4631
+ const numberLabel = number == null ? void 0 : formatBibliographyNumber(number, {
4632
+ documentDirection: previewOptions.documentDirection,
4633
+ digitForm: previewOptions.digitForm
4634
+ });
4597
4635
  islands.push(island);
4598
- return { kind: "math", ...island };
4636
+ return { kind: "math", ...island, ...numberLabel ? { numberLabel } : {} };
4599
4637
  });
4600
4638
  }
4601
4639
  function textBlockPreview(block, islands, output, equationSide, document2, previewOptions) {
@@ -4618,12 +4656,9 @@ function textBlockPreview(block, islands, output, equationSide, document2, previ
4618
4656
  ...block.centered ? { centered: true } : {}
4619
4657
  };
4620
4658
  }
4621
- function floatNumberLabel(document2, key, previewOptions) {
4622
- if (!key || key.trim().length === 0) {
4623
- return void 0;
4624
- }
4625
- const hit = labelNumberMap(document2).get(key.trim());
4626
- if (!hit || hit.kind !== "fig" && hit.kind !== "tab") {
4659
+ function floatNumberLabel(document2, ownerId, previewOptions) {
4660
+ const hit = collectDocumentReferenceTargets(document2).find((target) => target.ownerId === ownerId);
4661
+ if (!hit || hit.number === null || hit.kind !== "fig" && hit.kind !== "tab") {
4627
4662
  return void 0;
4628
4663
  }
4629
4664
  return formatFloatCaptionTitle(hit.kind, hit.number, {
@@ -4665,7 +4700,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
4665
4700
  id: block.id,
4666
4701
  rows: block.rows.map((row) => row.map((cell) => previewInlines(cell, islands, output, equationSide, document2, previewOptions))),
4667
4702
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
4668
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
4703
+ ...label.length > 0 ? { label } : {},
4704
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
4669
4705
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
4670
4706
  };
4671
4707
  }
@@ -4679,7 +4715,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
4679
4715
  options: block.options,
4680
4716
  centered: block.centered,
4681
4717
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
4682
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
4718
+ ...label.length > 0 ? { label } : {},
4719
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
4683
4720
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
4684
4721
  };
4685
4722
  }
@@ -4733,14 +4770,16 @@ function documentPreview(document2, output = "svg", equationSide = "arabic", pre
4733
4770
  const options = {
4734
4771
  documentDirection: previewOptions.documentDirection ?? "rtl",
4735
4772
  digitForm: previewOptions.digitForm ?? (previewOptions.documentDirection === "ltr" ? "western" : "arabicIndic"),
4736
- uiLocale: previewOptions.uiLocale ?? "ar"
4773
+ uiLocale: previewOptions.uiLocale ?? "ar",
4774
+ mode: previewOptions.mode ?? "article",
4775
+ bookContext: previewOptions.bookContext
4737
4776
  };
4738
- const body = document2.blocks.map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
4777
+ const body = document2.blocks.filter((block) => options.mode !== "book-content" || block.kind !== "bibliography").map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
4739
4778
  return {
4740
4779
  blocks: [
4741
- ...articleChromePreview(document2, options.uiLocale ?? "ar", options),
4780
+ ...options.mode === "book-content" ? [] : articleChromePreview(document2, options.uiLocale ?? "ar", options),
4742
4781
  ...body,
4743
- { kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }
4782
+ ...options.mode === "book-content" ? [] : [{ kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }]
4744
4783
  ],
4745
4784
  mathIslands
4746
4785
  };
@@ -7868,7 +7907,14 @@ function PreviewInlines({ inlines, output }) {
7868
7907
  if (inline.kind === "ref") {
7869
7908
  return /* @__PURE__ */ jsx2("span", { className: "butex-document-widget__preview-cite butex-document-widget__preview-ref", children: contextualRefLabel(inlines, index, inline.label) }, inline.id);
7870
7909
  }
7871
- return /* @__PURE__ */ jsx2(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }, inline.id);
7910
+ return /* @__PURE__ */ jsxs("span", { className: "butex-document-widget__preview-math-numbered", children: [
7911
+ /* @__PURE__ */ jsx2(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }),
7912
+ inline.numberLabel ? /* @__PURE__ */ jsxs("span", { className: "butex-document-widget__preview-equation-number", children: [
7913
+ "(",
7914
+ inline.numberLabel,
7915
+ ")"
7916
+ ] }) : null
7917
+ ] }, inline.id);
7872
7918
  }) });
7873
7919
  }
7874
7920
  function isBodyPreviewBlock(block) {
@@ -8056,6 +8102,8 @@ function resolvePreviewDocument(input, uiLocale = "ar") {
8056
8102
  }
8057
8103
  }
8058
8104
  function ButexDocumentPreview({
8105
+ mode = "article",
8106
+ bookContext,
8059
8107
  document: document2,
8060
8108
  className,
8061
8109
  documentDirection = "rtl",
@@ -8074,7 +8122,9 @@ function ButexDocumentPreview({
8074
8122
  preview: documentPreview(resolved.document, mathOutput, equationSide, {
8075
8123
  documentDirection,
8076
8124
  digitForm: resolvedDigitForm,
8077
- uiLocale
8125
+ uiLocale,
8126
+ mode,
8127
+ bookContext
8078
8128
  }),
8079
8129
  error: resolved.error
8080
8130
  };
@@ -8084,12 +8134,14 @@ function ButexDocumentPreview({
8084
8134
  preview: documentPreview(createEmptyDocument(), mathOutput, equationSide, {
8085
8135
  documentDirection,
8086
8136
  digitForm: resolvedDigitForm,
8087
- uiLocale
8137
+ uiLocale,
8138
+ mode,
8139
+ bookContext
8088
8140
  }),
8089
8141
  error: `${messages.loadDocumentError}: ${detail}`
8090
8142
  };
8091
8143
  }
8092
- }, [documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
8144
+ }, [bookContext, mode, documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
8093
8145
  useEffect2(() => {
8094
8146
  injectBuTeXDocumentStyles();
8095
8147
  }, []);