@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.
package/dist/document.js CHANGED
@@ -59,7 +59,9 @@ __export(public_exports, {
59
59
  clearDocumentImageAsset: () => clearDocumentImageAsset,
60
60
  cloneDocumentMeta: () => cloneDocumentMeta,
61
61
  cloneDocumentNode: () => cloneDocumentNode,
62
+ collectDocumentKeyLabels: () => collectDocumentKeyLabels,
62
63
  collectDocumentLabels: () => collectDocumentLabels,
64
+ collectDocumentReferenceTargets: () => collectDocumentReferenceTargets,
63
65
  createDocumentHistory: () => createDocumentHistory,
64
66
  createEmptyArabicEquationSession: () => createEmptyArabicEquationSession,
65
67
  createEmptyDocument: () => createEmptyDocument,
@@ -97,6 +99,7 @@ __export(public_exports, {
97
99
  getDocumentReferences: () => getDocumentReferences,
98
100
  hasXeLatexUnsupportedEmoji: () => hasXeLatexUnsupportedEmoji,
99
101
  hijriMonthLabel: () => hijriMonthLabel,
102
+ implicitReferenceKey: () => implicitReferenceKey,
100
103
  inlineFieldLatex: () => inlineFieldLatex,
101
104
  insertCiteTokenAtCaret: () => insertCiteTokenAtCaret,
102
105
  insertDocumentBlockAfter: () => insertDocumentBlockAfter,
@@ -105,6 +108,8 @@ __export(public_exports, {
105
108
  insertRefTokenAtCaret: () => insertRefTokenAtCaret,
106
109
  isDocumentValid: () => isDocumentValid,
107
110
  isDuplicateDocumentKey: () => isDuplicateDocumentKey,
111
+ isNumberedEquation: () => isNumberedEquation,
112
+ isNumberedFloat: () => isNumberedFloat,
108
113
  isValidDocumentKey: () => isValidDocumentKey,
109
114
  labelNumberMap: () => labelNumberMap,
110
115
  mathNodeFromArabicSession: () => mathNodeFromArabicSession,
@@ -1015,82 +1020,85 @@ function refTokenLatex(keys, refCommand) {
1015
1020
  const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
1016
1021
  return `${command}{${keys.join(",")}}`;
1017
1022
  }
1018
- function collectDocumentLabels(document2) {
1019
- const entries = [];
1023
+ function implicitReferenceKey(kind, ownerId, unitId) {
1024
+ const hex = (value) => Array.from(new TextEncoder().encode(value), (byte) => byte.toString(16).padStart(2, "0")).join("");
1025
+ const scope = unitId === void 0 ? "a" : "u" + hex(unitId);
1026
+ return kind + ":butex-auto-" + scope + "-" + hex(ownerId);
1027
+ }
1028
+ function supportsSingleEquationNumbering(token) {
1029
+ return token.display && (token.opening === "\\[" || token.opening === String.fromCharCode(36, 36) || token.opening === "\\begin{equation}" || token.opening === "\\begin{equation*}");
1030
+ }
1031
+ function isNumberedEquation(token) {
1032
+ if (!supportsSingleEquationNumbering(token)) return false;
1033
+ if (typeof token.numbered === "boolean") return token.numbered;
1034
+ return token.opening === "\\begin{equation}" || Boolean(token.labelEnabled) && Boolean(token.label?.trim());
1035
+ }
1036
+ function isNumberedFloat(block) {
1037
+ return block.captionEnabled && block.caption.trim().length > 0 || block.labelEnabled && block.label.trim().length > 0;
1038
+ }
1039
+ function collectDocumentReferenceTargets(document2) {
1040
+ const targets = [];
1020
1041
  const counters = { fig: 0, tab: 0, eq: 0 };
1021
- function pushEqFromField(tokens) {
1022
- for (const token of tokens) {
1023
- if (token.kind !== "math") {
1024
- continue;
1025
- }
1026
- if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
1027
- counters.eq += 1;
1028
- entries.push({
1029
- key: token.label.trim(),
1030
- kind: "eq",
1031
- caption: "",
1032
- ownerId: token.id,
1033
- number: counters.eq
1034
- });
1042
+ function append(kind, ownerId, caption, numbered, manualKey = "") {
1043
+ if (numbered) counters[kind] += 1;
1044
+ targets.push({
1045
+ key: implicitReferenceKey(kind, ownerId),
1046
+ kind,
1047
+ ownerId,
1048
+ caption,
1049
+ number: numbered ? counters[kind] : null,
1050
+ ...manualKey.trim() ? { manualKey: manualKey.trim() } : {}
1051
+ });
1052
+ }
1053
+ function visitField(field) {
1054
+ for (const token of field.tokens) {
1055
+ if (token.kind === "math" && supportsSingleEquationNumbering(token)) {
1056
+ append("eq", token.id, "", isNumberedEquation(token), token.labelEnabled ? token.label ?? "" : "");
1035
1057
  }
1036
1058
  }
1037
1059
  }
1038
1060
  function walk(blocks) {
1039
1061
  for (const block of blocks) {
1040
- if (block.kind === "image") {
1041
- if (block.labelEnabled && block.label.trim().length > 0) {
1042
- counters.fig += 1;
1043
- entries.push({
1044
- key: block.label.trim(),
1045
- kind: "fig",
1046
- caption: block.captionEnabled ? block.caption : "",
1047
- ownerId: block.id,
1048
- number: counters.fig
1049
- });
1050
- }
1051
- if (block.captionEnabled) {
1052
- pushEqFromField(block.captionField.tokens);
1053
- }
1054
- }
1055
- if (block.kind === "table") {
1056
- if (block.labelEnabled && block.label.trim().length > 0) {
1057
- counters.tab += 1;
1058
- entries.push({
1059
- key: block.label.trim(),
1060
- kind: "tab",
1061
- caption: block.captionEnabled ? block.caption : "",
1062
- ownerId: block.id,
1063
- number: counters.tab
1064
- });
1065
- }
1066
- if (block.captionEnabled) {
1067
- pushEqFromField(block.captionField.tokens);
1068
- }
1069
- for (const row of block.rows) {
1070
- for (const cell of row) {
1071
- pushEqFromField(cell.tokens);
1072
- }
1062
+ if (block.kind === "image" || block.kind === "table") {
1063
+ append(
1064
+ block.kind === "image" ? "fig" : "tab",
1065
+ block.id,
1066
+ block.captionEnabled ? block.caption : "",
1067
+ isNumberedFloat(block),
1068
+ block.labelEnabled ? block.label : ""
1069
+ );
1070
+ if (block.captionEnabled) visitField(block.captionField);
1071
+ if (block.kind === "table") {
1072
+ for (const row of block.rows) for (const cell of row) visitField(cell);
1073
1073
  }
1074
- }
1075
- if (block.kind === "textBlock") {
1076
- pushEqFromField(block.field.tokens);
1077
- }
1078
- if (block.kind === "list") {
1074
+ } else if (block.kind === "textBlock") {
1075
+ visitField(block.field);
1076
+ } else if (block.kind === "list") {
1079
1077
  for (const item of block.items) {
1080
- pushEqFromField(item.field.tokens);
1078
+ visitField(item.field);
1081
1079
  walk(item.blocks);
1082
1080
  }
1083
1081
  }
1084
1082
  }
1085
1083
  }
1086
1084
  walk(document2.blocks);
1087
- return entries;
1085
+ return targets;
1086
+ }
1087
+ function collectDocumentLabels(document2) {
1088
+ return collectDocumentReferenceTargets(document2).filter((target) => target.number !== null && Boolean(target.manualKey)).map(({ manualKey, kind, caption, ownerId, number }) => ({ key: manualKey, kind, caption, ownerId, number }));
1089
+ }
1090
+ function collectDocumentKeyLabels(document2) {
1091
+ return collectDocumentReferenceTargets(document2).filter((target) => target.number !== null).flatMap((target) => [
1092
+ { key: target.key, ownerId: target.ownerId },
1093
+ ...target.manualKey && target.manualKey !== target.key ? [{ key: target.manualKey, ownerId: target.ownerId }] : []
1094
+ ]);
1088
1095
  }
1089
1096
  function labelNumberMap(document2) {
1090
1097
  const map = /* @__PURE__ */ new Map();
1091
- for (const entry of collectDocumentLabels(document2)) {
1092
- if (!map.has(entry.key)) {
1093
- map.set(entry.key, { kind: entry.kind, number: entry.number });
1098
+ for (const target of collectDocumentReferenceTargets(document2)) {
1099
+ if (target.number === null) continue;
1100
+ for (const key of [target.key, target.manualKey]) {
1101
+ if (key && !map.has(key)) map.set(key, { kind: target.kind, number: target.number });
1094
1102
  }
1095
1103
  }
1096
1104
  return map;
@@ -1144,8 +1152,12 @@ function stripDisplayMathDelimiters(source) {
1144
1152
  if (trimmed.startsWith("$$") && trimmed.endsWith("$$")) {
1145
1153
  return trimmed.slice(2, -2).trim();
1146
1154
  }
1147
- if (trimmed.startsWith("\\begin{equation}") && trimmed.endsWith("\\end{equation}")) {
1148
- return trimmed.slice("\\begin{equation}".length, -"\\end{equation}".length).trim();
1155
+ for (const environment of ["equation", "equation*"]) {
1156
+ const opening = `\\begin{${environment}}`;
1157
+ const closing = `\\end{${environment}}`;
1158
+ if (trimmed.startsWith(opening) && trimmed.endsWith(closing)) {
1159
+ return trimmed.slice(opening.length, -closing.length).trim();
1160
+ }
1149
1161
  }
1150
1162
  return trimmed;
1151
1163
  }
@@ -1538,7 +1550,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1538
1550
  editable: true,
1539
1551
  sourceOwner: structuredMathJson.source_owner === "editor" ? "editor" : "imported-structured",
1540
1552
  ...typeof structuredMathJson.label_enabled === "boolean" ? { labelEnabled: structuredMathJson.label_enabled } : {},
1541
- ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {}
1553
+ ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {},
1554
+ ...typeof structuredMathJson.numbered === "boolean" ? { numbered: structuredMathJson.numbered } : {}
1542
1555
  });
1543
1556
  } else {
1544
1557
  tokens.push({
@@ -1553,7 +1566,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
1553
1566
  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",
1554
1567
  sourceOwner: "raw",
1555
1568
  ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label_enabled === "boolean" ? { labelEnabled: mathJson.label_enabled } : {},
1556
- ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {}
1569
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {},
1570
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.numbered === "boolean" ? { numbered: mathJson.numbered } : {}
1557
1571
  });
1558
1572
  }
1559
1573
  index = span.end;
@@ -5607,6 +5621,9 @@ function replaceMathTokenFromSession(document2, tokenId, session, opening, closi
5607
5621
  if (current.label !== void 0) {
5608
5622
  replaced.label = current.label;
5609
5623
  }
5624
+ if (current.numbered !== void 0) {
5625
+ replaced.numbered = current.numbered;
5626
+ }
5610
5627
  field.tokens[index] = replaced;
5611
5628
  return true;
5612
5629
  });
@@ -5831,6 +5848,9 @@ function updateMathTokenLabel(document2, tokenId, patch) {
5831
5848
  if (typeof patch.label === "string") {
5832
5849
  token.label = patch.label;
5833
5850
  }
5851
+ if (typeof patch.numbered === "boolean") {
5852
+ token.numbered = patch.numbered;
5853
+ }
5834
5854
  return true;
5835
5855
  });
5836
5856
  return next;
@@ -6139,7 +6159,7 @@ function normalizedNewFloatLabel(document2, label) {
6139
6159
  if (!normalized) return "";
6140
6160
  const error = documentKeyError(normalized, {
6141
6161
  references: document2.references,
6142
- labels: collectDocumentLabels(document2)
6162
+ labels: collectDocumentKeyLabels(document2)
6143
6163
  });
6144
6164
  if (error === "duplicate") {
6145
6165
  throw new DocumentCommandError("duplicate_key", "Float label is already in use");
@@ -6583,7 +6603,7 @@ function validateReferenceKey(document2, key, excludeReferenceId) {
6583
6603
  const normalized = normalizeDocumentKey(key);
6584
6604
  const error = documentKeyError(normalized, {
6585
6605
  references: document2.references,
6586
- labels: collectDocumentLabels(document2),
6606
+ labels: collectDocumentKeyLabels(document2),
6587
6607
  ...excludeReferenceId ? { excludeReferenceId } : {}
6588
6608
  });
6589
6609
  if (error === "duplicate") {
@@ -6805,6 +6825,17 @@ function parseToken(value) {
6805
6825
  throw new DocumentCommandError("invalid_inline_token", "Unsupported inline token kind");
6806
6826
  }
6807
6827
  function parseInlineCommand(value) {
6828
+ if (value.op === "set_equation_numbering") {
6829
+ if (typeof value.numbered !== "boolean") {
6830
+ throw new DocumentCommandError("invalid_command", "numbered must be boolean");
6831
+ }
6832
+ return {
6833
+ op: "set_equation_numbering",
6834
+ field_id: requireString2(value.field_id, "field_id", false).trim(),
6835
+ token_id: requireString2(value.token_id, "token_id", false).trim(),
6836
+ numbered: value.numbered
6837
+ };
6838
+ }
6808
6839
  if (value.op === "insert_inline_token") {
6809
6840
  return {
6810
6841
  op: value.op,
@@ -6858,7 +6889,8 @@ function liveToken(input, id) {
6858
6889
  editable: true,
6859
6890
  sourceOwner: mathObject.source_owner === "editor" ? "editor" : "imported-structured",
6860
6891
  ...mathObject.label_enabled !== void 0 ? { labelEnabled: mathObject.label_enabled } : {},
6861
- ...mathObject.label !== void 0 ? { label: mathObject.label } : {}
6892
+ ...mathObject.label !== void 0 ? { label: mathObject.label } : {},
6893
+ ...mathObject.numbered !== void 0 ? { numbered: mathObject.numbered } : {}
6862
6894
  };
6863
6895
  }
6864
6896
  function insertionIndex(tokens, anchor) {
@@ -6880,6 +6912,17 @@ function applyInlineCommand(document2, command) {
6880
6912
  if (!field) {
6881
6913
  throw new DocumentCommandError("field_not_found", "Inline field was not found");
6882
6914
  }
6915
+ if (command.op === "set_equation_numbering") {
6916
+ const token = field.tokens.find((entry) => entry.id === command.token_id);
6917
+ if (!token || token.kind !== "math") {
6918
+ throw new DocumentCommandError("token_not_found", "Equation token was not found");
6919
+ }
6920
+ if (!supportsSingleEquationNumbering(token)) {
6921
+ throw new DocumentCommandError("invalid_inline_token", "Only a single display equation can be numbered");
6922
+ }
6923
+ token.numbered = command.numbered;
6924
+ return;
6925
+ }
6883
6926
  if (command.op === "insert_inline_token") {
6884
6927
  const index2 = insertionIndex(field.tokens, command.anchor);
6885
6928
  field.tokens.splice(index2, 0, liveToken(command.token, newTokenId(document2, command.token.kind)));
@@ -7232,12 +7275,13 @@ function serializeField(field) {
7232
7275
  }
7233
7276
  value += token.source;
7234
7277
  if (!token.math || token.sourceOwner === "raw") {
7235
- if (token.labelEnabled !== void 0 || token.label !== void 0) {
7278
+ if (token.labelEnabled !== void 0 || token.label !== void 0 || token.numbered !== void 0) {
7236
7279
  hasPersistedMath = true;
7237
7280
  mathObjects.push({
7238
7281
  node_type: "RawMathObject",
7239
7282
  ...token.labelEnabled !== void 0 ? { label_enabled: token.labelEnabled } : {},
7240
- ...token.label !== void 0 ? { label: token.label } : {}
7283
+ ...token.label !== void 0 ? { label: token.label } : {},
7284
+ ...token.numbered !== void 0 ? { numbered: token.numbered } : {}
7241
7285
  });
7242
7286
  } else {
7243
7287
  mathObjects.push(null);
@@ -7250,7 +7294,8 @@ function serializeField(field) {
7250
7294
  ...token.sourceSide ? { source_side: token.sourceSide } : {},
7251
7295
  source_owner: token.sourceOwner,
7252
7296
  ...token.labelEnabled !== void 0 ? { label_enabled: token.labelEnabled } : {},
7253
- ...token.label !== void 0 ? { label: token.label } : {}
7297
+ ...token.label !== void 0 ? { label: token.label } : {},
7298
+ ...token.numbered !== void 0 ? { numbered: token.numbered } : {}
7254
7299
  });
7255
7300
  }
7256
7301
  return { value, formats, mathObjects, hasPersistedMath, inlineIds: inlineIdsFromField(field) };
@@ -7383,7 +7428,7 @@ function toDocumentJson(document2) {
7383
7428
 
7384
7429
  // src/document/jsonCommands.ts
7385
7430
  function isInlineCommand(command) {
7386
- return command.op === "insert_inline_token" || command.op === "replace_inline_token" || command.op === "remove_inline_token";
7431
+ return command.op === "insert_inline_token" || command.op === "replace_inline_token" || command.op === "remove_inline_token" || command.op === "set_equation_numbering";
7387
7432
  }
7388
7433
  function isListCommand(command) {
7389
7434
  return command.op === "insert_list" || command.op === "insert_list_item" || command.op === "replace_list_item" || command.op === "remove_list_item" || command.op === "move_list_item";
@@ -7498,7 +7543,7 @@ function parseCommand2(value) {
7498
7543
  if (!isObject3(value) || typeof value.op !== "string") {
7499
7544
  throw new DocumentCommandError("invalid_command", "command requires an op");
7500
7545
  }
7501
- if (value.op === "insert_inline_token" || value.op === "replace_inline_token" || value.op === "remove_inline_token") {
7546
+ if (value.op === "insert_inline_token" || value.op === "replace_inline_token" || value.op === "remove_inline_token" || value.op === "set_equation_numbering") {
7502
7547
  return parseInlineCommand(value);
7503
7548
  }
7504
7549
  if (value.op === "insert_list" || value.op === "insert_list_item" || value.op === "replace_list_item" || value.op === "remove_list_item" || value.op === "move_list_item") {
@@ -7632,7 +7677,7 @@ function applyBasicCommand(document2, command) {
7632
7677
  if (label2 !== void 0 && label2.length > 0) {
7633
7678
  const keyError = documentKeyError(label2, {
7634
7679
  references: document2.references,
7635
- labels: collectDocumentLabels(document2),
7680
+ labels: collectDocumentKeyLabels(document2),
7636
7681
  excludeOwnerId: block2.id
7637
7682
  });
7638
7683
  if (keyError === "duplicate") {
@@ -7887,7 +7932,7 @@ function getDocumentReferences(document2) {
7887
7932
  function getDocumentReferenceIndex(document2) {
7888
7933
  const referenceKeys = new Set(document2.references.map((reference) => reference.key));
7889
7934
  const labelEntries = collectDocumentLabels(document2);
7890
- const labelKeys = new Set(labelEntries.map((entry) => entry.key));
7935
+ const labelKeys = new Set(labelNumberMap(document2).keys());
7891
7936
  const citations = [];
7892
7937
  const crossReferences = [];
7893
7938
  const tokenLocations = /* @__PURE__ */ new Map();
@@ -7970,6 +8015,14 @@ function getDocumentReferenceIndex(document2) {
7970
8015
  citations,
7971
8016
  cross_references: crossReferences,
7972
8017
  labels,
8018
+ reference_targets: collectDocumentReferenceTargets(document2).map((target) => ({
8019
+ key: target.key,
8020
+ kind: target.kind,
8021
+ caption: target.caption,
8022
+ number: target.number,
8023
+ ...target.kind === "eq" ? { token_id: target.ownerId } : { block_id: target.ownerId },
8024
+ ...target.manualKey ? { manual_key: target.manualKey } : {}
8025
+ })),
7973
8026
  unresolved: {
7974
8027
  citation_keys: unresolvedCitationKeys,
7975
8028
  cross_reference_keys: unresolvedCrossReferenceKeys
@@ -8284,12 +8337,15 @@ function tokenLatex(token, labelKinds, previousToken) {
8284
8337
  if (token.kind === "ref") {
8285
8338
  return refTokenLatexForDocument(token, labelKinds, previousToken);
8286
8339
  }
8287
- if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
8340
+ if (isNumberedEquation(token)) {
8288
8341
  const body = mathBodyLatex(token);
8289
- return `\\begin{equation}
8290
- ${body}
8291
- \\label{${token.label.trim()}}
8292
- \\end{equation}`;
8342
+ const autoKey = implicitReferenceKey("eq", token.id);
8343
+ const aliases = [autoKey];
8344
+ if (token.labelEnabled && token.label?.trim() && token.label.trim() !== autoKey) aliases.push(token.label.trim());
8345
+ return "\\begin{equation}\n" + body + "\n" + aliases.map((key) => "\\label{" + key + "}").join("\n") + "\n\\end{equation}";
8346
+ }
8347
+ if (token.display && token.numbered === false && token.opening === "\\begin{equation}") {
8348
+ return "\\[" + mathBodyLatex(token) + "\\]";
8293
8349
  }
8294
8350
  if (token.sourceOwner === "editor") {
8295
8351
  return editorMathLatexForCompile(token);
@@ -8359,11 +8415,12 @@ function floatCaptionLabelLines(block, counter, labelKinds) {
8359
8415
  if (hasCaption) {
8360
8416
  lines.push(` \\caption{${caption}}`);
8361
8417
  }
8362
- if (block.labelEnabled && block.label.trim().length > 0) {
8363
- if (!hasCaption) {
8364
- lines.push(` \\refstepcounter{${counter}}`);
8365
- }
8366
- lines.push(` \\label{${block.label.trim()}}`);
8418
+ const hasManualLabel = block.labelEnabled && block.label.trim().length > 0;
8419
+ if (hasCaption || hasManualLabel) {
8420
+ if (!hasCaption) lines.push(" \\refstepcounter{" + counter + "}");
8421
+ const autoKey = implicitReferenceKey(counter === "figure" ? "fig" : "tab", block.id);
8422
+ lines.push(" \\label{" + autoKey + "}");
8423
+ if (hasManualLabel && block.label.trim() !== autoKey) lines.push(" \\label{" + block.label.trim() + "}");
8367
8424
  }
8368
8425
  return lines;
8369
8426
  }
@@ -8459,10 +8516,8 @@ ${meta.abstract.trim()}
8459
8516
  }
8460
8517
  function documentLabelKindMap(document2) {
8461
8518
  const map = /* @__PURE__ */ new Map();
8462
- for (const entry of collectDocumentLabels(document2)) {
8463
- if (!map.has(entry.key)) {
8464
- map.set(entry.key, entry.kind);
8465
- }
8519
+ for (const [key, entry] of labelNumberMap(document2)) {
8520
+ map.set(key, entry.kind);
8466
8521
  }
8467
8522
  return map;
8468
8523
  }
@@ -8561,10 +8616,17 @@ function validateToken(context, token, path, blockId) {
8561
8616
  return;
8562
8617
  }
8563
8618
  validateTextValue(context, token.source, `${path}.source`, blockId, token.id);
8619
+ if (token.numbered === true && !supportsSingleEquationNumbering(token)) {
8620
+ addIssue(context, "invalid_equation_label", `${path}.numbered`, blockId, token.id);
8621
+ }
8564
8622
  if (!token.labelEnabled) {
8565
8623
  return;
8566
8624
  }
8567
- if (!token.display) {
8625
+ if (!supportsSingleEquationNumbering(token)) {
8626
+ addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
8627
+ return;
8628
+ }
8629
+ if (!isNumberedEquation(token)) {
8568
8630
  addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
8569
8631
  return;
8570
8632
  }
@@ -8662,7 +8724,7 @@ function validatePendingReferences(context) {
8662
8724
  for (const pending of context.pendingReferences) {
8663
8725
  for (const key of pending.token.keys) {
8664
8726
  const normalized = normalizeDocumentKey(key);
8665
- if (!isValidDocumentKey(normalized)) {
8727
+ if (!isValidDocumentKey(normalized) || !context.labelKinds.has(normalized)) {
8666
8728
  addIssue(
8667
8729
  context,
8668
8730
  "missing_reference",
@@ -8761,6 +8823,18 @@ function validateDocument(document2) {
8761
8823
  resolved.blocks.forEach((block, index) => {
8762
8824
  validateBlock(context, block, `$.blocks[${String(index)}]`);
8763
8825
  });
8826
+ for (const target of collectDocumentReferenceTargets(resolved)) {
8827
+ if (target.number !== null && target.manualKey !== target.key) {
8828
+ registerKey(
8829
+ context,
8830
+ target.key,
8831
+ "$.automatic_labels",
8832
+ target.kind,
8833
+ target.kind === "eq" ? void 0 : target.ownerId,
8834
+ target.kind === "eq" ? target.ownerId : void 0
8835
+ );
8836
+ }
8837
+ }
8764
8838
  for (const diagnostic of resolved.diagnostics) {
8765
8839
  addIssue(context, "parser_diagnostic", diagnostic.path);
8766
8840
  }
@@ -8909,6 +8983,7 @@ function previewInlines(field, islands, output, equationSide, document2, preview
8909
8983
  })
8910
8984
  };
8911
8985
  }
8986
+ const number = collectDocumentReferenceTargets(document2).find((target) => target.kind === "eq" && target.ownerId === token.id)?.number;
8912
8987
  const island = {
8913
8988
  id: token.id,
8914
8989
  tex: mathTex(token, equationSide, previewOptions.digitForm),
@@ -8916,8 +8991,12 @@ function previewInlines(field, islands, output, equationSide, document2, preview
8916
8991
  output,
8917
8992
  editable: token.editable
8918
8993
  };
8994
+ const numberLabel = number == null ? void 0 : formatBibliographyNumber(number, {
8995
+ documentDirection: previewOptions.documentDirection,
8996
+ digitForm: previewOptions.digitForm
8997
+ });
8919
8998
  islands.push(island);
8920
- return { kind: "math", ...island };
8999
+ return { kind: "math", ...island, ...numberLabel ? { numberLabel } : {} };
8921
9000
  });
8922
9001
  }
8923
9002
  function textBlockPreview(block, islands, output, equationSide, document2, previewOptions) {
@@ -8940,12 +9019,9 @@ function textBlockPreview(block, islands, output, equationSide, document2, previ
8940
9019
  ...block.centered ? { centered: true } : {}
8941
9020
  };
8942
9021
  }
8943
- function floatNumberLabel(document2, key, previewOptions) {
8944
- if (!key || key.trim().length === 0) {
8945
- return void 0;
8946
- }
8947
- const hit = labelNumberMap(document2).get(key.trim());
8948
- if (!hit || hit.kind !== "fig" && hit.kind !== "tab") {
9022
+ function floatNumberLabel(document2, ownerId, previewOptions) {
9023
+ const hit = collectDocumentReferenceTargets(document2).find((target) => target.ownerId === ownerId);
9024
+ if (!hit || hit.number === null || hit.kind !== "fig" && hit.kind !== "tab") {
8949
9025
  return void 0;
8950
9026
  }
8951
9027
  return formatFloatCaptionTitle(hit.kind, hit.number, {
@@ -8987,7 +9063,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
8987
9063
  id: block.id,
8988
9064
  rows: block.rows.map((row) => row.map((cell) => previewInlines(cell, islands, output, equationSide, document2, previewOptions))),
8989
9065
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
8990
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
9066
+ ...label.length > 0 ? { label } : {},
9067
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
8991
9068
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
8992
9069
  };
8993
9070
  }
@@ -9001,7 +9078,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
9001
9078
  options: block.options,
9002
9079
  centered: block.centered,
9003
9080
  ...captionPreview(block, islands, output, equationSide, document2, previewOptions),
9004
- ...label.length > 0 ? { label, numberLabel: floatNumberLabel(document2, label, previewOptions) } : {},
9081
+ ...label.length > 0 ? { label } : {},
9082
+ ...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
9005
9083
  ...block.metadata ? { metadata: { ...block.metadata } } : {}
9006
9084
  };
9007
9085
  }
@@ -9117,7 +9195,9 @@ function documentLatex2(document2, options = {}) {
9117
9195
  clearDocumentImageAsset,
9118
9196
  cloneDocumentMeta,
9119
9197
  cloneDocumentNode,
9198
+ collectDocumentKeyLabels,
9120
9199
  collectDocumentLabels,
9200
+ collectDocumentReferenceTargets,
9121
9201
  createDocumentHistory,
9122
9202
  createEmptyArabicEquationSession,
9123
9203
  createEmptyDocument,
@@ -9155,6 +9235,7 @@ function documentLatex2(document2, options = {}) {
9155
9235
  getDocumentReferences,
9156
9236
  hasXeLatexUnsupportedEmoji,
9157
9237
  hijriMonthLabel,
9238
+ implicitReferenceKey,
9158
9239
  inlineFieldLatex,
9159
9240
  insertCiteTokenAtCaret,
9160
9241
  insertDocumentBlockAfter,
@@ -9163,6 +9244,8 @@ function documentLatex2(document2, options = {}) {
9163
9244
  insertRefTokenAtCaret,
9164
9245
  isDocumentValid,
9165
9246
  isDuplicateDocumentKey,
9247
+ isNumberedEquation,
9248
+ isNumberedFloat,
9166
9249
  isValidDocumentKey,
9167
9250
  labelNumberMap,
9168
9251
  mathNodeFromArabicSession,