@drghaliasri/butex 8.0.3 → 8.1.1
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.
- package/README.md +7 -7
- package/dist/document-cli.js +278 -113
- package/dist/document-cli.js.map +1 -1
- package/dist/document.d.mts +80 -4
- package/dist/document.d.ts +80 -4
- package/dist/document.js +380 -129
- package/dist/document.js.map +1 -1
- package/dist/document.mjs +369 -129
- package/dist/document.mjs.map +1 -1
- package/dist/react-document-preview.d.mts +32 -5
- package/dist/react-document-preview.d.ts +32 -5
- package/dist/react-document-preview.js +132 -80
- package/dist/react-document-preview.js.map +1 -1
- package/dist/react-document-preview.mjs +132 -80
- package/dist/react-document-preview.mjs.map +1 -1
- package/dist/react-document.d.mts +40 -4
- package/dist/react-document.d.ts +40 -4
- package/dist/react-document.js +444 -187
- package/dist/react-document.js.map +1 -1
- package/dist/react-document.mjs +444 -187
- package/dist/react-document.mjs.map +1 -1
- package/package.json +1 -1
package/dist/document.mjs
CHANGED
|
@@ -851,82 +851,91 @@ function refTokenLatex(keys, refCommand) {
|
|
|
851
851
|
const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
|
|
852
852
|
return `${command}{${keys.join(",")}}`;
|
|
853
853
|
}
|
|
854
|
-
function
|
|
855
|
-
const
|
|
854
|
+
function implicitObjectKey(kind, ownerId, unitId) {
|
|
855
|
+
const hex = (value) => Array.from(new TextEncoder().encode(value), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
856
|
+
const scope = unitId === void 0 ? "a" : "u" + hex(unitId);
|
|
857
|
+
return kind + ":butex-auto-" + scope + "-" + hex(ownerId);
|
|
858
|
+
}
|
|
859
|
+
function implicitReferenceKey(kind, ownerId, unitId) {
|
|
860
|
+
return implicitObjectKey(kind, ownerId, unitId);
|
|
861
|
+
}
|
|
862
|
+
function implicitHeadingReferenceKey(kind, ownerId, unitId) {
|
|
863
|
+
return implicitObjectKey(kind, ownerId, unitId);
|
|
864
|
+
}
|
|
865
|
+
function supportsSingleEquationNumbering(token) {
|
|
866
|
+
return token.display && (token.opening === "\\[" || token.opening === String.fromCharCode(36, 36) || token.opening === "\\begin{equation}" || token.opening === "\\begin{equation*}");
|
|
867
|
+
}
|
|
868
|
+
function isNumberedEquation(token) {
|
|
869
|
+
if (!supportsSingleEquationNumbering(token)) return false;
|
|
870
|
+
if (typeof token.numbered === "boolean") return token.numbered;
|
|
871
|
+
return token.opening === "\\begin{equation}" || Boolean(token.labelEnabled) && Boolean(token.label?.trim());
|
|
872
|
+
}
|
|
873
|
+
function isNumberedFloat(block) {
|
|
874
|
+
return block.captionEnabled && block.caption.trim().length > 0 || block.labelEnabled && block.label.trim().length > 0;
|
|
875
|
+
}
|
|
876
|
+
function collectDocumentReferenceTargets(document2) {
|
|
877
|
+
const targets = [];
|
|
856
878
|
const counters = { fig: 0, tab: 0, eq: 0 };
|
|
857
|
-
function
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
879
|
+
function append(kind, ownerId, caption, numbered, manualKey = "") {
|
|
880
|
+
if (numbered) counters[kind] += 1;
|
|
881
|
+
targets.push({
|
|
882
|
+
key: implicitReferenceKey(kind, ownerId),
|
|
883
|
+
kind,
|
|
884
|
+
ownerId,
|
|
885
|
+
caption,
|
|
886
|
+
number: numbered ? counters[kind] : null,
|
|
887
|
+
...manualKey.trim() ? { manualKey: manualKey.trim() } : {}
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
function visitField(field) {
|
|
891
|
+
for (const token of field.tokens) {
|
|
892
|
+
if (token.kind === "math" && supportsSingleEquationNumbering(token)) {
|
|
893
|
+
append("eq", token.id, "", isNumberedEquation(token), token.labelEnabled ? token.label ?? "" : "");
|
|
871
894
|
}
|
|
872
895
|
}
|
|
873
896
|
}
|
|
874
897
|
function walk(blocks) {
|
|
875
898
|
for (const block of blocks) {
|
|
876
|
-
if (block.kind === "image") {
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
if (block.captionEnabled) {
|
|
888
|
-
pushEqFromField(block.captionField.tokens);
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
if (block.kind === "table") {
|
|
892
|
-
if (block.labelEnabled && block.label.trim().length > 0) {
|
|
893
|
-
counters.tab += 1;
|
|
894
|
-
entries.push({
|
|
895
|
-
key: block.label.trim(),
|
|
896
|
-
kind: "tab",
|
|
897
|
-
caption: block.captionEnabled ? block.caption : "",
|
|
898
|
-
ownerId: block.id,
|
|
899
|
-
number: counters.tab
|
|
900
|
-
});
|
|
901
|
-
}
|
|
902
|
-
if (block.captionEnabled) {
|
|
903
|
-
pushEqFromField(block.captionField.tokens);
|
|
904
|
-
}
|
|
905
|
-
for (const row of block.rows) {
|
|
906
|
-
for (const cell of row) {
|
|
907
|
-
pushEqFromField(cell.tokens);
|
|
908
|
-
}
|
|
899
|
+
if (block.kind === "image" || block.kind === "table") {
|
|
900
|
+
append(
|
|
901
|
+
block.kind === "image" ? "fig" : "tab",
|
|
902
|
+
block.id,
|
|
903
|
+
block.captionEnabled ? block.caption : "",
|
|
904
|
+
isNumberedFloat(block),
|
|
905
|
+
block.labelEnabled ? block.label : ""
|
|
906
|
+
);
|
|
907
|
+
if (block.captionEnabled) visitField(block.captionField);
|
|
908
|
+
if (block.kind === "table") {
|
|
909
|
+
for (const row of block.rows) for (const cell of row) visitField(cell);
|
|
909
910
|
}
|
|
910
|
-
}
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
}
|
|
914
|
-
if (block.kind === "list") {
|
|
911
|
+
} else if (block.kind === "textBlock") {
|
|
912
|
+
visitField(block.field);
|
|
913
|
+
} else if (block.kind === "list") {
|
|
915
914
|
for (const item of block.items) {
|
|
916
|
-
|
|
915
|
+
visitField(item.field);
|
|
917
916
|
walk(item.blocks);
|
|
918
917
|
}
|
|
919
918
|
}
|
|
920
919
|
}
|
|
921
920
|
}
|
|
922
921
|
walk(document2.blocks);
|
|
923
|
-
return
|
|
922
|
+
return targets;
|
|
923
|
+
}
|
|
924
|
+
function collectDocumentLabels(document2) {
|
|
925
|
+
return collectDocumentReferenceTargets(document2).filter((target) => target.number !== null && Boolean(target.manualKey)).map(({ manualKey, kind, caption, ownerId, number }) => ({ key: manualKey, kind, caption, ownerId, number }));
|
|
926
|
+
}
|
|
927
|
+
function collectDocumentKeyLabels(document2) {
|
|
928
|
+
return collectDocumentReferenceTargets(document2).filter((target) => target.number !== null).flatMap((target) => [
|
|
929
|
+
{ key: target.key, ownerId: target.ownerId },
|
|
930
|
+
...target.manualKey && target.manualKey !== target.key ? [{ key: target.manualKey, ownerId: target.ownerId }] : []
|
|
931
|
+
]);
|
|
924
932
|
}
|
|
925
933
|
function labelNumberMap(document2) {
|
|
926
934
|
const map = /* @__PURE__ */ new Map();
|
|
927
|
-
for (const
|
|
928
|
-
if (
|
|
929
|
-
|
|
935
|
+
for (const target of collectDocumentReferenceTargets(document2)) {
|
|
936
|
+
if (target.number === null) continue;
|
|
937
|
+
for (const key of [target.key, target.manualKey]) {
|
|
938
|
+
if (key && !map.has(key)) map.set(key, { kind: target.kind, number: target.number });
|
|
930
939
|
}
|
|
931
940
|
}
|
|
932
941
|
return map;
|
|
@@ -980,8 +989,12 @@ function stripDisplayMathDelimiters(source) {
|
|
|
980
989
|
if (trimmed.startsWith("$$") && trimmed.endsWith("$$")) {
|
|
981
990
|
return trimmed.slice(2, -2).trim();
|
|
982
991
|
}
|
|
983
|
-
|
|
984
|
-
|
|
992
|
+
for (const environment of ["equation", "equation*"]) {
|
|
993
|
+
const opening = `\\begin{${environment}}`;
|
|
994
|
+
const closing = `\\end{${environment}}`;
|
|
995
|
+
if (trimmed.startsWith(opening) && trimmed.endsWith(closing)) {
|
|
996
|
+
return trimmed.slice(opening.length, -closing.length).trim();
|
|
997
|
+
}
|
|
985
998
|
}
|
|
986
999
|
return trimmed;
|
|
987
1000
|
}
|
|
@@ -1374,7 +1387,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
|
|
|
1374
1387
|
editable: true,
|
|
1375
1388
|
sourceOwner: structuredMathJson.source_owner === "editor" ? "editor" : "imported-structured",
|
|
1376
1389
|
...typeof structuredMathJson.label_enabled === "boolean" ? { labelEnabled: structuredMathJson.label_enabled } : {},
|
|
1377
|
-
...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {}
|
|
1390
|
+
...typeof structuredMathJson.label === "string" ? { label: structuredMathJson.label } : {},
|
|
1391
|
+
...typeof structuredMathJson.numbered === "boolean" ? { numbered: structuredMathJson.numbered } : {}
|
|
1378
1392
|
});
|
|
1379
1393
|
} else {
|
|
1380
1394
|
tokens.push({
|
|
@@ -1389,7 +1403,8 @@ function createInlineField(value = "", mathObjects = [], options = {}, path = "$
|
|
|
1389
1403
|
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",
|
|
1390
1404
|
sourceOwner: "raw",
|
|
1391
1405
|
...mathJson?.node_type === "RawMathObject" && typeof mathJson.label_enabled === "boolean" ? { labelEnabled: mathJson.label_enabled } : {},
|
|
1392
|
-
...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {}
|
|
1406
|
+
...mathJson?.node_type === "RawMathObject" && typeof mathJson.label === "string" ? { label: mathJson.label } : {},
|
|
1407
|
+
...mathJson?.node_type === "RawMathObject" && typeof mathJson.numbered === "boolean" ? { numbered: mathJson.numbered } : {}
|
|
1393
1408
|
});
|
|
1394
1409
|
}
|
|
1395
1410
|
index = span.end;
|
|
@@ -1486,6 +1501,7 @@ function parseTextBlock(json, options, path, diagnostics, identities) {
|
|
|
1486
1501
|
identities
|
|
1487
1502
|
),
|
|
1488
1503
|
...metadataFromJson(json.metadata),
|
|
1504
|
+
...typeof json.label === "string" && json.command !== "\\paragraph" ? { label: json.label } : {},
|
|
1489
1505
|
...json.centered === true ? { centered: true } : {}
|
|
1490
1506
|
};
|
|
1491
1507
|
}
|
|
@@ -5443,6 +5459,9 @@ function replaceMathTokenFromSession(document2, tokenId, session, opening, closi
|
|
|
5443
5459
|
if (current.label !== void 0) {
|
|
5444
5460
|
replaced.label = current.label;
|
|
5445
5461
|
}
|
|
5462
|
+
if (current.numbered !== void 0) {
|
|
5463
|
+
replaced.numbered = current.numbered;
|
|
5464
|
+
}
|
|
5446
5465
|
field.tokens[index] = replaced;
|
|
5447
5466
|
return true;
|
|
5448
5467
|
});
|
|
@@ -5479,6 +5498,24 @@ function updateDocumentTextBlockCentered(document2, blockId, centered) {
|
|
|
5479
5498
|
visit(next.blocks);
|
|
5480
5499
|
return next;
|
|
5481
5500
|
}
|
|
5501
|
+
function updateDocumentHeadingLabel(document2, blockId, label) {
|
|
5502
|
+
const next = cloneDocument(document2);
|
|
5503
|
+
function visit(blocks) {
|
|
5504
|
+
for (const block of blocks) {
|
|
5505
|
+
if (block.id === blockId && block.kind === "textBlock" && block.command !== "\\paragraph") {
|
|
5506
|
+
if (label.trim()) block.label = label.trim();
|
|
5507
|
+
else delete block.label;
|
|
5508
|
+
return true;
|
|
5509
|
+
}
|
|
5510
|
+
if (block.kind === "list") {
|
|
5511
|
+
for (const item of block.items) if (visit(item.blocks)) return true;
|
|
5512
|
+
}
|
|
5513
|
+
}
|
|
5514
|
+
return false;
|
|
5515
|
+
}
|
|
5516
|
+
visit(next.blocks);
|
|
5517
|
+
return next;
|
|
5518
|
+
}
|
|
5482
5519
|
function removeDocumentBlockById(document2, blockId) {
|
|
5483
5520
|
const next = cloneDocument(document2);
|
|
5484
5521
|
next.blocks = next.blocks.filter((block) => block.id !== blockId);
|
|
@@ -5667,6 +5704,9 @@ function updateMathTokenLabel(document2, tokenId, patch) {
|
|
|
5667
5704
|
if (typeof patch.label === "string") {
|
|
5668
5705
|
token.label = patch.label;
|
|
5669
5706
|
}
|
|
5707
|
+
if (typeof patch.numbered === "boolean") {
|
|
5708
|
+
token.numbered = patch.numbered;
|
|
5709
|
+
}
|
|
5670
5710
|
return true;
|
|
5671
5711
|
});
|
|
5672
5712
|
return next;
|
|
@@ -5975,7 +6015,7 @@ function normalizedNewFloatLabel(document2, label) {
|
|
|
5975
6015
|
if (!normalized) return "";
|
|
5976
6016
|
const error = documentKeyError(normalized, {
|
|
5977
6017
|
references: document2.references,
|
|
5978
|
-
labels:
|
|
6018
|
+
labels: collectDocumentKeyLabels(document2)
|
|
5979
6019
|
});
|
|
5980
6020
|
if (error === "duplicate") {
|
|
5981
6021
|
throw new DocumentCommandError("duplicate_key", "Float label is already in use");
|
|
@@ -6419,7 +6459,7 @@ function validateReferenceKey(document2, key, excludeReferenceId) {
|
|
|
6419
6459
|
const normalized = normalizeDocumentKey(key);
|
|
6420
6460
|
const error = documentKeyError(normalized, {
|
|
6421
6461
|
references: document2.references,
|
|
6422
|
-
labels:
|
|
6462
|
+
labels: collectDocumentKeyLabels(document2),
|
|
6423
6463
|
...excludeReferenceId ? { excludeReferenceId } : {}
|
|
6424
6464
|
});
|
|
6425
6465
|
if (error === "duplicate") {
|
|
@@ -6641,6 +6681,17 @@ function parseToken(value) {
|
|
|
6641
6681
|
throw new DocumentCommandError("invalid_inline_token", "Unsupported inline token kind");
|
|
6642
6682
|
}
|
|
6643
6683
|
function parseInlineCommand(value) {
|
|
6684
|
+
if (value.op === "set_equation_numbering") {
|
|
6685
|
+
if (typeof value.numbered !== "boolean") {
|
|
6686
|
+
throw new DocumentCommandError("invalid_command", "numbered must be boolean");
|
|
6687
|
+
}
|
|
6688
|
+
return {
|
|
6689
|
+
op: "set_equation_numbering",
|
|
6690
|
+
field_id: requireString2(value.field_id, "field_id", false).trim(),
|
|
6691
|
+
token_id: requireString2(value.token_id, "token_id", false).trim(),
|
|
6692
|
+
numbered: value.numbered
|
|
6693
|
+
};
|
|
6694
|
+
}
|
|
6644
6695
|
if (value.op === "insert_inline_token") {
|
|
6645
6696
|
return {
|
|
6646
6697
|
op: value.op,
|
|
@@ -6694,7 +6745,8 @@ function liveToken(input, id) {
|
|
|
6694
6745
|
editable: true,
|
|
6695
6746
|
sourceOwner: mathObject.source_owner === "editor" ? "editor" : "imported-structured",
|
|
6696
6747
|
...mathObject.label_enabled !== void 0 ? { labelEnabled: mathObject.label_enabled } : {},
|
|
6697
|
-
...mathObject.label !== void 0 ? { label: mathObject.label } : {}
|
|
6748
|
+
...mathObject.label !== void 0 ? { label: mathObject.label } : {},
|
|
6749
|
+
...mathObject.numbered !== void 0 ? { numbered: mathObject.numbered } : {}
|
|
6698
6750
|
};
|
|
6699
6751
|
}
|
|
6700
6752
|
function insertionIndex(tokens, anchor) {
|
|
@@ -6716,6 +6768,17 @@ function applyInlineCommand(document2, command) {
|
|
|
6716
6768
|
if (!field) {
|
|
6717
6769
|
throw new DocumentCommandError("field_not_found", "Inline field was not found");
|
|
6718
6770
|
}
|
|
6771
|
+
if (command.op === "set_equation_numbering") {
|
|
6772
|
+
const token = field.tokens.find((entry) => entry.id === command.token_id);
|
|
6773
|
+
if (!token || token.kind !== "math") {
|
|
6774
|
+
throw new DocumentCommandError("token_not_found", "Equation token was not found");
|
|
6775
|
+
}
|
|
6776
|
+
if (!supportsSingleEquationNumbering(token)) {
|
|
6777
|
+
throw new DocumentCommandError("invalid_inline_token", "Only a single display equation can be numbered");
|
|
6778
|
+
}
|
|
6779
|
+
token.numbered = command.numbered;
|
|
6780
|
+
return;
|
|
6781
|
+
}
|
|
6719
6782
|
if (command.op === "insert_inline_token") {
|
|
6720
6783
|
const index2 = insertionIndex(field.tokens, command.anchor);
|
|
6721
6784
|
field.tokens.splice(index2, 0, liveToken(command.token, newTokenId(document2, command.token.kind)));
|
|
@@ -7068,12 +7131,13 @@ function serializeField(field) {
|
|
|
7068
7131
|
}
|
|
7069
7132
|
value += token.source;
|
|
7070
7133
|
if (!token.math || token.sourceOwner === "raw") {
|
|
7071
|
-
if (token.labelEnabled !== void 0 || token.label !== void 0) {
|
|
7134
|
+
if (token.labelEnabled !== void 0 || token.label !== void 0 || token.numbered !== void 0) {
|
|
7072
7135
|
hasPersistedMath = true;
|
|
7073
7136
|
mathObjects.push({
|
|
7074
7137
|
node_type: "RawMathObject",
|
|
7075
7138
|
...token.labelEnabled !== void 0 ? { label_enabled: token.labelEnabled } : {},
|
|
7076
|
-
...token.label !== void 0 ? { label: token.label } : {}
|
|
7139
|
+
...token.label !== void 0 ? { label: token.label } : {},
|
|
7140
|
+
...token.numbered !== void 0 ? { numbered: token.numbered } : {}
|
|
7077
7141
|
});
|
|
7078
7142
|
} else {
|
|
7079
7143
|
mathObjects.push(null);
|
|
@@ -7086,7 +7150,8 @@ function serializeField(field) {
|
|
|
7086
7150
|
...token.sourceSide ? { source_side: token.sourceSide } : {},
|
|
7087
7151
|
source_owner: token.sourceOwner,
|
|
7088
7152
|
...token.labelEnabled !== void 0 ? { label_enabled: token.labelEnabled } : {},
|
|
7089
|
-
...token.label !== void 0 ? { label: token.label } : {}
|
|
7153
|
+
...token.label !== void 0 ? { label: token.label } : {},
|
|
7154
|
+
...token.numbered !== void 0 ? { numbered: token.numbered } : {}
|
|
7090
7155
|
});
|
|
7091
7156
|
}
|
|
7092
7157
|
return { value, formats, mathObjects, hasPersistedMath, inlineIds: inlineIdsFromField(field) };
|
|
@@ -7126,6 +7191,7 @@ function blockJson(block) {
|
|
|
7126
7191
|
id: block.id,
|
|
7127
7192
|
command: block.command,
|
|
7128
7193
|
...fieldJson(block.field),
|
|
7194
|
+
...block.command !== "\\paragraph" && block.label ? { label: block.label } : {},
|
|
7129
7195
|
...block.metadata ? { metadata: { ...block.metadata } } : {},
|
|
7130
7196
|
...block.command === "\\paragraph" ? { centered: block.centered === true } : {}
|
|
7131
7197
|
};
|
|
@@ -7219,7 +7285,7 @@ function toDocumentJson(document2) {
|
|
|
7219
7285
|
|
|
7220
7286
|
// src/document/jsonCommands.ts
|
|
7221
7287
|
function isInlineCommand(command) {
|
|
7222
|
-
return command.op === "insert_inline_token" || command.op === "replace_inline_token" || command.op === "remove_inline_token";
|
|
7288
|
+
return command.op === "insert_inline_token" || command.op === "replace_inline_token" || command.op === "remove_inline_token" || command.op === "set_equation_numbering";
|
|
7223
7289
|
}
|
|
7224
7290
|
function isListCommand(command) {
|
|
7225
7291
|
return command.op === "insert_list" || command.op === "insert_list_item" || command.op === "replace_list_item" || command.op === "remove_list_item" || command.op === "move_list_item";
|
|
@@ -7254,6 +7320,13 @@ function parseBasicCommand(value) {
|
|
|
7254
7320
|
text: requireString2(value.text, "text")
|
|
7255
7321
|
};
|
|
7256
7322
|
}
|
|
7323
|
+
if (value.op === "set_heading_label") {
|
|
7324
|
+
return {
|
|
7325
|
+
op: value.op,
|
|
7326
|
+
block_id: requireString2(value.block_id, "block_id", false).trim(),
|
|
7327
|
+
label: value.label === null ? null : requireString2(value.label, "label")
|
|
7328
|
+
};
|
|
7329
|
+
}
|
|
7257
7330
|
if (value.op === "remove_block") {
|
|
7258
7331
|
return { op: value.op, block_id: requireString2(value.block_id, "block_id", false).trim() };
|
|
7259
7332
|
}
|
|
@@ -7334,7 +7407,7 @@ function parseCommand2(value) {
|
|
|
7334
7407
|
if (!isObject3(value) || typeof value.op !== "string") {
|
|
7335
7408
|
throw new DocumentCommandError("invalid_command", "command requires an op");
|
|
7336
7409
|
}
|
|
7337
|
-
if (value.op === "insert_inline_token" || value.op === "replace_inline_token" || value.op === "remove_inline_token") {
|
|
7410
|
+
if (value.op === "insert_inline_token" || value.op === "replace_inline_token" || value.op === "remove_inline_token" || value.op === "set_equation_numbering") {
|
|
7338
7411
|
return parseInlineCommand(value);
|
|
7339
7412
|
}
|
|
7340
7413
|
if (value.op === "insert_list" || value.op === "insert_list_item" || value.op === "replace_list_item" || value.op === "remove_list_item" || value.op === "move_list_item") {
|
|
@@ -7346,7 +7419,7 @@ function parseCommand2(value) {
|
|
|
7346
7419
|
if (value.op === "update_document_meta" || value.op === "insert_reference" || value.op === "update_reference" || value.op === "remove_reference" || value.op === "move_reference") {
|
|
7347
7420
|
return parseArticleCommand(value);
|
|
7348
7421
|
}
|
|
7349
|
-
if (value.op === "insert_text_block" || value.op === "replace_text_block" || value.op === "remove_block" || value.op === "move_block" || value.op === "insert_figure" || value.op === "update_figure" || value.op === "update_float_meta" || value.op === "insert_bibliography") {
|
|
7422
|
+
if (value.op === "insert_text_block" || value.op === "replace_text_block" || value.op === "set_heading_label" || value.op === "remove_block" || value.op === "move_block" || value.op === "insert_figure" || value.op === "update_figure" || value.op === "update_float_meta" || value.op === "insert_bibliography") {
|
|
7350
7423
|
return parseBasicCommand(value);
|
|
7351
7424
|
}
|
|
7352
7425
|
throw new DocumentCommandError("invalid_command", "Unsupported document command");
|
|
@@ -7391,6 +7464,20 @@ function applyBasicCommand(document2, command) {
|
|
|
7391
7464
|
replacePlainField(document2, block2.field, command.text);
|
|
7392
7465
|
return;
|
|
7393
7466
|
}
|
|
7467
|
+
if (command.op === "set_heading_label") {
|
|
7468
|
+
const block2 = document2.blocks.find((entry) => entry.id === command.block_id);
|
|
7469
|
+
if (!block2) throw new DocumentCommandError("block_not_found", "Heading was not found");
|
|
7470
|
+
if (block2.kind !== "textBlock" || block2.command === "\\paragraph") {
|
|
7471
|
+
throw new DocumentCommandError("block_kind_mismatch", "Target is not a heading");
|
|
7472
|
+
}
|
|
7473
|
+
const key = command.label?.trim() ?? "";
|
|
7474
|
+
if (key && documentKeyError(key, { references: document2.references, labels: collectDocumentKeyLabels(document2), excludeOwnerId: block2.id })) {
|
|
7475
|
+
throw new DocumentCommandError("invalid_command", "Heading label is invalid or duplicated");
|
|
7476
|
+
}
|
|
7477
|
+
if (key) block2.label = key;
|
|
7478
|
+
else delete block2.label;
|
|
7479
|
+
return;
|
|
7480
|
+
}
|
|
7394
7481
|
if (command.op === "remove_block") {
|
|
7395
7482
|
const index = document2.blocks.findIndex((block2) => block2.id === command.block_id);
|
|
7396
7483
|
if (index < 0) {
|
|
@@ -7468,7 +7555,7 @@ function applyBasicCommand(document2, command) {
|
|
|
7468
7555
|
if (label2 !== void 0 && label2.length > 0) {
|
|
7469
7556
|
const keyError = documentKeyError(label2, {
|
|
7470
7557
|
references: document2.references,
|
|
7471
|
-
labels:
|
|
7558
|
+
labels: collectDocumentKeyLabels(document2),
|
|
7472
7559
|
excludeOwnerId: block2.id
|
|
7473
7560
|
});
|
|
7474
7561
|
if (keyError === "duplicate") {
|
|
@@ -7723,7 +7810,7 @@ function getDocumentReferences(document2) {
|
|
|
7723
7810
|
function getDocumentReferenceIndex(document2) {
|
|
7724
7811
|
const referenceKeys = new Set(document2.references.map((reference) => reference.key));
|
|
7725
7812
|
const labelEntries = collectDocumentLabels(document2);
|
|
7726
|
-
const labelKeys = new Set(
|
|
7813
|
+
const labelKeys = new Set(labelNumberMap(document2).keys());
|
|
7727
7814
|
const citations = [];
|
|
7728
7815
|
const crossReferences = [];
|
|
7729
7816
|
const tokenLocations = /* @__PURE__ */ new Map();
|
|
@@ -7806,6 +7893,14 @@ function getDocumentReferenceIndex(document2) {
|
|
|
7806
7893
|
citations,
|
|
7807
7894
|
cross_references: crossReferences,
|
|
7808
7895
|
labels,
|
|
7896
|
+
reference_targets: collectDocumentReferenceTargets(document2).map((target) => ({
|
|
7897
|
+
key: target.key,
|
|
7898
|
+
kind: target.kind,
|
|
7899
|
+
caption: target.caption,
|
|
7900
|
+
number: target.number,
|
|
7901
|
+
...target.kind === "eq" ? { token_id: target.ownerId } : { block_id: target.ownerId },
|
|
7902
|
+
...target.manualKey ? { manual_key: target.manualKey } : {}
|
|
7903
|
+
})),
|
|
7809
7904
|
unresolved: {
|
|
7810
7905
|
citation_keys: unresolvedCitationKeys,
|
|
7811
7906
|
cross_reference_keys: unresolvedCrossReferenceKeys
|
|
@@ -8110,7 +8205,7 @@ function refTokenLatexForDocument(token, labelKinds, previousToken) {
|
|
|
8110
8205
|
}
|
|
8111
8206
|
return `${semanticRefPrefix(kind)}~${reference}`;
|
|
8112
8207
|
}
|
|
8113
|
-
function tokenLatex(token, labelKinds, previousToken) {
|
|
8208
|
+
function tokenLatex(token, labelKinds, previousToken, unitId) {
|
|
8114
8209
|
if (token.kind === "text") {
|
|
8115
8210
|
return styledTextLatex(token.text, token.style);
|
|
8116
8211
|
}
|
|
@@ -8120,12 +8215,15 @@ function tokenLatex(token, labelKinds, previousToken) {
|
|
|
8120
8215
|
if (token.kind === "ref") {
|
|
8121
8216
|
return refTokenLatexForDocument(token, labelKinds, previousToken);
|
|
8122
8217
|
}
|
|
8123
|
-
if (token
|
|
8218
|
+
if (isNumberedEquation(token)) {
|
|
8124
8219
|
const body = mathBodyLatex(token);
|
|
8125
|
-
|
|
8126
|
-
|
|
8127
|
-
|
|
8128
|
-
\\end{equation}
|
|
8220
|
+
const autoKey = implicitReferenceKey("eq", token.id, unitId);
|
|
8221
|
+
const aliases = [autoKey];
|
|
8222
|
+
if (token.labelEnabled && token.label?.trim() && token.label.trim() !== autoKey) aliases.push(token.label.trim());
|
|
8223
|
+
return "\\begin{equation}\n" + body + "\n" + aliases.map((key) => "\\label{" + key + "}").join("\n") + "\n\\end{equation}";
|
|
8224
|
+
}
|
|
8225
|
+
if (token.display && token.numbered === false && token.opening === "\\begin{equation}") {
|
|
8226
|
+
return "\\[" + mathBodyLatex(token) + "\\]";
|
|
8129
8227
|
}
|
|
8130
8228
|
if (token.sourceOwner === "editor") {
|
|
8131
8229
|
return editorMathLatexForCompile(token);
|
|
@@ -8135,16 +8233,21 @@ ${body}
|
|
|
8135
8233
|
}
|
|
8136
8234
|
return renderMathNodeLatex(token.math);
|
|
8137
8235
|
}
|
|
8138
|
-
function inlineFieldLatexWithLabelKinds(field, labelKinds) {
|
|
8139
|
-
return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1])).join("");
|
|
8236
|
+
function inlineFieldLatexWithLabelKinds(field, labelKinds, unitId) {
|
|
8237
|
+
return field.tokens.map((token, index) => tokenLatex(token, labelKinds, field.tokens[index - 1], unitId)).join("");
|
|
8140
8238
|
}
|
|
8141
8239
|
function inlineFieldLatex(field) {
|
|
8142
8240
|
return inlineFieldLatexWithLabelKinds(field);
|
|
8143
8241
|
}
|
|
8144
|
-
function textBlockLatex(block, labelKinds) {
|
|
8145
|
-
const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds);
|
|
8242
|
+
function textBlockLatex(block, labelKinds, unitId) {
|
|
8243
|
+
const content = inlineFieldLatexWithLabelKinds(block.field, labelKinds, unitId);
|
|
8146
8244
|
const body = block.command === "\\paragraph" ? `\\par
|
|
8147
8245
|
${content}` : `${block.command}{${content}}`;
|
|
8246
|
+
if (block.command !== "\\paragraph") {
|
|
8247
|
+
const keys = unitId ? [implicitHeadingReferenceKey(block.command.slice(1), block.id, unitId)] : [];
|
|
8248
|
+
if (block.label?.trim() && !keys.includes(block.label.trim())) keys.push(block.label.trim());
|
|
8249
|
+
return [body, ...keys.map((key) => `\\label{${key}}`)].join("\n");
|
|
8250
|
+
}
|
|
8148
8251
|
if (block.command === "\\paragraph" && block.centered) {
|
|
8149
8252
|
return `\\begin{center}
|
|
8150
8253
|
${body}
|
|
@@ -8155,10 +8258,10 @@ ${body}
|
|
|
8155
8258
|
function indent(value) {
|
|
8156
8259
|
return value.split("\n").map((line) => ` ${line}`).join("\n");
|
|
8157
8260
|
}
|
|
8158
|
-
function listBlockLatex(block, references, labelKinds, fixedFloats) {
|
|
8261
|
+
function listBlockLatex(block, references, labelKinds, fixedFloats, unitId) {
|
|
8159
8262
|
const items = block.items.map((item) => {
|
|
8160
|
-
const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats))).join("");
|
|
8161
|
-
return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds)}${nested}`;
|
|
8263
|
+
const nested = item.blocks.map((child) => "\n" + indent(blockLatex(child, references, labelKinds, fixedFloats, unitId))).join("");
|
|
8264
|
+
return ` \\item ${inlineFieldLatexWithLabelKinds(item.field, labelKinds, unitId)}${nested}`;
|
|
8162
8265
|
}).join("\n");
|
|
8163
8266
|
return `${block.command}
|
|
8164
8267
|
${items}
|
|
@@ -8172,9 +8275,9 @@ function defaultGridColumnSpec(block) {
|
|
|
8172
8275
|
}
|
|
8173
8276
|
return `|${compact.split("").join("|")}|`;
|
|
8174
8277
|
}
|
|
8175
|
-
function tabularInnerLatex(block, labelKinds) {
|
|
8278
|
+
function tabularInnerLatex(block, labelKinds, unitId) {
|
|
8176
8279
|
const rows = block.rows.map(
|
|
8177
|
-
(row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds)).join(" & ")}`
|
|
8280
|
+
(row) => ` ${row.map((cell) => inlineFieldLatexWithLabelKinds(cell, labelKinds, unitId)).join(" & ")}`
|
|
8178
8281
|
);
|
|
8179
8282
|
const gridColumns = defaultGridColumnSpec(block);
|
|
8180
8283
|
if (gridColumns) {
|
|
@@ -8188,29 +8291,30 @@ ${block.closing}`;
|
|
|
8188
8291
|
${rows.join(" \\\\\n")}
|
|
8189
8292
|
${block.closing}`;
|
|
8190
8293
|
}
|
|
8191
|
-
function floatCaptionLabelLines(block, counter, labelKinds) {
|
|
8294
|
+
function floatCaptionLabelLines(block, counter, labelKinds, unitId) {
|
|
8192
8295
|
const lines = [];
|
|
8193
|
-
const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds).trim();
|
|
8296
|
+
const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds, unitId).trim();
|
|
8194
8297
|
const hasCaption = block.captionEnabled && caption.length > 0;
|
|
8195
8298
|
if (hasCaption) {
|
|
8196
8299
|
lines.push(` \\caption{${caption}}`);
|
|
8197
8300
|
}
|
|
8198
|
-
|
|
8199
|
-
|
|
8200
|
-
|
|
8201
|
-
|
|
8202
|
-
lines.push(
|
|
8301
|
+
const hasManualLabel = block.labelEnabled && block.label.trim().length > 0;
|
|
8302
|
+
if (hasCaption || hasManualLabel) {
|
|
8303
|
+
if (!hasCaption) lines.push(" \\refstepcounter{" + counter + "}");
|
|
8304
|
+
const autoKey = implicitReferenceKey(counter === "figure" ? "fig" : "tab", block.id, unitId);
|
|
8305
|
+
lines.push(" \\label{" + autoKey + "}");
|
|
8306
|
+
if (hasManualLabel && block.label.trim() !== autoKey) lines.push(" \\label{" + block.label.trim() + "}");
|
|
8203
8307
|
}
|
|
8204
8308
|
return lines;
|
|
8205
8309
|
}
|
|
8206
|
-
function tableBlockLatex(block, labelKinds, fixedFloats) {
|
|
8310
|
+
function tableBlockLatex(block, labelKinds, fixedFloats, unitId) {
|
|
8207
8311
|
const placement = fixedFloats ? "H" : "htbp";
|
|
8208
8312
|
const lines = [`\\begin{table}[${placement}]`];
|
|
8209
8313
|
if (block.centered) {
|
|
8210
8314
|
lines.push(" \\centering");
|
|
8211
8315
|
}
|
|
8212
|
-
lines.push(...floatCaptionLabelLines(block, "table", labelKinds));
|
|
8213
|
-
lines.push(indent(tabularInnerLatex(block, labelKinds)));
|
|
8316
|
+
lines.push(...floatCaptionLabelLines(block, "table", labelKinds, unitId));
|
|
8317
|
+
lines.push(indent(tabularInnerLatex(block, labelKinds, unitId)));
|
|
8214
8318
|
lines.push("\\end{table}");
|
|
8215
8319
|
return lines.join("\n");
|
|
8216
8320
|
}
|
|
@@ -8229,29 +8333,29 @@ function imageOptionsLatex(block, constrainToColumn) {
|
|
|
8229
8333
|
}
|
|
8230
8334
|
return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
|
|
8231
8335
|
}
|
|
8232
|
-
function imageBlockLatex(block, labelKinds, fixedFloats) {
|
|
8336
|
+
function imageBlockLatex(block, labelKinds, fixedFloats, unitId) {
|
|
8233
8337
|
const placement = fixedFloats ? "H" : "htbp";
|
|
8234
8338
|
const lines = [`\\begin{figure}[${placement}]`];
|
|
8235
8339
|
if (block.centered) {
|
|
8236
8340
|
lines.push(" \\centering");
|
|
8237
8341
|
}
|
|
8238
8342
|
lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
|
|
8239
|
-
lines.push(...floatCaptionLabelLines(block, "figure", labelKinds));
|
|
8343
|
+
lines.push(...floatCaptionLabelLines(block, "figure", labelKinds, unitId));
|
|
8240
8344
|
lines.push("\\end{figure}");
|
|
8241
8345
|
return lines.join("\n");
|
|
8242
8346
|
}
|
|
8243
|
-
function blockLatex(block, references, labelKinds, fixedFloats) {
|
|
8347
|
+
function blockLatex(block, references, labelKinds, fixedFloats, unitId) {
|
|
8244
8348
|
if (block.kind === "textBlock") {
|
|
8245
|
-
return textBlockLatex(block, labelKinds);
|
|
8349
|
+
return textBlockLatex(block, labelKinds, unitId);
|
|
8246
8350
|
}
|
|
8247
8351
|
if (block.kind === "list") {
|
|
8248
|
-
return listBlockLatex(block, references, labelKinds, fixedFloats);
|
|
8352
|
+
return listBlockLatex(block, references, labelKinds, fixedFloats, unitId);
|
|
8249
8353
|
}
|
|
8250
8354
|
if (block.kind === "table") {
|
|
8251
|
-
return tableBlockLatex(block, labelKinds, fixedFloats);
|
|
8355
|
+
return tableBlockLatex(block, labelKinds, fixedFloats, unitId);
|
|
8252
8356
|
}
|
|
8253
8357
|
if (block.kind === "image") {
|
|
8254
|
-
return imageBlockLatex(block, labelKinds, fixedFloats);
|
|
8358
|
+
return imageBlockLatex(block, labelKinds, fixedFloats, unitId);
|
|
8255
8359
|
}
|
|
8256
8360
|
if (block.kind === "bibliography") {
|
|
8257
8361
|
const width = String(Math.max(references.length, 9));
|
|
@@ -8295,10 +8399,8 @@ ${meta.abstract.trim()}
|
|
|
8295
8399
|
}
|
|
8296
8400
|
function documentLabelKindMap(document2) {
|
|
8297
8401
|
const map = /* @__PURE__ */ new Map();
|
|
8298
|
-
for (const entry of
|
|
8299
|
-
|
|
8300
|
-
map.set(entry.key, entry.kind);
|
|
8301
|
-
}
|
|
8402
|
+
for (const [key, entry] of labelNumberMap(document2)) {
|
|
8403
|
+
map.set(key, entry.kind);
|
|
8302
8404
|
}
|
|
8303
8405
|
return map;
|
|
8304
8406
|
}
|
|
@@ -8307,6 +8409,17 @@ function closingHamdalaLatex() {
|
|
|
8307
8409
|
\\maghribi{${BUTEX_CLOSING_HAMDALA}}
|
|
8308
8410
|
\\end{center}`;
|
|
8309
8411
|
}
|
|
8412
|
+
function documentContentLatex(document2, context) {
|
|
8413
|
+
if (!context?.unitId?.trim()) throw new Error("book-content export requires a stable unitId");
|
|
8414
|
+
const labelKinds = documentLabelKindMap(document2);
|
|
8415
|
+
for (const target of context?.targets ?? []) {
|
|
8416
|
+
if (target.kind === "fig" || target.kind === "tab" || target.kind === "eq") {
|
|
8417
|
+
labelKinds.set(target.key, target.kind);
|
|
8418
|
+
if (target.manualKey) labelKinds.set(target.manualKey, target.kind);
|
|
8419
|
+
}
|
|
8420
|
+
}
|
|
8421
|
+
return document2.blocks.filter((block) => block.kind !== "bibliography").map((block) => blockLatex(block, document2.references, labelKinds, false, context?.unitId)).filter((part) => part.length > 0).join("\n\n");
|
|
8422
|
+
}
|
|
8310
8423
|
function documentLatex(document2, options = {}) {
|
|
8311
8424
|
const meta = document2.meta ?? emptyDocumentMeta();
|
|
8312
8425
|
const twocolumn = options.twocolumn === true;
|
|
@@ -8338,6 +8451,65 @@ ${body}
|
|
|
8338
8451
|
`;
|
|
8339
8452
|
}
|
|
8340
8453
|
|
|
8454
|
+
// src/document/bookContent.ts
|
|
8455
|
+
function bookContextReferences(context) {
|
|
8456
|
+
return (context?.references ?? []).map((entry, index) => ({
|
|
8457
|
+
...referenceFromJson(entry),
|
|
8458
|
+
id: "book-ref-" + String(index)
|
|
8459
|
+
}));
|
|
8460
|
+
}
|
|
8461
|
+
function bookContentTargets(document2, context) {
|
|
8462
|
+
const local = collectDocumentReferenceTargets(document2).map((target) => {
|
|
8463
|
+
const key = implicitReferenceKey(target.kind, target.ownerId, context.unitId);
|
|
8464
|
+
const projection = context.targets?.find((entry) => entry.unitId === context.unitId && (entry.ownerId === target.ownerId || entry.key === key));
|
|
8465
|
+
return {
|
|
8466
|
+
...target,
|
|
8467
|
+
key,
|
|
8468
|
+
...projection?.displayLabel ? { displayLabel: projection.displayLabel } : {}
|
|
8469
|
+
};
|
|
8470
|
+
});
|
|
8471
|
+
const headings = document2.blocks.flatMap((block) => {
|
|
8472
|
+
if (block.kind !== "textBlock" || block.command === "\\paragraph") return [];
|
|
8473
|
+
const kind = block.command.slice(1);
|
|
8474
|
+
const caption = block.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("");
|
|
8475
|
+
return [{
|
|
8476
|
+
unitId: context.unitId,
|
|
8477
|
+
ownerId: block.id,
|
|
8478
|
+
kind,
|
|
8479
|
+
key: implicitHeadingReferenceKey(kind, block.id, context.unitId),
|
|
8480
|
+
caption,
|
|
8481
|
+
number: null,
|
|
8482
|
+
displayLabel: context.targets?.find((target) => target.unitId === context.unitId && target.ownerId === block.id)?.displayLabel ?? (caption || kind),
|
|
8483
|
+
...block.label ? { manualKey: block.label } : {}
|
|
8484
|
+
}];
|
|
8485
|
+
});
|
|
8486
|
+
const external = (context.targets ?? []).filter((target) => target.unitId !== context.unitId).map((target) => ({
|
|
8487
|
+
...target,
|
|
8488
|
+
caption: target.caption ?? "",
|
|
8489
|
+
number: null
|
|
8490
|
+
}));
|
|
8491
|
+
return [...local, ...headings, ...external];
|
|
8492
|
+
}
|
|
8493
|
+
function formatBookReferenceLabel(keys, document2, command, context, options = {}) {
|
|
8494
|
+
const targets = context.targets ?? [];
|
|
8495
|
+
return keys.map((key) => {
|
|
8496
|
+
const target = targets.find((entry) => entry.key === key || entry.manualKey === key);
|
|
8497
|
+
if (target?.displayLabel) return target.displayLabel;
|
|
8498
|
+
const local = collectDocumentReferenceTargets(document2).find((entry) => entry.manualKey === key || implicitReferenceKey(entry.kind, entry.ownerId, context.unitId) === key);
|
|
8499
|
+
if (local) return formatRefLabel([local.manualKey || local.key], document2, command, options);
|
|
8500
|
+
const heading = document2.blocks.find((block) => block.kind === "textBlock" && block.command !== "\\paragraph" && (block.label === key || implicitHeadingReferenceKey(
|
|
8501
|
+
block.command.slice(1),
|
|
8502
|
+
block.id,
|
|
8503
|
+
context.unitId
|
|
8504
|
+
) === key));
|
|
8505
|
+
if (heading?.kind === "textBlock") {
|
|
8506
|
+
const caption = heading.field.tokens.filter((token) => token.kind === "text").map((token) => token.text).join("").trim();
|
|
8507
|
+
return caption || heading.command.slice(1);
|
|
8508
|
+
}
|
|
8509
|
+
return command === "eqref" ? "(?)" : "?";
|
|
8510
|
+
}).join(options.documentDirection === "ltr" ? ", " : "\u060C");
|
|
8511
|
+
}
|
|
8512
|
+
|
|
8341
8513
|
// src/document/validation.ts
|
|
8342
8514
|
var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
|
|
8343
8515
|
function hasXeLatexUnsupportedEmoji(value) {
|
|
@@ -8397,10 +8569,17 @@ function validateToken(context, token, path, blockId) {
|
|
|
8397
8569
|
return;
|
|
8398
8570
|
}
|
|
8399
8571
|
validateTextValue(context, token.source, `${path}.source`, blockId, token.id);
|
|
8572
|
+
if (token.numbered === true && !supportsSingleEquationNumbering(token)) {
|
|
8573
|
+
addIssue(context, "invalid_equation_label", `${path}.numbered`, blockId, token.id);
|
|
8574
|
+
}
|
|
8400
8575
|
if (!token.labelEnabled) {
|
|
8401
8576
|
return;
|
|
8402
8577
|
}
|
|
8403
|
-
if (!token
|
|
8578
|
+
if (!supportsSingleEquationNumbering(token)) {
|
|
8579
|
+
addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
|
|
8580
|
+
return;
|
|
8581
|
+
}
|
|
8582
|
+
if (!isNumberedEquation(token)) {
|
|
8404
8583
|
addIssue(context, "invalid_equation_label", `${path}.label`, blockId, token.id);
|
|
8405
8584
|
return;
|
|
8406
8585
|
}
|
|
@@ -8414,6 +8593,13 @@ function validateInlineField(context, field, path, blockId) {
|
|
|
8414
8593
|
function validateBlock(context, block, path) {
|
|
8415
8594
|
if (block.kind === "textBlock") {
|
|
8416
8595
|
validateInlineField(context, block.field, `${path}.field`, block.id);
|
|
8596
|
+
if (block.command !== "\\paragraph") {
|
|
8597
|
+
const kind = block.command.slice(1);
|
|
8598
|
+
if (block.label) registerKey(context, block.label, `${path}.label`, kind, block.id);
|
|
8599
|
+
if (context.bookMode && context.bookContext) {
|
|
8600
|
+
context.labelKinds.set(implicitHeadingReferenceKey(kind, block.id, context.bookContext.unitId), kind);
|
|
8601
|
+
}
|
|
8602
|
+
}
|
|
8417
8603
|
return;
|
|
8418
8604
|
}
|
|
8419
8605
|
if (block.kind === "list") {
|
|
@@ -8463,6 +8649,10 @@ function validateBlock(context, block, path) {
|
|
|
8463
8649
|
}
|
|
8464
8650
|
return;
|
|
8465
8651
|
}
|
|
8652
|
+
if (block.kind === "bibliography" && context.bookMode) {
|
|
8653
|
+
addIssue(context, "book_local_bibliography", path, block.id);
|
|
8654
|
+
return;
|
|
8655
|
+
}
|
|
8466
8656
|
if (block.kind === "raw") {
|
|
8467
8657
|
validateTextValue(context, block.value, `${path}.value`, block.id);
|
|
8468
8658
|
}
|
|
@@ -8485,7 +8675,7 @@ function validateDuplicateKeys(context) {
|
|
|
8485
8675
|
}
|
|
8486
8676
|
function validatePendingReferences(context) {
|
|
8487
8677
|
for (const pending of context.pendingCitations) {
|
|
8488
|
-
if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)))) {
|
|
8678
|
+
if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)) || context.citationKeys !== void 0 && !context.citationKeys.has(normalizeDocumentKey(key)))) {
|
|
8489
8679
|
addIssue(
|
|
8490
8680
|
context,
|
|
8491
8681
|
"missing_citation",
|
|
@@ -8498,7 +8688,7 @@ function validatePendingReferences(context) {
|
|
|
8498
8688
|
for (const pending of context.pendingReferences) {
|
|
8499
8689
|
for (const key of pending.token.keys) {
|
|
8500
8690
|
const normalized = normalizeDocumentKey(key);
|
|
8501
|
-
if (!isValidDocumentKey(normalized)) {
|
|
8691
|
+
if (!isValidDocumentKey(normalized) || !context.labelKinds.has(normalized)) {
|
|
8502
8692
|
addIssue(
|
|
8503
8693
|
context,
|
|
8504
8694
|
"missing_reference",
|
|
@@ -8567,7 +8757,7 @@ function resolveDocument(document2) {
|
|
|
8567
8757
|
return null;
|
|
8568
8758
|
}
|
|
8569
8759
|
}
|
|
8570
|
-
function validateDocument(document2) {
|
|
8760
|
+
function validateDocument(document2, options = {}) {
|
|
8571
8761
|
const resolved = resolveDocument(document2);
|
|
8572
8762
|
if (!resolved) {
|
|
8573
8763
|
return {
|
|
@@ -8579,6 +8769,9 @@ function validateDocument(document2) {
|
|
|
8579
8769
|
issues: [],
|
|
8580
8770
|
keyOwners: [],
|
|
8581
8771
|
labelKinds: /* @__PURE__ */ new Map(),
|
|
8772
|
+
bookMode: options.mode === "book-content",
|
|
8773
|
+
bookContext: options.bookContext,
|
|
8774
|
+
...options.mode === "book-content" && options.bookContext?.references ? { citationKeys: new Set(options.bookContext.references.map((entry) => normalizeDocumentKey(entry.key))) } : {},
|
|
8582
8775
|
pendingCitations: [],
|
|
8583
8776
|
pendingReferences: []
|
|
8584
8777
|
};
|
|
@@ -8597,6 +8790,28 @@ function validateDocument(document2) {
|
|
|
8597
8790
|
resolved.blocks.forEach((block, index) => {
|
|
8598
8791
|
validateBlock(context, block, `$.blocks[${String(index)}]`);
|
|
8599
8792
|
});
|
|
8793
|
+
for (const target of collectDocumentReferenceTargets(resolved)) {
|
|
8794
|
+
const autoKey = context.bookMode && context.bookContext ? implicitReferenceKey(target.kind, target.ownerId, context.bookContext.unitId) : target.key;
|
|
8795
|
+
if (target.number !== null && target.manualKey !== autoKey) {
|
|
8796
|
+
registerKey(
|
|
8797
|
+
context,
|
|
8798
|
+
autoKey,
|
|
8799
|
+
"$.automatic_labels",
|
|
8800
|
+
target.kind,
|
|
8801
|
+
target.kind === "eq" ? void 0 : target.ownerId,
|
|
8802
|
+
target.kind === "eq" ? target.ownerId : void 0
|
|
8803
|
+
);
|
|
8804
|
+
}
|
|
8805
|
+
}
|
|
8806
|
+
if (context.bookMode) {
|
|
8807
|
+
for (const target of context.bookContext?.targets ?? []) {
|
|
8808
|
+
context.labelKinds.set(normalizeDocumentKey(target.key), target.kind);
|
|
8809
|
+
if (target.manualKey) context.labelKinds.set(normalizeDocumentKey(target.manualKey), target.kind);
|
|
8810
|
+
}
|
|
8811
|
+
}
|
|
8812
|
+
if (context.bookMode && !context.bookContext?.unitId?.trim()) {
|
|
8813
|
+
addIssue(context, "book_context_required", "$.book_context");
|
|
8814
|
+
}
|
|
8600
8815
|
for (const diagnostic of resolved.diagnostics) {
|
|
8601
8816
|
addIssue(context, "parser_diagnostic", diagnostic.path);
|
|
8602
8817
|
}
|
|
@@ -8727,7 +8942,7 @@ function previewInlines(field, islands, output, equationSide, document2, preview
|
|
|
8727
8942
|
kind: "cite",
|
|
8728
8943
|
id: token.id,
|
|
8729
8944
|
keys: [...token.keys],
|
|
8730
|
-
label: formatCiteLabel(token.keys, document2.references, {
|
|
8945
|
+
label: formatCiteLabel(token.keys, previewOptions.mode === "book-content" ? bookContextReferences(previewOptions.bookContext) : document2.references, {
|
|
8731
8946
|
documentDirection: previewOptions.documentDirection,
|
|
8732
8947
|
digitForm: previewOptions.digitForm
|
|
8733
8948
|
})
|
|
@@ -8739,12 +8954,16 @@ function previewInlines(field, islands, output, equationSide, document2, preview
|
|
|
8739
8954
|
id: token.id,
|
|
8740
8955
|
keys: [...token.keys],
|
|
8741
8956
|
refCommand: token.refCommand,
|
|
8742
|
-
label:
|
|
8957
|
+
label: previewOptions.mode === "book-content" && previewOptions.bookContext ? formatBookReferenceLabel(token.keys, document2, token.refCommand, previewOptions.bookContext, {
|
|
8958
|
+
documentDirection: previewOptions.documentDirection,
|
|
8959
|
+
digitForm: previewOptions.digitForm
|
|
8960
|
+
}) : formatRefLabel(token.keys, document2, token.refCommand, {
|
|
8743
8961
|
documentDirection: previewOptions.documentDirection,
|
|
8744
8962
|
digitForm: previewOptions.digitForm
|
|
8745
8963
|
})
|
|
8746
8964
|
};
|
|
8747
8965
|
}
|
|
8966
|
+
const number = collectDocumentReferenceTargets(document2).find((target) => target.kind === "eq" && target.ownerId === token.id)?.number;
|
|
8748
8967
|
const island = {
|
|
8749
8968
|
id: token.id,
|
|
8750
8969
|
tex: mathTex(token, equationSide, previewOptions.digitForm),
|
|
@@ -8752,8 +8971,12 @@ function previewInlines(field, islands, output, equationSide, document2, preview
|
|
|
8752
8971
|
output,
|
|
8753
8972
|
editable: token.editable
|
|
8754
8973
|
};
|
|
8974
|
+
const numberLabel = number == null ? void 0 : formatBibliographyNumber(number, {
|
|
8975
|
+
documentDirection: previewOptions.documentDirection,
|
|
8976
|
+
digitForm: previewOptions.digitForm
|
|
8977
|
+
});
|
|
8755
8978
|
islands.push(island);
|
|
8756
|
-
return { kind: "math", ...island };
|
|
8979
|
+
return { kind: "math", ...island, ...numberLabel ? { numberLabel } : {} };
|
|
8757
8980
|
});
|
|
8758
8981
|
}
|
|
8759
8982
|
function textBlockPreview(block, islands, output, equationSide, document2, previewOptions) {
|
|
@@ -8776,12 +8999,9 @@ function textBlockPreview(block, islands, output, equationSide, document2, previ
|
|
|
8776
8999
|
...block.centered ? { centered: true } : {}
|
|
8777
9000
|
};
|
|
8778
9001
|
}
|
|
8779
|
-
function floatNumberLabel(document2,
|
|
8780
|
-
|
|
8781
|
-
|
|
8782
|
-
}
|
|
8783
|
-
const hit = labelNumberMap(document2).get(key.trim());
|
|
8784
|
-
if (!hit || hit.kind !== "fig" && hit.kind !== "tab") {
|
|
9002
|
+
function floatNumberLabel(document2, ownerId, previewOptions) {
|
|
9003
|
+
const hit = collectDocumentReferenceTargets(document2).find((target) => target.ownerId === ownerId);
|
|
9004
|
+
if (!hit || hit.number === null || hit.kind !== "fig" && hit.kind !== "tab") {
|
|
8785
9005
|
return void 0;
|
|
8786
9006
|
}
|
|
8787
9007
|
return formatFloatCaptionTitle(hit.kind, hit.number, {
|
|
@@ -8823,7 +9043,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
|
|
|
8823
9043
|
id: block.id,
|
|
8824
9044
|
rows: block.rows.map((row) => row.map((cell) => previewInlines(cell, islands, output, equationSide, document2, previewOptions))),
|
|
8825
9045
|
...captionPreview(block, islands, output, equationSide, document2, previewOptions),
|
|
8826
|
-
...label.length > 0 ? { label
|
|
9046
|
+
...label.length > 0 ? { label } : {},
|
|
9047
|
+
...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
|
|
8827
9048
|
...block.metadata ? { metadata: { ...block.metadata } } : {}
|
|
8828
9049
|
};
|
|
8829
9050
|
}
|
|
@@ -8837,7 +9058,8 @@ function blockPreview(block, islands, output, equationSide, document2, previewOp
|
|
|
8837
9058
|
options: block.options,
|
|
8838
9059
|
centered: block.centered,
|
|
8839
9060
|
...captionPreview(block, islands, output, equationSide, document2, previewOptions),
|
|
8840
|
-
...label.length > 0 ? { label
|
|
9061
|
+
...label.length > 0 ? { label } : {},
|
|
9062
|
+
...floatNumberLabel(document2, block.id, previewOptions) ? { numberLabel: floatNumberLabel(document2, block.id, previewOptions) } : {},
|
|
8841
9063
|
...block.metadata ? { metadata: { ...block.metadata } } : {}
|
|
8842
9064
|
};
|
|
8843
9065
|
}
|
|
@@ -8891,14 +9113,16 @@ function documentPreview(document2, output = "svg", equationSide = "arabic", pre
|
|
|
8891
9113
|
const options = {
|
|
8892
9114
|
documentDirection: previewOptions.documentDirection ?? "rtl",
|
|
8893
9115
|
digitForm: previewOptions.digitForm ?? (previewOptions.documentDirection === "ltr" ? "western" : "arabicIndic"),
|
|
8894
|
-
uiLocale: previewOptions.uiLocale ?? "ar"
|
|
9116
|
+
uiLocale: previewOptions.uiLocale ?? "ar",
|
|
9117
|
+
mode: previewOptions.mode ?? "article",
|
|
9118
|
+
bookContext: previewOptions.bookContext
|
|
8895
9119
|
};
|
|
8896
|
-
const body = document2.blocks.map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
|
|
9120
|
+
const body = document2.blocks.filter((block) => options.mode !== "book-content" || block.kind !== "bibliography").map((block) => blockPreview(block, mathIslands, output, equationSide, document2, options));
|
|
8897
9121
|
return {
|
|
8898
9122
|
blocks: [
|
|
8899
|
-
...articleChromePreview(document2, options.uiLocale ?? "ar", options),
|
|
9123
|
+
...options.mode === "book-content" ? [] : articleChromePreview(document2, options.uiLocale ?? "ar", options),
|
|
8900
9124
|
...body,
|
|
8901
|
-
{ kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }
|
|
9125
|
+
...options.mode === "book-content" ? [] : [{ kind: "closing", id: "article-closing", text: BUTEX_CLOSING_HAMDALA, tex: butexMaghribiDisplayTex(BUTEX_CLOSING_HAMDALA) }]
|
|
8902
9126
|
],
|
|
8903
9127
|
mathIslands
|
|
8904
9128
|
};
|
|
@@ -8912,6 +9136,11 @@ function documentLatex2(document2, options = {}) {
|
|
|
8912
9136
|
}
|
|
8913
9137
|
return documentLatex(document2, options);
|
|
8914
9138
|
}
|
|
9139
|
+
function documentContentLatex2(document2, context) {
|
|
9140
|
+
const validation = validateDocument(document2, { mode: "book-content", bookContext: context });
|
|
9141
|
+
if (!validation.valid) throw new DocumentValidationError(validation.issues);
|
|
9142
|
+
return documentContentLatex(document2, context);
|
|
9143
|
+
}
|
|
8915
9144
|
export {
|
|
8916
9145
|
BUTEX_BASMALA,
|
|
8917
9146
|
BUTEX_BASMALA_LINE1,
|
|
@@ -8940,6 +9169,8 @@ export {
|
|
|
8940
9169
|
arabicXeLatexPreamblePkg,
|
|
8941
9170
|
bibliographyEntryBody,
|
|
8942
9171
|
bibliographyEntryLatex,
|
|
9172
|
+
bookContentTargets,
|
|
9173
|
+
bookContextReferences,
|
|
8943
9174
|
butexBasmalaDisplayLatex,
|
|
8944
9175
|
butexBasmalaDisplayTex,
|
|
8945
9176
|
butexBasmalaTitlingPreamble,
|
|
@@ -8952,7 +9183,9 @@ export {
|
|
|
8952
9183
|
clearDocumentImageAsset,
|
|
8953
9184
|
cloneDocumentMeta,
|
|
8954
9185
|
cloneDocumentNode,
|
|
9186
|
+
collectDocumentKeyLabels,
|
|
8955
9187
|
collectDocumentLabels,
|
|
9188
|
+
collectDocumentReferenceTargets,
|
|
8956
9189
|
createDocumentHistory,
|
|
8957
9190
|
createEmptyArabicEquationSession,
|
|
8958
9191
|
createEmptyDocument,
|
|
@@ -8963,6 +9196,7 @@ export {
|
|
|
8963
9196
|
detectInlineSpans,
|
|
8964
9197
|
detectMathSpans,
|
|
8965
9198
|
diffDocument,
|
|
9199
|
+
documentContentLatex2 as documentContentLatex,
|
|
8966
9200
|
documentHasAbstract,
|
|
8967
9201
|
documentHasMaketitle,
|
|
8968
9202
|
documentHasTitleStack,
|
|
@@ -8979,6 +9213,7 @@ export {
|
|
|
8979
9213
|
fillDocumentMetaGaps,
|
|
8980
9214
|
findFirstInlineField,
|
|
8981
9215
|
formatBibliographyNumber,
|
|
9216
|
+
formatBookReferenceLabel,
|
|
8982
9217
|
formatCiteLabel,
|
|
8983
9218
|
formatFloatCaptionPreview,
|
|
8984
9219
|
formatFloatCaptionTitle,
|
|
@@ -8990,6 +9225,8 @@ export {
|
|
|
8990
9225
|
getDocumentReferences,
|
|
8991
9226
|
hasXeLatexUnsupportedEmoji,
|
|
8992
9227
|
hijriMonthLabel,
|
|
9228
|
+
implicitHeadingReferenceKey,
|
|
9229
|
+
implicitReferenceKey,
|
|
8993
9230
|
inlineFieldLatex,
|
|
8994
9231
|
insertCiteTokenAtCaret,
|
|
8995
9232
|
insertDocumentBlockAfter,
|
|
@@ -8998,6 +9235,8 @@ export {
|
|
|
8998
9235
|
insertRefTokenAtCaret,
|
|
8999
9236
|
isDocumentValid,
|
|
9000
9237
|
isDuplicateDocumentKey,
|
|
9238
|
+
isNumberedEquation,
|
|
9239
|
+
isNumberedFloat,
|
|
9001
9240
|
isValidDocumentKey,
|
|
9002
9241
|
labelNumberMap,
|
|
9003
9242
|
mathNodeFromArabicSession,
|
|
@@ -9041,6 +9280,7 @@ export {
|
|
|
9041
9280
|
toggleBlockInSelection,
|
|
9042
9281
|
toggleTextTokenStyle,
|
|
9043
9282
|
updateCiteTokenKeys,
|
|
9283
|
+
updateDocumentHeadingLabel,
|
|
9044
9284
|
updateDocumentImageAsset,
|
|
9045
9285
|
updateDocumentImageMeta,
|
|
9046
9286
|
updateDocumentImageValue,
|