@drghaliasri/butex 8.0.3 → 8.1.0

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.
@@ -74,12 +74,14 @@ type MathObject2Json = {
74
74
  source_owner?: 'imported-structured' | 'editor';
75
75
  label_enabled?: boolean;
76
76
  label?: string;
77
+ numbered?: boolean;
77
78
  };
78
79
  /** Positional metadata for a raw-only span that has no structured equation AST. */
79
80
  type RawMathObject2Json = {
80
81
  node_type: 'RawMathObject';
81
82
  label_enabled?: boolean;
82
83
  label?: string;
84
+ numbered?: boolean;
83
85
  };
84
86
  type DocumentMathObjectJson = MathObject2Json | RawMathObject2Json;
85
87
  type ReferenceFieldSeparator = ',' | '،';
@@ -211,6 +213,8 @@ type MathToken2 = {
211
213
  /** When true on display math, export as equation env with \\label. */
212
214
  labelEnabled?: boolean;
213
215
  label?: string;
216
+ /** Independent display-equation numbering override; absent means legacy semantics. */
217
+ numbered?: boolean;
214
218
  };
215
219
  type CiteToken2 = {
216
220
  id: string;
@@ -74,12 +74,14 @@ type MathObject2Json = {
74
74
  source_owner?: 'imported-structured' | 'editor';
75
75
  label_enabled?: boolean;
76
76
  label?: string;
77
+ numbered?: boolean;
77
78
  };
78
79
  /** Positional metadata for a raw-only span that has no structured equation AST. */
79
80
  type RawMathObject2Json = {
80
81
  node_type: 'RawMathObject';
81
82
  label_enabled?: boolean;
82
83
  label?: string;
84
+ numbered?: boolean;
83
85
  };
84
86
  type DocumentMathObjectJson = MathObject2Json | RawMathObject2Json;
85
87
  type ReferenceFieldSeparator = ',' | '،';
@@ -211,6 +213,8 @@ type MathToken2 = {
211
213
  /** When true on display math, export as equation env with \\label. */
212
214
  labelEnabled?: boolean;
213
215
  label?: string;
216
+ /** Independent display-equation numbering override; absent means legacy semantics. */
217
+ numbered?: boolean;
214
218
  };
215
219
  type CiteToken2 = {
216
220
  id: string;
@@ -667,82 +667,76 @@ 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 implicitReferenceKey(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 supportsSingleEquationNumbering(token) {
676
+ return token.display && (token.opening === "\\[" || token.opening === String.fromCharCode(36, 36) || token.opening === "\\begin{equation}" || token.opening === "\\begin{equation*}");
677
+ }
678
+ function isNumberedEquation(token) {
679
+ if (!supportsSingleEquationNumbering(token)) return false;
680
+ if (typeof token.numbered === "boolean") return token.numbered;
681
+ return token.opening === "\\begin{equation}" || Boolean(token.labelEnabled) && Boolean(token.label?.trim());
682
+ }
683
+ function isNumberedFloat(block) {
684
+ return block.captionEnabled && block.caption.trim().length > 0 || block.labelEnabled && block.label.trim().length > 0;
685
+ }
686
+ function collectDocumentReferenceTargets(document2) {
687
+ const targets = [];
672
688
  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
- });
689
+ function append(kind, ownerId, caption, numbered, manualKey = "") {
690
+ if (numbered) counters[kind] += 1;
691
+ targets.push({
692
+ key: implicitReferenceKey(kind, ownerId),
693
+ kind,
694
+ ownerId,
695
+ caption,
696
+ number: numbered ? counters[kind] : null,
697
+ ...manualKey.trim() ? { manualKey: manualKey.trim() } : {}
698
+ });
699
+ }
700
+ function visitField(field) {
701
+ for (const token of field.tokens) {
702
+ if (token.kind === "math" && supportsSingleEquationNumbering(token)) {
703
+ append("eq", token.id, "", isNumberedEquation(token), token.labelEnabled ? token.label ?? "" : "");
687
704
  }
688
705
  }
689
706
  }
690
707
  function walk(blocks) {
691
708
  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);
709
+ if (block.kind === "image" || block.kind === "table") {
710
+ append(
711
+ block.kind === "image" ? "fig" : "tab",
712
+ block.id,
713
+ block.captionEnabled ? block.caption : "",
714
+ isNumberedFloat(block),
715
+ block.labelEnabled ? block.label : ""
716
+ );
717
+ if (block.captionEnabled) visitField(block.captionField);
718
+ if (block.kind === "table") {
719
+ for (const row of block.rows) for (const cell of row) visitField(cell);
705
720
  }
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
- }
725
- }
726
- }
727
- if (block.kind === "textBlock") {
728
- pushEqFromField(block.field.tokens);
729
- }
730
- if (block.kind === "list") {
721
+ } else if (block.kind === "textBlock") {
722
+ visitField(block.field);
723
+ } else if (block.kind === "list") {
731
724
  for (const item of block.items) {
732
- pushEqFromField(item.field.tokens);
725
+ visitField(item.field);
733
726
  walk(item.blocks);
734
727
  }
735
728
  }
736
729
  }
737
730
  }
738
731
  walk(document2.blocks);
739
- return entries;
732
+ return targets;
740
733
  }
741
734
  function labelNumberMap(document2) {
742
735
  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 });
736
+ for (const target of collectDocumentReferenceTargets(document2)) {
737
+ if (target.number === null) continue;
738
+ for (const key of [target.key, target.manualKey]) {
739
+ if (key && !map.has(key)) map.set(key, { kind: target.kind, number: target.number });
746
740
  }
747
741
  }
748
742
  return map;
@@ -1144,7 +1138,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1144
1138
  editable: true,
1145
1139
  sourceOwner: structuredMathJson.source_owner === "editor" ? "editor" : "imported-structured",
1146
1140
  ...typeof structuredMathJson.label_enabled === "boolean" ? { labelEnabled: structuredMathJson.label_enabled } : {},
1147
- ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {}
1141
+ ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {},
1142
+ ...typeof structuredMathJson.numbered === "boolean" ? { numbered: structuredMathJson.numbered } : {}
1148
1143
  });
1149
1144
  } else {
1150
1145
  tokens.push({
@@ -1159,7 +1154,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1159
1154
  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
1155
  sourceOwner: "raw",
1161
1156
  ...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 } : {}
1157
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {},
1158
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.numbered === "boolean" ? { numbered: mathJson.numbered } : {}
1163
1159
  });
1164
1160
  }
1165
1161
  index = span.end;
@@ -4616,6 +4612,7 @@ function previewInlines(field, islands, output, equationSide, document2, preview
4616
4612
  })
4617
4613
  };
4618
4614
  }
4615
+ const number = collectDocumentReferenceTargets(document2).find((target) => target.kind === "eq" && target.ownerId === token.id)?.number;
4619
4616
  const island = {
4620
4617
  id: token.id,
4621
4618
  tex: mathTex(token, equationSide, previewOptions.digitForm),
@@ -4623,8 +4620,12 @@ function previewInlines(field, islands, output, equationSide, document2, preview
4623
4620
  output,
4624
4621
  editable: token.editable
4625
4622
  };
4623
+ const numberLabel = number == null ? void 0 : formatBibliographyNumber(number, {
4624
+ documentDirection: previewOptions.documentDirection,
4625
+ digitForm: previewOptions.digitForm
4626
+ });
4626
4627
  islands.push(island);
4627
- return { kind: "math", ...island };
4628
+ return { kind: "math", ...island, ...numberLabel ? { numberLabel } : {} };
4628
4629
  });
4629
4630
  }
4630
4631
  function textBlockPreview(block, islands, output, equationSide, document2, previewOptions) {
@@ -4647,12 +4648,9 @@ function textBlockPreview(block, islands, output, equationSide, document2, previ
4647
4648
  ...block.centered ? { centered: true } : {}
4648
4649
  };
4649
4650
  }
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") {
4651
+ function floatNumberLabel(document2, ownerId, previewOptions) {
4652
+ const hit = collectDocumentReferenceTargets(document2).find((target) => target.ownerId === ownerId);
4653
+ if (!hit || hit.number === null || hit.kind !== "fig" && hit.kind !== "tab") {
4656
4654
  return void 0;
4657
4655
  }
4658
4656
  return formatFloatCaptionTitle(hit.kind, hit.number, {
@@ -4694,7 +4692,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
4694
4692
  id: block.id,
4695
4693
  rows: block.rows.map((row) => row.map((cell) => previewInlines(cell, islands, output, equationSide, document2, previewOptions))),
4696
4694
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
4697
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
4695
+ ...label.length > 0 ? { label } : {},
4696
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
4698
4697
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
4699
4698
  };
4700
4699
  }
@@ -4708,7 +4707,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
4708
4707
  options: block.options,
4709
4708
  centered: block.centered,
4710
4709
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
4711
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
4710
+ ...label.length > 0 ? { label } : {},
4711
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
4712
4712
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
4713
4713
  };
4714
4714
  }
@@ -7897,7 +7897,14 @@ function PreviewInlines({ inlines, output }) {
7897
7897
  if (inline.kind === "ref") {
7898
7898
  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
7899
  }
7900
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }, inline.id);
7900
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "butex-document-widget__preview-math-numbered", children: [
7901
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }),
7902
+ inline.numberLabel ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { className: "butex-document-widget__preview-equation-number", children: [
7903
+ "(",
7904
+ inline.numberLabel,
7905
+ ")"
7906
+ ] }) : null
7907
+ ] }, inline.id);
7901
7908
  }) });
7902
7909
  }
7903
7910
  function isBodyPreviewBlock(block) {