@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.
@@ -75,11 +75,6 @@ function butexFontDisplayTex(macro, text) {
75
75
  function butexMaghribiDisplayTex(text) {
76
76
  return butexFontDisplayTex("\\butexmaghribi", text);
77
77
  }
78
- function butexMaghribiDisplayLatex(text) {
79
- return `\\[
80
- ${butexMaghribiDisplayTex(text)}
81
- \\]`;
82
- }
83
78
  var BASMALA_LINES = [BUTEX_BASMALA_LINE1, BUTEX_BASMALA_LINE2, BUTEX_BASMALA_SALAWAT];
84
79
  function butexBasmalaDisplayTex() {
85
80
  return BASMALA_LINES.map((line) => butexMaghribiDisplayTex(line)).join(" \\\\ ");
@@ -925,14 +920,22 @@ function labelNumberMap(document2) {
925
920
  }
926
921
  return map;
927
922
  }
923
+ function labelKindWord(kind, uiLocale) {
924
+ if (kind === "fig") {
925
+ return uiLocale === "ar" ? "\u0627\u0644\u0634\u0643\u0644" : "Figure";
926
+ }
927
+ if (kind === "tab") {
928
+ return uiLocale === "ar" ? "\u0627\u0644\u062C\u062F\u0648\u0644" : "Table";
929
+ }
930
+ return uiLocale === "ar" ? "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629" : "Equation";
931
+ }
928
932
  function formatFloatCaptionTitle(kind, number, options = {}) {
929
933
  const locale = options.uiLocale ?? "ar";
930
- 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";
931
934
  const digits = formatBibliographyNumber(number, {
932
935
  documentDirection: options.documentDirection ?? (locale === "ar" ? "rtl" : "ltr"),
933
936
  digitForm: options.digitForm ?? (locale === "ar" ? "arabicIndic" : "western")
934
937
  });
935
- return `${kindWord} ${digits}`;
938
+ return `${labelKindWord(kind, locale)} ${digits}`;
936
939
  }
937
940
  function formatFloatCaptionPreview(title, caption, uiLocale = "ar") {
938
941
  const trimmed = caption?.trim() ?? "";
@@ -945,19 +948,18 @@ function formatFloatCaptionPreview(title, caption, uiLocale = "ar") {
945
948
  function formatRefLabel(keys, document2, refCommand, options = {}) {
946
949
  const documentDirection = options.documentDirection ?? "rtl";
947
950
  const digitForm = options.digitForm ?? (documentDirection === "rtl" ? "arabicIndic" : "western");
951
+ const uiLocale = documentDirection === "rtl" ? "ar" : "en";
948
952
  const map = labelNumberMap(document2);
949
953
  const parts = keys.map((key) => {
950
954
  const hit = map.get(key);
951
955
  if (!hit) {
952
- return "?";
956
+ return refCommand === "eqref" ? "(?)" : "?";
953
957
  }
954
- return formatDigits(String(hit.number), digitForm);
958
+ const digits = formatDigits(String(hit.number), digitForm);
959
+ const numberLabel = refCommand === "eqref" ? `(${digits})` : digits;
960
+ return `${labelKindWord(hit.kind, uiLocale)} ${numberLabel}`;
955
961
  });
956
- const body = parts.join(documentDirection === "rtl" ? "\u060C" : ", ");
957
- if (refCommand === "eqref") {
958
- return `(${body})`;
959
- }
960
- return body;
962
+ return parts.join(documentDirection === "rtl" ? "\u060C" : ", ");
961
963
  }
962
964
  function stripDisplayMathDelimiters(source) {
963
965
  const trimmed = source.trim();
@@ -9349,6 +9351,10 @@ var DOCUMENT2_KEY_PATTERN = /^[\p{L}\p{M}\p{Nd}:._-]+$/u;
9349
9351
  function normalizeDocument2Key(key) {
9350
9352
  return key.normalize("NFC").trim();
9351
9353
  }
9354
+ function isValidDocument2Key(key) {
9355
+ const normalized = normalizeDocument2Key(key);
9356
+ return normalized.length > 0 && DOCUMENT2_KEY_PATTERN.test(normalized);
9357
+ }
9352
9358
  function isDuplicateDocument2Key(key, namespace) {
9353
9359
  const normalized = normalizeDocument2Key(key);
9354
9360
  if (normalized.length === 0) {
@@ -9642,7 +9648,43 @@ function styledTextLatex(text, style) {
9642
9648
  }
9643
9649
  return output;
9644
9650
  }
9645
- function tokenLatex(token) {
9651
+ function semanticRefPrefix(kind) {
9652
+ if (kind === "fig") {
9653
+ return "\u0627\u0644\u0634\u0643\u0644";
9654
+ }
9655
+ if (kind === "tab") {
9656
+ return "\u0627\u0644\u062C\u062F\u0648\u0644";
9657
+ }
9658
+ return "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629";
9659
+ }
9660
+ function semanticRefNames(kind) {
9661
+ if (kind === "fig") {
9662
+ return ["\u0627\u0644\u0634\u0643\u0644", "\u0634\u0643\u0644", "Figure", "figure"];
9663
+ }
9664
+ if (kind === "tab") {
9665
+ return ["\u0627\u0644\u062C\u062F\u0648\u0644", "\u062C\u062F\u0648\u0644", "Table", "table"];
9666
+ }
9667
+ return ["\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629", "\u0645\u0639\u0627\u062F\u0644\u0629", "Equation", "equation"];
9668
+ }
9669
+ function previousTextAlreadyNamesKind(previousToken, kind) {
9670
+ if (previousToken?.kind !== "text") {
9671
+ return false;
9672
+ }
9673
+ const text = previousToken.text.trimEnd();
9674
+ return semanticRefNames(kind).some((name) => text.endsWith(name));
9675
+ }
9676
+ function refTokenLatexForDocument(token, labelKinds, previousToken) {
9677
+ const reference = refTokenLatex(token.keys, token.refCommand);
9678
+ if (!labelKinds || token.keys.length !== 1) {
9679
+ return reference;
9680
+ }
9681
+ const kind = labelKinds.get(token.keys[0]);
9682
+ if (!kind || previousTextAlreadyNamesKind(previousToken, kind)) {
9683
+ return reference;
9684
+ }
9685
+ return `${semanticRefPrefix(kind)}~${reference}`;
9686
+ }
9687
+ function tokenLatex(token, labelKinds, previousToken) {
9646
9688
  if (token.kind === "text") {
9647
9689
  return styledTextLatex(token.text, token.style);
9648
9690
  }
@@ -9650,7 +9692,7 @@ function tokenLatex(token) {
9650
9692
  return citeTokenLatex(token.keys);
9651
9693
  }
9652
9694
  if (token.kind === "ref") {
9653
- return refTokenLatex(token.keys, token.refCommand);
9695
+ return refTokenLatexForDocument(token, labelKinds, previousToken);
9654
9696
  }
9655
9697
  if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
9656
9698
  const body = mathBodyLatex(token);
@@ -9667,11 +9709,11 @@ ${body}
9667
9709
  }
9668
9710
  return renderMathNodeLatex(token.math);
9669
9711
  }
9670
- function inlineFieldLatex(field) {
9671
- return field.tokens.map((token) => tokenLatex(token)).join("");
9712
+ function inlineFieldLatexWithLabelKinds(field, labelKinds) {
9713
+ return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
9672
9714
  }
9673
- function textBlockLatex(block) {
9674
- const content = inlineFieldLatex(block.field);
9715
+ function textBlockLatex(block, labelKinds) {
9716
+ const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
9675
9717
  const body = block.command === "\\paragraph" ? `\\par
9676
9718
  ${content}` : `${block.command}{${content}}`;
9677
9719
  if (block.command === "\\paragraph" && block.centered) {
@@ -9684,38 +9726,61 @@ ${body}
9684
9726
  function indent(value) {
9685
9727
  return value.split("\n").map((line) => ` ${line}`).join("\n");
9686
9728
  }
9687
- function listBlockLatex(block, references) {
9729
+ function listBlockLatex(block, references, labelKinds, fixedFloats) {
9688
9730
  const items = block.items.map((item) => {
9689
- const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references))).join("");
9690
- return ` \\item ${inlineFieldLatex(item.field)}${nested}`;
9731
+ const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
9732
+ return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
9691
9733
  }).join("\n");
9692
9734
  return `${block.command}
9693
9735
  ${items}
9694
9736
  ${block.closing}`;
9695
9737
  }
9696
- function tabularInnerLatex(block) {
9697
- const rows = block.rows.map((row) => ` ${row.map((cell) => inlineFieldLatex(cell)).join(" & ")}`).join(" \\\\\n");
9738
+ function defaultGridColumnSpec(block) {
9739
+ const compact = block.columns.replace(/\s+/g, "");
9740
+ const columnCount = block.rows[0]?.length ?? 0;
9741
+ if (columnCount === 0 || compact.length !== columnCount || !/^[lcr]+$/.test(compact)) {
9742
+ return null;
9743
+ }
9744
+ return `|${compact.split("").join("|")}|`;
9745
+ }
9746
+ function tabularInnerLatex(block, labelKinds) {
9747
+ const rows = block.rows.map(
9748
+ (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
9749
+ );
9750
+ const gridColumns = defaultGridColumnSpec(block);
9751
+ if (gridColumns) {
9752
+ const griddedRows = rows.map((row) => `${row} \\\\ \\hline`).join("\n");
9753
+ return `${block.command}{${gridColumns}}
9754
+ \\hline
9755
+ ${griddedRows}
9756
+ ${block.closing}`;
9757
+ }
9698
9758
  return `${block.command}{${block.columns}}
9699
- ${rows}
9759
+ ${rows.join(" \\\\\n")}
9700
9760
  ${block.closing}`;
9701
9761
  }
9702
- function floatCaptionLabelLines(block) {
9762
+ function floatCaptionLabelLines(block, counter) {
9703
9763
  const lines = [];
9704
- if (block.captionEnabled && block.caption.trim().length > 0) {
9764
+ const hasCaption = block.captionEnabled && block.caption.trim().length > 0;
9765
+ if (hasCaption) {
9705
9766
  lines.push(` \\caption{${block.caption.trim()}}`);
9706
9767
  }
9707
9768
  if (block.labelEnabled && block.label.trim().length > 0) {
9769
+ if (!hasCaption) {
9770
+ lines.push(` \\refstepcounter{${counter}}`);
9771
+ }
9708
9772
  lines.push(` \\label{${block.label.trim()}}`);
9709
9773
  }
9710
9774
  return lines;
9711
9775
  }
9712
- function tableBlockLatex(block) {
9713
- const lines = ["\\begin{table}[htbp]"];
9776
+ function tableBlockLatex(block, labelKinds, fixedFloats) {
9777
+ const placement = fixedFloats ? "H" : "htbp";
9778
+ const lines = [`\\begin{table}[${placement}]`];
9714
9779
  if (block.centered) {
9715
9780
  lines.push(" \\centering");
9716
9781
  }
9717
- lines.push(...floatCaptionLabelLines(block));
9718
- lines.push(indent(tabularInnerLatex(block)));
9782
+ lines.push(...floatCaptionLabelLines(block, "table"));
9783
+ lines.push(indent(tabularInnerLatex(block, labelKinds)));
9719
9784
  lines.push("\\end{table}");
9720
9785
  return lines.join("\n");
9721
9786
  }
@@ -9734,28 +9799,29 @@ function imageOptionsLatex(block, constrainToColumn) {
9734
9799
  }
9735
9800
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
9736
9801
  }
9737
- function imageBlockLatex(block) {
9738
- const lines = ["\\begin{figure}[htbp]"];
9802
+ function imageBlockLatex(block, fixedFloats) {
9803
+ const placement = fixedFloats ? "H" : "htbp";
9804
+ const lines = [`\\begin{figure}[${placement}]`];
9739
9805
  if (block.centered) {
9740
9806
  lines.push(" \\centering");
9741
9807
  }
9742
9808
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
9743
- lines.push(...floatCaptionLabelLines(block));
9809
+ lines.push(...floatCaptionLabelLines(block, "figure"));
9744
9810
  lines.push("\\end{figure}");
9745
9811
  return lines.join("\n");
9746
9812
  }
9747
- function blockLatex(block, references) {
9813
+ function blockLatex(block, references, labelKinds, fixedFloats) {
9748
9814
  if (block.kind === "textBlock") {
9749
- return textBlockLatex(block);
9815
+ return textBlockLatex(block, labelKinds);
9750
9816
  }
9751
9817
  if (block.kind === "list") {
9752
- return listBlockLatex(block, references);
9818
+ return listBlockLatex(block, references, labelKinds, fixedFloats);
9753
9819
  }
9754
9820
  if (block.kind === "table") {
9755
- return tableBlockLatex(block);
9821
+ return tableBlockLatex(block, labelKinds, fixedFloats);
9756
9822
  }
9757
9823
  if (block.kind === "image") {
9758
- return imageBlockLatex(block);
9824
+ return imageBlockLatex(block, fixedFloats);
9759
9825
  }
9760
9826
  if (block.kind === "bibliography") {
9761
9827
  const width = String(Math.max(references.length, 9));
@@ -9797,12 +9863,29 @@ ${meta.abstract.trim()}
9797
9863
  }
9798
9864
  return parts;
9799
9865
  }
9866
+ function documentLabelKindMap(document2) {
9867
+ const map = /* @__PURE__ */ new Map();
9868
+ for (const entry of collectDocument2Labels(document2)) {
9869
+ if (!map.has(entry.key)) {
9870
+ map.set(entry.key, entry.kind);
9871
+ }
9872
+ }
9873
+ return map;
9874
+ }
9875
+ function closingHamdalaLatex() {
9876
+ return `\\begin{center}
9877
+ \\maghribi{${BUTEX_CLOSING_HAMDALA}}
9878
+ \\end{center}`;
9879
+ }
9800
9880
  function document2Latex(document2, options = {}) {
9801
9881
  const meta = document2.meta ?? emptyDocument2Meta();
9802
9882
  const twocolumn = options.twocolumn === true;
9803
9883
  const front = articleFrontMatterLatex(meta);
9804
- const bodyBlocks = document2.blocks.map((block) => blockLatex(block, document2.references));
9805
- const closing = butexMaghribiDisplayLatex(BUTEX_CLOSING_HAMDALA);
9884
+ const labelKinds = documentLabelKindMap(document2);
9885
+ const bodyBlocks = document2.blocks.map(
9886
+ (block) => blockLatex(block, document2.references, labelKinds, twocolumn)
9887
+ );
9888
+ const closing = closingHamdalaLatex();
9806
9889
  const body = [...front, ...bodyBlocks, closing].filter((part) => part.length > 0).join("\n\n");
9807
9890
  const wrapDocument = options.wrapDocument !== false;
9808
9891
  if (!wrapDocument) {
@@ -9811,10 +9894,11 @@ function document2Latex(document2, options = {}) {
9811
9894
 
9812
9895
  ${body}` : body;
9813
9896
  }
9897
+ const floatPackage = twocolumn ? "\n\\usepackage{float}\n" : "\n";
9814
9898
  const preamble = getArabicXeLatexPreamble({
9815
9899
  digitsMapping: options.digitsMapping ?? digitsMappingFromDigitForm(options.digitForm),
9816
9900
  twocolumn
9817
- }) + "\n" + articleMaketitlePreamble(meta, twocolumn);
9901
+ }) + floatPackage + articleMaketitlePreamble(meta, twocolumn);
9818
9902
  return `${preamble}
9819
9903
  \\begin{document}
9820
9904
 
@@ -9824,6 +9908,279 @@ ${body}
9824
9908
  `;
9825
9909
  }
9826
9910
 
9911
+ // src/document2/validation.ts
9912
+ var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
9913
+ function hasXeLatexUnsupportedEmoji(value) {
9914
+ return XELATEX_UNSUPPORTED_EMOJI_PATTERN.test(value);
9915
+ }
9916
+ function addIssue(context, code, path, blockId, tokenId) {
9917
+ context.issues.push({
9918
+ code,
9919
+ path,
9920
+ ...blockId ? { blockId } : {},
9921
+ ...tokenId ? { tokenId } : {}
9922
+ });
9923
+ }
9924
+ function validateTextValue(context, value, path, blockId, tokenId) {
9925
+ if (hasXeLatexUnsupportedEmoji(value)) {
9926
+ addIssue(context, "unsupported_emoji", path, blockId, tokenId);
9927
+ }
9928
+ }
9929
+ function registerKey(context, key, path, kind, blockId, tokenId) {
9930
+ const normalized = normalizeDocument2Key(key);
9931
+ if (normalized.length === 0) {
9932
+ addIssue(context, "key_empty", path, blockId, tokenId);
9933
+ return;
9934
+ }
9935
+ if (!isValidDocument2Key(normalized)) {
9936
+ addIssue(context, "key_invalid", path, blockId, tokenId);
9937
+ return;
9938
+ }
9939
+ context.keyOwners.push({
9940
+ key: normalized,
9941
+ path,
9942
+ kind,
9943
+ ...blockId ? { blockId } : {},
9944
+ ...tokenId ? { tokenId } : {}
9945
+ });
9946
+ if (kind !== "reference" && !context.labelKinds.has(normalized)) {
9947
+ context.labelKinds.set(normalized, kind);
9948
+ }
9949
+ }
9950
+ function validateToken(context, token, path, blockId) {
9951
+ if (token.kind === "text") {
9952
+ validateTextValue(context, token.text, `${path}.text`, blockId, token.id);
9953
+ return;
9954
+ }
9955
+ if (token.kind === "cite") {
9956
+ if (token.keys.length === 0) {
9957
+ addIssue(context, "empty_citation", `${path}.keys`, blockId, token.id);
9958
+ }
9959
+ context.pendingCitations.push({ token, path, blockId });
9960
+ return;
9961
+ }
9962
+ if (token.kind === "ref") {
9963
+ if (token.keys.length === 0) {
9964
+ addIssue(context, "empty_reference", `${path}.keys`, blockId, token.id);
9965
+ }
9966
+ context.pendingReferences.push({ token, path, blockId });
9967
+ return;
9968
+ }
9969
+ validateTextValue(context, token.source, `${path}.source`, blockId, token.id);
9970
+ if (!token.labelEnabled) {
9971
+ return;
9972
+ }
9973
+ if (!token.display) {
9974
+ addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
9975
+ return;
9976
+ }
9977
+ registerKey(context, token.label ?? "", `${path}.label`, "eq", blockId, token.id);
9978
+ }
9979
+ function validateInlineField(context, field, path, blockId) {
9980
+ field.tokens.forEach((token, index) => {
9981
+ validateToken(context, token, `${path}.tokens[${String(index)}]`, blockId);
9982
+ });
9983
+ }
9984
+ function validateBlock(context, block, path) {
9985
+ if (block.kind === "textBlock") {
9986
+ validateInlineField(context, block.field, `${path}.field`, block.id);
9987
+ return;
9988
+ }
9989
+ if (block.kind === "list") {
9990
+ block.items.forEach((item, itemIndex) => {
9991
+ const itemPath = `${path}.items[${String(itemIndex)}]`;
9992
+ validateInlineField(context, item.field, `${itemPath}.field`, block.id);
9993
+ item.blocks.forEach((child, childIndex) => {
9994
+ validateBlock(context, child, `${itemPath}.blocks[${String(childIndex)}]`);
9995
+ });
9996
+ });
9997
+ return;
9998
+ }
9999
+ if (block.kind === "table") {
10000
+ if (block.columns.trim().length === 0 || block.rows.length === 0) {
10001
+ addIssue(context, "invalid_table", path, block.id);
10002
+ }
10003
+ const expectedColumns = block.rows[0]?.length ?? 0;
10004
+ if (expectedColumns === 0 || block.rows.some((row) => row.length !== expectedColumns)) {
10005
+ addIssue(context, "invalid_table", `${path}.rows`, block.id);
10006
+ }
10007
+ block.rows.forEach((row, rowIndex) => {
10008
+ row.forEach((cell, columnIndex) => {
10009
+ validateInlineField(
10010
+ context,
10011
+ cell,
10012
+ `${path}.rows[${String(rowIndex)}][${String(columnIndex)}]`,
10013
+ block.id
10014
+ );
10015
+ });
10016
+ });
10017
+ validateTextValue(context, block.caption, `${path}.caption`, block.id);
10018
+ if (block.labelEnabled) {
10019
+ registerKey(context, block.label, `${path}.label`, "tab", block.id);
10020
+ }
10021
+ return;
10022
+ }
10023
+ if (block.kind === "image") {
10024
+ const hasAssetId = typeof block.assetId === "string" && block.assetId.trim().length > 0;
10025
+ const hasValue = block.value.trim().length > 0;
10026
+ if (!hasAssetId && !hasValue) {
10027
+ addIssue(context, "empty_image", path, block.id);
10028
+ }
10029
+ validateTextValue(context, block.value, `${path}.value`, block.id);
10030
+ validateTextValue(context, block.caption, `${path}.caption`, block.id);
10031
+ if (block.labelEnabled) {
10032
+ registerKey(context, block.label, `${path}.label`, "fig", block.id);
10033
+ }
10034
+ return;
10035
+ }
10036
+ if (block.kind === "raw") {
10037
+ validateTextValue(context, block.value, `${path}.value`, block.id);
10038
+ }
10039
+ }
10040
+ function validateDuplicateKeys(context) {
10041
+ const grouped = /* @__PURE__ */ new Map();
10042
+ for (const owner of context.keyOwners) {
10043
+ const group = grouped.get(owner.key) ?? [];
10044
+ group.push(owner);
10045
+ grouped.set(owner.key, group);
10046
+ }
10047
+ for (const owners of grouped.values()) {
10048
+ if (owners.length < 2) {
10049
+ continue;
10050
+ }
10051
+ for (const owner of owners) {
10052
+ addIssue(context, "key_duplicate", owner.path, owner.blockId, owner.tokenId);
10053
+ }
10054
+ }
10055
+ }
10056
+ function validatePendingReferences(context) {
10057
+ for (const pending of context.pendingCitations) {
10058
+ if (pending.token.keys.some((key) => !isValidDocument2Key(normalizeDocument2Key(key)))) {
10059
+ addIssue(
10060
+ context,
10061
+ "missing_citation",
10062
+ `${pending.path}.keys`,
10063
+ pending.blockId,
10064
+ pending.token.id
10065
+ );
10066
+ }
10067
+ }
10068
+ for (const pending of context.pendingReferences) {
10069
+ for (const key of pending.token.keys) {
10070
+ const normalized = normalizeDocument2Key(key);
10071
+ if (!isValidDocument2Key(normalized)) {
10072
+ addIssue(
10073
+ context,
10074
+ "missing_reference",
10075
+ `${pending.path}.keys`,
10076
+ pending.blockId,
10077
+ pending.token.id
10078
+ );
10079
+ break;
10080
+ }
10081
+ const kind = context.labelKinds.get(normalized);
10082
+ if (kind && pending.token.refCommand === "eqref" !== (kind === "eq")) {
10083
+ addIssue(
10084
+ context,
10085
+ "reference_kind_mismatch",
10086
+ `${pending.path}.keys`,
10087
+ pending.blockId,
10088
+ pending.token.id
10089
+ );
10090
+ break;
10091
+ }
10092
+ }
10093
+ }
10094
+ }
10095
+ function isRecord(value) {
10096
+ return typeof value === "object" && value !== null;
10097
+ }
10098
+ function hasRuntimeDocumentShape(document2) {
10099
+ const value = document2;
10100
+ return isRecord(value) && value.nodeType === "DocumentObject";
10101
+ }
10102
+ function hasWireDocumentShape(document2) {
10103
+ const value = document2;
10104
+ if (!isRecord(value) || value.node_type !== "DocumentObject" || !Array.isArray(value.blocks)) {
10105
+ return false;
10106
+ }
10107
+ if (value.meta !== void 0 && !isRecord(value.meta)) {
10108
+ return false;
10109
+ }
10110
+ if (value.references !== void 0) {
10111
+ if (!Array.isArray(value.references)) {
10112
+ return false;
10113
+ }
10114
+ for (const reference of value.references) {
10115
+ if (!isRecord(reference) || typeof reference.key !== "string" || reference.key.trim().length === 0) {
10116
+ return false;
10117
+ }
10118
+ }
10119
+ }
10120
+ return true;
10121
+ }
10122
+ function resolveDocument(document2) {
10123
+ if (hasRuntimeDocumentShape(document2)) {
10124
+ try {
10125
+ toDocumentJson2(document2);
10126
+ return document2;
10127
+ } catch {
10128
+ return null;
10129
+ }
10130
+ }
10131
+ if (!hasWireDocumentShape(document2)) {
10132
+ return null;
10133
+ }
10134
+ try {
10135
+ return fromDocumentJson2(document2);
10136
+ } catch {
10137
+ return null;
10138
+ }
10139
+ }
10140
+ function validateDocument2(document2) {
10141
+ const resolved = resolveDocument(document2);
10142
+ if (!resolved) {
10143
+ return {
10144
+ valid: false,
10145
+ issues: [{ code: "malformed_document", path: "$" }]
10146
+ };
10147
+ }
10148
+ const context = {
10149
+ issues: [],
10150
+ keyOwners: [],
10151
+ labelKinds: /* @__PURE__ */ new Map(),
10152
+ pendingCitations: [],
10153
+ pendingReferences: []
10154
+ };
10155
+ validateTextValue(context, resolved.meta.title, "$.meta.title");
10156
+ validateTextValue(context, resolved.meta.authors, "$.meta.authors");
10157
+ validateTextValue(context, resolved.meta.abstract, "$.meta.abstract");
10158
+ resolved.references.forEach((reference, index) => {
10159
+ const path = `$.references[${String(index)}]`;
10160
+ registerKey(context, reference.key, `${path}.key`, "reference");
10161
+ validateTextValue(context, reference.authors, `${path}.authors`);
10162
+ validateTextValue(context, reference.title, `${path}.title`);
10163
+ validateTextValue(context, reference.year, `${path}.year`);
10164
+ validateTextValue(context, reference.url, `${path}.url`);
10165
+ validateTextValue(context, reference.venue, `${path}.venue`);
10166
+ });
10167
+ resolved.blocks.forEach((block, index) => {
10168
+ validateBlock(context, block, `$.blocks[${String(index)}]`);
10169
+ });
10170
+ for (const diagnostic of resolved.diagnostics) {
10171
+ addIssue(context, "parser_diagnostic", diagnostic.path);
10172
+ }
10173
+ validateDuplicateKeys(context);
10174
+ validatePendingReferences(context);
10175
+ return {
10176
+ valid: context.issues.length === 0,
10177
+ issues: context.issues
10178
+ };
10179
+ }
10180
+ function isDocument2Valid(document2) {
10181
+ return validateDocument2(document2).valid;
10182
+ }
10183
+
9827
10184
  // src/document2/history.ts
9828
10185
  var DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH = 100;
9829
10186
  function createDocument2History(maxDepth = DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH) {
@@ -10413,10 +10770,6 @@ function document2Messages(locale) {
10413
10770
  }
10414
10771
 
10415
10772
  // src/react-document2/xelatexTextValidation.ts
10416
- var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
10417
- function hasXeLatexUnsupportedEmoji(value) {
10418
- return XELATEX_UNSUPPORTED_EMOJI_PATTERN.test(value);
10419
- }
10420
10773
  function unsupportedEmojiMessage(uiLocale) {
10421
10774
  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.";
10422
10775
  }
@@ -10657,6 +11010,9 @@ function focusArticleMetaPanel(panel) {
10657
11010
  }
10658
11011
  }
10659
11012
 
11013
+ // src/react-document2/ValidatedButexDocumentEditor2.tsx
11014
+ import { forwardRef as forwardRef3, useCallback as useCallback3, useRef as useRef9 } from "react";
11015
+
10660
11016
  // src/react-document2/ButexDocumentEditor2.tsx
10661
11017
  import { forwardRef as forwardRef2, useCallback as useCallback2, useEffect as useEffect10, useImperativeHandle as useImperativeHandle2, useMemo as useMemo2, useRef as useRef8, useState as useState12 } from "react";
10662
11018
 
@@ -12790,6 +13146,28 @@ function DocumentInsertToolbar({
12790
13146
 
12791
13147
  // src/react-document2/DocumentPreview.tsx
12792
13148
  import { Fragment as Fragment3, jsx as jsx11, jsxs as jsxs10 } from "react/jsx-runtime";
13149
+ var REF_KIND_NAMES = [
13150
+ { names: ["\u0627\u0644\u0634\u0643\u0644", "\u0634\u0643\u0644", "Figure", "figure"], prefixes: ["\u0627\u0644\u0634\u0643\u0644 ", "Figure "] },
13151
+ { names: ["\u0627\u0644\u062C\u062F\u0648\u0644", "\u062C\u062F\u0648\u0644", "Table", "table"], prefixes: ["\u0627\u0644\u062C\u062F\u0648\u0644 ", "Table "] },
13152
+ { 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 "] }
13153
+ ];
13154
+ function contextualRefLabel(inlines, index, label) {
13155
+ const previous = inlines[index - 1];
13156
+ if (previous?.kind !== "text") {
13157
+ return label;
13158
+ }
13159
+ const previousText = previous.text.trimEnd();
13160
+ for (const group of REF_KIND_NAMES) {
13161
+ if (!group.names.some((name) => previousText.endsWith(name))) {
13162
+ continue;
13163
+ }
13164
+ const prefix = group.prefixes.find((candidate) => label.startsWith(candidate));
13165
+ if (prefix) {
13166
+ return label.slice(prefix.length);
13167
+ }
13168
+ }
13169
+ return label;
13170
+ }
12793
13171
  function PreviewInlines({ inlines, output }) {
12794
13172
  return /* @__PURE__ */ jsx11(Fragment3, { children: inlines.map((inline, index) => {
12795
13173
  if (inline.kind === "text") {
@@ -12809,7 +13187,7 @@ function PreviewInlines({ inlines, output }) {
12809
13187
  return /* @__PURE__ */ jsx11("span", { className: "butex-document2-widget__preview-cite", children: inline.label }, inline.id);
12810
13188
  }
12811
13189
  if (inline.kind === "ref") {
12812
- return /* @__PURE__ */ jsx11("span", { className: "butex-document2-widget__preview-cite butex-document2-widget__preview-ref", children: inline.label }, inline.id);
13190
+ return /* @__PURE__ */ jsx11("span", { className: "butex-document2-widget__preview-cite butex-document2-widget__preview-ref", children: contextualRefLabel(inlines, index, inline.label) }, inline.id);
12813
13191
  }
12814
13192
  return /* @__PURE__ */ jsx11(MathIsland, { id: inline.id, tex: inline.tex, display: inline.display, output }, inline.id);
12815
13193
  }) });
@@ -19138,8 +19516,102 @@ var ButexDocumentEditor2 = forwardRef2(
19138
19516
  }
19139
19517
  );
19140
19518
  ButexDocumentEditor2.displayName = "ButexDocumentEditor2";
19519
+
19520
+ // src/react-document2/ValidatedButexDocumentEditor2.tsx
19521
+ import { jsx as jsx18, jsxs as jsxs17 } from "react/jsx-runtime";
19522
+ function imageValidationMessage(uiLocale) {
19523
+ 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.";
19524
+ }
19525
+ function imageHasIdentity(ctx) {
19526
+ return Boolean(ctx.assetId?.trim()) || ctx.value.trim().length > 0;
19527
+ }
19528
+ function isMalformedInitialDocument(initialDocument) {
19529
+ if (initialDocument === void 0) {
19530
+ return false;
19531
+ }
19532
+ return validateDocument2(initialDocument).issues.some((issue) => issue.code === "malformed_document");
19533
+ }
19534
+ var ButexDocumentEditor23 = forwardRef3(
19535
+ function ValidatedButexDocumentEditor2({
19536
+ initialDocument,
19537
+ uiLocale = "ar",
19538
+ onDocumentChange,
19539
+ onValidityChange,
19540
+ renderImageBlockEditor,
19541
+ ...rest
19542
+ }, ref) {
19543
+ const lastValidityRef = useRef9(null);
19544
+ const initialDocumentRef = useRef9(initialDocument);
19545
+ const awaitingInitialResolutionRef = useRef9(true);
19546
+ const malformedInitialDocument = isMalformedInitialDocument(initialDocument);
19547
+ if (initialDocumentRef.current !== initialDocument) {
19548
+ initialDocumentRef.current = initialDocument;
19549
+ awaitingInitialResolutionRef.current = true;
19550
+ lastValidityRef.current = null;
19551
+ }
19552
+ const emitValidity = useCallback3(
19553
+ (isValid) => {
19554
+ if (lastValidityRef.current === isValid) {
19555
+ return;
19556
+ }
19557
+ lastValidityRef.current = isValid;
19558
+ onValidityChange?.(isValid);
19559
+ },
19560
+ [onValidityChange]
19561
+ );
19562
+ const handleDocumentChange = useCallback3(
19563
+ (document2) => {
19564
+ const validationTarget = initialDocument !== void 0 && (malformedInitialDocument || awaitingInitialResolutionRef.current) ? initialDocument : document2;
19565
+ awaitingInitialResolutionRef.current = false;
19566
+ emitValidity(isDocument2Valid(validationTarget));
19567
+ onDocumentChange?.(document2);
19568
+ },
19569
+ [emitValidity, initialDocument, malformedInitialDocument, onDocumentChange]
19570
+ );
19571
+ const handleImageBlockEditor = useCallback3(
19572
+ (ctx) => {
19573
+ const rendered = renderImageBlockEditor?.(ctx) ?? ctx.defaultUi;
19574
+ if (imageHasIdentity(ctx)) {
19575
+ return rendered;
19576
+ }
19577
+ const errorId = `butex-document2-image-validation-${ctx.blockId}`;
19578
+ return /* @__PURE__ */ jsxs17(
19579
+ "div",
19580
+ {
19581
+ role: "group",
19582
+ "aria-invalid": "true",
19583
+ "aria-describedby": errorId,
19584
+ style: {
19585
+ border: "1px solid var(--butex-document2-error)",
19586
+ background: "var(--butex-document2-error-bg)",
19587
+ borderRadius: "0.65rem",
19588
+ padding: "0.6rem"
19589
+ },
19590
+ children: [
19591
+ rendered,
19592
+ /* @__PURE__ */ jsx18("p", { id: errorId, className: "butex-document2-widget__field-error", children: imageValidationMessage(ctx.uiLocale) })
19593
+ ]
19594
+ }
19595
+ );
19596
+ },
19597
+ [renderImageBlockEditor]
19598
+ );
19599
+ return /* @__PURE__ */ jsx18(
19600
+ ButexDocumentEditor2,
19601
+ {
19602
+ ref,
19603
+ ...rest,
19604
+ initialDocument,
19605
+ uiLocale,
19606
+ onDocumentChange: handleDocumentChange,
19607
+ renderImageBlockEditor: handleImageBlockEditor
19608
+ }
19609
+ );
19610
+ }
19611
+ );
19612
+ ButexDocumentEditor23.displayName = "ButexDocumentEditor2";
19141
19613
  export {
19142
- ButexDocumentEditor2,
19614
+ ButexDocumentEditor23 as ButexDocumentEditor2,
19143
19615
  DOCUMENT2_WIDGET_CSS,
19144
19616
  DocumentInsertToolbar,
19145
19617
  DocumentPreview,