@drghaliasri/butex 6.1.3 → 6.1.5

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.
@@ -20,7 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/react-document2-entry.ts
21
21
  var react_document2_entry_exports = {};
22
22
  __export(react_document2_entry_exports, {
23
- ButexDocumentEditor2: () => ButexDocumentEditor2,
23
+ ButexDocumentEditor2: () => ButexDocumentEditor23,
24
24
  DOCUMENT2_WIDGET_CSS: () => DOCUMENT2_WIDGET_CSS,
25
25
  DocumentInsertToolbar: () => DocumentInsertToolbar,
26
26
  DocumentPreview: () => DocumentPreview,
@@ -110,11 +110,6 @@ function butexFontDisplayTex(macro, text) {
110
110
  function butexMaghribiDisplayTex(text) {
111
111
  return butexFontDisplayTex("\\butexmaghribi", text);
112
112
  }
113
- function butexMaghribiDisplayLatex(text) {
114
- return `\\[
115
- ${butexMaghribiDisplayTex(text)}
116
- \\]`;
117
- }
118
113
  var BASMALA_LINES = [BUTEX_BASMALA_LINE1, BUTEX_BASMALA_LINE2, BUTEX_BASMALA_SALAWAT];
119
114
  function butexBasmalaDisplayTex() {
120
115
  return BASMALA_LINES.map((line) => butexMaghribiDisplayTex(line)).join(" \\\\ ");
@@ -960,14 +955,22 @@ function labelNumberMap(document2) {
960
955
  }
961
956
  return map;
962
957
  }
958
+ function labelKindWord(kind, uiLocale) {
959
+ if (kind === "fig") {
960
+ return uiLocale === "ar" ? "\u0627\u0644\u0634\u0643\u0644" : "Figure";
961
+ }
962
+ if (kind === "tab") {
963
+ return uiLocale === "ar" ? "\u0627\u0644\u062C\u062F\u0648\u0644" : "Table";
964
+ }
965
+ return uiLocale === "ar" ? "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629" : "Equation";
966
+ }
963
967
  function formatFloatCaptionTitle(kind, number, options = {}) {
964
968
  const locale = options.uiLocale ?? "ar";
965
- const kindWord = kind === "fig" ? locale === "ar" ? "\u0627\u0644\u0634\u0643\u0644" : "Figure" : kind === "tab" ? locale === "ar" ? "\u0627\u0644\u062C\u062F\u0648\u0644" : "Table" : locale === "ar" ? "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629" : "Equation";
966
969
  const digits = formatBibliographyNumber(number, {
967
970
  documentDirection: options.documentDirection ?? (locale === "ar" ? "rtl" : "ltr"),
968
971
  digitForm: options.digitForm ?? (locale === "ar" ? "arabicIndic" : "western")
969
972
  });
970
- return `${kindWord} ${digits}`;
973
+ return `${labelKindWord(kind, locale)} ${digits}`;
971
974
  }
972
975
  function formatFloatCaptionPreview(title, caption, uiLocale = "ar") {
973
976
  const trimmed = caption?.trim() ?? "";
@@ -980,19 +983,18 @@ function formatFloatCaptionPreview(title, caption, uiLocale = "ar") {
980
983
  function formatRefLabel(keys, document2, refCommand, options = {}) {
981
984
  const documentDirection = options.documentDirection ?? "rtl";
982
985
  const digitForm = options.digitForm ?? (documentDirection === "rtl" ? "arabicIndic" : "western");
986
+ const uiLocale = documentDirection === "rtl" ? "ar" : "en";
983
987
  const map = labelNumberMap(document2);
984
988
  const parts = keys.map((key) => {
985
989
  const hit = map.get(key);
986
990
  if (!hit) {
987
- return "?";
991
+ return refCommand === "eqref" ? "(?)" : "?";
988
992
  }
989
- return formatDigits(String(hit.number), digitForm);
993
+ const digits = formatDigits(String(hit.number), digitForm);
994
+ const numberLabel = refCommand === "eqref" ? `(${digits})` : digits;
995
+ return `${labelKindWord(hit.kind, uiLocale)} ${numberLabel}`;
990
996
  });
991
- const body = parts.join(documentDirection === "rtl" ? "\u060C" : ", ");
992
- if (refCommand === "eqref") {
993
- return `(${body})`;
994
- }
995
- return body;
997
+ return parts.join(documentDirection === "rtl" ? "\u060C" : ", ");
996
998
  }
997
999
  function stripDisplayMathDelimiters(source) {
998
1000
  const trimmed = source.trim();
@@ -9384,6 +9386,10 @@ var DOCUMENT2_KEY_PATTERN = /^[\p{L}\p{M}\p{Nd}:._-]+$/u;
9384
9386
  function normalizeDocument2Key(key) {
9385
9387
  return key.normalize("NFC").trim();
9386
9388
  }
9389
+ function isValidDocument2Key(key) {
9390
+ const normalized = normalizeDocument2Key(key);
9391
+ return normalized.length > 0 && DOCUMENT2_KEY_PATTERN.test(normalized);
9392
+ }
9387
9393
  function isDuplicateDocument2Key(key, namespace) {
9388
9394
  const normalized = normalizeDocument2Key(key);
9389
9395
  if (normalized.length === 0) {
@@ -9677,7 +9683,43 @@ function styledTextLatex(text, style) {
9677
9683
  }
9678
9684
  return output;
9679
9685
  }
9680
- function tokenLatex(token) {
9686
+ function semanticRefPrefix(kind) {
9687
+ if (kind === "fig") {
9688
+ return "\u0627\u0644\u0634\u0643\u0644";
9689
+ }
9690
+ if (kind === "tab") {
9691
+ return "\u0627\u0644\u062C\u062F\u0648\u0644";
9692
+ }
9693
+ return "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629";
9694
+ }
9695
+ function semanticRefNames(kind) {
9696
+ if (kind === "fig") {
9697
+ return ["\u0627\u0644\u0634\u0643\u0644", "\u0634\u0643\u0644", "Figure", "figure"];
9698
+ }
9699
+ if (kind === "tab") {
9700
+ return ["\u0627\u0644\u062C\u062F\u0648\u0644", "\u062C\u062F\u0648\u0644", "Table", "table"];
9701
+ }
9702
+ return ["\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629", "\u0645\u0639\u0627\u062F\u0644\u0629", "Equation", "equation"];
9703
+ }
9704
+ function previousTextAlreadyNamesKind(previousToken, kind) {
9705
+ if (previousToken?.kind !== "text") {
9706
+ return false;
9707
+ }
9708
+ const text = previousToken.text.trimEnd();
9709
+ return semanticRefNames(kind).some((name) => text.endsWith(name));
9710
+ }
9711
+ function refTokenLatexForDocument(token, labelKinds, previousToken) {
9712
+ const reference = refTokenLatex(token.keys, token.refCommand);
9713
+ if (!labelKinds || token.keys.length !== 1) {
9714
+ return reference;
9715
+ }
9716
+ const kind = labelKinds.get(token.keys[0]);
9717
+ if (!kind || previousTextAlreadyNamesKind(previousToken, kind)) {
9718
+ return reference;
9719
+ }
9720
+ return `${semanticRefPrefix(kind)}~${reference}`;
9721
+ }
9722
+ function tokenLatex(token, labelKinds, previousToken) {
9681
9723
  if (token.kind === "text") {
9682
9724
  return styledTextLatex(token.text, token.style);
9683
9725
  }
@@ -9685,7 +9727,7 @@ function tokenLatex(token) {
9685
9727
  return citeTokenLatex(token.keys);
9686
9728
  }
9687
9729
  if (token.kind === "ref") {
9688
- return refTokenLatex(token.keys, token.refCommand);
9730
+ return refTokenLatexForDocument(token, labelKinds, previousToken);
9689
9731
  }
9690
9732
  if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
9691
9733
  const body = mathBodyLatex(token);
@@ -9702,11 +9744,11 @@ ${body}
9702
9744
  }
9703
9745
  return renderMathNodeLatex(token.math);
9704
9746
  }
9705
- function inlineFieldLatex(field) {
9706
- return field.tokens.map((token) => tokenLatex(token)).join("");
9747
+ function inlineFieldLatexWithLabelKinds(field, labelKinds) {
9748
+ return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
9707
9749
  }
9708
- function textBlockLatex(block) {
9709
- const content = inlineFieldLatex(block.field);
9750
+ function textBlockLatex(block, labelKinds) {
9751
+ const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
9710
9752
  const body = block.command === "\\paragraph" ? `\\par
9711
9753
  ${content}` : `${block.command}{${content}}`;
9712
9754
  if (block.command === "\\paragraph" && block.centered) {
@@ -9719,38 +9761,61 @@ ${body}
9719
9761
  function indent(value) {
9720
9762
  return value.split("\n").map((line) => ` ${line}`).join("\n");
9721
9763
  }
9722
- function listBlockLatex(block, references) {
9764
+ function listBlockLatex(block, references, labelKinds, fixedFloats) {
9723
9765
  const items = block.items.map((item) => {
9724
- const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references))).join("");
9725
- return ` \\item ${inlineFieldLatex(item.field)}${nested}`;
9766
+ const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
9767
+ return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
9726
9768
  }).join("\n");
9727
9769
  return `${block.command}
9728
9770
  ${items}
9729
9771
  ${block.closing}`;
9730
9772
  }
9731
- function tabularInnerLatex(block) {
9732
- const rows = block.rows.map((row) => ` ${row.map((cell) => inlineFieldLatex(cell)).join(" & ")}`).join(" \\\\\n");
9773
+ function defaultGridColumnSpec(block) {
9774
+ const compact = block.columns.replace(/\s+/g, "");
9775
+ const columnCount = block.rows[0]?.length ?? 0;
9776
+ if (columnCount === 0 || compact.length !== columnCount || !/^[lcr]+$/.test(compact)) {
9777
+ return null;
9778
+ }
9779
+ return `|${compact.split("").join("|")}|`;
9780
+ }
9781
+ function tabularInnerLatex(block, labelKinds) {
9782
+ const rows = block.rows.map(
9783
+ (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
9784
+ );
9785
+ const gridColumns = defaultGridColumnSpec(block);
9786
+ if (gridColumns) {
9787
+ const griddedRows = rows.map((row) => `${row} \\\\ \\hline`).join("\n");
9788
+ return `${block.command}{${gridColumns}}
9789
+ \\hline
9790
+ ${griddedRows}
9791
+ ${block.closing}`;
9792
+ }
9733
9793
  return `${block.command}{${block.columns}}
9734
- ${rows}
9794
+ ${rows.join(" \\\\\n")}
9735
9795
  ${block.closing}`;
9736
9796
  }
9737
- function floatCaptionLabelLines(block) {
9797
+ function floatCaptionLabelLines(block, counter) {
9738
9798
  const lines = [];
9739
- if (block.captionEnabled && block.caption.trim().length > 0) {
9799
+ const hasCaption = block.captionEnabled && block.caption.trim().length > 0;
9800
+ if (hasCaption) {
9740
9801
  lines.push(` \\caption{${block.caption.trim()}}`);
9741
9802
  }
9742
9803
  if (block.labelEnabled && block.label.trim().length > 0) {
9804
+ if (!hasCaption) {
9805
+ lines.push(` \\refstepcounter{${counter}}`);
9806
+ }
9743
9807
  lines.push(` \\label{${block.label.trim()}}`);
9744
9808
  }
9745
9809
  return lines;
9746
9810
  }
9747
- function tableBlockLatex(block) {
9748
- const lines = ["\\begin{table}[htbp]"];
9811
+ function tableBlockLatex(block, labelKinds, fixedFloats) {
9812
+ const placement = fixedFloats ? "H" : "htbp";
9813
+ const lines = [`\\begin{table}[${placement}]`];
9749
9814
  if (block.centered) {
9750
9815
  lines.push(" \\centering");
9751
9816
  }
9752
- lines.push(...floatCaptionLabelLines(block));
9753
- lines.push(indent(tabularInnerLatex(block)));
9817
+ lines.push(...floatCaptionLabelLines(block, "table"));
9818
+ lines.push(indent(tabularInnerLatex(block, labelKinds)));
9754
9819
  lines.push("\\end{table}");
9755
9820
  return lines.join("\n");
9756
9821
  }
@@ -9769,28 +9834,29 @@ function imageOptionsLatex(block, constrainToColumn) {
9769
9834
  }
9770
9835
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
9771
9836
  }
9772
- function imageBlockLatex(block) {
9773
- const lines = ["\\begin{figure}[htbp]"];
9837
+ function imageBlockLatex(block, fixedFloats) {
9838
+ const placement = fixedFloats ? "H" : "htbp";
9839
+ const lines = [`\\begin{figure}[${placement}]`];
9774
9840
  if (block.centered) {
9775
9841
  lines.push(" \\centering");
9776
9842
  }
9777
9843
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
9778
- lines.push(...floatCaptionLabelLines(block));
9844
+ lines.push(...floatCaptionLabelLines(block, "figure"));
9779
9845
  lines.push("\\end{figure}");
9780
9846
  return lines.join("\n");
9781
9847
  }
9782
- function blockLatex(block, references) {
9848
+ function blockLatex(block, references, labelKinds, fixedFloats) {
9783
9849
  if (block.kind === "textBlock") {
9784
- return textBlockLatex(block);
9850
+ return textBlockLatex(block, labelKinds);
9785
9851
  }
9786
9852
  if (block.kind === "list") {
9787
- return listBlockLatex(block, references);
9853
+ return listBlockLatex(block, references, labelKinds, fixedFloats);
9788
9854
  }
9789
9855
  if (block.kind === "table") {
9790
- return tableBlockLatex(block);
9856
+ return tableBlockLatex(block, labelKinds, fixedFloats);
9791
9857
  }
9792
9858
  if (block.kind === "image") {
9793
- return imageBlockLatex(block);
9859
+ return imageBlockLatex(block, fixedFloats);
9794
9860
  }
9795
9861
  if (block.kind === "bibliography") {
9796
9862
  const width = String(Math.max(references.length, 9));
@@ -9832,12 +9898,29 @@ ${meta.abstract.trim()}
9832
9898
  }
9833
9899
  return parts;
9834
9900
  }
9901
+ function documentLabelKindMap(document2) {
9902
+ const map = /* @__PURE__ */ new Map();
9903
+ for (const entry of collectDocument2Labels(document2)) {
9904
+ if (!map.has(entry.key)) {
9905
+ map.set(entry.key, entry.kind);
9906
+ }
9907
+ }
9908
+ return map;
9909
+ }
9910
+ function closingHamdalaLatex() {
9911
+ return `\\begin{center}
9912
+ \\maghribi{${BUTEX_CLOSING_HAMDALA}}
9913
+ \\end{center}`;
9914
+ }
9835
9915
  function document2Latex(document2, options = {}) {
9836
9916
  const meta = document2.meta ?? emptyDocument2Meta();
9837
9917
  const twocolumn = options.twocolumn === true;
9838
9918
  const front = articleFrontMatterLatex(meta);
9839
- const bodyBlocks = document2.blocks.map((block) => blockLatex(block, document2.references));
9840
- const closing = butexMaghribiDisplayLatex(BUTEX_CLOSING_HAMDALA);
9919
+ const labelKinds = documentLabelKindMap(document2);
9920
+ const bodyBlocks = document2.blocks.map(
9921
+ (block) => blockLatex(block, document2.references, labelKinds, twocolumn)
9922
+ );
9923
+ const closing = closingHamdalaLatex();
9841
9924
  const body = [...front, ...bodyBlocks, closing].filter((part) => part.length > 0).join("\n\n");
9842
9925
  const wrapDocument = options.wrapDocument !== false;
9843
9926
  if (!wrapDocument) {
@@ -9846,10 +9929,11 @@ function document2Latex(document2, options = {}) {
9846
9929
 
9847
9930
  ${body}` : body;
9848
9931
  }
9932
+ const floatPackage = twocolumn ? "\n\\usepackage{float}\n" : "\n";
9849
9933
  const preamble = getArabicXeLatexPreamble({
9850
9934
  digitsMapping: options.digitsMapping ?? digitsMappingFromDigitForm(options.digitForm),
9851
9935
  twocolumn
9852
- }) + "\n" + articleMaketitlePreamble(meta, twocolumn);
9936
+ }) + floatPackage + articleMaketitlePreamble(meta, twocolumn);
9853
9937
  return `${preamble}
9854
9938
  \\begin{document}
9855
9939
 
@@ -9859,6 +9943,279 @@ ${body}
9859
9943
  `;
9860
9944
  }
9861
9945
 
9946
+ // src/document2/validation.ts
9947
+ var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
9948
+ function hasXeLatexUnsupportedEmoji(value) {
9949
+ return XELATEX_UNSUPPORTED_EMOJI_PATTERN.test(value);
9950
+ }
9951
+ function addIssue(context, code, path, blockId, tokenId) {
9952
+ context.issues.push({
9953
+ code,
9954
+ path,
9955
+ ...blockId ? { blockId } : {},
9956
+ ...tokenId ? { tokenId } : {}
9957
+ });
9958
+ }
9959
+ function validateTextValue(context, value, path, blockId, tokenId) {
9960
+ if (hasXeLatexUnsupportedEmoji(value)) {
9961
+ addIssue(context, "unsupported_emoji", path, blockId, tokenId);
9962
+ }
9963
+ }
9964
+ function registerKey(context, key, path, kind, blockId, tokenId) {
9965
+ const normalized = normalizeDocument2Key(key);
9966
+ if (normalized.length === 0) {
9967
+ addIssue(context, "key_empty", path, blockId, tokenId);
9968
+ return;
9969
+ }
9970
+ if (!isValidDocument2Key(normalized)) {
9971
+ addIssue(context, "key_invalid", path, blockId, tokenId);
9972
+ return;
9973
+ }
9974
+ context.keyOwners.push({
9975
+ key: normalized,
9976
+ path,
9977
+ kind,
9978
+ ...blockId ? { blockId } : {},
9979
+ ...tokenId ? { tokenId } : {}
9980
+ });
9981
+ if (kind !== "reference" && !context.labelKinds.has(normalized)) {
9982
+ context.labelKinds.set(normalized, kind);
9983
+ }
9984
+ }
9985
+ function validateToken(context, token, path, blockId) {
9986
+ if (token.kind === "text") {
9987
+ validateTextValue(context, token.text, `${path}.text`, blockId, token.id);
9988
+ return;
9989
+ }
9990
+ if (token.kind === "cite") {
9991
+ if (token.keys.length === 0) {
9992
+ addIssue(context, "empty_citation", `${path}.keys`, blockId, token.id);
9993
+ }
9994
+ context.pendingCitations.push({ token, path, blockId });
9995
+ return;
9996
+ }
9997
+ if (token.kind === "ref") {
9998
+ if (token.keys.length === 0) {
9999
+ addIssue(context, "empty_reference", `${path}.keys`, blockId, token.id);
10000
+ }
10001
+ context.pendingReferences.push({ token, path, blockId });
10002
+ return;
10003
+ }
10004
+ validateTextValue(context, token.source, `${path}.source`, blockId, token.id);
10005
+ if (!token.labelEnabled) {
10006
+ return;
10007
+ }
10008
+ if (!token.display) {
10009
+ addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
10010
+ return;
10011
+ }
10012
+ registerKey(context, token.label ?? "", `${path}.label`, "eq", blockId, token.id);
10013
+ }
10014
+ function validateInlineField(context, field, path, blockId) {
10015
+ field.tokens.forEach((token, index) => {
10016
+ validateToken(context, token, `${path}.tokens[${String(index)}]`, blockId);
10017
+ });
10018
+ }
10019
+ function validateBlock(context, block, path) {
10020
+ if (block.kind === "textBlock") {
10021
+ validateInlineField(context, block.field, `${path}.field`, block.id);
10022
+ return;
10023
+ }
10024
+ if (block.kind === "list") {
10025
+ block.items.forEach((item, itemIndex) => {
10026
+ const itemPath = `${path}.items[${String(itemIndex)}]`;
10027
+ validateInlineField(context, item.field, `${itemPath}.field`, block.id);
10028
+ item.blocks.forEach((child, childIndex) => {
10029
+ validateBlock(context, child, `${itemPath}.blocks[${String(childIndex)}]`);
10030
+ });
10031
+ });
10032
+ return;
10033
+ }
10034
+ if (block.kind === "table") {
10035
+ if (block.columns.trim().length === 0 || block.rows.length === 0) {
10036
+ addIssue(context, "invalid_table", path, block.id);
10037
+ }
10038
+ const expectedColumns = block.rows[0]?.length ?? 0;
10039
+ if (expectedColumns === 0 || block.rows.some((row) => row.length !== expectedColumns)) {
10040
+ addIssue(context, "invalid_table", `${path}.rows`, block.id);
10041
+ }
10042
+ block.rows.forEach((row, rowIndex) => {
10043
+ row.forEach((cell, columnIndex) => {
10044
+ validateInlineField(
10045
+ context,
10046
+ cell,
10047
+ `${path}.rows[${String(rowIndex)}][${String(columnIndex)}]`,
10048
+ block.id
10049
+ );
10050
+ });
10051
+ });
10052
+ validateTextValue(context, block.caption, `${path}.caption`, block.id);
10053
+ if (block.labelEnabled) {
10054
+ registerKey(context, block.label, `${path}.label`, "tab", block.id);
10055
+ }
10056
+ return;
10057
+ }
10058
+ if (block.kind === "image") {
10059
+ const hasAssetId = typeof block.assetId === "string" && block.assetId.trim().length > 0;
10060
+ const hasValue = block.value.trim().length > 0;
10061
+ if (!hasAssetId && !hasValue) {
10062
+ addIssue(context, "empty_image", path, block.id);
10063
+ }
10064
+ validateTextValue(context, block.value, `${path}.value`, block.id);
10065
+ validateTextValue(context, block.caption, `${path}.caption`, block.id);
10066
+ if (block.labelEnabled) {
10067
+ registerKey(context, block.label, `${path}.label`, "fig", block.id);
10068
+ }
10069
+ return;
10070
+ }
10071
+ if (block.kind === "raw") {
10072
+ validateTextValue(context, block.value, `${path}.value`, block.id);
10073
+ }
10074
+ }
10075
+ function validateDuplicateKeys(context) {
10076
+ const grouped = /* @__PURE__ */ new Map();
10077
+ for (const owner of context.keyOwners) {
10078
+ const group = grouped.get(owner.key) ?? [];
10079
+ group.push(owner);
10080
+ grouped.set(owner.key, group);
10081
+ }
10082
+ for (const owners of grouped.values()) {
10083
+ if (owners.length < 2) {
10084
+ continue;
10085
+ }
10086
+ for (const owner of owners) {
10087
+ addIssue(context, "key_duplicate", owner.path, owner.blockId, owner.tokenId);
10088
+ }
10089
+ }
10090
+ }
10091
+ function validatePendingReferences(context) {
10092
+ for (const pending of context.pendingCitations) {
10093
+ if (pending.token.keys.some((key) => !isValidDocument2Key(normalizeDocument2Key(key)))) {
10094
+ addIssue(
10095
+ context,
10096
+ "missing_citation",
10097
+ `${pending.path}.keys`,
10098
+ pending.blockId,
10099
+ pending.token.id
10100
+ );
10101
+ }
10102
+ }
10103
+ for (const pending of context.pendingReferences) {
10104
+ for (const key of pending.token.keys) {
10105
+ const normalized = normalizeDocument2Key(key);
10106
+ if (!isValidDocument2Key(normalized)) {
10107
+ addIssue(
10108
+ context,
10109
+ "missing_reference",
10110
+ `${pending.path}.keys`,
10111
+ pending.blockId,
10112
+ pending.token.id
10113
+ );
10114
+ break;
10115
+ }
10116
+ const kind = context.labelKinds.get(normalized);
10117
+ if (kind && pending.token.refCommand === "eqref" !== (kind === "eq")) {
10118
+ addIssue(
10119
+ context,
10120
+ "reference_kind_mismatch",
10121
+ `${pending.path}.keys`,
10122
+ pending.blockId,
10123
+ pending.token.id
10124
+ );
10125
+ break;
10126
+ }
10127
+ }
10128
+ }
10129
+ }
10130
+ function isRecord(value) {
10131
+ return typeof value === "object" && value !== null;
10132
+ }
10133
+ function hasRuntimeDocumentShape(document2) {
10134
+ const value = document2;
10135
+ return isRecord(value) && value.nodeType === "DocumentObject";
10136
+ }
10137
+ function hasWireDocumentShape(document2) {
10138
+ const value = document2;
10139
+ if (!isRecord(value) || value.node_type !== "DocumentObject" || !Array.isArray(value.blocks)) {
10140
+ return false;
10141
+ }
10142
+ if (value.meta !== void 0 && !isRecord(value.meta)) {
10143
+ return false;
10144
+ }
10145
+ if (value.references !== void 0) {
10146
+ if (!Array.isArray(value.references)) {
10147
+ return false;
10148
+ }
10149
+ for (const reference of value.references) {
10150
+ if (!isRecord(reference) || typeof reference.key !== "string" || reference.key.trim().length === 0) {
10151
+ return false;
10152
+ }
10153
+ }
10154
+ }
10155
+ return true;
10156
+ }
10157
+ function resolveDocument(document2) {
10158
+ if (hasRuntimeDocumentShape(document2)) {
10159
+ try {
10160
+ toDocumentJson2(document2);
10161
+ return document2;
10162
+ } catch {
10163
+ return null;
10164
+ }
10165
+ }
10166
+ if (!hasWireDocumentShape(document2)) {
10167
+ return null;
10168
+ }
10169
+ try {
10170
+ return fromDocumentJson2(document2);
10171
+ } catch {
10172
+ return null;
10173
+ }
10174
+ }
10175
+ function validateDocument2(document2) {
10176
+ const resolved = resolveDocument(document2);
10177
+ if (!resolved) {
10178
+ return {
10179
+ valid: false,
10180
+ issues: [{ code: "malformed_document", path: "$" }]
10181
+ };
10182
+ }
10183
+ const context = {
10184
+ issues: [],
10185
+ keyOwners: [],
10186
+ labelKinds: /* @__PURE__ */ new Map(),
10187
+ pendingCitations: [],
10188
+ pendingReferences: []
10189
+ };
10190
+ validateTextValue(context, resolved.meta.title, "$.meta.title");
10191
+ validateTextValue(context, resolved.meta.authors, "$.meta.authors");
10192
+ validateTextValue(context, resolved.meta.abstract, "$.meta.abstract");
10193
+ resolved.references.forEach((reference, index) => {
10194
+ const path = `$.references[${String(index)}]`;
10195
+ registerKey(context, reference.key, `${path}.key`, "reference");
10196
+ validateTextValue(context, reference.authors, `${path}.authors`);
10197
+ validateTextValue(context, reference.title, `${path}.title`);
10198
+ validateTextValue(context, reference.year, `${path}.year`);
10199
+ validateTextValue(context, reference.url, `${path}.url`);
10200
+ validateTextValue(context, reference.venue, `${path}.venue`);
10201
+ });
10202
+ resolved.blocks.forEach((block, index) => {
10203
+ validateBlock(context, block, `$.blocks[${String(index)}]`);
10204
+ });
10205
+ for (const diagnostic of resolved.diagnostics) {
10206
+ addIssue(context, "parser_diagnostic", diagnostic.path);
10207
+ }
10208
+ validateDuplicateKeys(context);
10209
+ validatePendingReferences(context);
10210
+ return {
10211
+ valid: context.issues.length === 0,
10212
+ issues: context.issues
10213
+ };
10214
+ }
10215
+ function isDocument2Valid(document2) {
10216
+ return validateDocument2(document2).valid;
10217
+ }
10218
+
9862
10219
  // src/document2/history.ts
9863
10220
  var DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH = 100;
9864
10221
  function createDocument2History(maxDepth = DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH) {
@@ -10448,10 +10805,6 @@ function document2Messages(locale) {
10448
10805
  }
10449
10806
 
10450
10807
  // src/react-document2/xelatexTextValidation.ts
10451
- var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
10452
- function hasXeLatexUnsupportedEmoji(value) {
10453
- return XELATEX_UNSUPPORTED_EMOJI_PATTERN.test(value);
10454
- }
10455
10808
  function unsupportedEmojiMessage(uiLocale) {
10456
10809
  return uiLocale === "ar" ? "\u0647\u0630\u0627 \u0627\u0644\u0631\u0645\u0632 \u0627\u0644\u062A\u0639\u0628\u064A\u0631\u064A \u063A\u064A\u0631 \u0645\u062F\u0639\u0648\u0645." : "This emoji is not supported.";
10457
10810
  }
@@ -10692,6 +11045,9 @@ function focusArticleMetaPanel(panel) {
10692
11045
  }
10693
11046
  }
10694
11047
 
11048
+ // src/react-document2/ValidatedButexDocumentEditor2.tsx
11049
+ var import_react14 = require("react");
11050
+
10695
11051
  // src/react-document2/ButexDocumentEditor2.tsx
10696
11052
  var import_react13 = require("react");
10697
11053
 
@@ -12825,6 +13181,28 @@ function DocumentInsertToolbar({
12825
13181
 
12826
13182
  // src/react-document2/DocumentPreview.tsx
12827
13183
  var import_jsx_runtime11 = require("react/jsx-runtime");
13184
+ var REF_KIND_NAMES = [
13185
+ { names: ["\u0627\u0644\u0634\u0643\u0644", "\u0634\u0643\u0644", "Figure", "figure"], prefixes: ["\u0627\u0644\u0634\u0643\u0644 ", "Figure "] },
13186
+ { names: ["\u0627\u0644\u062C\u062F\u0648\u0644", "\u062C\u062F\u0648\u0644", "Table", "table"], prefixes: ["\u0627\u0644\u062C\u062F\u0648\u0644 ", "Table "] },
13187
+ { names: ["\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629", "\u0645\u0639\u0627\u062F\u0644\u0629", "Equation", "equation"], prefixes: ["\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629 ", "Equation "] }
13188
+ ];
13189
+ function contextualRefLabel(inlines, index, label) {
13190
+ const previous = inlines[index - 1];
13191
+ if (previous?.kind !== "text") {
13192
+ return label;
13193
+ }
13194
+ const previousText = previous.text.trimEnd();
13195
+ for (const group of REF_KIND_NAMES) {
13196
+ if (!group.names.some((name) => previousText.endsWith(name))) {
13197
+ continue;
13198
+ }
13199
+ const prefix = group.prefixes.find((candidate) => label.startsWith(candidate));
13200
+ if (prefix) {
13201
+ return label.slice(prefix.length);
13202
+ }
13203
+ }
13204
+ return label;
13205
+ }
12828
13206
  function PreviewInlines({ inlines, output }) {
12829
13207
  return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(import_jsx_runtime11.Fragment, { children: inlines.map((inline, index) => {
12830
13208
  if (inline.kind === "text") {
@@ -12844,7 +13222,7 @@ function PreviewInlines({ inlines, output }) {
12844
13222
  return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("span", { className: "butex-document2-widget__preview-cite", children: inline.label }, inline.id);
12845
13223
  }
12846
13224
  if (inline.kind === "ref") {
12847
- return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("span", { className: "butex-document2-widget__preview-cite butex-document2-widget__preview-ref", children: inline.label }, inline.id);
13225
+ return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)("span", { className: "butex-document2-widget__preview-cite butex-document2-widget__preview-ref", children: contextualRefLabel(inlines, index, inline.label) }, inline.id);
12848
13226
  }
12849
13227
  return /* @__PURE__ */ (0, import_jsx_runtime11.jsx)(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }, inline.id);
12850
13228
  }) });
@@ -19166,6 +19544,100 @@ var ButexDocumentEditor2 = (0, import_react13.forwardRef)(
19166
19544
  }
19167
19545
  );
19168
19546
  ButexDocumentEditor2.displayName = "ButexDocumentEditor2";
19547
+
19548
+ // src/react-document2/ValidatedButexDocumentEditor2.tsx
19549
+ var import_jsx_runtime18 = require("react/jsx-runtime");
19550
+ function imageValidationMessage(uiLocale) {
19551
+ return uiLocale === "ar" ? "\u0644\u0645 \u062A\u064F\u062D\u062F\u0651\u062F \u0635\u0648\u0631\u0629 \u0644\u0647\u0630\u0647 \u0627\u0644\u0643\u062A\u0644\u0629." : "No image has been selected for this block.";
19552
+ }
19553
+ function imageHasIdentity(ctx) {
19554
+ return Boolean(ctx.assetId?.trim()) || ctx.value.trim().length > 0;
19555
+ }
19556
+ function isMalformedInitialDocument(initialDocument) {
19557
+ if (initialDocument === void 0) {
19558
+ return false;
19559
+ }
19560
+ return validateDocument2(initialDocument).issues.some((issue) => issue.code === "malformed_document");
19561
+ }
19562
+ var ButexDocumentEditor23 = (0, import_react14.forwardRef)(
19563
+ function ValidatedButexDocumentEditor2({
19564
+ initialDocument,
19565
+ uiLocale = "ar",
19566
+ onDocumentChange,
19567
+ onValidityChange,
19568
+ renderImageBlockEditor,
19569
+ ...rest
19570
+ }, ref) {
19571
+ const lastValidityRef = (0, import_react14.useRef)(null);
19572
+ const initialDocumentRef = (0, import_react14.useRef)(initialDocument);
19573
+ const awaitingInitialResolutionRef = (0, import_react14.useRef)(true);
19574
+ const malformedInitialDocument = isMalformedInitialDocument(initialDocument);
19575
+ if (initialDocumentRef.current !== initialDocument) {
19576
+ initialDocumentRef.current = initialDocument;
19577
+ awaitingInitialResolutionRef.current = true;
19578
+ lastValidityRef.current = null;
19579
+ }
19580
+ const emitValidity = (0, import_react14.useCallback)(
19581
+ (isValid) => {
19582
+ if (lastValidityRef.current === isValid) {
19583
+ return;
19584
+ }
19585
+ lastValidityRef.current = isValid;
19586
+ onValidityChange?.(isValid);
19587
+ },
19588
+ [onValidityChange]
19589
+ );
19590
+ const handleDocumentChange = (0, import_react14.useCallback)(
19591
+ (document2) => {
19592
+ const validationTarget = initialDocument !== void 0 && (malformedInitialDocument || awaitingInitialResolutionRef.current) ? initialDocument : document2;
19593
+ awaitingInitialResolutionRef.current = false;
19594
+ emitValidity(isDocument2Valid(validationTarget));
19595
+ onDocumentChange?.(document2);
19596
+ },
19597
+ [emitValidity, initialDocument, malformedInitialDocument, onDocumentChange]
19598
+ );
19599
+ const handleImageBlockEditor = (0, import_react14.useCallback)(
19600
+ (ctx) => {
19601
+ const rendered = renderImageBlockEditor?.(ctx) ?? ctx.defaultUi;
19602
+ if (imageHasIdentity(ctx)) {
19603
+ return rendered;
19604
+ }
19605
+ const errorId = `butex-document2-image-validation-${ctx.blockId}`;
19606
+ return /* @__PURE__ */ (0, import_jsx_runtime18.jsxs)(
19607
+ "div",
19608
+ {
19609
+ role: "group",
19610
+ "aria-invalid": "true",
19611
+ "aria-describedby": errorId,
19612
+ style: {
19613
+ border: "1px solid var(--butex-document2-error)",
19614
+ background: "var(--butex-document2-error-bg)",
19615
+ borderRadius: "0.65rem",
19616
+ padding: "0.6rem"
19617
+ },
19618
+ children: [
19619
+ rendered,
19620
+ /* @__PURE__ */ (0, import_jsx_runtime18.jsx)("p", { id: errorId, className: "butex-document2-widget__field-error", children: imageValidationMessage(ctx.uiLocale) })
19621
+ ]
19622
+ }
19623
+ );
19624
+ },
19625
+ [renderImageBlockEditor]
19626
+ );
19627
+ return /* @__PURE__ */ (0, import_jsx_runtime18.jsx)(
19628
+ ButexDocumentEditor2,
19629
+ {
19630
+ ref,
19631
+ ...rest,
19632
+ initialDocument,
19633
+ uiLocale,
19634
+ onDocumentChange: handleDocumentChange,
19635
+ renderImageBlockEditor: handleImageBlockEditor
19636
+ }
19637
+ );
19638
+ }
19639
+ );
19640
+ ButexDocumentEditor23.displayName = "ButexDocumentEditor2";
19169
19641
  // Annotate the CommonJS export names for ESM import in node:
19170
19642
  0 && (module.exports = {
19171
19643
  ButexDocumentEditor2,