@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.
@@ -913,14 +913,22 @@ function labelNumberMap(document2) {
913
913
  }
914
914
  return map;
915
915
  }
916
+ function labelKindWord(kind, uiLocale) {
917
+ if (kind === "fig") {
918
+ return uiLocale === "ar" ? "\u0627\u0644\u0634\u0643\u0644" : "Figure";
919
+ }
920
+ if (kind === "tab") {
921
+ return uiLocale === "ar" ? "\u0627\u0644\u062C\u062F\u0648\u0644" : "Table";
922
+ }
923
+ return uiLocale === "ar" ? "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629" : "Equation";
924
+ }
916
925
  function formatFloatCaptionTitle(kind, number, options = {}) {
917
926
  const locale = options.uiLocale ?? "ar";
918
- 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";
919
927
  const digits = formatBibliographyNumber(number, {
920
928
  documentDirection: options.documentDirection ?? (locale === "ar" ? "rtl" : "ltr"),
921
929
  digitForm: options.digitForm ?? (locale === "ar" ? "arabicIndic" : "western")
922
930
  });
923
- return `${kindWord} ${digits}`;
931
+ return `${labelKindWord(kind, locale)} ${digits}`;
924
932
  }
925
933
  function formatFloatCaptionPreview(title, caption, uiLocale = "ar") {
926
934
  const trimmed = caption?.trim() ?? "";
@@ -933,19 +941,18 @@ function formatFloatCaptionPreview(title, caption, uiLocale = "ar") {
933
941
  function formatRefLabel(keys, document2, refCommand, options = {}) {
934
942
  const documentDirection = options.documentDirection ?? "rtl";
935
943
  const digitForm = options.digitForm ?? (documentDirection === "rtl" ? "arabicIndic" : "western");
944
+ const uiLocale = documentDirection === "rtl" ? "ar" : "en";
936
945
  const map = labelNumberMap(document2);
937
946
  const parts = keys.map((key) => {
938
947
  const hit = map.get(key);
939
948
  if (!hit) {
940
- return "?";
949
+ return refCommand === "eqref" ? "(?)" : "?";
941
950
  }
942
- return formatDigits(String(hit.number), digitForm);
951
+ const digits = formatDigits(String(hit.number), digitForm);
952
+ const numberLabel = refCommand === "eqref" ? `(${digits})` : digits;
953
+ return `${labelKindWord(hit.kind, uiLocale)} ${numberLabel}`;
943
954
  });
944
- const body = parts.join(documentDirection === "rtl" ? "\u060C" : ", ");
945
- if (refCommand === "eqref") {
946
- return `(${body})`;
947
- }
948
- return body;
955
+ return parts.join(documentDirection === "rtl" ? "\u060C" : ", ");
949
956
  }
950
957
  function stripDisplayMathDelimiters(source) {
951
958
  const trimmed = source.trim();
@@ -6837,7 +6844,43 @@ function styledTextLatex(text, style) {
6837
6844
  }
6838
6845
  return output;
6839
6846
  }
6840
- function tokenLatex(token) {
6847
+ function semanticRefPrefix(kind) {
6848
+ if (kind === "fig") {
6849
+ return "\u0627\u0644\u0634\u0643\u0644";
6850
+ }
6851
+ if (kind === "tab") {
6852
+ return "\u0627\u0644\u062C\u062F\u0648\u0644";
6853
+ }
6854
+ return "\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629";
6855
+ }
6856
+ function semanticRefNames(kind) {
6857
+ if (kind === "fig") {
6858
+ return ["\u0627\u0644\u0634\u0643\u0644", "\u0634\u0643\u0644", "Figure", "figure"];
6859
+ }
6860
+ if (kind === "tab") {
6861
+ return ["\u0627\u0644\u062C\u062F\u0648\u0644", "\u062C\u062F\u0648\u0644", "Table", "table"];
6862
+ }
6863
+ return ["\u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629", "\u0645\u0639\u0627\u062F\u0644\u0629", "Equation", "equation"];
6864
+ }
6865
+ function previousTextAlreadyNamesKind(previousToken, kind) {
6866
+ if (previousToken?.kind !== "text") {
6867
+ return false;
6868
+ }
6869
+ const text = previousToken.text.trimEnd();
6870
+ return semanticRefNames(kind).some((name) => text.endsWith(name));
6871
+ }
6872
+ function refTokenLatexForDocument(token, labelKinds, previousToken) {
6873
+ const reference = refTokenLatex(token.keys, token.refCommand);
6874
+ if (!labelKinds || token.keys.length !== 1) {
6875
+ return reference;
6876
+ }
6877
+ const kind = labelKinds.get(token.keys[0]);
6878
+ if (!kind || previousTextAlreadyNamesKind(previousToken, kind)) {
6879
+ return reference;
6880
+ }
6881
+ return `${semanticRefPrefix(kind)}~${reference}`;
6882
+ }
6883
+ function tokenLatex(token, labelKinds, previousToken) {
6841
6884
  if (token.kind === "text") {
6842
6885
  return styledTextLatex(token.text, token.style);
6843
6886
  }
@@ -6845,7 +6888,7 @@ function tokenLatex(token) {
6845
6888
  return citeTokenLatex(token.keys);
6846
6889
  }
6847
6890
  if (token.kind === "ref") {
6848
- return refTokenLatex(token.keys, token.refCommand);
6891
+ return refTokenLatexForDocument(token, labelKinds, previousToken);
6849
6892
  }
6850
6893
  if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
6851
6894
  const body = mathBodyLatex(token);
@@ -6862,11 +6905,14 @@ ${body}
6862
6905
  }
6863
6906
  return renderMathNodeLatex(token.math);
6864
6907
  }
6908
+ function inlineFieldLatexWithLabelKinds(field, labelKinds) {
6909
+ return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
6910
+ }
6865
6911
  function inlineFieldLatex(field) {
6866
- return field.tokens.map((token) => tokenLatex(token)).join("");
6912
+ return inlineFieldLatexWithLabelKinds(field);
6867
6913
  }
6868
- function textBlockLatex(block) {
6869
- const content = inlineFieldLatex(block.field);
6914
+ function textBlockLatex(block, labelKinds) {
6915
+ const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
6870
6916
  const body = block.command === "\\paragraph" ? `\\par
6871
6917
  ${content}` : `${block.command}{${content}}`;
6872
6918
  if (block.command === "\\paragraph" && block.centered) {
@@ -6879,38 +6925,61 @@ ${body}
6879
6925
  function indent(value) {
6880
6926
  return value.split("\n").map((line) => ` ${line}`).join("\n");
6881
6927
  }
6882
- function listBlockLatex(block, references) {
6928
+ function listBlockLatex(block, references, labelKinds, fixedFloats) {
6883
6929
  const items = block.items.map((item) => {
6884
- const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references))).join("");
6885
- return ` \\item ${inlineFieldLatex(item.field)}${nested}`;
6930
+ const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
6931
+ return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
6886
6932
  }).join("\n");
6887
6933
  return `${block.command}
6888
6934
  ${items}
6889
6935
  ${block.closing}`;
6890
6936
  }
6891
- function tabularInnerLatex(block) {
6892
- const rows = block.rows.map((row) => ` ${row.map((cell) => inlineFieldLatex(cell)).join(" & ")}`).join(" \\\\\n");
6937
+ function defaultGridColumnSpec(block) {
6938
+ const compact = block.columns.replace(/\s+/g, "");
6939
+ const columnCount = block.rows[0]?.length ?? 0;
6940
+ if (columnCount === 0 || compact.length !== columnCount || !/^[lcr]+$/.test(compact)) {
6941
+ return null;
6942
+ }
6943
+ return `|${compact.split("").join("|")}|`;
6944
+ }
6945
+ function tabularInnerLatex(block, labelKinds) {
6946
+ const rows = block.rows.map(
6947
+ (row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
6948
+ );
6949
+ const gridColumns = defaultGridColumnSpec(block);
6950
+ if (gridColumns) {
6951
+ const griddedRows = rows.map((row) => `${row} \\\\ \\hline`).join("\n");
6952
+ return `${block.command}{${gridColumns}}
6953
+ \\hline
6954
+ ${griddedRows}
6955
+ ${block.closing}`;
6956
+ }
6893
6957
  return `${block.command}{${block.columns}}
6894
- ${rows}
6958
+ ${rows.join(" \\\\\n")}
6895
6959
  ${block.closing}`;
6896
6960
  }
6897
- function floatCaptionLabelLines(block) {
6961
+ function floatCaptionLabelLines(block, counter) {
6898
6962
  const lines = [];
6899
- if (block.captionEnabled && block.caption.trim().length > 0) {
6963
+ const hasCaption = block.captionEnabled && block.caption.trim().length > 0;
6964
+ if (hasCaption) {
6900
6965
  lines.push(` \\caption{${block.caption.trim()}}`);
6901
6966
  }
6902
6967
  if (block.labelEnabled && block.label.trim().length > 0) {
6968
+ if (!hasCaption) {
6969
+ lines.push(` \\refstepcounter{${counter}}`);
6970
+ }
6903
6971
  lines.push(` \\label{${block.label.trim()}}`);
6904
6972
  }
6905
6973
  return lines;
6906
6974
  }
6907
- function tableBlockLatex(block) {
6908
- const lines = ["\\begin{table}[htbp]"];
6975
+ function tableBlockLatex(block, labelKinds, fixedFloats) {
6976
+ const placement = fixedFloats ? "H" : "htbp";
6977
+ const lines = [`\\begin{table}[${placement}]`];
6909
6978
  if (block.centered) {
6910
6979
  lines.push(" \\centering");
6911
6980
  }
6912
- lines.push(...floatCaptionLabelLines(block));
6913
- lines.push(indent(tabularInnerLatex(block)));
6981
+ lines.push(...floatCaptionLabelLines(block, "table"));
6982
+ lines.push(indent(tabularInnerLatex(block, labelKinds)));
6914
6983
  lines.push("\\end{table}");
6915
6984
  return lines.join("\n");
6916
6985
  }
@@ -6929,28 +6998,29 @@ function imageOptionsLatex(block, constrainToColumn) {
6929
6998
  }
6930
6999
  return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
6931
7000
  }
6932
- function imageBlockLatex(block) {
6933
- const lines = ["\\begin{figure}[htbp]"];
7001
+ function imageBlockLatex(block, fixedFloats) {
7002
+ const placement = fixedFloats ? "H" : "htbp";
7003
+ const lines = [`\\begin{figure}[${placement}]`];
6934
7004
  if (block.centered) {
6935
7005
  lines.push(" \\centering");
6936
7006
  }
6937
7007
  lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
6938
- lines.push(...floatCaptionLabelLines(block));
7008
+ lines.push(...floatCaptionLabelLines(block, "figure"));
6939
7009
  lines.push("\\end{figure}");
6940
7010
  return lines.join("\n");
6941
7011
  }
6942
- function blockLatex(block, references) {
7012
+ function blockLatex(block, references, labelKinds, fixedFloats) {
6943
7013
  if (block.kind === "textBlock") {
6944
- return textBlockLatex(block);
7014
+ return textBlockLatex(block, labelKinds);
6945
7015
  }
6946
7016
  if (block.kind === "list") {
6947
- return listBlockLatex(block, references);
7017
+ return listBlockLatex(block, references, labelKinds, fixedFloats);
6948
7018
  }
6949
7019
  if (block.kind === "table") {
6950
- return tableBlockLatex(block);
7020
+ return tableBlockLatex(block, labelKinds, fixedFloats);
6951
7021
  }
6952
7022
  if (block.kind === "image") {
6953
- return imageBlockLatex(block);
7023
+ return imageBlockLatex(block, fixedFloats);
6954
7024
  }
6955
7025
  if (block.kind === "bibliography") {
6956
7026
  const width = String(Math.max(references.length, 9));
@@ -6992,12 +7062,29 @@ ${meta.abstract.trim()}
6992
7062
  }
6993
7063
  return parts;
6994
7064
  }
7065
+ function documentLabelKindMap(document2) {
7066
+ const map = /* @__PURE__ */ new Map();
7067
+ for (const entry of collectDocument2Labels(document2)) {
7068
+ if (!map.has(entry.key)) {
7069
+ map.set(entry.key, entry.kind);
7070
+ }
7071
+ }
7072
+ return map;
7073
+ }
7074
+ function closingHamdalaLatex() {
7075
+ return `\\begin{center}
7076
+ \\maghribi{${BUTEX_CLOSING_HAMDALA}}
7077
+ \\end{center}`;
7078
+ }
6995
7079
  function document2Latex(document2, options = {}) {
6996
7080
  const meta = document2.meta ?? emptyDocument2Meta();
6997
7081
  const twocolumn = options.twocolumn === true;
6998
7082
  const front = articleFrontMatterLatex(meta);
6999
- const bodyBlocks = document2.blocks.map((block) => blockLatex(block, document2.references));
7000
- const closing = butexMaghribiDisplayLatex(BUTEX_CLOSING_HAMDALA);
7083
+ const labelKinds = documentLabelKindMap(document2);
7084
+ const bodyBlocks = document2.blocks.map(
7085
+ (block) => blockLatex(block, document2.references, labelKinds, twocolumn)
7086
+ );
7087
+ const closing = closingHamdalaLatex();
7001
7088
  const body = [...front, ...bodyBlocks, closing].filter((part) => part.length > 0).join("\n\n");
7002
7089
  const wrapDocument = options.wrapDocument !== false;
7003
7090
  if (!wrapDocument) {
@@ -7006,10 +7093,11 @@ function document2Latex(document2, options = {}) {
7006
7093
 
7007
7094
  ${body}` : body;
7008
7095
  }
7096
+ const floatPackage = twocolumn ? "\n\\usepackage{float}\n" : "\n";
7009
7097
  const preamble = getArabicXeLatexPreamble({
7010
7098
  digitsMapping: options.digitsMapping ?? digitsMappingFromDigitForm(options.digitForm),
7011
7099
  twocolumn
7012
- }) + "\n" + articleMaketitlePreamble(meta, twocolumn);
7100
+ }) + floatPackage + articleMaketitlePreamble(meta, twocolumn);
7013
7101
  return `${preamble}
7014
7102
  \\begin{document}
7015
7103
 
@@ -7019,6 +7107,287 @@ ${body}
7019
7107
  `;
7020
7108
  }
7021
7109
 
7110
+ // src/document2/validation.ts
7111
+ var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
7112
+ function hasXeLatexUnsupportedEmoji(value) {
7113
+ return XELATEX_UNSUPPORTED_EMOJI_PATTERN.test(value);
7114
+ }
7115
+ function addIssue(context, code, path, blockId, tokenId) {
7116
+ context.issues.push({
7117
+ code,
7118
+ path,
7119
+ ...blockId ? { blockId } : {},
7120
+ ...tokenId ? { tokenId } : {}
7121
+ });
7122
+ }
7123
+ function validateTextValue(context, value, path, blockId, tokenId) {
7124
+ if (hasXeLatexUnsupportedEmoji(value)) {
7125
+ addIssue(context, "unsupported_emoji", path, blockId, tokenId);
7126
+ }
7127
+ }
7128
+ function registerKey(context, key, path, kind, blockId, tokenId) {
7129
+ const normalized = normalizeDocument2Key(key);
7130
+ if (normalized.length === 0) {
7131
+ addIssue(context, "key_empty", path, blockId, tokenId);
7132
+ return;
7133
+ }
7134
+ if (!isValidDocument2Key(normalized)) {
7135
+ addIssue(context, "key_invalid", path, blockId, tokenId);
7136
+ return;
7137
+ }
7138
+ context.keyOwners.push({
7139
+ key: normalized,
7140
+ path,
7141
+ kind,
7142
+ ...blockId ? { blockId } : {},
7143
+ ...tokenId ? { tokenId } : {}
7144
+ });
7145
+ if (kind !== "reference" && !context.labelKinds.has(normalized)) {
7146
+ context.labelKinds.set(normalized, kind);
7147
+ }
7148
+ }
7149
+ function validateToken(context, token, path, blockId) {
7150
+ if (token.kind === "text") {
7151
+ validateTextValue(context, token.text, `${path}.text`, blockId, token.id);
7152
+ return;
7153
+ }
7154
+ if (token.kind === "cite") {
7155
+ if (token.keys.length === 0) {
7156
+ addIssue(context, "empty_citation", `${path}.keys`, blockId, token.id);
7157
+ }
7158
+ context.pendingCitations.push({ token, path, blockId });
7159
+ return;
7160
+ }
7161
+ if (token.kind === "ref") {
7162
+ if (token.keys.length === 0) {
7163
+ addIssue(context, "empty_reference", `${path}.keys`, blockId, token.id);
7164
+ }
7165
+ context.pendingReferences.push({ token, path, blockId });
7166
+ return;
7167
+ }
7168
+ validateTextValue(context, token.source, `${path}.source`, blockId, token.id);
7169
+ if (!token.labelEnabled) {
7170
+ return;
7171
+ }
7172
+ if (!token.display) {
7173
+ addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
7174
+ return;
7175
+ }
7176
+ registerKey(context, token.label ?? "", `${path}.label`, "eq", blockId, token.id);
7177
+ }
7178
+ function validateInlineField(context, field, path, blockId) {
7179
+ field.tokens.forEach((token, index) => {
7180
+ validateToken(context, token, `${path}.tokens[${String(index)}]`, blockId);
7181
+ });
7182
+ }
7183
+ function validateBlock(context, block, path) {
7184
+ if (block.kind === "textBlock") {
7185
+ validateInlineField(context, block.field, `${path}.field`, block.id);
7186
+ return;
7187
+ }
7188
+ if (block.kind === "list") {
7189
+ block.items.forEach((item, itemIndex) => {
7190
+ const itemPath = `${path}.items[${String(itemIndex)}]`;
7191
+ validateInlineField(context, item.field, `${itemPath}.field`, block.id);
7192
+ item.blocks.forEach((child, childIndex) => {
7193
+ validateBlock(context, child, `${itemPath}.blocks[${String(childIndex)}]`);
7194
+ });
7195
+ });
7196
+ return;
7197
+ }
7198
+ if (block.kind === "table") {
7199
+ if (block.columns.trim().length === 0 || block.rows.length === 0) {
7200
+ addIssue(context, "invalid_table", path, block.id);
7201
+ }
7202
+ const expectedColumns = block.rows[0]?.length ?? 0;
7203
+ if (expectedColumns === 0 || block.rows.some((row) => row.length !== expectedColumns)) {
7204
+ addIssue(context, "invalid_table", `${path}.rows`, block.id);
7205
+ }
7206
+ block.rows.forEach((row, rowIndex) => {
7207
+ row.forEach((cell, columnIndex) => {
7208
+ validateInlineField(
7209
+ context,
7210
+ cell,
7211
+ `${path}.rows[${String(rowIndex)}][${String(columnIndex)}]`,
7212
+ block.id
7213
+ );
7214
+ });
7215
+ });
7216
+ validateTextValue(context, block.caption, `${path}.caption`, block.id);
7217
+ if (block.labelEnabled) {
7218
+ registerKey(context, block.label, `${path}.label`, "tab", block.id);
7219
+ }
7220
+ return;
7221
+ }
7222
+ if (block.kind === "image") {
7223
+ const hasAssetId = typeof block.assetId === "string" && block.assetId.trim().length > 0;
7224
+ const hasValue = block.value.trim().length > 0;
7225
+ if (!hasAssetId && !hasValue) {
7226
+ addIssue(context, "empty_image", path, block.id);
7227
+ }
7228
+ validateTextValue(context, block.value, `${path}.value`, block.id);
7229
+ validateTextValue(context, block.caption, `${path}.caption`, block.id);
7230
+ if (block.labelEnabled) {
7231
+ registerKey(context, block.label, `${path}.label`, "fig", block.id);
7232
+ }
7233
+ return;
7234
+ }
7235
+ if (block.kind === "raw") {
7236
+ validateTextValue(context, block.value, `${path}.value`, block.id);
7237
+ }
7238
+ }
7239
+ function validateDuplicateKeys(context) {
7240
+ const grouped = /* @__PURE__ */ new Map();
7241
+ for (const owner of context.keyOwners) {
7242
+ const group = grouped.get(owner.key) ?? [];
7243
+ group.push(owner);
7244
+ grouped.set(owner.key, group);
7245
+ }
7246
+ for (const owners of grouped.values()) {
7247
+ if (owners.length < 2) {
7248
+ continue;
7249
+ }
7250
+ for (const owner of owners) {
7251
+ addIssue(context, "key_duplicate", owner.path, owner.blockId, owner.tokenId);
7252
+ }
7253
+ }
7254
+ }
7255
+ function validatePendingReferences(context) {
7256
+ for (const pending of context.pendingCitations) {
7257
+ if (pending.token.keys.some((key) => !isValidDocument2Key(normalizeDocument2Key(key)))) {
7258
+ addIssue(
7259
+ context,
7260
+ "missing_citation",
7261
+ `${pending.path}.keys`,
7262
+ pending.blockId,
7263
+ pending.token.id
7264
+ );
7265
+ }
7266
+ }
7267
+ for (const pending of context.pendingReferences) {
7268
+ for (const key of pending.token.keys) {
7269
+ const normalized = normalizeDocument2Key(key);
7270
+ if (!isValidDocument2Key(normalized)) {
7271
+ addIssue(
7272
+ context,
7273
+ "missing_reference",
7274
+ `${pending.path}.keys`,
7275
+ pending.blockId,
7276
+ pending.token.id
7277
+ );
7278
+ break;
7279
+ }
7280
+ const kind = context.labelKinds.get(normalized);
7281
+ if (kind && pending.token.refCommand === "eqref" !== (kind === "eq")) {
7282
+ addIssue(
7283
+ context,
7284
+ "reference_kind_mismatch",
7285
+ `${pending.path}.keys`,
7286
+ pending.blockId,
7287
+ pending.token.id
7288
+ );
7289
+ break;
7290
+ }
7291
+ }
7292
+ }
7293
+ }
7294
+ function isRecord(value) {
7295
+ return typeof value === "object" && value !== null;
7296
+ }
7297
+ function hasRuntimeDocumentShape(document2) {
7298
+ const value = document2;
7299
+ return isRecord(value) && value.nodeType === "DocumentObject";
7300
+ }
7301
+ function hasWireDocumentShape(document2) {
7302
+ const value = document2;
7303
+ if (!isRecord(value) || value.node_type !== "DocumentObject" || !Array.isArray(value.blocks)) {
7304
+ return false;
7305
+ }
7306
+ if (value.meta !== void 0 && !isRecord(value.meta)) {
7307
+ return false;
7308
+ }
7309
+ if (value.references !== void 0) {
7310
+ if (!Array.isArray(value.references)) {
7311
+ return false;
7312
+ }
7313
+ for (const reference of value.references) {
7314
+ if (!isRecord(reference) || typeof reference.key !== "string" || reference.key.trim().length === 0) {
7315
+ return false;
7316
+ }
7317
+ }
7318
+ }
7319
+ return true;
7320
+ }
7321
+ function resolveDocument(document2) {
7322
+ if (hasRuntimeDocumentShape(document2)) {
7323
+ try {
7324
+ toDocumentJson2(document2);
7325
+ return document2;
7326
+ } catch {
7327
+ return null;
7328
+ }
7329
+ }
7330
+ if (!hasWireDocumentShape(document2)) {
7331
+ return null;
7332
+ }
7333
+ try {
7334
+ return fromDocumentJson2(document2);
7335
+ } catch {
7336
+ return null;
7337
+ }
7338
+ }
7339
+ function validateDocument2(document2) {
7340
+ const resolved = resolveDocument(document2);
7341
+ if (!resolved) {
7342
+ return {
7343
+ valid: false,
7344
+ issues: [{ code: "malformed_document", path: "$" }]
7345
+ };
7346
+ }
7347
+ const context = {
7348
+ issues: [],
7349
+ keyOwners: [],
7350
+ labelKinds: /* @__PURE__ */ new Map(),
7351
+ pendingCitations: [],
7352
+ pendingReferences: []
7353
+ };
7354
+ validateTextValue(context, resolved.meta.title, "$.meta.title");
7355
+ validateTextValue(context, resolved.meta.authors, "$.meta.authors");
7356
+ validateTextValue(context, resolved.meta.abstract, "$.meta.abstract");
7357
+ resolved.references.forEach((reference, index) => {
7358
+ const path = `$.references[${String(index)}]`;
7359
+ registerKey(context, reference.key, `${path}.key`, "reference");
7360
+ validateTextValue(context, reference.authors, `${path}.authors`);
7361
+ validateTextValue(context, reference.title, `${path}.title`);
7362
+ validateTextValue(context, reference.year, `${path}.year`);
7363
+ validateTextValue(context, reference.url, `${path}.url`);
7364
+ validateTextValue(context, reference.venue, `${path}.venue`);
7365
+ });
7366
+ resolved.blocks.forEach((block, index) => {
7367
+ validateBlock(context, block, `$.blocks[${String(index)}]`);
7368
+ });
7369
+ for (const diagnostic of resolved.diagnostics) {
7370
+ addIssue(context, "parser_diagnostic", diagnostic.path);
7371
+ }
7372
+ validateDuplicateKeys(context);
7373
+ validatePendingReferences(context);
7374
+ return {
7375
+ valid: context.issues.length === 0,
7376
+ issues: context.issues
7377
+ };
7378
+ }
7379
+ function isDocument2Valid(document2) {
7380
+ return validateDocument2(document2).valid;
7381
+ }
7382
+ var Document2ValidationError = class extends Error {
7383
+ issues;
7384
+ constructor(issues) {
7385
+ super("Document2 validation failed; invalid drafts cannot be exported to LaTeX.");
7386
+ this.name = "Document2ValidationError";
7387
+ this.issues = issues;
7388
+ }
7389
+ };
7390
+
7022
7391
  // src/document2/history.ts
7023
7392
  var DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH = 100;
7024
7393
  function createDocument2History(maxDepth = DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH) {
@@ -7293,6 +7662,15 @@ function document2Preview(document2, output = "svg", equationSide = "arabic", pr
7293
7662
  mathIslands
7294
7663
  };
7295
7664
  }
7665
+
7666
+ // src/document2/validatedLatex.ts
7667
+ function document2Latex2(document2, options = {}) {
7668
+ const validation = validateDocument2(document2);
7669
+ if (!validation.valid) {
7670
+ throw new Document2ValidationError(validation.issues);
7671
+ }
7672
+ return document2Latex(document2, options);
7673
+ }
7296
7674
  export {
7297
7675
  BUTEX_BASMALA,
7298
7676
  BUTEX_BASMALA_LINE1,
@@ -7303,6 +7681,7 @@ export {
7303
7681
  DEFAULT_HIJRI_DATE,
7304
7682
  DEFAULT_REFERENCE_FIELD_SEPARATOR,
7305
7683
  Document2CommandError,
7684
+ Document2ValidationError,
7306
7685
  HIJRI_MONTH_IDS,
7307
7686
  HIJRI_YEAR_MAX,
7308
7687
  HIJRI_YEAR_MIN,
@@ -7348,7 +7727,7 @@ export {
7348
7727
  document2HistoryCanRedo,
7349
7728
  document2HistoryCanUndo,
7350
7729
  document2KeyError,
7351
- document2Latex,
7730
+ document2Latex2 as document2Latex,
7352
7731
  document2Outline,
7353
7732
  document2Preview,
7354
7733
  emptyBlockSelection,
@@ -7365,6 +7744,7 @@ export {
7365
7744
  formatRefLabel,
7366
7745
  fromDocumentJson2,
7367
7746
  getArabicXeLatexPreamble,
7747
+ hasXeLatexUnsupportedEmoji,
7368
7748
  hijriMonthLabel,
7369
7749
  inlineFieldLatex,
7370
7750
  insertCiteTokenAtCaret,
@@ -7372,6 +7752,7 @@ export {
7372
7752
  insertMathToken,
7373
7753
  insertMathTokenAtCaret,
7374
7754
  insertRefTokenAtCaret,
7755
+ isDocument2Valid,
7375
7756
  isDuplicateDocument2Key,
7376
7757
  isValidDocument2Key,
7377
7758
  labelNumberMap,
@@ -7425,6 +7806,7 @@ export {
7425
7806
  updateDocument2TextBlockCentered,
7426
7807
  updateMathTokenLabel,
7427
7808
  updateRefTokenKeys,
7428
- updateTextToken
7809
+ updateTextToken,
7810
+ validateDocument2
7429
7811
  };
7430
7812
  //# sourceMappingURL=document2.mjs.map