@drghaliasri/butex 5.5.2 → 5.5.3

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.
@@ -704,6 +704,66 @@ function fromMathObjectJson(json, mode = "english") {
704
704
  closing: json.closing
705
705
  };
706
706
  }
707
+ function scriptsToJson(node, path) {
708
+ return {
709
+ ...node.superscript ? { superscript: chainToJson(node.superscript, `${path}.superscript`) } : {},
710
+ ...node.subscript ? { subscript: chainToJson(node.subscript, `${path}.subscript`) } : {}
711
+ };
712
+ }
713
+ function nodeToJson(node, path) {
714
+ const scripts = scriptsToJson(node, path);
715
+ if (node instanceof CharNode || node instanceof NumberNode || node instanceof OperatorNode) {
716
+ return { node_type: node.nodeType, expr: node.expr, ...scripts };
717
+ }
718
+ if (node instanceof DelimiterNode) {
719
+ return {
720
+ node_type: "DelimiterObject",
721
+ left_delim_expr: node.leftDelimExpr,
722
+ inner_expr: chainToJson(node.innerExpr, `${path}.inner_expr`),
723
+ right_delim_expr: node.rightDelimExpr,
724
+ ...scripts
725
+ };
726
+ }
727
+ if (node instanceof CommandNode) {
728
+ return {
729
+ node_type: "CommandObject",
730
+ name: node.name,
731
+ optional_args: node.optionalArgs.map((arg, index) => chainToJson(arg, `${path}.optional_args[${String(index)}]`)),
732
+ mandatory_args: node.mandatoryArgs.map((arg, index) => chainToJson(arg, `${path}.mandatory_args[${String(index)}]`)),
733
+ ...scripts
734
+ };
735
+ }
736
+ if (node instanceof EnvNode) {
737
+ return {
738
+ node_type: "EnvObject",
739
+ opening: node.opening,
740
+ lines: node.lines.map((line, index) => chainToJson(line, `${path}.lines[${String(index)}]`)),
741
+ closing: node.closing,
742
+ ...scripts
743
+ };
744
+ }
745
+ throw new Error(`Unsupported runtime math node at ${path}: ${node.nodeType}`);
746
+ }
747
+ function chainToJson(chain, path) {
748
+ if (!(chain instanceof ChainNode)) {
749
+ throw new Error(`Expected live ChainNode at ${path}`);
750
+ }
751
+ return {
752
+ node_type: "ChainClass",
753
+ chain: chain.chain.map((node, index) => nodeToJson(node, `${path}.chain[${String(index)}]`))
754
+ };
755
+ }
756
+ function toMathObjectJson(math) {
757
+ if (math.nodeType !== "MathObject" || !Array.isArray(math.lines) || math.lines.length === 0) {
758
+ throw new Error("Cannot serialize invalid runtime MathObject");
759
+ }
760
+ return {
761
+ node_type: "MathObject",
762
+ math_mode: math.mathMode,
763
+ lines: math.lines.map((line, index) => chainToJson(line, `$.lines[${String(index)}]`)),
764
+ closing: math.closing
765
+ };
766
+ }
707
767
  function renderMathNodeLatex(math) {
708
768
  const lines = math.lines.map((line) => line.arabicLatex());
709
769
  if (math.mathMode === "$" || math.mathMode === "\\(") {
@@ -1168,10 +1228,11 @@ function createInlineField2(value = "", mathObjects = [], options = {}, path = "
1168
1228
  }
1169
1229
  const mathJson = mathObjects[mathObjectIndex];
1170
1230
  mathObjectIndex += 1;
1171
- if (mathJson && (mathJson.math_mode !== span.opening || mathJson.closing !== span.closing)) {
1231
+ const structuredMathJson = mathJson?.node_type === "MathObject" ? mathJson : null;
1232
+ if (structuredMathJson && (structuredMathJson.math_mode !== span.opening || structuredMathJson.closing !== span.closing)) {
1172
1233
  pushDiagnostic(diagnostics, options, path, "math_objects order mismatch");
1173
1234
  }
1174
- if (mathJson && mathJson.math_mode === span.opening && mathJson.closing === span.closing) {
1235
+ if (structuredMathJson && structuredMathJson.math_mode === span.opening && structuredMathJson.closing === span.closing) {
1175
1236
  tokens.push({
1176
1237
  id: document2Id("math"),
1177
1238
  kind: "math",
@@ -1179,10 +1240,12 @@ function createInlineField2(value = "", mathObjects = [], options = {}, path = "
1179
1240
  opening: span.opening,
1180
1241
  closing: span.closing,
1181
1242
  source: span.source,
1182
- sourceSide: mode,
1183
- math: fromMathObjectJson(mathJson, mode),
1243
+ sourceSide: structuredMathJson.source_side === "arabic" || structuredMathJson.source_side === "english" ? structuredMathJson.source_side : mode,
1244
+ math: fromMathObjectJson(structuredMathJson, mode),
1184
1245
  editable: true,
1185
- sourceOwner: "imported-structured"
1246
+ sourceOwner: structuredMathJson.source_owner === "editor" ? "editor" : "imported-structured",
1247
+ ...typeof structuredMathJson.label_enabled === "boolean" ? { labelEnabled: structuredMathJson.label_enabled } : {},
1248
+ ...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {}
1186
1249
  });
1187
1250
  } else {
1188
1251
  tokens.push({
@@ -1195,7 +1258,9 @@ function createInlineField2(value = "", mathObjects = [], options = {}, path = "
1195
1258
  math: null,
1196
1259
  editable: false,
1197
1260
  reason: "\u0647\u0630\u0647 \u0627\u0644\u0645\u0639\u0627\u062F\u0644\u0629 \u0645\u062D\u0641\u0648\u0638\u0629 \u0643\u0646\u0635 \u062E\u0627\u0645 \u0641\u0642\u0637 \u062D\u0627\u0644\u064A\u0627",
1198
- sourceOwner: "raw"
1261
+ sourceOwner: "raw",
1262
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label_enabled === "boolean" ? { labelEnabled: mathJson.label_enabled } : {},
1263
+ ...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {}
1199
1264
  });
1200
1265
  }
1201
1266
  index = span.end;
@@ -5403,6 +5468,155 @@ ${body}
5403
5468
  `;
5404
5469
  }
5405
5470
 
5471
+ // src/document2/exportJson.ts
5472
+ function serializeField(field) {
5473
+ let value = "";
5474
+ let hasPersistedMath = false;
5475
+ const formats = [];
5476
+ const mathObjects = [];
5477
+ for (const token of field.tokens) {
5478
+ if (token.kind === "text") {
5479
+ const start = value.length;
5480
+ value += token.text;
5481
+ if (token.text.length > 0 && (token.style?.bold || token.style?.italic || token.style?.underline)) {
5482
+ formats.push({
5483
+ start,
5484
+ end: value.length,
5485
+ ...token.style.bold ? { bold: true } : {},
5486
+ ...token.style.italic ? { italic: true } : {},
5487
+ ...token.style.underline ? { underline: true } : {}
5488
+ });
5489
+ }
5490
+ continue;
5491
+ }
5492
+ if (token.kind === "cite") {
5493
+ value += citeTokenLatex(token.keys);
5494
+ continue;
5495
+ }
5496
+ if (token.kind === "ref") {
5497
+ value += refTokenLatex(token.keys, token.refCommand);
5498
+ continue;
5499
+ }
5500
+ value += token.source;
5501
+ if (!token.math || token.sourceOwner === "raw") {
5502
+ if (token.labelEnabled !== void 0 || token.label !== void 0) {
5503
+ hasPersistedMath = true;
5504
+ mathObjects.push({
5505
+ node_type: "RawMathObject",
5506
+ ...token.labelEnabled !== void 0 ? { label_enabled: token.labelEnabled } : {},
5507
+ ...token.label !== void 0 ? { label: token.label } : {}
5508
+ });
5509
+ } else {
5510
+ mathObjects.push(null);
5511
+ }
5512
+ continue;
5513
+ }
5514
+ hasPersistedMath = true;
5515
+ mathObjects.push({
5516
+ ...toMathObjectJson(token.math),
5517
+ ...token.sourceSide ? { source_side: token.sourceSide } : {},
5518
+ source_owner: token.sourceOwner,
5519
+ ...token.labelEnabled !== void 0 ? { label_enabled: token.labelEnabled } : {},
5520
+ ...token.label !== void 0 ? { label: token.label } : {}
5521
+ });
5522
+ }
5523
+ return { value, formats, mathObjects, hasPersistedMath };
5524
+ }
5525
+ function fieldJson(field) {
5526
+ const serialized = serializeField(field);
5527
+ return {
5528
+ value: serialized.value,
5529
+ ...serialized.formats.length > 0 ? { formats: serialized.formats } : {},
5530
+ ...serialized.hasPersistedMath ? { math_objects: serialized.mathObjects } : {}
5531
+ };
5532
+ }
5533
+ function listItemJson(item) {
5534
+ const field = fieldJson(item.field);
5535
+ return {
5536
+ value: field.value ?? "",
5537
+ ...field.formats ? { formats: field.formats } : {},
5538
+ ...field.math_objects ? { math_objects: field.math_objects } : {},
5539
+ ...item.blocks.length > 0 ? { blocks: item.blocks.map(blockJson) } : {}
5540
+ };
5541
+ }
5542
+ function blockJson(block) {
5543
+ if (block.kind === "textBlock") {
5544
+ return {
5545
+ command: block.command,
5546
+ ...fieldJson(block.field),
5547
+ ...block.command === "\\paragraph" ? { centered: block.centered === true } : {}
5548
+ };
5549
+ }
5550
+ if (block.kind === "list") {
5551
+ return {
5552
+ command: block.command,
5553
+ closing: block.closing,
5554
+ items: block.items.map(listItemJson)
5555
+ };
5556
+ }
5557
+ if (block.kind === "table") {
5558
+ const fields = block.rows.map((row) => row.map(serializeField));
5559
+ const hasPersistedMath = fields.some((row) => row.some((field) => field.hasPersistedMath));
5560
+ return {
5561
+ command: block.command,
5562
+ closing: block.closing,
5563
+ columns: block.columns,
5564
+ rows: fields.map((row) => row.map((field) => field.value)),
5565
+ ...fields.some((row) => row.some((field) => field.formats.length > 0)) ? { cell_formats: fields.map((row) => row.map((field) => field.formats)) } : {},
5566
+ ...hasPersistedMath ? { math_objects: fields.flatMap((row) => row.flatMap((field) => field.mathObjects)) } : {},
5567
+ centered: block.centered,
5568
+ caption_enabled: block.captionEnabled,
5569
+ caption: block.caption,
5570
+ label_enabled: block.labelEnabled,
5571
+ label: block.label
5572
+ };
5573
+ }
5574
+ if (block.kind === "image") {
5575
+ return {
5576
+ command: block.command,
5577
+ value: block.value,
5578
+ ...block.assetId !== void 0 ? { asset_id: block.assetId } : {},
5579
+ options: { ...block.options },
5580
+ centered: block.centered,
5581
+ caption_enabled: block.captionEnabled,
5582
+ caption: block.caption,
5583
+ label_enabled: block.labelEnabled,
5584
+ label: block.label
5585
+ };
5586
+ }
5587
+ if (block.kind === "bibliography") {
5588
+ return { command: block.command, closing: block.closing };
5589
+ }
5590
+ return { command: block.command, value: block.value };
5591
+ }
5592
+ function referenceJson(reference) {
5593
+ return {
5594
+ key: reference.key,
5595
+ authors: reference.authors,
5596
+ title: reference.title,
5597
+ year: reference.year,
5598
+ url: reference.url,
5599
+ venue: reference.venue,
5600
+ field_separator: reference.fieldSeparator
5601
+ };
5602
+ }
5603
+ function toDocumentJson2(document2) {
5604
+ if (document2.nodeType !== "DocumentObject" || !Array.isArray(document2.blocks)) {
5605
+ throw new Error("toDocumentJson2 requires a live Document2Node");
5606
+ }
5607
+ return {
5608
+ node_type: "DocumentObject",
5609
+ meta: {
5610
+ title: document2.meta.title,
5611
+ authors: document2.meta.authors,
5612
+ date: { ...document2.meta.date },
5613
+ abstract: document2.meta.abstract
5614
+ },
5615
+ references: document2.references.map(referenceJson),
5616
+ blocks: document2.blocks.map(blockJson)
5617
+ };
5618
+ }
5619
+
5406
5620
  // src/document2/history.ts
5407
5621
  var DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH = 100;
5408
5622
  function createDocument2History(maxDepth = DEFAULT_DOCUMENT2_HISTORY_MAX_DEPTH) {
@@ -5784,6 +5998,7 @@ export {
5784
5998
  restoreDocument2Undo,
5785
5999
  setDocument2References,
5786
6000
  stripDisplayMathDelimiters,
6001
+ toDocumentJson2,
5787
6002
  toggleBlockInSelection,
5788
6003
  toggleTextTokenStyle,
5789
6004
  updateCiteTokenKeys,