@drghaliasri/butex 6.1.4 → 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();
@@ -7100,6 +7107,287 @@ ${body}
7100
7107
  `;
7101
7108
  }
7102
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
+
7103
7391
  // src/document2/history.ts
7104
7392
  var DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH = 100;
7105
7393
  function createDocument2History(maxDepth = DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH) {
@@ -7374,6 +7662,15 @@ function document2Preview(document2, output = "svg", equationSide = "arabic", pr
7374
7662
  mathIslands
7375
7663
  };
7376
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
+ }
7377
7674
  export {
7378
7675
  BUTEX_BASMALA,
7379
7676
  BUTEX_BASMALA_LINE1,
@@ -7384,6 +7681,7 @@ export {
7384
7681
  DEFAULT_HIJRI_DATE,
7385
7682
  DEFAULT_REFERENCE_FIELD_SEPARATOR,
7386
7683
  Document2CommandError,
7684
+ Document2ValidationError,
7387
7685
  HIJRI_MONTH_IDS,
7388
7686
  HIJRI_YEAR_MAX,
7389
7687
  HIJRI_YEAR_MIN,
@@ -7429,7 +7727,7 @@ export {
7429
7727
  document2HistoryCanRedo,
7430
7728
  document2HistoryCanUndo,
7431
7729
  document2KeyError,
7432
- document2Latex,
7730
+ document2Latex2 as document2Latex,
7433
7731
  document2Outline,
7434
7732
  document2Preview,
7435
7733
  emptyBlockSelection,
@@ -7446,6 +7744,7 @@ export {
7446
7744
  formatRefLabel,
7447
7745
  fromDocumentJson2,
7448
7746
  getArabicXeLatexPreamble,
7747
+ hasXeLatexUnsupportedEmoji,
7449
7748
  hijriMonthLabel,
7450
7749
  inlineFieldLatex,
7451
7750
  insertCiteTokenAtCaret,
@@ -7453,6 +7752,7 @@ export {
7453
7752
  insertMathToken,
7454
7753
  insertMathTokenAtCaret,
7455
7754
  insertRefTokenAtCaret,
7755
+ isDocument2Valid,
7456
7756
  isDuplicateDocument2Key,
7457
7757
  isValidDocument2Key,
7458
7758
  labelNumberMap,
@@ -7506,6 +7806,7 @@ export {
7506
7806
  updateDocument2TextBlockCentered,
7507
7807
  updateMathTokenLabel,
7508
7808
  updateRefTokenKeys,
7509
- updateTextToken
7809
+ updateTextToken,
7810
+ validateDocument2
7510
7811
  };
7511
7812
  //# sourceMappingURL=document2.mjs.map