@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.
@@ -667,82 +667,82 @@ function refTokenLatex(keys, refCommand) {
667
667
  const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
668
668
  return `${command}{${keys.join(",")}}`;
669
669
  }
670
- function collectDocumentLabels(document2) {
671
- const entries = [];
670
+ function implicitObjectKey(kind, ownerId, unitId) {
671
+ const hex = (value) => Array.from(new TextEncoder().encode(value), (byte) => byte.toString(16).padStart(2, "0")).join("");
672
+ const scope = unitId === void 0 ? "a" : "u" + hex(unitId);
673
+ return kind + ":butex-auto-" + scope + "-" + hex(ownerId);
674
+ }
675
+ function implicitReferenceKey(kind, ownerId, unitId) {
676
+ return implicitObjectKey(kind, ownerId, unitId);
677
+ }
678
+ function implicitHeadingReferenceKey(kind, ownerId, unitId) {
679
+ return implicitObjectKey(kind, ownerId, unitId);
680
+ }
681
+ function supportsSingleEquationNumbering(token) {
682
+ return token.display && (token.opening === "\\[" || token.opening === String.fromCharCode(36, 36) || token.opening === "\\begin{equation}" || token.opening === "\\begin{equation*}");
683
+ }
684
+ function isNumberedEquation(token) {
685
+ if (!supportsSingleEquationNumbering(token)) return false;
686
+ if (typeof token.numbered === "boolean") return token.numbered;
687
+ return token.opening === "\\begin{equation}" || Boolean(token.labelEnabled) && Boolean(token.label?.trim());
688
+ }
689
+ function isNumberedFloat(block) {
690
+ return block.captionEnabled && block.caption.trim().length > 0 || block.labelEnabled && block.label.trim().length > 0;
691
+ }
692
+ function collectDocumentReferenceTargets(document2) {
693
+ const targets = [];
672
694
  const counters = { fig: 0, tab: 0, eq: 0 };
673
- function pushEqFromField(tokens) {
674
- for (const token of tokens) {
675
- if (token.kind !== "math") {
676
- continue;
677
- }
678
- if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
679
- counters.eq += 1;
680
- entries.push({
681
- key: token.label.trim(),
682
- kind: "eq",
683
- caption: "",
684
- ownerId: token.id,
685
- number: counters.eq
686
- });
695
+ function append(kind, ownerId, caption, numbered, manualKey = "") {
696
+ if (numbered) counters[kind] += 1;
697
+ targets.push({
698
+ key: implicitReferenceKey(kind, ownerId),
699
+ kind,
700
+ ownerId,
701
+ caption,
702
+ number: numbered ? counters[kind] : null,
703
+ ...manualKey.trim() ? { manualKey: manualKey.trim() } : {}
704
+ });
705
+ }
706
+ function visitField(field) {
707
+ for (const token of field.tokens) {
708
+ if (token.kind === "math" && supportsSingleEquationNumbering(token)) {
709
+ append("eq", token.id, "", isNumberedEquation(token), token.labelEnabled ? token.label ?? "" : "");
687
710
  }
688
711
  }
689
712
  }
690
713
  function walk(blocks) {
691
714
  for (const block of blocks) {
692
- if (block.kind === "image") {
693
- if (block.labelEnabled && block.label.trim().length > 0) {
694
- counters.fig += 1;
695
- entries.push({
696
- key: block.label.trim(),
697
- kind: "fig",
698
- caption: block.captionEnabled ? block.caption : "",
699
- ownerId: block.id,
700
- number: counters.fig
701
- });
702
- }
703
- if (block.captionEnabled) {
704
- pushEqFromField(block.captionField.tokens);
705
- }
706
- }
707
- if (block.kind === "table") {
708
- if (block.labelEnabled && block.label.trim().length > 0) {
709
- counters.tab += 1;
710
- entries.push({
711
- key: block.label.trim(),
712
- kind: "tab",
713
- caption: block.captionEnabled ? block.caption : "",
714
- ownerId: block.id,
715
- number: counters.tab
716
- });
717
- }
718
- if (block.captionEnabled) {
719
- pushEqFromField(block.captionField.tokens);
720
- }
721
- for (const row of block.rows) {
722
- for (const cell of row) {
723
- pushEqFromField(cell.tokens);
724
- }
715
+ if (block.kind === "image" || block.kind === "table") {
716
+ append(
717
+ block.kind === "image" ? "fig" : "tab",
718
+ block.id,
719
+ block.captionEnabled ? block.caption : "",
720
+ isNumberedFloat(block),
721
+ block.labelEnabled ? block.label : ""
722
+ );
723
+ if (block.captionEnabled) visitField(block.captionField);
724
+ if (block.kind === "table") {
725
+ for (const row of block.rows) for (const cell of row) visitField(cell);
725
726
  }
726
- }
727
- if (block.kind === "textBlock") {
728
- pushEqFromField(block.field.tokens);
729
- }
730
- if (block.kind === "list") {
727
+ } else if (block.kind === "textBlock") {
728
+ visitField(block.field);
729
+ } else if (block.kind === "list") {
731
730
  for (const item of block.items) {
732
- pushEqFromField(item.field.tokens);
731
+ visitField(item.field);
733
732
  walk(item.blocks);
734
733
  }
735
734
  }
736
735
  }
737
736
  }
738
737
  walk(document2.blocks);
739
- return entries;
738
+ return targets;
740
739
  }
741
740
  function labelNumberMap(document2) {
742
741
  const map = /* @__PURE__ */ new Map();
743
- for (const entry of collectDocumentLabels(document2)) {
744
- if (!map.has(entry.key)) {
745
- map.set(entry.key, { kind: entry.kind, number: entry.number });
742
+ for (const target of collectDocumentReferenceTargets(document2)) {
743
+ if (target.number === null) continue;
744
+ for (const key of [target.key, target.manualKey]) {
745
+ if (key && !map.has(key)) map.set(key, { kind: target.kind, number: target.number });
746
746
  }
747
747
  }
748
748
  return map;
@@ -1144,7 +1144,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1144
1144
  editable: true,
1145
1145
  sourceOwner: structuredMathJson.source_owner === "editor" ? "editor" : "imported-structured",
1146
1146
  ...typeof structuredMathJson.label_enabled === "boolean" ? { labelEnabled: structuredMathJson.label_enabled } : {},
1147
- ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {}
1147
+ ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {},
1148
+ ...typeof structuredMathJson.numbered === "boolean" ? { numbered: structuredMathJson.numbered } : {}
1148
1149
  });
1149
1150
  } else {
1150
1151
  tokens.push({
@@ -1159,7 +1160,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1159
1160
  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",
1160
1161
  sourceOwner: "raw",
1161
1162
  ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label_enabled === "boolean" ? { labelEnabled: mathJson.label_enabled } : {},
1162
- ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {}
1163
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {},
1164
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.numbered === "boolean" ? { numbered: mathJson.numbered } : {}
1163
1165
  });
1164
1166
  }
1165
1167
  index = span.end;
@@ -1256,6 +1258,7 @@ function parseTextBlock(json, options, path, diagnostics, identities) {
1256
1258
  identities
1257
1259
  ),
1258
1260
  ...metadataFromJson(json.metadata),
1261
+ ...typeof json.label === "string" && json.command !== "\\paragraph" ? { label: json.label } : {},
1259
1262
  ...json.centered === true ? { centered: true } : {}
1260
1263
  };
1261
1264
  }
@@ -1435,6 +1438,33 @@ function createEmptyDocument() {
1435
1438
  };
1436
1439
  }
1437
1440
 
1441
+ // src/document/bookContent.ts
1442
+ function bookContextReferences(context) {
1443
+ return (context?.references ?? []).map((entry, index) => ({
1444
+ ...referenceFromJson(entry),
1445
+ id: "book-ref-" + String(index)
1446
+ }));
1447
+ }
1448
+ function formatBookReferenceLabel(keys, document2, command, context, options = {}) {
1449
+ const targets = context.targets ?? [];
1450
+ return keys.map((key) => {
1451
+ const target = targets.find((entry) => entry.key === key || entry.manualKey === key);
1452
+ if (target?.displayLabel) return target.displayLabel;
1453
+ const local = collectDocumentReferenceTargets(document2).find((entry) => entry.manualKey === key || implicitReferenceKey(entry.kind, entry.ownerId, context.unitId) === key);
1454
+ if (local) return formatRefLabel([local.manualKey || local.key], document2, command, options);
1455
+ const heading = document2.blocks.find((block) => block.kind === "textBlock" && block.command !== "\\paragraph" && (block.label === key || implicitHeadingReferenceKey(
1456
+ block.command.slice(1),
1457
+ block.id,
1458
+ context.unitId
1459
+ ) === key));
1460
+ if (heading?.kind === "textBlock") {
1461
+ const caption = heading.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("").trim();
1462
+ return caption || heading.command.slice(1);
1463
+ }
1464
+ return command === "eqref" ? "(?)" : "?";
1465
+ }).join(options.documentDirection === "ltr" ? ", " : "\u060C");
1466
+ }
1467
+
1438
1468
  // src/editor/accentCommands.ts
1439
1469
  var ACCENT_COMMANDS = {
1440
1470
  vec: {
@@ -4598,7 +4628,7 @@ function previewInlines(field, islands, output, equationSide, document2, preview
4598
4628
  kind: "cite",
4599
4629
  id: token.id,
4600
4630
  keys: [...token.keys],
4601
- label: formatCiteLabel(token.keys, document2.references, {
4631
+ label: formatCiteLabel(token.keys, previewOptions.mode === "book-content" ? bookContextReferences(previewOptions.bookContext) : document2.references, {
4602
4632
  documentDirection: previewOptions.documentDirection,
4603
4633
  digitForm: previewOptions.digitForm
4604
4634
  })
@@ -4610,12 +4640,16 @@ function previewInlines(field, islands, output, equationSide, document2, preview
4610
4640
  id: token.id,
4611
4641
  keys: [...token.keys],
4612
4642
  refCommand: token.refCommand,
4613
- label: formatRefLabel(token.keys, document2, token.refCommand, {
4643
+ label: previewOptions.mode === "book-content" && previewOptions.bookContext ? formatBookReferenceLabel(token.keys, document2, token.refCommand, previewOptions.bookContext, {
4644
+ documentDirection: previewOptions.documentDirection,
4645
+ digitForm: previewOptions.digitForm
4646
+ }) : formatRefLabel(token.keys, document2, token.refCommand, {
4614
4647
  documentDirection: previewOptions.documentDirection,
4615
4648
  digitForm: previewOptions.digitForm
4616
4649
  })
4617
4650
  };
4618
4651
  }
4652
+ const number = collectDocumentReferenceTargets(document2).find((target) => target.kind === "eq" && target.ownerId === token.id)?.number;
4619
4653
  const island = {
4620
4654
  id: token.id,
4621
4655
  tex: mathTex(token, equationSide, previewOptions.digitForm),
@@ -4623,8 +4657,12 @@ function previewInlines(field, islands, output, equationSide, document2, preview
4623
4657
  output,
4624
4658
  editable: token.editable
4625
4659
  };
4660
+ const numberLabel = number == null ? void 0 : formatBibliographyNumber(number, {
4661
+ documentDirection: previewOptions.documentDirection,
4662
+ digitForm: previewOptions.digitForm
4663
+ });
4626
4664
  islands.push(island);
4627
- return { kind: "math", ...island };
4665
+ return { kind: "math", ...island, ...numberLabel ? { numberLabel } : {} };
4628
4666
  });
4629
4667
  }
4630
4668
  function textBlockPreview(block, islands, output, equationSide, document2, previewOptions) {
@@ -4647,12 +4685,9 @@ function textBlockPreview(block, islands, output, equationSide, document2, previ
4647
4685
  ...block.centered ? { centered: true } : {}
4648
4686
  };
4649
4687
  }
4650
- function floatNumberLabel(document2, key, previewOptions) {
4651
- if (!key || key.trim().length === 0) {
4652
- return void 0;
4653
- }
4654
- const hit = labelNumberMap(document2).get(key.trim());
4655
- if (!hit || hit.kind !== "fig" && hit.kind !== "tab") {
4688
+ function floatNumberLabel(document2, ownerId, previewOptions) {
4689
+ const hit = collectDocumentReferenceTargets(document2).find((target) => target.ownerId === ownerId);
4690
+ if (!hit || hit.number === null || hit.kind !== "fig" && hit.kind !== "tab") {
4656
4691
  return void 0;
4657
4692
  }
4658
4693
  return formatFloatCaptionTitle(hit.kind, hit.number, {
@@ -4694,7 +4729,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
4694
4729
  id: block.id,
4695
4730
  rows: block.rows.map((row) => row.map((cell) => previewInlines(cell, islands, output, equationSide, document2, previewOptions))),
4696
4731
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
4697
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
4732
+ ...label.length > 0 ? { label } : {},
4733
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
4698
4734
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
4699
4735
  };
4700
4736
  }
@@ -4708,7 +4744,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
4708
4744
  options: block.options,
4709
4745
  centered: block.centered,
4710
4746
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
4711
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
4747
+ ...label.length > 0 ? { label } : {},
4748
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
4712
4749
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
4713
4750
  };
4714
4751
  }
@@ -4762,14 +4799,16 @@ function documentPreview(document2, output = "svg", equationSide = "arabic", pre
4762
4799
  const options = {
4763
4800
  documentDirection: previewOptions.documentDirection ?? "rtl",
4764
4801
  digitForm: previewOptions.digitForm ?? (previewOptions.documentDirection === "ltr" ? "western" : "arabicIndic"),
4765
- uiLocale: previewOptions.uiLocale ?? "ar"
4802
+ uiLocale: previewOptions.uiLocale ?? "ar",
4803
+ mode: previewOptions.mode ?? "article",
4804
+ bookContext: previewOptions.bookContext
4766
4805
  };
4767
- const body = document2.blocks.map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
4806
+ const body = document2.blocks.filter((block) => options.mode !== "book-content" || block.kind !== "bibliography").map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
4768
4807
  return {
4769
4808
  blocks: [
4770
- ...articleChromePreview(document2, options.uiLocale ?? "ar", options),
4809
+ ...options.mode === "book-content" ? [] : articleChromePreview(document2, options.uiLocale ?? "ar", options),
4771
4810
  ...body,
4772
- { kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }
4811
+ ...options.mode === "book-content" ? [] : [{ kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }]
4773
4812
  ],
4774
4813
  mathIslands
4775
4814
  };
@@ -7897,7 +7936,14 @@ function PreviewInlines({ inlines, output }) {
7897
7936
  if (inline.kind === "ref") {
7898
7937
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { className: "butex-document-widget__preview-cite butex-document-widget__preview-ref", children: contextualRefLabel(inlines, index, inline.label) }, inline.id);
7899
7938
  }
7900
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }, inline.id);
7939
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "butex-document-widget__preview-math-numbered", children: [
7940
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }),
7941
+ inline.numberLabel ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "butex-document-widget__preview-equation-number", children: [
7942
+ "(",
7943
+ inline.numberLabel,
7944
+ ")"
7945
+ ] }) : null
7946
+ ] }, inline.id);
7901
7947
  }) });
7902
7948
  }
7903
7949
  function isBodyPreviewBlock(block) {
@@ -8085,6 +8131,8 @@ function resolvePreviewDocument(input, uiLocale = "ar") {
8085
8131
  }
8086
8132
  }
8087
8133
  function ButexDocumentPreview({
8134
+ mode = "article",
8135
+ bookContext,
8088
8136
  document: document2,
8089
8137
  className,
8090
8138
  documentDirection = "rtl",
@@ -8103,7 +8151,9 @@ function ButexDocumentPreview({
8103
8151
  preview: documentPreview(resolved.document, mathOutput, equationSide, {
8104
8152
  documentDirection,
8105
8153
  digitForm: resolvedDigitForm,
8106
- uiLocale
8154
+ uiLocale,
8155
+ mode,
8156
+ bookContext
8107
8157
  }),
8108
8158
  error: resolved.error
8109
8159
  };
@@ -8113,12 +8163,14 @@ function ButexDocumentPreview({
8113
8163
  preview: documentPreview(createEmptyDocument(), mathOutput, equationSide, {
8114
8164
  documentDirection,
8115
8165
  digitForm: resolvedDigitForm,
8116
- uiLocale
8166
+ uiLocale,
8167
+ mode,
8168
+ bookContext
8117
8169
  }),
8118
8170
  error: `${messages.loadDocumentError}: ${detail}`
8119
8171
  };
8120
8172
  }
8121
- }, [documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
8173
+ }, [bookContext, mode, documentDirection, equationSide, mathOutput, messages.loadDocumentError, resolved, resolvedDigitForm, uiLocale]);
8122
8174
  (0, import_react2.useEffect)(() => {
8123
8175
  injectBuTeXDocumentStyles();
8124
8176
  }, []);