@drghaliasri/butex 7.4.2 → 8.0.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 +49 -553
- package/dist/{document2-cli.js → document-cli.js} +419 -348
- package/dist/document-cli.js.map +1 -0
- package/dist/document.d.mts +1022 -186
- package/dist/document.d.ts +1022 -186
- package/dist/document.js +6103 -652
- package/dist/document.js.map +1 -1
- package/dist/document.mjs +5963 -612
- package/dist/document.mjs.map +1 -1
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.global.js +14 -0
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +14 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -0
- package/dist/index.mjs.map +1 -1
- package/dist/react-document-preview.d.mts +338 -0
- package/dist/react-document-preview.d.ts +338 -0
- package/dist/{react-document2-preview.js → react-document-preview.js} +811 -791
- package/dist/react-document-preview.js.map +1 -0
- package/dist/{react-document2-preview.mjs → react-document-preview.mjs} +804 -784
- package/dist/react-document-preview.mjs.map +1 -0
- package/dist/react-document.d.mts +634 -35
- package/dist/react-document.d.ts +634 -35
- package/dist/react-document.js +13540 -4542
- package/dist/react-document.js.map +1 -1
- package/dist/react-document.mjs +13536 -4546
- package/dist/react-document.mjs.map +1 -1
- package/dist/react.d.mts +12 -0
- package/dist/react.d.ts +12 -0
- package/dist/react.js +33 -12
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +33 -12
- package/dist/react.mjs.map +1 -1
- package/package.json +7 -17
- package/dist/document2-cli.js.map +0 -1
- package/dist/document2.d.mts +0 -1156
- package/dist/document2.d.ts +0 -1156
- package/dist/document2.js +0 -9137
- package/dist/document2.js.map +0 -1
- package/dist/document2.mjs +0 -8972
- package/dist/document2.mjs.map +0 -1
- package/dist/react-document2-preview.d.mts +0 -338
- package/dist/react-document2-preview.d.ts +0 -338
- package/dist/react-document2-preview.js.map +0 -1
- package/dist/react-document2-preview.mjs.map +0 -1
- package/dist/react-document2.d.mts +0 -754
- package/dist/react-document2.d.ts +0 -754
- package/dist/react-document2.js +0 -23011
- package/dist/react-document2.js.map +0 -1
- package/dist/react-document2.mjs +0 -22981
- package/dist/react-document2.mjs.map +0 -1
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
3
|
|
|
4
|
-
// src/
|
|
4
|
+
// src/document-cli/main.ts
|
|
5
5
|
var import_node_events = require("events");
|
|
6
6
|
|
|
7
|
-
// src/
|
|
7
|
+
// src/document-cli/httpServer.ts
|
|
8
8
|
var import_node_crypto = require("crypto");
|
|
9
9
|
var import_node_http = require("http");
|
|
10
10
|
|
|
11
|
-
// src/
|
|
11
|
+
// src/document/ids.ts
|
|
12
12
|
var nextId = 1;
|
|
13
13
|
var instanceId = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
14
|
-
function
|
|
14
|
+
function documentId(prefix) {
|
|
15
15
|
const id = `${prefix}_${instanceId}_${String(nextId)}`;
|
|
16
16
|
nextId += 1;
|
|
17
17
|
return id;
|
|
@@ -45,7 +45,7 @@ function formatDigits(value, digitForm = "western") {
|
|
|
45
45
|
}).join("");
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
// src/
|
|
48
|
+
// src/document/articleMeta.ts
|
|
49
49
|
var BUTEX_BASMALA_LINE1 = "\u0628\u0650\u0633\u0652\u0645\u0650 \u0671\u0644\u0644\u064E\u0651\u0670\u0647\u0650 \u0671\u0644\u0631\u064E\u0651\u062D\u0652\u0645\u064E\u0670\u0646\u0650 \u0671\u0644\u0631\u064E\u0651\u062D\u0650\u064A\u0645\u0650";
|
|
50
50
|
var BUTEX_BASMALA_LINE2 = "\u0648\u064E\u0671\u0644\u0635\u064E\u0651\u0644\u064E\u0627\u0629\u064F \u0648\u064E\u0671\u0644\u0633\u064E\u0651\u0644\u064E\u0627\u0645\u064F \u0639\u064E\u0644\u064E\u0649\u0670 \u0631\u064E\u0633\u064F\u0648\u0644\u0650 \u0671\u0644\u0644\u064E\u0651\u0670\u0647\u0650";
|
|
51
51
|
var BUTEX_BASMALA_SALAWAT = "\u0635\u064E\u0644\u064E\u0651\u0649 \u0671\u0644\u0644\u064E\u0651\u0647\u064F \u0639\u064E\u0644\u064E\u064A\u0652\u0647\u0650 \u0648\u064E\u0633\u064E\u0644\u064E\u0651\u0645\u064E";
|
|
@@ -168,7 +168,7 @@ var DEFAULT_HIJRI_DATE = {
|
|
|
168
168
|
function hijriMonthLabel(month, locale = "ar") {
|
|
169
169
|
return locale === "en" ? HIJRI_MONTH_LABELS_EN[month] : month;
|
|
170
170
|
}
|
|
171
|
-
function
|
|
171
|
+
function emptyDocumentMeta() {
|
|
172
172
|
return {
|
|
173
173
|
title: "",
|
|
174
174
|
authors: "",
|
|
@@ -194,7 +194,7 @@ function resolveHijriMonthId(value) {
|
|
|
194
194
|
}
|
|
195
195
|
return DEFAULT_HIJRI_DATE.month;
|
|
196
196
|
}
|
|
197
|
-
function
|
|
197
|
+
function normalizeDocumentHijriDate(value) {
|
|
198
198
|
if (typeof value !== "object" || value === null) {
|
|
199
199
|
return { ...DEFAULT_HIJRI_DATE };
|
|
200
200
|
}
|
|
@@ -204,22 +204,22 @@ function normalizeDocument2HijriDate(value) {
|
|
|
204
204
|
const month = resolveHijriMonthId(raw.month);
|
|
205
205
|
return { day, month, year };
|
|
206
206
|
}
|
|
207
|
-
function
|
|
207
|
+
function normalizeDocumentMeta(value) {
|
|
208
208
|
if (typeof value !== "object" || value === null) {
|
|
209
|
-
return
|
|
209
|
+
return emptyDocumentMeta();
|
|
210
210
|
}
|
|
211
211
|
const raw = value;
|
|
212
212
|
return {
|
|
213
213
|
title: typeof raw.title === "string" ? raw.title : "",
|
|
214
214
|
authors: typeof raw.authors === "string" ? raw.authors : "",
|
|
215
|
-
date:
|
|
215
|
+
date: normalizeDocumentHijriDate(raw.date),
|
|
216
216
|
abstract: typeof raw.abstract === "string" ? raw.abstract : ""
|
|
217
217
|
};
|
|
218
218
|
}
|
|
219
|
-
function
|
|
219
|
+
function documentHasMaketitle(meta) {
|
|
220
220
|
return meta.title.trim().length > 0 || meta.authors.trim().length > 0;
|
|
221
221
|
}
|
|
222
|
-
function
|
|
222
|
+
function documentHasAbstract(meta) {
|
|
223
223
|
return meta.abstract.trim().length > 0;
|
|
224
224
|
}
|
|
225
225
|
function formatHijriDate(date, options = "ar") {
|
|
@@ -235,7 +235,7 @@ function formatHijriDate(date, options = "ar") {
|
|
|
235
235
|
return `${day} ${month} ${year}\u0647\u0640`;
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
-
// src/
|
|
238
|
+
// src/document/citations.ts
|
|
239
239
|
var DEFAULT_REFERENCE_FIELD_SEPARATOR = "\u060C";
|
|
240
240
|
function normalizeReferenceFieldSeparator(value) {
|
|
241
241
|
return value === "," ? "," : DEFAULT_REFERENCE_FIELD_SEPARATOR;
|
|
@@ -252,7 +252,7 @@ function citeTokenLatex(keys) {
|
|
|
252
252
|
}
|
|
253
253
|
function referenceFromJson(json) {
|
|
254
254
|
return {
|
|
255
|
-
id:
|
|
255
|
+
id: documentId("ref"),
|
|
256
256
|
key: json.key,
|
|
257
257
|
authors: typeof json.authors === "string" ? json.authors : "",
|
|
258
258
|
title: typeof json.title === "string" ? json.title : "",
|
|
@@ -589,7 +589,7 @@ function fromArabicJson(json) {
|
|
|
589
589
|
return parseNode(json, "arabic");
|
|
590
590
|
}
|
|
591
591
|
|
|
592
|
-
// src/document/mathObject.ts
|
|
592
|
+
// src/shared/math-document/mathObject.ts
|
|
593
593
|
function isObject(value) {
|
|
594
594
|
return typeof value === "object" && value !== null;
|
|
595
595
|
}
|
|
@@ -706,15 +706,15 @@ ${lines.map((line) => ` ${line}`).join(" \\\\\n")}
|
|
|
706
706
|
${math.closing}`;
|
|
707
707
|
}
|
|
708
708
|
|
|
709
|
-
// src/
|
|
709
|
+
// src/document/labels.ts
|
|
710
710
|
function defaultFloatMeta() {
|
|
711
711
|
return {
|
|
712
712
|
centered: true,
|
|
713
713
|
captionEnabled: false,
|
|
714
714
|
caption: "",
|
|
715
715
|
captionField: {
|
|
716
|
-
id:
|
|
717
|
-
tokens: [{ id:
|
|
716
|
+
id: documentId("field"),
|
|
717
|
+
tokens: [{ id: documentId("text"), kind: "text", text: "" }]
|
|
718
718
|
},
|
|
719
719
|
labelEnabled: false,
|
|
720
720
|
label: ""
|
|
@@ -752,7 +752,7 @@ function refTokenLatex(keys, refCommand) {
|
|
|
752
752
|
const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
|
|
753
753
|
return `${command}{${keys.join(",")}}`;
|
|
754
754
|
}
|
|
755
|
-
function
|
|
755
|
+
function collectDocumentLabels(document2) {
|
|
756
756
|
const entries = [];
|
|
757
757
|
const counters = { fig: 0, tab: 0, eq: 0 };
|
|
758
758
|
function pushEqFromField(tokens) {
|
|
@@ -848,7 +848,7 @@ function applyFloatMetaPatch(block, patch) {
|
|
|
848
848
|
const existingText = block.captionField.tokens.find((token) => token.kind === "text");
|
|
849
849
|
block.captionField.tokens = [
|
|
850
850
|
{
|
|
851
|
-
id: existingText?.id ??
|
|
851
|
+
id: existingText?.id ?? documentId("text"),
|
|
852
852
|
kind: "text",
|
|
853
853
|
text: patch.caption
|
|
854
854
|
}
|
|
@@ -862,8 +862,8 @@ function applyFloatMetaPatch(block, patch) {
|
|
|
862
862
|
}
|
|
863
863
|
}
|
|
864
864
|
|
|
865
|
-
// src/
|
|
866
|
-
function
|
|
865
|
+
// src/document/inlineIdentity.ts
|
|
866
|
+
function createDocumentIdentityState() {
|
|
867
867
|
return {
|
|
868
868
|
blockIds: /* @__PURE__ */ new Set(),
|
|
869
869
|
itemIds: /* @__PURE__ */ new Set(),
|
|
@@ -877,9 +877,9 @@ function importedOrGeneratedId(value, prefix, used) {
|
|
|
877
877
|
used.add(imported);
|
|
878
878
|
return imported;
|
|
879
879
|
}
|
|
880
|
-
let generated =
|
|
880
|
+
let generated = documentId(prefix);
|
|
881
881
|
while (used.has(generated)) {
|
|
882
|
-
generated =
|
|
882
|
+
generated = documentId(prefix);
|
|
883
883
|
}
|
|
884
884
|
used.add(generated);
|
|
885
885
|
return generated;
|
|
@@ -932,7 +932,7 @@ function applyImportedInlineIds(field, sidecar, state) {
|
|
|
932
932
|
return field;
|
|
933
933
|
}
|
|
934
934
|
|
|
935
|
-
// src/
|
|
935
|
+
// src/document/inlineScanner.ts
|
|
936
936
|
var MATH_ENVIRONMENTS = /* @__PURE__ */ new Set([
|
|
937
937
|
"equation",
|
|
938
938
|
"equation*",
|
|
@@ -1074,8 +1074,8 @@ function mathSpanAt(value, i) {
|
|
|
1074
1074
|
}
|
|
1075
1075
|
return null;
|
|
1076
1076
|
}
|
|
1077
|
-
function
|
|
1078
|
-
return
|
|
1077
|
+
function detectMathSpans(value) {
|
|
1078
|
+
return detectInlineSpans(value).filter((span) => span.kind === "math").map((span) => ({
|
|
1079
1079
|
start: span.start,
|
|
1080
1080
|
end: span.end,
|
|
1081
1081
|
source: span.source,
|
|
@@ -1084,7 +1084,7 @@ function detectMathSpans2(value) {
|
|
|
1084
1084
|
display: span.display
|
|
1085
1085
|
}));
|
|
1086
1086
|
}
|
|
1087
|
-
function
|
|
1087
|
+
function detectInlineSpans(value) {
|
|
1088
1088
|
const spans = [];
|
|
1089
1089
|
let i = 0;
|
|
1090
1090
|
while (i < value.length) {
|
|
@@ -1111,7 +1111,7 @@ function detectInlineSpans2(value) {
|
|
|
1111
1111
|
return spans;
|
|
1112
1112
|
}
|
|
1113
1113
|
|
|
1114
|
-
// src/
|
|
1114
|
+
// src/document/importJson.ts
|
|
1115
1115
|
var TEXT_COMMANDS = /* @__PURE__ */ new Set(["\\section", "\\subsection", "\\subsubsection", "\\paragraph"]);
|
|
1116
1116
|
var LIST_COMMANDS = /* @__PURE__ */ new Set(["\\begin{itemize}", "\\begin{enumerate}"]);
|
|
1117
1117
|
function isObject2(value) {
|
|
@@ -1173,9 +1173,9 @@ function parseReferences(json) {
|
|
|
1173
1173
|
}
|
|
1174
1174
|
return references;
|
|
1175
1175
|
}
|
|
1176
|
-
function
|
|
1176
|
+
function createInlineField(value = "", mathObjects = [], options = {}, path = "$", diagnostics = [], formats = []) {
|
|
1177
1177
|
const mode = options.mode ?? "english";
|
|
1178
|
-
const spans =
|
|
1178
|
+
const spans = detectInlineSpans(value);
|
|
1179
1179
|
const mathSpans = spans.filter((span) => span.kind === "math");
|
|
1180
1180
|
const tokens = [];
|
|
1181
1181
|
let index = 0;
|
|
@@ -1189,7 +1189,7 @@ function createInlineField2(value = "", mathObjects = [], options = {}, path = "
|
|
|
1189
1189
|
}
|
|
1190
1190
|
if (span.kind === "cite") {
|
|
1191
1191
|
tokens.push({
|
|
1192
|
-
id:
|
|
1192
|
+
id: documentId("cite"),
|
|
1193
1193
|
kind: "cite",
|
|
1194
1194
|
keys: span.keys.length > 0 ? span.keys : []
|
|
1195
1195
|
});
|
|
@@ -1198,7 +1198,7 @@ function createInlineField2(value = "", mathObjects = [], options = {}, path = "
|
|
|
1198
1198
|
}
|
|
1199
1199
|
if (span.kind === "ref") {
|
|
1200
1200
|
tokens.push({
|
|
1201
|
-
id:
|
|
1201
|
+
id: documentId("ref"),
|
|
1202
1202
|
kind: "ref",
|
|
1203
1203
|
keys: span.keys.length > 0 ? span.keys : [],
|
|
1204
1204
|
refCommand: span.refCommand
|
|
@@ -1214,7 +1214,7 @@ function createInlineField2(value = "", mathObjects = [], options = {}, path = "
|
|
|
1214
1214
|
}
|
|
1215
1215
|
if (structuredMathJson && structuredMathJson.math_mode === span.opening && structuredMathJson.closing === span.closing) {
|
|
1216
1216
|
tokens.push({
|
|
1217
|
-
id:
|
|
1217
|
+
id: documentId("math"),
|
|
1218
1218
|
kind: "math",
|
|
1219
1219
|
display: span.display,
|
|
1220
1220
|
opening: span.opening,
|
|
@@ -1229,7 +1229,7 @@ function createInlineField2(value = "", mathObjects = [], options = {}, path = "
|
|
|
1229
1229
|
});
|
|
1230
1230
|
} else {
|
|
1231
1231
|
tokens.push({
|
|
1232
|
-
id:
|
|
1232
|
+
id: documentId("math"),
|
|
1233
1233
|
kind: "math",
|
|
1234
1234
|
display: span.display,
|
|
1235
1235
|
opening: span.opening,
|
|
@@ -1248,7 +1248,7 @@ function createInlineField2(value = "", mathObjects = [], options = {}, path = "
|
|
|
1248
1248
|
if (index < value.length || tokens.length === 0) {
|
|
1249
1249
|
pushFormattedTextTokens(tokens, value.slice(index), index, formats);
|
|
1250
1250
|
}
|
|
1251
|
-
return { id:
|
|
1251
|
+
return { id: documentId("field"), tokens: compactTextTokens(tokens) };
|
|
1252
1252
|
}
|
|
1253
1253
|
function styleFromFormat(format) {
|
|
1254
1254
|
const style = {};
|
|
@@ -1293,7 +1293,7 @@ function styleKey(style) {
|
|
|
1293
1293
|
}
|
|
1294
1294
|
function pushFormattedTextTokens(tokens, text, sourceStart, formats) {
|
|
1295
1295
|
if (text.length === 0) {
|
|
1296
|
-
tokens.push({ id:
|
|
1296
|
+
tokens.push({ id: documentId("text"), kind: "text", text });
|
|
1297
1297
|
return;
|
|
1298
1298
|
}
|
|
1299
1299
|
const styles = Array.from({ length: text.length });
|
|
@@ -1318,7 +1318,7 @@ function pushFormattedTextTokens(tokens, text, sourceStart, formats) {
|
|
|
1318
1318
|
}
|
|
1319
1319
|
const chunk = text.slice(chunkStart, index);
|
|
1320
1320
|
const style = styles[chunkStart];
|
|
1321
|
-
tokens.push({ id:
|
|
1321
|
+
tokens.push({ id: documentId("text"), kind: "text", text: chunk, ...style ? { style } : {} });
|
|
1322
1322
|
chunkStart = index;
|
|
1323
1323
|
}
|
|
1324
1324
|
}
|
|
@@ -1332,7 +1332,7 @@ function parseTextBlock(json, options, path, diagnostics, identities) {
|
|
|
1332
1332
|
kind: "textBlock",
|
|
1333
1333
|
command: json.command,
|
|
1334
1334
|
field: applyImportedInlineIds(
|
|
1335
|
-
|
|
1335
|
+
createInlineField(value, json.math_objects ?? [], options, `${path}.value`, diagnostics, json.command === "\\paragraph" ? json.formats ?? [] : []),
|
|
1336
1336
|
json.inline_ids,
|
|
1337
1337
|
identities
|
|
1338
1338
|
),
|
|
@@ -1346,7 +1346,7 @@ function parseListItem(json, options, path, diagnostics, identities) {
|
|
|
1346
1346
|
return {
|
|
1347
1347
|
id: importedOrGeneratedId(json.id, "item", identities.itemIds),
|
|
1348
1348
|
field: applyImportedInlineIds(
|
|
1349
|
-
|
|
1349
|
+
createInlineField(value, json.math_objects ?? [], options, `${path}.value`, diagnostics, json.formats ?? []),
|
|
1350
1350
|
json.inline_ids,
|
|
1351
1351
|
identities
|
|
1352
1352
|
),
|
|
@@ -1375,7 +1375,7 @@ function parseListBlock(json, options, path, diagnostics, identities) {
|
|
|
1375
1375
|
function captionFieldFromJson(json, options, path, diagnostics, identities) {
|
|
1376
1376
|
const caption = typeof json.caption === "string" ? json.caption : "";
|
|
1377
1377
|
return applyImportedInlineIds(
|
|
1378
|
-
|
|
1378
|
+
createInlineField(
|
|
1379
1379
|
caption,
|
|
1380
1380
|
json.caption_math_objects ?? [],
|
|
1381
1381
|
options,
|
|
@@ -1399,12 +1399,12 @@ function parseTableBlock(json, options, path, diagnostics, identities) {
|
|
|
1399
1399
|
}
|
|
1400
1400
|
return row.map((cell, columnIndex) => {
|
|
1401
1401
|
const value = requireString(cell, "\\begin{tabular} cells must be strings");
|
|
1402
|
-
const spanCount =
|
|
1402
|
+
const spanCount = detectMathSpans(value).length;
|
|
1403
1403
|
const cellMathObjects = mathObjects.slice(mathObjectIndex, mathObjectIndex + spanCount);
|
|
1404
1404
|
mathObjectIndex += spanCount;
|
|
1405
1405
|
const cellFormats = json.cell_formats?.[rowIndex]?.[columnIndex] ?? [];
|
|
1406
1406
|
return applyImportedInlineIds(
|
|
1407
|
-
|
|
1407
|
+
createInlineField(value, cellMathObjects, options, `${path}.rows[${String(rowIndex)}][${String(columnIndex)}]`, diagnostics, cellFormats),
|
|
1408
1408
|
json.cell_inline_ids?.[rowIndex]?.[columnIndex],
|
|
1409
1409
|
identities
|
|
1410
1410
|
);
|
|
@@ -1487,7 +1487,7 @@ function parseBlock(json, options, path, diagnostics, identities) {
|
|
|
1487
1487
|
...metadataFromJson(json.metadata)
|
|
1488
1488
|
};
|
|
1489
1489
|
}
|
|
1490
|
-
function
|
|
1490
|
+
function fromDocumentJson(json, options = {}) {
|
|
1491
1491
|
if (!isObject2(json) || json.node_type !== "DocumentObject") {
|
|
1492
1492
|
throw new Error('DocumentObject requires node_type "DocumentObject"');
|
|
1493
1493
|
}
|
|
@@ -1495,10 +1495,10 @@ function fromDocumentJson2(json, options = {}) {
|
|
|
1495
1495
|
throw new Error("DocumentObject requires blocks array");
|
|
1496
1496
|
}
|
|
1497
1497
|
const diagnostics = [];
|
|
1498
|
-
const identities =
|
|
1498
|
+
const identities = createDocumentIdentityState();
|
|
1499
1499
|
return {
|
|
1500
1500
|
nodeType: "DocumentObject",
|
|
1501
|
-
meta:
|
|
1501
|
+
meta: normalizeDocumentMeta(json.meta),
|
|
1502
1502
|
references: parseReferences(json.references),
|
|
1503
1503
|
blocks: json.blocks.map(
|
|
1504
1504
|
(block, index) => parseBlock(asBlockJson(block), options, `$.blocks[${String(index)}]`, diagnostics, identities)
|
|
@@ -1986,6 +1986,15 @@ var ATOMIC_COMMANDS = {
|
|
|
1986
1986
|
title: "\u0641\u0631\u0627\u063A \u0633\u0627\u0644\u0628 \u0631\u0641\u064A\u0639",
|
|
1987
1987
|
spacing: { direction: "negative", level: 1 }
|
|
1988
1988
|
},
|
|
1989
|
+
thinSpace: {
|
|
1990
|
+
id: "thinSpace",
|
|
1991
|
+
category: "spacing",
|
|
1992
|
+
englishTex: "\\,",
|
|
1993
|
+
arabicTex: "\\,",
|
|
1994
|
+
arabicLabel: "",
|
|
1995
|
+
title: "\u0641\u0631\u0627\u063A \u0631\u0641\u064A\u0639",
|
|
1996
|
+
spacing: { direction: "positive", level: 1 }
|
|
1997
|
+
},
|
|
1989
1998
|
mediumSpace: {
|
|
1990
1999
|
id: "mediumSpace",
|
|
1991
2000
|
category: "spacing",
|
|
@@ -3146,6 +3155,10 @@ function renderNodeEnglish(node, options) {
|
|
|
3146
3155
|
}
|
|
3147
3156
|
if (node.kind === "char") {
|
|
3148
3157
|
if (node.sourceLatex) {
|
|
3158
|
+
if (node.wrapperCommand === "\\boldarabic" || node.wrapperCommand === "\\mathbf") {
|
|
3159
|
+
const boldSource = node.sourceLatex.startsWith("\\mathbf{") ? node.sourceLatex : `\\mathbf{${node.sourceLatex}}`;
|
|
3160
|
+
return `${boldSource}${latinScripts(node, options)}`;
|
|
3161
|
+
}
|
|
3149
3162
|
return `${node.sourceLatex}${latinScripts(node, options)}`;
|
|
3150
3163
|
}
|
|
3151
3164
|
if (node.wrapperCommand) {
|
|
@@ -3999,7 +4012,7 @@ ${ATOMIC_OPERATOR_COMMAND_CSS}
|
|
|
3999
4012
|
${STANDARD_COMMAND_CSS}
|
|
4000
4013
|
`.trim();
|
|
4001
4014
|
|
|
4002
|
-
// src/document/normalizeCharImport.ts
|
|
4015
|
+
// src/shared/math-document/normalizeCharImport.ts
|
|
4003
4016
|
var WRAPPER_SPECS = [
|
|
4004
4017
|
...NON_DEFAULT_CHARACTER_FONT_SPECS.flatMap((font) => [
|
|
4005
4018
|
{ prefix: font.macro, font: font.id },
|
|
@@ -4063,7 +4076,7 @@ function normalizeCharImportExpr(expr) {
|
|
|
4063
4076
|
return { expr: current, characterFont };
|
|
4064
4077
|
}
|
|
4065
4078
|
|
|
4066
|
-
// src/document/mathEditorAdapter.ts
|
|
4079
|
+
// src/shared/math-document/mathEditorAdapter.ts
|
|
4067
4080
|
var COMMAND_FONT_MAP = Object.fromEntries([
|
|
4068
4081
|
["\\text", "infer"],
|
|
4069
4082
|
...NON_DEFAULT_CHARACTER_FONT_SPECS.flatMap((font) => [
|
|
@@ -4261,20 +4274,27 @@ function editableWrapperCommand(node) {
|
|
|
4261
4274
|
return null;
|
|
4262
4275
|
}
|
|
4263
4276
|
const child = singleCommandChar(node);
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
4269
|
-
|
|
4270
|
-
|
|
4277
|
+
let editorNode;
|
|
4278
|
+
if (child) {
|
|
4279
|
+
const normalized = normalizeCharImportExpr(child.expr);
|
|
4280
|
+
editorNode = createEditorNode("char", normalized.expr);
|
|
4281
|
+
if (normalized.characterFont !== "default") {
|
|
4282
|
+
editorNode.characterFont = normalized.characterFont;
|
|
4283
|
+
}
|
|
4284
|
+
} else {
|
|
4285
|
+
if (node.optionalArgs.length || node.mandatoryArgs.length !== 1) return null;
|
|
4286
|
+
const nodes = node.mandatoryArgs[0]?.chain;
|
|
4287
|
+
if (!nodes || nodes.length !== 1 || !(nodes[0] instanceof CommandNode)) return null;
|
|
4288
|
+
const nested = charFromTextLikeCommand(nodes[0]);
|
|
4289
|
+
if (!nested?.editable) return null;
|
|
4290
|
+
editorNode = nested.node;
|
|
4271
4291
|
}
|
|
4272
4292
|
editorNode.wrapperCommand = node.name === "\\mathtt" ? "\\mathtt" : node.name === "\\mathbf" ? "\\mathbf" : node.name === "\\boldarabic" ? "\\boldarabic" : "\\unit";
|
|
4273
4293
|
const scripts = attachScripts(node, editorNode);
|
|
4274
4294
|
if (scripts) {
|
|
4275
4295
|
return scripts;
|
|
4276
4296
|
}
|
|
4277
|
-
if (!editorNode.sourceLatex && node.name === "\\mathsf") {
|
|
4297
|
+
if (!editorNode.sourceLatex && node.name === "\\mathsf" && child) {
|
|
4278
4298
|
editorNode.sourceLatex = `\\mathsf{${child.expr}}`;
|
|
4279
4299
|
}
|
|
4280
4300
|
return { editable: true, node: editorNode };
|
|
@@ -4762,23 +4782,85 @@ function mathNodeFromEditorSession(session, mathMode = "$", closing = "$") {
|
|
|
4762
4782
|
};
|
|
4763
4783
|
}
|
|
4764
4784
|
|
|
4765
|
-
// src/
|
|
4766
|
-
var
|
|
4785
|
+
// src/document/keys.ts
|
|
4786
|
+
var DOCUMENT_KEY_PATTERN = /^[\p{L}\p{M}\p{Nd}:._-]+$/u;
|
|
4787
|
+
function normalizeDocumentKey(key) {
|
|
4788
|
+
return key.normalize("NFC").trim();
|
|
4789
|
+
}
|
|
4790
|
+
function isValidDocumentKey(key) {
|
|
4791
|
+
const normalized = normalizeDocumentKey(key);
|
|
4792
|
+
return normalized.length > 0 && DOCUMENT_KEY_PATTERN.test(normalized);
|
|
4793
|
+
}
|
|
4794
|
+
function isDuplicateDocumentKey(key, namespace) {
|
|
4795
|
+
const normalized = normalizeDocumentKey(key);
|
|
4796
|
+
if (normalized.length === 0) {
|
|
4797
|
+
return false;
|
|
4798
|
+
}
|
|
4799
|
+
for (const reference of namespace.references ?? []) {
|
|
4800
|
+
if (namespace.excludeReferenceId && reference.id === namespace.excludeReferenceId) {
|
|
4801
|
+
continue;
|
|
4802
|
+
}
|
|
4803
|
+
if (normalizeDocumentKey(reference.key) === normalized) {
|
|
4804
|
+
return true;
|
|
4805
|
+
}
|
|
4806
|
+
}
|
|
4807
|
+
for (const label of namespace.labels ?? []) {
|
|
4808
|
+
if (namespace.excludeOwnerId && label.ownerId === namespace.excludeOwnerId) {
|
|
4809
|
+
continue;
|
|
4810
|
+
}
|
|
4811
|
+
if (normalizeDocumentKey(label.key) === normalized) {
|
|
4812
|
+
return true;
|
|
4813
|
+
}
|
|
4814
|
+
}
|
|
4815
|
+
return false;
|
|
4816
|
+
}
|
|
4817
|
+
function documentKeyError(key, namespace = {}) {
|
|
4818
|
+
const normalized = normalizeDocumentKey(key);
|
|
4819
|
+
if (normalized.length === 0) {
|
|
4820
|
+
return "empty";
|
|
4821
|
+
}
|
|
4822
|
+
if (!DOCUMENT_KEY_PATTERN.test(normalized)) {
|
|
4823
|
+
return "invalid";
|
|
4824
|
+
}
|
|
4825
|
+
if (isDuplicateDocumentKey(normalized, namespace)) {
|
|
4826
|
+
return "duplicate";
|
|
4827
|
+
}
|
|
4828
|
+
return null;
|
|
4829
|
+
}
|
|
4830
|
+
|
|
4831
|
+
// src/document/jsonCommandTypes.ts
|
|
4832
|
+
var DocumentCommandError = class extends Error {
|
|
4767
4833
|
code;
|
|
4768
4834
|
constructor(code, message) {
|
|
4769
4835
|
super(message);
|
|
4770
|
-
this.name = "
|
|
4836
|
+
this.name = "DocumentCommandError";
|
|
4771
4837
|
this.code = code;
|
|
4772
4838
|
}
|
|
4773
4839
|
};
|
|
4774
4840
|
|
|
4775
|
-
// src/
|
|
4841
|
+
// src/document/jsonCommandHelpers.ts
|
|
4842
|
+
function normalizedNewFloatLabel(document2, label) {
|
|
4843
|
+
if (label === void 0) return void 0;
|
|
4844
|
+
const normalized = label.trim().length > 0 ? normalizeDocumentKey(label) : "";
|
|
4845
|
+
if (!normalized) return "";
|
|
4846
|
+
const error = documentKeyError(normalized, {
|
|
4847
|
+
references: document2.references,
|
|
4848
|
+
labels: collectDocumentLabels(document2)
|
|
4849
|
+
});
|
|
4850
|
+
if (error === "duplicate") {
|
|
4851
|
+
throw new DocumentCommandError("duplicate_key", "Float label is already in use");
|
|
4852
|
+
}
|
|
4853
|
+
if (error !== null) {
|
|
4854
|
+
throw new DocumentCommandError("invalid_key", "Float label is invalid");
|
|
4855
|
+
}
|
|
4856
|
+
return normalized;
|
|
4857
|
+
}
|
|
4776
4858
|
function isObject3(value) {
|
|
4777
4859
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4778
4860
|
}
|
|
4779
4861
|
function requireString2(value, field, allowEmpty = true) {
|
|
4780
4862
|
if (typeof value !== "string" || !allowEmpty && value.trim().length === 0) {
|
|
4781
|
-
throw new
|
|
4863
|
+
throw new DocumentCommandError("invalid_command", `${field} must be a${allowEmpty ? "" : " non-empty"} string`);
|
|
4782
4864
|
}
|
|
4783
4865
|
return value;
|
|
4784
4866
|
}
|
|
@@ -4787,13 +4869,13 @@ function optionalString(value, field) {
|
|
|
4787
4869
|
return void 0;
|
|
4788
4870
|
}
|
|
4789
4871
|
if (typeof value[field] !== "string") {
|
|
4790
|
-
throw new
|
|
4872
|
+
throw new DocumentCommandError("invalid_command", `${field} must be a string when provided`);
|
|
4791
4873
|
}
|
|
4792
4874
|
return value[field];
|
|
4793
4875
|
}
|
|
4794
4876
|
function requireBoolean(value, field) {
|
|
4795
4877
|
if (typeof value !== "boolean") {
|
|
4796
|
-
throw new
|
|
4878
|
+
throw new DocumentCommandError("invalid_command", `${field} must be a boolean`);
|
|
4797
4879
|
}
|
|
4798
4880
|
return value;
|
|
4799
4881
|
}
|
|
@@ -4805,7 +4887,7 @@ function optionalBoolean(value, field) {
|
|
|
4805
4887
|
}
|
|
4806
4888
|
function requireIndex(value, field) {
|
|
4807
4889
|
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
|
|
4808
|
-
throw new
|
|
4890
|
+
throw new DocumentCommandError("invalid_command", `${field} must be a non-negative integer`);
|
|
4809
4891
|
}
|
|
4810
4892
|
return value;
|
|
4811
4893
|
}
|
|
@@ -4814,25 +4896,25 @@ function parseMetadata(value) {
|
|
|
4814
4896
|
return void 0;
|
|
4815
4897
|
}
|
|
4816
4898
|
if (!isObject3(value)) {
|
|
4817
|
-
throw new
|
|
4899
|
+
throw new DocumentCommandError("invalid_command", "metadata must be an object");
|
|
4818
4900
|
}
|
|
4819
4901
|
if (value.source === void 0) {
|
|
4820
4902
|
return void 0;
|
|
4821
4903
|
}
|
|
4822
4904
|
if (value.source !== "agent" && value.source !== "user") {
|
|
4823
|
-
throw new
|
|
4905
|
+
throw new DocumentCommandError("invalid_command", "metadata.source must be agent or user");
|
|
4824
4906
|
}
|
|
4825
4907
|
return { source: value.source };
|
|
4826
4908
|
}
|
|
4827
4909
|
function parseBlockAnchor(value) {
|
|
4828
4910
|
if (!isObject3(value)) {
|
|
4829
|
-
throw new
|
|
4911
|
+
throw new DocumentCommandError("invalid_anchor", "anchor must be an object");
|
|
4830
4912
|
}
|
|
4831
4913
|
const before = typeof value.before_block_id === "string" ? value.before_block_id.trim() : "";
|
|
4832
4914
|
const after = typeof value.after_block_id === "string" ? value.after_block_id.trim() : "";
|
|
4833
4915
|
const choices = Number(before.length > 0) + Number(after.length > 0) + Number(value.end === true);
|
|
4834
4916
|
if (choices !== 1) {
|
|
4835
|
-
throw new
|
|
4917
|
+
throw new DocumentCommandError("invalid_anchor", "anchor requires exactly one block position");
|
|
4836
4918
|
}
|
|
4837
4919
|
if (before) {
|
|
4838
4920
|
return { before_block_id: before };
|
|
@@ -4844,13 +4926,13 @@ function parseBlockAnchor(value) {
|
|
|
4844
4926
|
}
|
|
4845
4927
|
function parseListItemAnchor(value) {
|
|
4846
4928
|
if (!isObject3(value)) {
|
|
4847
|
-
throw new
|
|
4929
|
+
throw new DocumentCommandError("invalid_anchor", "anchor must be an object");
|
|
4848
4930
|
}
|
|
4849
4931
|
const before = typeof value.before_item_id === "string" ? value.before_item_id.trim() : "";
|
|
4850
4932
|
const after = typeof value.after_item_id === "string" ? value.after_item_id.trim() : "";
|
|
4851
4933
|
const choices = Number(before.length > 0) + Number(after.length > 0) + Number(value.end === true);
|
|
4852
4934
|
if (choices !== 1) {
|
|
4853
|
-
throw new
|
|
4935
|
+
throw new DocumentCommandError("invalid_anchor", "anchor requires exactly one list-item position");
|
|
4854
4936
|
}
|
|
4855
4937
|
if (before) {
|
|
4856
4938
|
return { before_item_id: before };
|
|
@@ -4867,7 +4949,7 @@ function blockInsertionIndex(document2, anchor) {
|
|
|
4867
4949
|
const id = "before_block_id" in anchor ? anchor.before_block_id : anchor.after_block_id;
|
|
4868
4950
|
const index = document2.blocks.findIndex((block) => block.id === id);
|
|
4869
4951
|
if (index < 0) {
|
|
4870
|
-
throw new
|
|
4952
|
+
throw new DocumentCommandError("anchor_not_found", "Anchor block was not found");
|
|
4871
4953
|
}
|
|
4872
4954
|
return "before_block_id" in anchor ? index : index + 1;
|
|
4873
4955
|
}
|
|
@@ -4878,7 +4960,7 @@ function itemInsertionIndex(list, anchor) {
|
|
|
4878
4960
|
const id = "before_item_id" in anchor ? anchor.before_item_id : anchor.after_item_id;
|
|
4879
4961
|
const index = list.items.findIndex((item) => item.id === id);
|
|
4880
4962
|
if (index < 0) {
|
|
4881
|
-
throw new
|
|
4963
|
+
throw new DocumentCommandError("anchor_not_found", "Anchor list item was not found");
|
|
4882
4964
|
}
|
|
4883
4965
|
return "before_item_id" in anchor ? index : index + 1;
|
|
4884
4966
|
}
|
|
@@ -4966,9 +5048,9 @@ function documentIdentitySets(document2) {
|
|
|
4966
5048
|
return sets;
|
|
4967
5049
|
}
|
|
4968
5050
|
function unusedGeneratedId(prefix, used) {
|
|
4969
|
-
let id =
|
|
5051
|
+
let id = documentId(prefix);
|
|
4970
5052
|
while (used.has(id)) {
|
|
4971
|
-
id =
|
|
5053
|
+
id = documentId(prefix);
|
|
4972
5054
|
}
|
|
4973
5055
|
return id;
|
|
4974
5056
|
}
|
|
@@ -4982,7 +5064,7 @@ function newTokenId(document2, prefix) {
|
|
|
4982
5064
|
return unusedGeneratedId(prefix, documentIdentitySets(document2).tokens);
|
|
4983
5065
|
}
|
|
4984
5066
|
function createCommandInlineField(document2, text) {
|
|
4985
|
-
const field =
|
|
5067
|
+
const field = createInlineField(text);
|
|
4986
5068
|
const used = documentIdentitySets(document2);
|
|
4987
5069
|
field.id = unusedGeneratedId("field", used.fields);
|
|
4988
5070
|
field.tokens.forEach((token) => {
|
|
@@ -4994,20 +5076,20 @@ function createCommandInlineField(document2, text) {
|
|
|
4994
5076
|
function requireList(document2, listId) {
|
|
4995
5077
|
const block = findBlock(document2.blocks, listId);
|
|
4996
5078
|
if (!block) {
|
|
4997
|
-
throw new
|
|
5079
|
+
throw new DocumentCommandError("list_not_found", "Document list was not found");
|
|
4998
5080
|
}
|
|
4999
5081
|
if (block.kind !== "list") {
|
|
5000
|
-
throw new
|
|
5082
|
+
throw new DocumentCommandError("block_kind_mismatch", "Target block is not a list");
|
|
5001
5083
|
}
|
|
5002
5084
|
return block;
|
|
5003
5085
|
}
|
|
5004
5086
|
function requireTable(document2, tableId) {
|
|
5005
5087
|
const block = findBlock(document2.blocks, tableId);
|
|
5006
5088
|
if (!block) {
|
|
5007
|
-
throw new
|
|
5089
|
+
throw new DocumentCommandError("table_not_found", "Document table was not found");
|
|
5008
5090
|
}
|
|
5009
5091
|
if (block.kind !== "table") {
|
|
5010
|
-
throw new
|
|
5092
|
+
throw new DocumentCommandError("block_kind_mismatch", "Target block is not a table");
|
|
5011
5093
|
}
|
|
5012
5094
|
return block;
|
|
5013
5095
|
}
|
|
@@ -5016,18 +5098,21 @@ function fieldHasStructuredContent(field) {
|
|
|
5016
5098
|
}
|
|
5017
5099
|
function replacePlainField(document2, field, text) {
|
|
5018
5100
|
if (fieldHasStructuredContent(field)) {
|
|
5019
|
-
throw new
|
|
5101
|
+
throw new DocumentCommandError(
|
|
5020
5102
|
"unsupported_inline_content",
|
|
5021
5103
|
"Whole-field replacement is not supported for formatted or structured inline content"
|
|
5022
5104
|
);
|
|
5023
5105
|
}
|
|
5106
|
+
if (field.tokens.map((token) => token.kind === "text" ? token.text : "").join("") === text) {
|
|
5107
|
+
return;
|
|
5108
|
+
}
|
|
5024
5109
|
const next = createCommandInlineField(document2, text);
|
|
5025
5110
|
field.tokens = next.tokens;
|
|
5026
5111
|
}
|
|
5027
5112
|
function textStylesEqual2(a, b) {
|
|
5028
5113
|
return Boolean(a?.bold) === Boolean(b?.bold) && Boolean(a?.italic) === Boolean(b?.italic) && Boolean(a?.underline) === Boolean(b?.underline);
|
|
5029
5114
|
}
|
|
5030
|
-
function compactTextTokens2(tokens, emptyTokenId =
|
|
5115
|
+
function compactTextTokens2(tokens, emptyTokenId = documentId("text")) {
|
|
5031
5116
|
const compacted = [];
|
|
5032
5117
|
for (const token of tokens) {
|
|
5033
5118
|
const previous = compacted[compacted.length - 1];
|
|
@@ -5040,77 +5125,31 @@ function compactTextTokens2(tokens, emptyTokenId = document2Id("text")) {
|
|
|
5040
5125
|
return compacted.length > 0 ? compacted : [{ id: emptyTokenId, kind: "text", text: "" }];
|
|
5041
5126
|
}
|
|
5042
5127
|
|
|
5043
|
-
// src/
|
|
5044
|
-
var DOCUMENT2_KEY_PATTERN = /^[\p{L}\p{M}\p{Nd}:._-]+$/u;
|
|
5045
|
-
function normalizeDocument2Key(key) {
|
|
5046
|
-
return key.normalize("NFC").trim();
|
|
5047
|
-
}
|
|
5048
|
-
function isValidDocument2Key(key) {
|
|
5049
|
-
const normalized = normalizeDocument2Key(key);
|
|
5050
|
-
return normalized.length > 0 && DOCUMENT2_KEY_PATTERN.test(normalized);
|
|
5051
|
-
}
|
|
5052
|
-
function isDuplicateDocument2Key(key, namespace) {
|
|
5053
|
-
const normalized = normalizeDocument2Key(key);
|
|
5054
|
-
if (normalized.length === 0) {
|
|
5055
|
-
return false;
|
|
5056
|
-
}
|
|
5057
|
-
for (const reference of namespace.references ?? []) {
|
|
5058
|
-
if (namespace.excludeReferenceId && reference.id === namespace.excludeReferenceId) {
|
|
5059
|
-
continue;
|
|
5060
|
-
}
|
|
5061
|
-
if (normalizeDocument2Key(reference.key) === normalized) {
|
|
5062
|
-
return true;
|
|
5063
|
-
}
|
|
5064
|
-
}
|
|
5065
|
-
for (const label of namespace.labels ?? []) {
|
|
5066
|
-
if (namespace.excludeOwnerId && label.ownerId === namespace.excludeOwnerId) {
|
|
5067
|
-
continue;
|
|
5068
|
-
}
|
|
5069
|
-
if (normalizeDocument2Key(label.key) === normalized) {
|
|
5070
|
-
return true;
|
|
5071
|
-
}
|
|
5072
|
-
}
|
|
5073
|
-
return false;
|
|
5074
|
-
}
|
|
5075
|
-
function document2KeyError(key, namespace = {}) {
|
|
5076
|
-
const normalized = normalizeDocument2Key(key);
|
|
5077
|
-
if (normalized.length === 0) {
|
|
5078
|
-
return "empty";
|
|
5079
|
-
}
|
|
5080
|
-
if (!DOCUMENT2_KEY_PATTERN.test(normalized)) {
|
|
5081
|
-
return "invalid";
|
|
5082
|
-
}
|
|
5083
|
-
if (isDuplicateDocument2Key(normalized, namespace)) {
|
|
5084
|
-
return "duplicate";
|
|
5085
|
-
}
|
|
5086
|
-
return null;
|
|
5087
|
-
}
|
|
5088
|
-
|
|
5089
|
-
// src/document2/articleJsonCommands.ts
|
|
5128
|
+
// src/document/articleJsonCommands.ts
|
|
5090
5129
|
var REFERENCE_TEXT_FIELDS = ["authors", "title", "year", "venue", "url"];
|
|
5091
5130
|
function hasOwn(value, field) {
|
|
5092
5131
|
return Object.prototype.hasOwnProperty.call(value, field);
|
|
5093
5132
|
}
|
|
5094
5133
|
function parseDatePatch(value) {
|
|
5095
5134
|
if (!isObject3(value)) {
|
|
5096
|
-
throw new
|
|
5135
|
+
throw new DocumentCommandError("invalid_command", "date must be an object");
|
|
5097
5136
|
}
|
|
5098
5137
|
const patch = {};
|
|
5099
5138
|
if (hasOwn(value, "day")) {
|
|
5100
5139
|
if (typeof value.day !== "number" || !Number.isInteger(value.day) || value.day < 1 || value.day > 30) {
|
|
5101
|
-
throw new
|
|
5140
|
+
throw new DocumentCommandError("invalid_command", "date.day must be an integer from 1 to 30");
|
|
5102
5141
|
}
|
|
5103
5142
|
patch.day = value.day;
|
|
5104
5143
|
}
|
|
5105
5144
|
if (hasOwn(value, "month")) {
|
|
5106
5145
|
if (typeof value.month !== "string" || !HIJRI_MONTH_IDS.includes(value.month)) {
|
|
5107
|
-
throw new
|
|
5146
|
+
throw new DocumentCommandError("invalid_command", "date.month must be a supported Hijri month");
|
|
5108
5147
|
}
|
|
5109
5148
|
patch.month = value.month;
|
|
5110
5149
|
}
|
|
5111
5150
|
if (hasOwn(value, "year")) {
|
|
5112
5151
|
if (typeof value.year !== "number" || !Number.isInteger(value.year) || value.year < HIJRI_YEAR_MIN || value.year > HIJRI_YEAR_MAX) {
|
|
5113
|
-
throw new
|
|
5152
|
+
throw new DocumentCommandError(
|
|
5114
5153
|
"invalid_command",
|
|
5115
5154
|
`date.year must be an integer from ${String(HIJRI_YEAR_MIN)} to ${String(HIJRI_YEAR_MAX)}`
|
|
5116
5155
|
);
|
|
@@ -5118,7 +5157,7 @@ function parseDatePatch(value) {
|
|
|
5118
5157
|
patch.year = value.year;
|
|
5119
5158
|
}
|
|
5120
5159
|
if (Object.keys(patch).length === 0) {
|
|
5121
|
-
throw new
|
|
5160
|
+
throw new DocumentCommandError("invalid_command", "date requires at least one field");
|
|
5122
5161
|
}
|
|
5123
5162
|
return patch;
|
|
5124
5163
|
}
|
|
@@ -5127,19 +5166,19 @@ function parseReferenceSeparator(value) {
|
|
|
5127
5166
|
return void 0;
|
|
5128
5167
|
}
|
|
5129
5168
|
if (value.field_separator !== "," && value.field_separator !== "\u060C") {
|
|
5130
|
-
throw new
|
|
5169
|
+
throw new DocumentCommandError("invalid_command", "field_separator must be , or \u060C");
|
|
5131
5170
|
}
|
|
5132
5171
|
return value.field_separator;
|
|
5133
5172
|
}
|
|
5134
5173
|
function parseReferenceAnchor(value) {
|
|
5135
5174
|
if (!isObject3(value)) {
|
|
5136
|
-
throw new
|
|
5175
|
+
throw new DocumentCommandError("invalid_anchor", "anchor must be an object");
|
|
5137
5176
|
}
|
|
5138
|
-
const before = typeof value.before_reference_key === "string" ?
|
|
5139
|
-
const after = typeof value.after_reference_key === "string" ?
|
|
5177
|
+
const before = typeof value.before_reference_key === "string" ? normalizeDocumentKey(value.before_reference_key) : "";
|
|
5178
|
+
const after = typeof value.after_reference_key === "string" ? normalizeDocumentKey(value.after_reference_key) : "";
|
|
5140
5179
|
const choices = Number(before.length > 0) + Number(after.length > 0) + Number(value.end === true);
|
|
5141
5180
|
if (choices !== 1) {
|
|
5142
|
-
throw new
|
|
5181
|
+
throw new DocumentCommandError("invalid_anchor", "anchor requires exactly one reference position");
|
|
5143
5182
|
}
|
|
5144
5183
|
if (before) {
|
|
5145
5184
|
return { before_reference_key: before };
|
|
@@ -5155,7 +5194,7 @@ function parseUpdateDocumentMeta(value) {
|
|
|
5155
5194
|
const abstract = optionalString(value, "abstract");
|
|
5156
5195
|
const date = hasOwn(value, "date") ? parseDatePatch(value.date) : void 0;
|
|
5157
5196
|
if (title === void 0 && authors === void 0 && abstract === void 0 && date === void 0) {
|
|
5158
|
-
throw new
|
|
5197
|
+
throw new DocumentCommandError("invalid_command", "update_document_meta requires at least one field");
|
|
5159
5198
|
}
|
|
5160
5199
|
return {
|
|
5161
5200
|
op: "update_document_meta",
|
|
@@ -5192,7 +5231,7 @@ function parseUpdateReference(value) {
|
|
|
5192
5231
|
const nextKey = optionalString(value, "key");
|
|
5193
5232
|
const fields = referenceFields(value);
|
|
5194
5233
|
if (nextKey === void 0 && Object.keys(fields).length === 0) {
|
|
5195
|
-
throw new
|
|
5234
|
+
throw new DocumentCommandError("invalid_command", "update_reference requires at least one field");
|
|
5196
5235
|
}
|
|
5197
5236
|
return {
|
|
5198
5237
|
op: "update_reference",
|
|
@@ -5224,13 +5263,13 @@ function parseArticleCommand(value) {
|
|
|
5224
5263
|
anchor: parseReferenceAnchor(value.anchor)
|
|
5225
5264
|
};
|
|
5226
5265
|
}
|
|
5227
|
-
throw new
|
|
5266
|
+
throw new DocumentCommandError("invalid_command", "Unsupported article command");
|
|
5228
5267
|
}
|
|
5229
5268
|
function referenceIndexes(document2, key) {
|
|
5230
|
-
const normalized =
|
|
5269
|
+
const normalized = normalizeDocumentKey(key);
|
|
5231
5270
|
const indexes = [];
|
|
5232
5271
|
document2.references.forEach((reference, index) => {
|
|
5233
|
-
if (
|
|
5272
|
+
if (normalizeDocumentKey(reference.key) === normalized) {
|
|
5234
5273
|
indexes.push(index);
|
|
5235
5274
|
}
|
|
5236
5275
|
});
|
|
@@ -5239,25 +5278,25 @@ function referenceIndexes(document2, key) {
|
|
|
5239
5278
|
function referenceTargetIndex(document2, key) {
|
|
5240
5279
|
const indexes = referenceIndexes(document2, key);
|
|
5241
5280
|
if (indexes.length === 0) {
|
|
5242
|
-
throw new
|
|
5281
|
+
throw new DocumentCommandError("reference_not_found", "Document reference was not found");
|
|
5243
5282
|
}
|
|
5244
5283
|
if (indexes.length > 1) {
|
|
5245
|
-
throw new
|
|
5284
|
+
throw new DocumentCommandError("duplicate_key", "Reference key is ambiguous");
|
|
5246
5285
|
}
|
|
5247
5286
|
return indexes[0];
|
|
5248
5287
|
}
|
|
5249
5288
|
function validateReferenceKey(document2, key, excludeReferenceId) {
|
|
5250
|
-
const normalized =
|
|
5251
|
-
const error =
|
|
5289
|
+
const normalized = normalizeDocumentKey(key);
|
|
5290
|
+
const error = documentKeyError(normalized, {
|
|
5252
5291
|
references: document2.references,
|
|
5253
|
-
labels:
|
|
5292
|
+
labels: collectDocumentLabels(document2),
|
|
5254
5293
|
...excludeReferenceId ? { excludeReferenceId } : {}
|
|
5255
5294
|
});
|
|
5256
5295
|
if (error === "duplicate") {
|
|
5257
|
-
throw new
|
|
5296
|
+
throw new DocumentCommandError("duplicate_key", "Reference key is already in use");
|
|
5258
5297
|
}
|
|
5259
5298
|
if (error !== null) {
|
|
5260
|
-
throw new
|
|
5299
|
+
throw new DocumentCommandError("invalid_key", "Reference key is invalid");
|
|
5261
5300
|
}
|
|
5262
5301
|
return normalized;
|
|
5263
5302
|
}
|
|
@@ -5268,10 +5307,10 @@ function referenceInsertionIndex(document2, anchor) {
|
|
|
5268
5307
|
const key = "before_reference_key" in anchor ? anchor.before_reference_key : anchor.after_reference_key;
|
|
5269
5308
|
const indexes = referenceIndexes(document2, key);
|
|
5270
5309
|
if (indexes.length === 0) {
|
|
5271
|
-
throw new
|
|
5310
|
+
throw new DocumentCommandError("anchor_not_found", "Anchor reference was not found");
|
|
5272
5311
|
}
|
|
5273
5312
|
if (indexes.length > 1) {
|
|
5274
|
-
throw new
|
|
5313
|
+
throw new DocumentCommandError("duplicate_key", "Anchor reference key is ambiguous");
|
|
5275
5314
|
}
|
|
5276
5315
|
return "before_reference_key" in anchor ? indexes[0] : indexes[0] + 1;
|
|
5277
5316
|
}
|
|
@@ -5323,7 +5362,7 @@ function applyArticleCommand(document2, command) {
|
|
|
5323
5362
|
title: command.title ?? document2.meta.title,
|
|
5324
5363
|
authors: command.authors ?? document2.meta.authors,
|
|
5325
5364
|
abstract: command.abstract ?? document2.meta.abstract,
|
|
5326
|
-
date: command.date ?
|
|
5365
|
+
date: command.date ? normalizeDocumentHijriDate({ ...document2.meta.date, ...command.date }) : { ...document2.meta.date }
|
|
5327
5366
|
};
|
|
5328
5367
|
return;
|
|
5329
5368
|
}
|
|
@@ -5357,10 +5396,10 @@ function applyArticleCommand(document2, command) {
|
|
|
5357
5396
|
document2.references.splice(targetIndex, 1);
|
|
5358
5397
|
return;
|
|
5359
5398
|
}
|
|
5360
|
-
const targetKey =
|
|
5399
|
+
const targetKey = normalizeDocumentKey(target.key);
|
|
5361
5400
|
const anchorKey = "before_reference_key" in command.anchor ? command.anchor.before_reference_key : "after_reference_key" in command.anchor ? command.anchor.after_reference_key : null;
|
|
5362
|
-
if (anchorKey !== null &&
|
|
5363
|
-
throw new
|
|
5401
|
+
if (anchorKey !== null && normalizeDocumentKey(anchorKey) === targetKey) {
|
|
5402
|
+
throw new DocumentCommandError("invalid_anchor", "A reference cannot be moved relative to itself");
|
|
5364
5403
|
}
|
|
5365
5404
|
if (anchorKey !== null) {
|
|
5366
5405
|
referenceInsertionIndex(document2, command.anchor);
|
|
@@ -5369,16 +5408,16 @@ function applyArticleCommand(document2, command) {
|
|
|
5369
5408
|
document2.references.splice(referenceInsertionIndex(document2, command.anchor), 0, reference);
|
|
5370
5409
|
}
|
|
5371
5410
|
|
|
5372
|
-
// src/
|
|
5411
|
+
// src/document/inlineJsonCommands.ts
|
|
5373
5412
|
function parseInlineAnchor(value) {
|
|
5374
5413
|
if (!isObject3(value)) {
|
|
5375
|
-
throw new
|
|
5414
|
+
throw new DocumentCommandError("invalid_anchor", "anchor must be an object");
|
|
5376
5415
|
}
|
|
5377
5416
|
const before = typeof value.before_token_id === "string" ? value.before_token_id.trim() : "";
|
|
5378
5417
|
const after = typeof value.after_token_id === "string" ? value.after_token_id.trim() : "";
|
|
5379
5418
|
const choices = Number(before.length > 0) + Number(after.length > 0) + Number(value.start === true) + Number(value.end === true);
|
|
5380
5419
|
if (choices !== 1) {
|
|
5381
|
-
throw new
|
|
5420
|
+
throw new DocumentCommandError("invalid_anchor", "anchor requires exactly one inline-token position");
|
|
5382
5421
|
}
|
|
5383
5422
|
if (before) {
|
|
5384
5423
|
return { before_token_id: before };
|
|
@@ -5393,12 +5432,12 @@ function parseStyle(value) {
|
|
|
5393
5432
|
return void 0;
|
|
5394
5433
|
}
|
|
5395
5434
|
if (!isObject3(value)) {
|
|
5396
|
-
throw new
|
|
5435
|
+
throw new DocumentCommandError("invalid_inline_token", "text token style must be an object");
|
|
5397
5436
|
}
|
|
5398
5437
|
const style = {};
|
|
5399
5438
|
for (const key of ["bold", "italic", "underline"]) {
|
|
5400
5439
|
if (value[key] !== void 0 && value[key] !== true) {
|
|
5401
|
-
throw new
|
|
5440
|
+
throw new DocumentCommandError("invalid_inline_token", `text token style.${key} must be true when provided`);
|
|
5402
5441
|
}
|
|
5403
5442
|
if (value[key] === true) {
|
|
5404
5443
|
style[key] = true;
|
|
@@ -5408,12 +5447,12 @@ function parseStyle(value) {
|
|
|
5408
5447
|
}
|
|
5409
5448
|
function normalizedKeys(value) {
|
|
5410
5449
|
if (!Array.isArray(value)) {
|
|
5411
|
-
throw new
|
|
5450
|
+
throw new DocumentCommandError("invalid_inline_token", "citation/reference keys must be an array");
|
|
5412
5451
|
}
|
|
5413
5452
|
const keys = [];
|
|
5414
5453
|
for (const entry of value) {
|
|
5415
5454
|
if (typeof entry !== "string") {
|
|
5416
|
-
throw new
|
|
5455
|
+
throw new DocumentCommandError("invalid_inline_token", "citation/reference keys must be strings");
|
|
5417
5456
|
}
|
|
5418
5457
|
const key = entry.trim();
|
|
5419
5458
|
if (key.length > 0 && !keys.includes(key)) {
|
|
@@ -5421,37 +5460,37 @@ function normalizedKeys(value) {
|
|
|
5421
5460
|
}
|
|
5422
5461
|
}
|
|
5423
5462
|
if (keys.length === 0) {
|
|
5424
|
-
throw new
|
|
5463
|
+
throw new DocumentCommandError("invalid_inline_token", "citation/reference requires at least one non-empty key");
|
|
5425
5464
|
}
|
|
5426
5465
|
return keys;
|
|
5427
5466
|
}
|
|
5428
5467
|
function parseMathObject(value, source) {
|
|
5429
5468
|
if (!isObject3(value) || value.node_type !== "MathObject") {
|
|
5430
|
-
throw new
|
|
5469
|
+
throw new DocumentCommandError("math_object_mismatch", "math_object must be a structured MathObject");
|
|
5431
5470
|
}
|
|
5432
|
-
const spans =
|
|
5471
|
+
const spans = detectInlineSpans(source);
|
|
5433
5472
|
const span = spans[0];
|
|
5434
5473
|
if (spans.length !== 1 || span?.kind !== "math" || span.start !== 0 || span.end !== source.length) {
|
|
5435
|
-
throw new
|
|
5474
|
+
throw new DocumentCommandError("math_object_mismatch", "math source must contain exactly one complete delimited span");
|
|
5436
5475
|
}
|
|
5437
5476
|
if (value.math_mode !== span.opening || value.closing !== span.closing) {
|
|
5438
|
-
throw new
|
|
5477
|
+
throw new DocumentCommandError("math_object_mismatch", "math_object delimiters do not match math source");
|
|
5439
5478
|
}
|
|
5440
5479
|
try {
|
|
5441
5480
|
fromMathObjectJson(value, value.source_side === "arabic" ? "arabic" : "english");
|
|
5442
5481
|
} catch {
|
|
5443
|
-
throw new
|
|
5482
|
+
throw new DocumentCommandError("math_object_mismatch", "math_object must contain a valid structured equation");
|
|
5444
5483
|
}
|
|
5445
5484
|
return value;
|
|
5446
5485
|
}
|
|
5447
5486
|
function parseToken(value) {
|
|
5448
5487
|
if (!isObject3(value) || typeof value.kind !== "string") {
|
|
5449
|
-
throw new
|
|
5488
|
+
throw new DocumentCommandError("invalid_inline_token", "token requires a kind");
|
|
5450
5489
|
}
|
|
5451
5490
|
if (value.kind === "text") {
|
|
5452
5491
|
const text = requireString2(value.text, "token.text");
|
|
5453
|
-
if (
|
|
5454
|
-
throw new
|
|
5492
|
+
if (detectInlineSpans(text).length > 0) {
|
|
5493
|
+
throw new DocumentCommandError("invalid_inline_token", "text token contains structured inline syntax");
|
|
5455
5494
|
}
|
|
5456
5495
|
const style = parseStyle(value.style);
|
|
5457
5496
|
return { kind: "text", text, ...style ? { style } : {} };
|
|
@@ -5461,7 +5500,7 @@ function parseToken(value) {
|
|
|
5461
5500
|
}
|
|
5462
5501
|
if (value.kind === "ref") {
|
|
5463
5502
|
if (value.ref_command !== "ref" && value.ref_command !== "eqref") {
|
|
5464
|
-
throw new
|
|
5503
|
+
throw new DocumentCommandError("invalid_inline_token", "ref_command must be ref or eqref");
|
|
5465
5504
|
}
|
|
5466
5505
|
return { kind: "ref", keys: normalizedKeys(value.keys), ref_command: value.ref_command };
|
|
5467
5506
|
}
|
|
@@ -5469,7 +5508,7 @@ function parseToken(value) {
|
|
|
5469
5508
|
const source = requireString2(value.source, "token.source", false);
|
|
5470
5509
|
return { kind: "math", source, math_object: parseMathObject(value.math_object, source) };
|
|
5471
5510
|
}
|
|
5472
|
-
throw new
|
|
5511
|
+
throw new DocumentCommandError("invalid_inline_token", "Unsupported inline token kind");
|
|
5473
5512
|
}
|
|
5474
5513
|
function parseInlineCommand(value) {
|
|
5475
5514
|
if (value.op === "insert_inline_token") {
|
|
@@ -5495,7 +5534,7 @@ function parseInlineCommand(value) {
|
|
|
5495
5534
|
token_id: requireString2(value.token_id, "token_id", false).trim()
|
|
5496
5535
|
};
|
|
5497
5536
|
}
|
|
5498
|
-
throw new
|
|
5537
|
+
throw new DocumentCommandError("invalid_command", "Unsupported inline command");
|
|
5499
5538
|
}
|
|
5500
5539
|
function liveToken(input, id) {
|
|
5501
5540
|
if (input.kind === "text") {
|
|
@@ -5508,10 +5547,10 @@ function liveToken(input, id) {
|
|
|
5508
5547
|
return { id, kind: "ref", keys: [...input.keys], refCommand: input.ref_command };
|
|
5509
5548
|
}
|
|
5510
5549
|
const mathObject = input.math_object;
|
|
5511
|
-
const spans =
|
|
5550
|
+
const spans = detectInlineSpans(input.source);
|
|
5512
5551
|
const span = spans[0];
|
|
5513
5552
|
if (!span || span.kind !== "math") {
|
|
5514
|
-
throw new
|
|
5553
|
+
throw new DocumentCommandError("math_object_mismatch", "math source is not a complete delimited span");
|
|
5515
5554
|
}
|
|
5516
5555
|
return {
|
|
5517
5556
|
id,
|
|
@@ -5538,14 +5577,14 @@ function insertionIndex(tokens, anchor) {
|
|
|
5538
5577
|
const id = "before_token_id" in anchor ? anchor.before_token_id : anchor.after_token_id;
|
|
5539
5578
|
const index = tokens.findIndex((token) => token.id === id);
|
|
5540
5579
|
if (index < 0) {
|
|
5541
|
-
throw new
|
|
5580
|
+
throw new DocumentCommandError("token_not_found", "Anchor token was not found");
|
|
5542
5581
|
}
|
|
5543
5582
|
return "before_token_id" in anchor ? index : index + 1;
|
|
5544
5583
|
}
|
|
5545
5584
|
function applyInlineCommand(document2, command) {
|
|
5546
5585
|
const field = findField(document2.blocks, command.field_id);
|
|
5547
5586
|
if (!field) {
|
|
5548
|
-
throw new
|
|
5587
|
+
throw new DocumentCommandError("field_not_found", "Inline field was not found");
|
|
5549
5588
|
}
|
|
5550
5589
|
if (command.op === "insert_inline_token") {
|
|
5551
5590
|
const index2 = insertionIndex(field.tokens, command.anchor);
|
|
@@ -5554,7 +5593,7 @@ function applyInlineCommand(document2, command) {
|
|
|
5554
5593
|
}
|
|
5555
5594
|
const index = field.tokens.findIndex((token) => token.id === command.token_id);
|
|
5556
5595
|
if (index < 0) {
|
|
5557
|
-
throw new
|
|
5596
|
+
throw new DocumentCommandError("token_not_found", "Inline token was not found");
|
|
5558
5597
|
}
|
|
5559
5598
|
if (command.op === "replace_inline_token") {
|
|
5560
5599
|
field.tokens[index] = liveToken(command.token, command.token_id);
|
|
@@ -5566,10 +5605,10 @@ function applyInlineCommand(document2, command) {
|
|
|
5566
5605
|
);
|
|
5567
5606
|
}
|
|
5568
5607
|
|
|
5569
|
-
// src/
|
|
5608
|
+
// src/document/listJsonCommands.ts
|
|
5570
5609
|
function stringArray(value, field) {
|
|
5571
5610
|
if (!Array.isArray(value) || value.length === 0 || value.some((entry) => typeof entry !== "string")) {
|
|
5572
|
-
throw new
|
|
5611
|
+
throw new DocumentCommandError("invalid_command", `${field} must be a non-empty string array`);
|
|
5573
5612
|
}
|
|
5574
5613
|
return [...value];
|
|
5575
5614
|
}
|
|
@@ -5615,7 +5654,7 @@ function parseListCommand(value) {
|
|
|
5615
5654
|
anchor: parseListItemAnchor(value.anchor)
|
|
5616
5655
|
};
|
|
5617
5656
|
}
|
|
5618
|
-
throw new
|
|
5657
|
+
throw new DocumentCommandError("invalid_command", "Unsupported list command");
|
|
5619
5658
|
}
|
|
5620
5659
|
function newItem(document2, text) {
|
|
5621
5660
|
return { id: newItemId(document2), field: createCommandInlineField(document2, text), blocks: [] };
|
|
@@ -5644,7 +5683,7 @@ function applyListCommand(document2, command) {
|
|
|
5644
5683
|
}
|
|
5645
5684
|
const itemIndex = list.items.findIndex((item2) => item2.id === command.item_id);
|
|
5646
5685
|
if (itemIndex < 0) {
|
|
5647
|
-
throw new
|
|
5686
|
+
throw new DocumentCommandError("item_not_found", "Document list item was not found");
|
|
5648
5687
|
}
|
|
5649
5688
|
if (command.op === "replace_list_item") {
|
|
5650
5689
|
replacePlainField(document2, list.items[itemIndex].field, command.text);
|
|
@@ -5652,44 +5691,44 @@ function applyListCommand(document2, command) {
|
|
|
5652
5691
|
}
|
|
5653
5692
|
if (command.op === "remove_list_item") {
|
|
5654
5693
|
if (list.items.length === 1) {
|
|
5655
|
-
throw new
|
|
5694
|
+
throw new DocumentCommandError("minimum_structure", "A list must retain at least one item");
|
|
5656
5695
|
}
|
|
5657
5696
|
list.items.splice(itemIndex, 1);
|
|
5658
5697
|
return;
|
|
5659
5698
|
}
|
|
5660
5699
|
const anchorId = "before_item_id" in command.anchor ? command.anchor.before_item_id : "after_item_id" in command.anchor ? command.anchor.after_item_id : null;
|
|
5661
5700
|
if (anchorId === command.item_id) {
|
|
5662
|
-
throw new
|
|
5701
|
+
throw new DocumentCommandError("invalid_anchor", "A list item cannot be moved relative to itself");
|
|
5663
5702
|
}
|
|
5664
5703
|
const [item] = list.items.splice(itemIndex, 1);
|
|
5665
5704
|
if (!item) {
|
|
5666
|
-
throw new
|
|
5705
|
+
throw new DocumentCommandError("item_not_found", "Document list item was not found");
|
|
5667
5706
|
}
|
|
5668
5707
|
list.items.splice(itemInsertionIndex(list, command.anchor), 0, item);
|
|
5669
5708
|
}
|
|
5670
5709
|
|
|
5671
|
-
// src/
|
|
5710
|
+
// src/document/tableJsonCommands.ts
|
|
5672
5711
|
function stringArray2(value, field) {
|
|
5673
5712
|
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "string")) {
|
|
5674
|
-
throw new
|
|
5713
|
+
throw new DocumentCommandError("invalid_command", `${field} must be a string array`);
|
|
5675
5714
|
}
|
|
5676
5715
|
return [...value];
|
|
5677
5716
|
}
|
|
5678
5717
|
function rectangularRows(value) {
|
|
5679
5718
|
if (!Array.isArray(value) || value.length === 0) {
|
|
5680
|
-
throw new
|
|
5719
|
+
throw new DocumentCommandError("invalid_table_shape", "rows must be a non-empty array");
|
|
5681
5720
|
}
|
|
5682
5721
|
const rows = value.map((row, index) => stringArray2(row, `rows[${String(index)}]`));
|
|
5683
5722
|
const columnCount = rows[0]?.length ?? 0;
|
|
5684
5723
|
if (columnCount === 0 || rows.some((row) => row.length !== columnCount)) {
|
|
5685
|
-
throw new
|
|
5724
|
+
throw new DocumentCommandError("invalid_table_shape", "Table rows must form a non-empty rectangle");
|
|
5686
5725
|
}
|
|
5687
5726
|
return rows;
|
|
5688
5727
|
}
|
|
5689
5728
|
function columns(value) {
|
|
5690
5729
|
const result = requireString2(value, "columns", false);
|
|
5691
5730
|
if (result.trim().length === 0) {
|
|
5692
|
-
throw new
|
|
5731
|
+
throw new DocumentCommandError("invalid_table_shape", "columns must be a non-empty LaTeX specification");
|
|
5693
5732
|
}
|
|
5694
5733
|
return result;
|
|
5695
5734
|
}
|
|
@@ -5766,9 +5805,10 @@ function parseTableCommand(value) {
|
|
|
5766
5805
|
columns: columns(value.columns)
|
|
5767
5806
|
};
|
|
5768
5807
|
}
|
|
5769
|
-
throw new
|
|
5808
|
+
throw new DocumentCommandError("invalid_command", "Unsupported table command");
|
|
5770
5809
|
}
|
|
5771
5810
|
function insertTable(document2, command) {
|
|
5811
|
+
const label = normalizedNewFloatLabel(document2, command.label);
|
|
5772
5812
|
const block = {
|
|
5773
5813
|
id: newBlockId(document2),
|
|
5774
5814
|
kind: "table",
|
|
@@ -5777,7 +5817,7 @@ function insertTable(document2, command) {
|
|
|
5777
5817
|
columns: command.columns,
|
|
5778
5818
|
rows: command.rows.map((row) => row.map((cell) => createCommandInlineField(document2, cell))),
|
|
5779
5819
|
...defaultFloatMeta(),
|
|
5780
|
-
...
|
|
5820
|
+
...label !== void 0 ? { label, labelEnabled: label.length > 0 } : {},
|
|
5781
5821
|
...command.metadata ? { metadata: { ...command.metadata } } : {}
|
|
5782
5822
|
};
|
|
5783
5823
|
if (command.caption !== void 0) {
|
|
@@ -5790,12 +5830,12 @@ function insertTable(document2, command) {
|
|
|
5790
5830
|
}
|
|
5791
5831
|
function checkCell(table, rowIndex, columnIndex) {
|
|
5792
5832
|
if (rowIndex >= table.rows.length || columnIndex >= (table.rows[rowIndex]?.length ?? 0)) {
|
|
5793
|
-
throw new
|
|
5833
|
+
throw new DocumentCommandError("index_out_of_range", "Table cell index is out of range");
|
|
5794
5834
|
}
|
|
5795
5835
|
}
|
|
5796
5836
|
function checkExistingIndex(index, length, field) {
|
|
5797
5837
|
if (index >= length) {
|
|
5798
|
-
throw new
|
|
5838
|
+
throw new DocumentCommandError("index_out_of_range", `${field} is out of range`);
|
|
5799
5839
|
}
|
|
5800
5840
|
}
|
|
5801
5841
|
function applyTableCommand(document2, command) {
|
|
@@ -5806,7 +5846,7 @@ function applyTableCommand(document2, command) {
|
|
|
5806
5846
|
const table = requireTable(document2, command.table_id);
|
|
5807
5847
|
const columnCount = table.rows[0]?.length ?? 0;
|
|
5808
5848
|
if (table.rows.length === 0 || columnCount === 0 || table.rows.some((row) => row.length !== columnCount)) {
|
|
5809
|
-
throw new
|
|
5849
|
+
throw new DocumentCommandError("invalid_table_shape", "Target table must be a non-empty rectangle");
|
|
5810
5850
|
}
|
|
5811
5851
|
if (command.op === "replace_table_cell") {
|
|
5812
5852
|
checkCell(table, command.row_index, command.column_index);
|
|
@@ -5815,10 +5855,10 @@ function applyTableCommand(document2, command) {
|
|
|
5815
5855
|
}
|
|
5816
5856
|
if (command.op === "insert_table_row") {
|
|
5817
5857
|
if (command.index > table.rows.length) {
|
|
5818
|
-
throw new
|
|
5858
|
+
throw new DocumentCommandError("index_out_of_range", "Table row insertion index is out of range");
|
|
5819
5859
|
}
|
|
5820
5860
|
if (command.values.length !== columnCount) {
|
|
5821
|
-
throw new
|
|
5861
|
+
throw new DocumentCommandError("invalid_table_shape", "Inserted row must match the current column count");
|
|
5822
5862
|
}
|
|
5823
5863
|
table.rows.splice(command.index, 0, command.values.map((value) => createCommandInlineField(document2, value)));
|
|
5824
5864
|
return;
|
|
@@ -5826,7 +5866,7 @@ function applyTableCommand(document2, command) {
|
|
|
5826
5866
|
if (command.op === "remove_table_row") {
|
|
5827
5867
|
checkExistingIndex(command.index, table.rows.length, "Table row index");
|
|
5828
5868
|
if (table.rows.length === 1) {
|
|
5829
|
-
throw new
|
|
5869
|
+
throw new DocumentCommandError("minimum_structure", "A table must retain at least one row");
|
|
5830
5870
|
}
|
|
5831
5871
|
table.rows.splice(command.index, 1);
|
|
5832
5872
|
return;
|
|
@@ -5840,10 +5880,10 @@ function applyTableCommand(document2, command) {
|
|
|
5840
5880
|
}
|
|
5841
5881
|
if (command.op === "insert_table_column") {
|
|
5842
5882
|
if (command.index > columnCount) {
|
|
5843
|
-
throw new
|
|
5883
|
+
throw new DocumentCommandError("index_out_of_range", "Table column insertion index is out of range");
|
|
5844
5884
|
}
|
|
5845
5885
|
if (command.values.length !== table.rows.length) {
|
|
5846
|
-
throw new
|
|
5886
|
+
throw new DocumentCommandError("invalid_table_shape", "Inserted column requires one value per row");
|
|
5847
5887
|
}
|
|
5848
5888
|
table.rows.forEach((row, index) => row.splice(command.index, 0, createCommandInlineField(document2, command.values[index])));
|
|
5849
5889
|
table.columns = command.columns;
|
|
@@ -5852,7 +5892,7 @@ function applyTableCommand(document2, command) {
|
|
|
5852
5892
|
if (command.op === "remove_table_column") {
|
|
5853
5893
|
checkExistingIndex(command.index, columnCount, "Table column index");
|
|
5854
5894
|
if (columnCount === 1) {
|
|
5855
|
-
throw new
|
|
5895
|
+
throw new DocumentCommandError("minimum_structure", "A table must retain at least one column");
|
|
5856
5896
|
}
|
|
5857
5897
|
table.rows.forEach((row) => row.splice(command.index, 1));
|
|
5858
5898
|
table.columns = command.columns;
|
|
@@ -5867,7 +5907,7 @@ function applyTableCommand(document2, command) {
|
|
|
5867
5907
|
table.columns = command.columns;
|
|
5868
5908
|
}
|
|
5869
5909
|
|
|
5870
|
-
// src/
|
|
5910
|
+
// src/document/exportJson.ts
|
|
5871
5911
|
function serializeField(field) {
|
|
5872
5912
|
let value = "";
|
|
5873
5913
|
let hasPersistedMath = false;
|
|
@@ -6030,9 +6070,9 @@ function referenceJson(reference) {
|
|
|
6030
6070
|
field_separator: reference.fieldSeparator
|
|
6031
6071
|
};
|
|
6032
6072
|
}
|
|
6033
|
-
function
|
|
6073
|
+
function toDocumentJson(document2) {
|
|
6034
6074
|
if (document2.nodeType !== "DocumentObject" || !Array.isArray(document2.blocks)) {
|
|
6035
|
-
throw new Error("
|
|
6075
|
+
throw new Error("toDocumentJson requires a live DocumentNode");
|
|
6036
6076
|
}
|
|
6037
6077
|
return {
|
|
6038
6078
|
node_type: "DocumentObject",
|
|
@@ -6047,7 +6087,7 @@ function toDocumentJson2(document2) {
|
|
|
6047
6087
|
};
|
|
6048
6088
|
}
|
|
6049
6089
|
|
|
6050
|
-
// src/
|
|
6090
|
+
// src/document/jsonCommands.ts
|
|
6051
6091
|
function isInlineCommand(command) {
|
|
6052
6092
|
return command.op === "insert_inline_token" || command.op === "replace_inline_token" || command.op === "remove_inline_token";
|
|
6053
6093
|
}
|
|
@@ -6064,7 +6104,7 @@ function parseTextBlockKind(value) {
|
|
|
6064
6104
|
if (value === "section" || value === "subsection" || value === "subsubsection" || value === "paragraph") {
|
|
6065
6105
|
return value;
|
|
6066
6106
|
}
|
|
6067
|
-
throw new
|
|
6107
|
+
throw new DocumentCommandError("invalid_command", "kind must be section, subsection, subsubsection, or paragraph");
|
|
6068
6108
|
}
|
|
6069
6109
|
function parseBasicCommand(value) {
|
|
6070
6110
|
if (value.op === "insert_text_block") {
|
|
@@ -6106,7 +6146,7 @@ function parseBasicCommand(value) {
|
|
|
6106
6146
|
const hasAssetId = Object.prototype.hasOwnProperty.call(value, "asset_id");
|
|
6107
6147
|
const hasValue = Object.prototype.hasOwnProperty.call(value, "value");
|
|
6108
6148
|
if (!hasAssetId && !hasValue) {
|
|
6109
|
-
throw new
|
|
6149
|
+
throw new DocumentCommandError("invalid_command", "update_figure requires asset_id or value");
|
|
6110
6150
|
}
|
|
6111
6151
|
let assetId;
|
|
6112
6152
|
if (hasAssetId) {
|
|
@@ -6131,7 +6171,7 @@ function parseBasicCommand(value) {
|
|
|
6131
6171
|
const labelEnabled = optionalBoolean(value, "label_enabled");
|
|
6132
6172
|
const label = optionalString(value, "label");
|
|
6133
6173
|
if (centered === void 0 && captionEnabled === void 0 && caption === void 0 && labelEnabled === void 0 && label === void 0) {
|
|
6134
|
-
throw new
|
|
6174
|
+
throw new DocumentCommandError("invalid_command", "update_float_meta requires at least one field");
|
|
6135
6175
|
}
|
|
6136
6176
|
return {
|
|
6137
6177
|
op: value.op,
|
|
@@ -6158,11 +6198,11 @@ function parseBasicCommand(value) {
|
|
|
6158
6198
|
...metadata ? { metadata } : {}
|
|
6159
6199
|
};
|
|
6160
6200
|
}
|
|
6161
|
-
throw new
|
|
6201
|
+
throw new DocumentCommandError("invalid_command", "Unsupported block command");
|
|
6162
6202
|
}
|
|
6163
6203
|
function parseCommand2(value) {
|
|
6164
6204
|
if (!isObject3(value) || typeof value.op !== "string") {
|
|
6165
|
-
throw new
|
|
6205
|
+
throw new DocumentCommandError("invalid_command", "command requires an op");
|
|
6166
6206
|
}
|
|
6167
6207
|
if (value.op === "insert_inline_token" || value.op === "replace_inline_token" || value.op === "remove_inline_token") {
|
|
6168
6208
|
return parseInlineCommand(value);
|
|
@@ -6179,13 +6219,13 @@ function parseCommand2(value) {
|
|
|
6179
6219
|
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") {
|
|
6180
6220
|
return parseBasicCommand(value);
|
|
6181
6221
|
}
|
|
6182
|
-
throw new
|
|
6222
|
+
throw new DocumentCommandError("invalid_command", "Unsupported document command");
|
|
6183
6223
|
}
|
|
6184
6224
|
function parseDocument(json) {
|
|
6185
6225
|
try {
|
|
6186
|
-
return
|
|
6226
|
+
return fromDocumentJson(json);
|
|
6187
6227
|
} catch {
|
|
6188
|
-
throw new
|
|
6228
|
+
throw new DocumentCommandError("invalid_document", "Document JSON is invalid");
|
|
6189
6229
|
}
|
|
6190
6230
|
}
|
|
6191
6231
|
function textCommand(kind) {
|
|
@@ -6213,10 +6253,10 @@ function applyBasicCommand(document2, command) {
|
|
|
6213
6253
|
if (command.op === "replace_text_block") {
|
|
6214
6254
|
const block2 = document2.blocks.find((entry) => entry.id === command.block_id);
|
|
6215
6255
|
if (!block2) {
|
|
6216
|
-
throw new
|
|
6256
|
+
throw new DocumentCommandError("block_not_found", "Document block was not found");
|
|
6217
6257
|
}
|
|
6218
6258
|
if (block2.kind !== "textBlock") {
|
|
6219
|
-
throw new
|
|
6259
|
+
throw new DocumentCommandError("block_kind_mismatch", "Document block is not a text block");
|
|
6220
6260
|
}
|
|
6221
6261
|
replacePlainField(document2, block2.field, command.text);
|
|
6222
6262
|
return;
|
|
@@ -6224,7 +6264,7 @@ function applyBasicCommand(document2, command) {
|
|
|
6224
6264
|
if (command.op === "remove_block") {
|
|
6225
6265
|
const index = document2.blocks.findIndex((block2) => block2.id === command.block_id);
|
|
6226
6266
|
if (index < 0) {
|
|
6227
|
-
throw new
|
|
6267
|
+
throw new DocumentCommandError("block_not_found", "Document block was not found");
|
|
6228
6268
|
}
|
|
6229
6269
|
document2.blocks.splice(index, 1);
|
|
6230
6270
|
return;
|
|
@@ -6232,11 +6272,11 @@ function applyBasicCommand(document2, command) {
|
|
|
6232
6272
|
if (command.op === "move_block") {
|
|
6233
6273
|
const index = document2.blocks.findIndex((block3) => block3.id === command.block_id);
|
|
6234
6274
|
if (index < 0) {
|
|
6235
|
-
throw new
|
|
6275
|
+
throw new DocumentCommandError("block_not_found", "Document block was not found");
|
|
6236
6276
|
}
|
|
6237
6277
|
const anchorId = "before_block_id" in command.anchor ? command.anchor.before_block_id : "after_block_id" in command.anchor ? command.anchor.after_block_id : null;
|
|
6238
6278
|
if (anchorId === command.block_id) {
|
|
6239
|
-
throw new
|
|
6279
|
+
throw new DocumentCommandError("invalid_anchor", "A block cannot be moved relative to itself");
|
|
6240
6280
|
}
|
|
6241
6281
|
if (anchorId !== null) {
|
|
6242
6282
|
blockInsertionIndex(document2, command.anchor);
|
|
@@ -6251,7 +6291,7 @@ function applyBasicCommand(document2, command) {
|
|
|
6251
6291
|
};
|
|
6252
6292
|
var hasBibliography = hasBibliography2;
|
|
6253
6293
|
if (hasBibliography2(document2.blocks)) {
|
|
6254
|
-
throw new
|
|
6294
|
+
throw new DocumentCommandError("bibliography_exists", "Document already contains a bibliography block");
|
|
6255
6295
|
}
|
|
6256
6296
|
const block2 = {
|
|
6257
6297
|
id: newBlockId(document2),
|
|
@@ -6266,10 +6306,10 @@ function applyBasicCommand(document2, command) {
|
|
|
6266
6306
|
if (command.op === "update_figure") {
|
|
6267
6307
|
const block2 = findBlock(document2.blocks, command.block_id);
|
|
6268
6308
|
if (!block2) {
|
|
6269
|
-
throw new
|
|
6309
|
+
throw new DocumentCommandError("block_not_found", "Document block was not found");
|
|
6270
6310
|
}
|
|
6271
6311
|
if (block2.kind !== "image") {
|
|
6272
|
-
throw new
|
|
6312
|
+
throw new DocumentCommandError("block_kind_mismatch", "Target block is not a figure");
|
|
6273
6313
|
}
|
|
6274
6314
|
if ("asset_id" in command) {
|
|
6275
6315
|
if (command.asset_id === null) {
|
|
@@ -6286,26 +6326,26 @@ function applyBasicCommand(document2, command) {
|
|
|
6286
6326
|
if (command.op === "update_float_meta") {
|
|
6287
6327
|
const block2 = findBlock(document2.blocks, command.block_id);
|
|
6288
6328
|
if (!block2) {
|
|
6289
|
-
throw new
|
|
6329
|
+
throw new DocumentCommandError("block_not_found", "Document block was not found");
|
|
6290
6330
|
}
|
|
6291
6331
|
if (block2.kind !== "image" && block2.kind !== "table") {
|
|
6292
|
-
throw new
|
|
6332
|
+
throw new DocumentCommandError("block_kind_mismatch", "Target block is not a figure or table");
|
|
6293
6333
|
}
|
|
6294
|
-
let
|
|
6295
|
-
if (
|
|
6296
|
-
|
|
6334
|
+
let label2 = command.label;
|
|
6335
|
+
if (label2 !== void 0) {
|
|
6336
|
+
label2 = label2.trim().length > 0 ? normalizeDocumentKey(label2) : "";
|
|
6297
6337
|
}
|
|
6298
|
-
if (
|
|
6299
|
-
const keyError =
|
|
6338
|
+
if (label2 !== void 0 && label2.length > 0) {
|
|
6339
|
+
const keyError = documentKeyError(label2, {
|
|
6300
6340
|
references: document2.references,
|
|
6301
|
-
labels:
|
|
6341
|
+
labels: collectDocumentLabels(document2),
|
|
6302
6342
|
excludeOwnerId: block2.id
|
|
6303
6343
|
});
|
|
6304
6344
|
if (keyError === "duplicate") {
|
|
6305
|
-
throw new
|
|
6345
|
+
throw new DocumentCommandError("duplicate_key", "Float label is already in use");
|
|
6306
6346
|
}
|
|
6307
6347
|
if (keyError !== null) {
|
|
6308
|
-
throw new
|
|
6348
|
+
throw new DocumentCommandError("invalid_key", "Float label is invalid");
|
|
6309
6349
|
}
|
|
6310
6350
|
}
|
|
6311
6351
|
applyFloatMetaPatch(block2, {
|
|
@@ -6313,10 +6353,11 @@ function applyBasicCommand(document2, command) {
|
|
|
6313
6353
|
...command.caption_enabled !== void 0 ? { captionEnabled: command.caption_enabled } : {},
|
|
6314
6354
|
...command.caption !== void 0 ? { caption: command.caption } : {},
|
|
6315
6355
|
...command.label_enabled !== void 0 ? { labelEnabled: command.label_enabled } : {},
|
|
6316
|
-
...
|
|
6356
|
+
...label2 !== void 0 ? { label: label2 } : {}
|
|
6317
6357
|
});
|
|
6318
6358
|
return;
|
|
6319
6359
|
}
|
|
6360
|
+
const label = normalizedNewFloatLabel(document2, command.label);
|
|
6320
6361
|
const block = {
|
|
6321
6362
|
id: newBlockId(document2),
|
|
6322
6363
|
kind: "image",
|
|
@@ -6325,7 +6366,7 @@ function applyBasicCommand(document2, command) {
|
|
|
6325
6366
|
assetId: command.asset_id,
|
|
6326
6367
|
options: { width: "0.8\\columnwidth" },
|
|
6327
6368
|
...defaultFloatMeta(),
|
|
6328
|
-
...
|
|
6369
|
+
...label !== void 0 ? { label, labelEnabled: label.length > 0 } : {},
|
|
6329
6370
|
...command.metadata ? { metadata: { ...command.metadata } } : {}
|
|
6330
6371
|
};
|
|
6331
6372
|
if (command.caption !== void 0) {
|
|
@@ -6336,7 +6377,7 @@ function applyBasicCommand(document2, command) {
|
|
|
6336
6377
|
}
|
|
6337
6378
|
document2.blocks.splice(blockInsertionIndex(document2, command.anchor), 0, block);
|
|
6338
6379
|
}
|
|
6339
|
-
function
|
|
6380
|
+
function applyDocumentCommand(json, commandInput) {
|
|
6340
6381
|
const document2 = parseDocument(json);
|
|
6341
6382
|
const command = parseCommand2(commandInput);
|
|
6342
6383
|
if (isInlineCommand(command)) {
|
|
@@ -6350,10 +6391,10 @@ function applyDocument2Command(json, commandInput) {
|
|
|
6350
6391
|
} else {
|
|
6351
6392
|
applyBasicCommand(document2, command);
|
|
6352
6393
|
}
|
|
6353
|
-
return
|
|
6394
|
+
return toDocumentJson(document2);
|
|
6354
6395
|
}
|
|
6355
6396
|
|
|
6356
|
-
// src/
|
|
6397
|
+
// src/document/diff.ts
|
|
6357
6398
|
var import_diff = require("diff");
|
|
6358
6399
|
var MAX_TOTAL_TEXT_CHARS = 16e3;
|
|
6359
6400
|
var MAX_CHANGED_CHUNK_CHARS = 4e3;
|
|
@@ -6380,7 +6421,7 @@ function comparisonValue(value, key) {
|
|
|
6380
6421
|
return value;
|
|
6381
6422
|
}
|
|
6382
6423
|
function normalizedComparisonDocument(document2) {
|
|
6383
|
-
return comparisonValue(
|
|
6424
|
+
return comparisonValue(toDocumentJson(fromDocumentJson(document2)));
|
|
6384
6425
|
}
|
|
6385
6426
|
function withoutFinalNewline(value) {
|
|
6386
6427
|
return value.endsWith("\n") ? value.slice(0, -1) : value;
|
|
@@ -6406,7 +6447,7 @@ function shorten(text, limit, mode) {
|
|
|
6406
6447
|
truncated: true
|
|
6407
6448
|
};
|
|
6408
6449
|
}
|
|
6409
|
-
function
|
|
6450
|
+
function diffDocument(before, after) {
|
|
6410
6451
|
const beforeComparable = normalizedComparisonDocument(before);
|
|
6411
6452
|
const afterComparable = normalizedComparisonDocument(after);
|
|
6412
6453
|
const parts = (0, import_diff.diffJson)(beforeComparable, afterComparable);
|
|
@@ -6451,7 +6492,7 @@ function diffDocument2(before, after) {
|
|
|
6451
6492
|
return { version: 1, changed: true, truncated, chunks };
|
|
6452
6493
|
}
|
|
6453
6494
|
|
|
6454
|
-
// src/
|
|
6495
|
+
// src/document/outline.ts
|
|
6455
6496
|
var EXCERPT_LIMIT = 160;
|
|
6456
6497
|
function normalizeExcerpt(value) {
|
|
6457
6498
|
const normalized = value.replace(/\s+/g, " ").trim();
|
|
@@ -6499,21 +6540,21 @@ function blockExcerpt(block) {
|
|
|
6499
6540
|
}
|
|
6500
6541
|
return normalizeExcerpt(block.value ?? "");
|
|
6501
6542
|
}
|
|
6502
|
-
function
|
|
6543
|
+
function documentOutline(json) {
|
|
6503
6544
|
if (!json || json.node_type !== "DocumentObject" || !Array.isArray(json.blocks)) {
|
|
6504
|
-
throw new
|
|
6545
|
+
throw new DocumentCommandError("invalid_document", "DocumentObject requires a blocks array");
|
|
6505
6546
|
}
|
|
6506
6547
|
const usedIds = /* @__PURE__ */ new Set();
|
|
6507
6548
|
return json.blocks.map((block) => {
|
|
6508
6549
|
if (!block || typeof block !== "object" || typeof block.command !== "string") {
|
|
6509
|
-
throw new
|
|
6550
|
+
throw new DocumentCommandError("invalid_document", "Document outline requires blocks with string commands");
|
|
6510
6551
|
}
|
|
6511
6552
|
const id = typeof block.id === "string" ? block.id.trim() : "";
|
|
6512
6553
|
if (id.length === 0) {
|
|
6513
|
-
throw new
|
|
6554
|
+
throw new DocumentCommandError("missing_block_id", "Document outline requires canonical block IDs");
|
|
6514
6555
|
}
|
|
6515
6556
|
if (usedIds.has(id)) {
|
|
6516
|
-
throw new
|
|
6557
|
+
throw new DocumentCommandError("duplicate_block_id", `Duplicate document block ID: ${id}`);
|
|
6517
6558
|
}
|
|
6518
6559
|
usedIds.add(id);
|
|
6519
6560
|
return {
|
|
@@ -6525,7 +6566,7 @@ function document2Outline(json) {
|
|
|
6525
6566
|
});
|
|
6526
6567
|
}
|
|
6527
6568
|
|
|
6528
|
-
// src/
|
|
6569
|
+
// src/document/introspection.ts
|
|
6529
6570
|
function appendUnique(target, seen, values) {
|
|
6530
6571
|
for (const value of values) {
|
|
6531
6572
|
if (!seen.has(value)) {
|
|
@@ -6534,7 +6575,7 @@ function appendUnique(target, seen, values) {
|
|
|
6534
6575
|
}
|
|
6535
6576
|
}
|
|
6536
6577
|
}
|
|
6537
|
-
function
|
|
6578
|
+
function getDocumentReferences(document2) {
|
|
6538
6579
|
return document2.references.map((reference) => ({
|
|
6539
6580
|
key: reference.key,
|
|
6540
6581
|
authors: reference.authors,
|
|
@@ -6545,9 +6586,9 @@ function getDocument2References(document2) {
|
|
|
6545
6586
|
field_separator: reference.fieldSeparator
|
|
6546
6587
|
}));
|
|
6547
6588
|
}
|
|
6548
|
-
function
|
|
6589
|
+
function getDocumentReferenceIndex(document2) {
|
|
6549
6590
|
const referenceKeys = new Set(document2.references.map((reference) => reference.key));
|
|
6550
|
-
const labelEntries =
|
|
6591
|
+
const labelEntries = collectDocumentLabels(document2);
|
|
6551
6592
|
const labelKeys = new Set(labelEntries.map((entry) => entry.key));
|
|
6552
6593
|
const citations = [];
|
|
6553
6594
|
const crossReferences = [];
|
|
@@ -6638,7 +6679,7 @@ function getDocument2ReferenceIndex(document2) {
|
|
|
6638
6679
|
};
|
|
6639
6680
|
}
|
|
6640
6681
|
|
|
6641
|
-
// src/
|
|
6682
|
+
// src/document/arabicPreamble.ts
|
|
6642
6683
|
function fontOptions(digitsMapping, includeScript = true) {
|
|
6643
6684
|
const parts = [];
|
|
6644
6685
|
if (includeScript) {
|
|
@@ -6862,7 +6903,7 @@ function getArabicXeLatexPreamble(digitsMappingOrOptions = "arabicdigits") {
|
|
|
6862
6903
|
return arabicXeLatexPreamblePkg(twocolumn) + arabicXeLatexPreambleFont(digitsMapping) + arabicXeLatexPreambleCmd();
|
|
6863
6904
|
}
|
|
6864
6905
|
|
|
6865
|
-
// src/
|
|
6906
|
+
// src/document/exportLatex.ts
|
|
6866
6907
|
function digitsMappingFromDigitForm(digitForm) {
|
|
6867
6908
|
if (digitForm === "western") {
|
|
6868
6909
|
return "digits";
|
|
@@ -7084,7 +7125,7 @@ function authorLatex(authors) {
|
|
|
7084
7125
|
return authors.split(/\n+/).map((line) => line.trim()).filter((line) => line.length > 0).join(" \\\\ ");
|
|
7085
7126
|
}
|
|
7086
7127
|
function articleMaketitlePreamble(meta, twocolumn = false) {
|
|
7087
|
-
if (!
|
|
7128
|
+
if (!documentHasMaketitle(meta)) {
|
|
7088
7129
|
return "";
|
|
7089
7130
|
}
|
|
7090
7131
|
const parts = [butexBasmalaTitlingPreamble({ twocolumn })];
|
|
@@ -7099,12 +7140,12 @@ function articleMaketitlePreamble(meta, twocolumn = false) {
|
|
|
7099
7140
|
}
|
|
7100
7141
|
function articleFrontMatterLatex(meta) {
|
|
7101
7142
|
const parts = [];
|
|
7102
|
-
if (
|
|
7143
|
+
if (documentHasMaketitle(meta)) {
|
|
7103
7144
|
parts.push("\\maketitle");
|
|
7104
7145
|
} else {
|
|
7105
7146
|
parts.push(butexBasmalaDisplayLatex());
|
|
7106
7147
|
}
|
|
7107
|
-
if (
|
|
7148
|
+
if (documentHasAbstract(meta)) {
|
|
7108
7149
|
parts.push(`\\begin{abstract}
|
|
7109
7150
|
${meta.abstract.trim()}
|
|
7110
7151
|
\\end{abstract}`);
|
|
@@ -7113,7 +7154,7 @@ ${meta.abstract.trim()}
|
|
|
7113
7154
|
}
|
|
7114
7155
|
function documentLabelKindMap(document2) {
|
|
7115
7156
|
const map = /* @__PURE__ */ new Map();
|
|
7116
|
-
for (const entry of
|
|
7157
|
+
for (const entry of collectDocumentLabels(document2)) {
|
|
7117
7158
|
if (!map.has(entry.key)) {
|
|
7118
7159
|
map.set(entry.key, entry.kind);
|
|
7119
7160
|
}
|
|
@@ -7125,8 +7166,8 @@ function closingHamdalaLatex() {
|
|
|
7125
7166
|
\\maghribi{${BUTEX_CLOSING_HAMDALA}}
|
|
7126
7167
|
\\end{center}`;
|
|
7127
7168
|
}
|
|
7128
|
-
function
|
|
7129
|
-
const meta = document2.meta ??
|
|
7169
|
+
function documentLatex(document2, options = {}) {
|
|
7170
|
+
const meta = document2.meta ?? emptyDocumentMeta();
|
|
7130
7171
|
const twocolumn = options.twocolumn === true;
|
|
7131
7172
|
const front = articleFrontMatterLatex(meta);
|
|
7132
7173
|
const labelKinds = documentLabelKindMap(document2);
|
|
@@ -7156,7 +7197,7 @@ ${body}
|
|
|
7156
7197
|
`;
|
|
7157
7198
|
}
|
|
7158
7199
|
|
|
7159
|
-
// src/
|
|
7200
|
+
// src/document/validation.ts
|
|
7160
7201
|
var XELATEX_UNSUPPORTED_EMOJI_PATTERN = /[\p{Emoji_Presentation}\p{Emoji_Modifier}\p{Regional_Indicator}\uFE0F\u20E3]/u;
|
|
7161
7202
|
function hasXeLatexUnsupportedEmoji(value) {
|
|
7162
7203
|
return XELATEX_UNSUPPORTED_EMOJI_PATTERN.test(value);
|
|
@@ -7175,12 +7216,12 @@ function validateTextValue(context, value, path, blockId, tokenId) {
|
|
|
7175
7216
|
}
|
|
7176
7217
|
}
|
|
7177
7218
|
function registerKey(context, key, path, kind, blockId, tokenId) {
|
|
7178
|
-
const normalized =
|
|
7219
|
+
const normalized = normalizeDocumentKey(key);
|
|
7179
7220
|
if (normalized.length === 0) {
|
|
7180
7221
|
addIssue(context, "key_empty", path, blockId, tokenId);
|
|
7181
7222
|
return;
|
|
7182
7223
|
}
|
|
7183
|
-
if (!
|
|
7224
|
+
if (!isValidDocumentKey(normalized)) {
|
|
7184
7225
|
addIssue(context, "key_invalid", path, blockId, tokenId);
|
|
7185
7226
|
return;
|
|
7186
7227
|
}
|
|
@@ -7303,7 +7344,7 @@ function validateDuplicateKeys(context) {
|
|
|
7303
7344
|
}
|
|
7304
7345
|
function validatePendingReferences(context) {
|
|
7305
7346
|
for (const pending of context.pendingCitations) {
|
|
7306
|
-
if (pending.token.keys.some((key) => !
|
|
7347
|
+
if (pending.token.keys.some((key) => !isValidDocumentKey(normalizeDocumentKey(key)))) {
|
|
7307
7348
|
addIssue(
|
|
7308
7349
|
context,
|
|
7309
7350
|
"missing_citation",
|
|
@@ -7315,8 +7356,8 @@ function validatePendingReferences(context) {
|
|
|
7315
7356
|
}
|
|
7316
7357
|
for (const pending of context.pendingReferences) {
|
|
7317
7358
|
for (const key of pending.token.keys) {
|
|
7318
|
-
const normalized =
|
|
7319
|
-
if (!
|
|
7359
|
+
const normalized = normalizeDocumentKey(key);
|
|
7360
|
+
if (!isValidDocumentKey(normalized)) {
|
|
7320
7361
|
addIssue(
|
|
7321
7362
|
context,
|
|
7322
7363
|
"missing_reference",
|
|
@@ -7370,7 +7411,7 @@ function hasWireDocumentShape(document2) {
|
|
|
7370
7411
|
function resolveDocument(document2) {
|
|
7371
7412
|
if (hasRuntimeDocumentShape(document2)) {
|
|
7372
7413
|
try {
|
|
7373
|
-
|
|
7414
|
+
toDocumentJson(document2);
|
|
7374
7415
|
return document2;
|
|
7375
7416
|
} catch {
|
|
7376
7417
|
return null;
|
|
@@ -7380,12 +7421,12 @@ function resolveDocument(document2) {
|
|
|
7380
7421
|
return null;
|
|
7381
7422
|
}
|
|
7382
7423
|
try {
|
|
7383
|
-
return
|
|
7424
|
+
return fromDocumentJson(document2);
|
|
7384
7425
|
} catch {
|
|
7385
7426
|
return null;
|
|
7386
7427
|
}
|
|
7387
7428
|
}
|
|
7388
|
-
function
|
|
7429
|
+
function validateDocument(document2) {
|
|
7389
7430
|
const resolved = resolveDocument(document2);
|
|
7390
7431
|
if (!resolved) {
|
|
7391
7432
|
return {
|
|
@@ -7425,25 +7466,25 @@ function validateDocument2(document2) {
|
|
|
7425
7466
|
issues: context.issues
|
|
7426
7467
|
};
|
|
7427
7468
|
}
|
|
7428
|
-
var
|
|
7469
|
+
var DocumentValidationError = class extends Error {
|
|
7429
7470
|
issues;
|
|
7430
7471
|
constructor(issues) {
|
|
7431
|
-
super("
|
|
7432
|
-
this.name = "
|
|
7472
|
+
super("Document validation failed; invalid drafts cannot be exported to LaTeX.");
|
|
7473
|
+
this.name = "DocumentValidationError";
|
|
7433
7474
|
this.issues = issues;
|
|
7434
7475
|
}
|
|
7435
7476
|
};
|
|
7436
7477
|
|
|
7437
|
-
// src/
|
|
7438
|
-
function
|
|
7439
|
-
const validation =
|
|
7478
|
+
// src/document/validatedLatex.ts
|
|
7479
|
+
function documentLatex2(document2, options = {}) {
|
|
7480
|
+
const validation = validateDocument(document2);
|
|
7440
7481
|
if (!validation.valid) {
|
|
7441
|
-
throw new
|
|
7482
|
+
throw new DocumentValidationError(validation.issues);
|
|
7442
7483
|
}
|
|
7443
|
-
return
|
|
7484
|
+
return documentLatex(document2, options);
|
|
7444
7485
|
}
|
|
7445
7486
|
|
|
7446
|
-
// src/
|
|
7487
|
+
// src/document-cli/mathDiagnostic.ts
|
|
7447
7488
|
function compactError(error) {
|
|
7448
7489
|
const message = error instanceof Error ? error.message : String(error);
|
|
7449
7490
|
return message.slice(0, 500);
|
|
@@ -7484,7 +7525,7 @@ function diagnoseMathObject(mathObject) {
|
|
|
7484
7525
|
};
|
|
7485
7526
|
}
|
|
7486
7527
|
|
|
7487
|
-
// src/
|
|
7528
|
+
// src/document-cli/mathProjection.ts
|
|
7488
7529
|
function sourceMode2(value) {
|
|
7489
7530
|
if (typeof value === "object" && value !== null && !Array.isArray(value) && value.source_side === "arabic") {
|
|
7490
7531
|
return "arabic";
|
|
@@ -7496,11 +7537,11 @@ function projectMathObjectToEnglish(mathObject) {
|
|
|
7496
7537
|
try {
|
|
7497
7538
|
math = fromMathObjectJson(mathObject, sourceMode2(mathObject));
|
|
7498
7539
|
} catch {
|
|
7499
|
-
throw new
|
|
7540
|
+
throw new DocumentCommandError("invalid_inline_token", "MathObject JSON is invalid");
|
|
7500
7541
|
}
|
|
7501
7542
|
const sessionResult = mathObjectToEditorSession(math);
|
|
7502
7543
|
if (!sessionResult.editable) {
|
|
7503
|
-
throw new
|
|
7544
|
+
throw new DocumentCommandError("unsupported_inline_content", sessionResult.reason);
|
|
7504
7545
|
}
|
|
7505
7546
|
const projected = mathNodeFromEditorSession(
|
|
7506
7547
|
sessionResult.session,
|
|
@@ -7508,52 +7549,67 @@ function projectMathObjectToEnglish(mathObject) {
|
|
|
7508
7549
|
math.closing
|
|
7509
7550
|
);
|
|
7510
7551
|
if (!projected.ok) {
|
|
7511
|
-
throw new
|
|
7552
|
+
throw new DocumentCommandError("unsupported_inline_content", projected.reason);
|
|
7512
7553
|
}
|
|
7513
7554
|
return toMathObjectJson(projected.math);
|
|
7514
7555
|
}
|
|
7515
7556
|
|
|
7516
|
-
// src/
|
|
7517
|
-
var
|
|
7557
|
+
// src/document-cli/protocol.ts
|
|
7558
|
+
var DOCUMENT_WORKER_ROUTES = {
|
|
7559
|
+
normalize: "/v1/document/normalize",
|
|
7560
|
+
outline: "/v1/document/outline",
|
|
7561
|
+
references: "/v1/document/references",
|
|
7562
|
+
reference_index: "/v1/document/reference-index",
|
|
7563
|
+
export: "/v1/document/export",
|
|
7564
|
+
diff: "/v1/document/diff",
|
|
7565
|
+
apply_command: "/v1/document/commands",
|
|
7566
|
+
diagnose_math_object: "/v1/document/diagnostics/math-object",
|
|
7567
|
+
project_math_object_english: "/v1/document/math-object/english",
|
|
7568
|
+
diagnose_contract: "/v1/document/diagnostics/contract"
|
|
7569
|
+
};
|
|
7570
|
+
var DocumentWorkerProtocolError = class extends Error {
|
|
7518
7571
|
code;
|
|
7519
7572
|
constructor(code, message) {
|
|
7520
7573
|
super(message);
|
|
7521
|
-
this.name = "
|
|
7574
|
+
this.name = "DocumentWorkerProtocolError";
|
|
7522
7575
|
this.code = code;
|
|
7523
7576
|
}
|
|
7524
7577
|
};
|
|
7525
7578
|
function isObject4(value) {
|
|
7526
7579
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7527
7580
|
}
|
|
7528
|
-
function
|
|
7581
|
+
function parseDocumentWorkerRequest(value) {
|
|
7529
7582
|
if (!isObject4(value) || typeof value.action !== "string") {
|
|
7530
|
-
throw new
|
|
7583
|
+
throw new DocumentWorkerProtocolError("invalid_request", "Worker request requires an action");
|
|
7584
|
+
}
|
|
7585
|
+
if (value.action === "diagnose_contract") {
|
|
7586
|
+
return { action: "diagnose_contract" };
|
|
7531
7587
|
}
|
|
7532
7588
|
if (value.action === "diff") {
|
|
7533
7589
|
if (!("before" in value) || !("after" in value)) {
|
|
7534
|
-
throw new
|
|
7590
|
+
throw new DocumentWorkerProtocolError("invalid_request", "diff requires before and after documents");
|
|
7535
7591
|
}
|
|
7536
7592
|
return { action: "diff", before: value.before, after: value.after };
|
|
7537
7593
|
}
|
|
7538
7594
|
if (value.action === "diagnose_math_object" || value.action === "project_math_object_english") {
|
|
7539
7595
|
if (!("math_object" in value)) {
|
|
7540
|
-
throw new
|
|
7596
|
+
throw new DocumentWorkerProtocolError("invalid_request", `${value.action} requires math_object`);
|
|
7541
7597
|
}
|
|
7542
7598
|
return { action: value.action, math_object: value.math_object };
|
|
7543
7599
|
}
|
|
7544
7600
|
if (!("document" in value)) {
|
|
7545
|
-
throw new
|
|
7601
|
+
throw new DocumentWorkerProtocolError("invalid_request", "Worker request requires a document");
|
|
7546
7602
|
}
|
|
7547
7603
|
if (value.action === "normalize" || value.action === "outline" || value.action === "references" || value.action === "reference_index" || value.action === "export") {
|
|
7548
7604
|
return { action: value.action, document: value.document };
|
|
7549
7605
|
}
|
|
7550
7606
|
if (value.action === "apply_command") {
|
|
7551
7607
|
if (!("command" in value)) {
|
|
7552
|
-
throw new
|
|
7608
|
+
throw new DocumentWorkerProtocolError("invalid_request", "apply_command requires command");
|
|
7553
7609
|
}
|
|
7554
7610
|
return { action: value.action, document: value.document, command: value.command };
|
|
7555
7611
|
}
|
|
7556
|
-
throw new
|
|
7612
|
+
throw new DocumentWorkerProtocolError("invalid_request", `Unsupported worker action: ${value.action}`);
|
|
7557
7613
|
}
|
|
7558
7614
|
function boundedValidationIssues(issues) {
|
|
7559
7615
|
return issues.slice(0, 100).map((issue) => ({
|
|
@@ -7564,7 +7620,7 @@ function boundedValidationIssues(issues) {
|
|
|
7564
7620
|
}));
|
|
7565
7621
|
}
|
|
7566
7622
|
function workerFailure(error) {
|
|
7567
|
-
if (error instanceof
|
|
7623
|
+
if (error instanceof DocumentValidationError) {
|
|
7568
7624
|
return {
|
|
7569
7625
|
response: {
|
|
7570
7626
|
ok: false,
|
|
@@ -7577,13 +7633,13 @@ function workerFailure(error) {
|
|
|
7577
7633
|
status: 422
|
|
7578
7634
|
};
|
|
7579
7635
|
}
|
|
7580
|
-
if (error instanceof
|
|
7636
|
+
if (error instanceof DocumentCommandError) {
|
|
7581
7637
|
return {
|
|
7582
7638
|
response: { ok: false, error: { code: error.code, message: error.message } },
|
|
7583
7639
|
status: 422
|
|
7584
7640
|
};
|
|
7585
7641
|
}
|
|
7586
|
-
if (error instanceof
|
|
7642
|
+
if (error instanceof DocumentWorkerProtocolError) {
|
|
7587
7643
|
let status = 400;
|
|
7588
7644
|
if (error.code === "payload_too_large") {
|
|
7589
7645
|
status = 413;
|
|
@@ -7603,23 +7659,23 @@ function workerFailure(error) {
|
|
|
7603
7659
|
unexpected: error
|
|
7604
7660
|
};
|
|
7605
7661
|
}
|
|
7606
|
-
function
|
|
7662
|
+
function asDocumentJson(value) {
|
|
7607
7663
|
return value;
|
|
7608
7664
|
}
|
|
7609
|
-
function
|
|
7665
|
+
function asDocumentCommand(value) {
|
|
7610
7666
|
return value;
|
|
7611
7667
|
}
|
|
7612
7668
|
|
|
7613
|
-
// src/
|
|
7669
|
+
// src/document-cli/execute.ts
|
|
7614
7670
|
function resolveDocument2(document2) {
|
|
7615
7671
|
try {
|
|
7616
|
-
return
|
|
7672
|
+
return fromDocumentJson(document2);
|
|
7617
7673
|
} catch {
|
|
7618
|
-
throw new
|
|
7674
|
+
throw new DocumentCommandError("invalid_document", "Document JSON is invalid");
|
|
7619
7675
|
}
|
|
7620
7676
|
}
|
|
7621
7677
|
function normalizeDocument(document2) {
|
|
7622
|
-
return
|
|
7678
|
+
return toDocumentJson(resolveDocument2(document2));
|
|
7623
7679
|
}
|
|
7624
7680
|
function collectAssetIds(blocks) {
|
|
7625
7681
|
const assetIds = [];
|
|
@@ -7646,8 +7702,20 @@ function collectAssetIds(blocks) {
|
|
|
7646
7702
|
}
|
|
7647
7703
|
return assetIds;
|
|
7648
7704
|
}
|
|
7649
|
-
function
|
|
7650
|
-
const request =
|
|
7705
|
+
function executeDocumentWorkerRequest(input) {
|
|
7706
|
+
const request = parseDocumentWorkerRequest(input);
|
|
7707
|
+
if (request.action === "diagnose_contract") {
|
|
7708
|
+
return {
|
|
7709
|
+
ok: true,
|
|
7710
|
+
contract: {
|
|
7711
|
+
service: "butex-document",
|
|
7712
|
+
routes: { ...DOCUMENT_WORKER_ROUTES },
|
|
7713
|
+
build_commit: process.env.BUTEX_BUILD_COMMIT_SHA ?? process.env.RAILWAY_GIT_COMMIT_SHA ?? null,
|
|
7714
|
+
package_version: process.env.npm_package_version ?? null,
|
|
7715
|
+
provenance: "runtime_environment_unverified"
|
|
7716
|
+
}
|
|
7717
|
+
};
|
|
7718
|
+
}
|
|
7651
7719
|
if (request.action === "diagnose_math_object") {
|
|
7652
7720
|
return { ok: true, math_diagnostic: diagnoseMathObject(request.math_object) };
|
|
7653
7721
|
}
|
|
@@ -7664,51 +7732,51 @@ function executeDocument2WorkerRequest(input) {
|
|
|
7664
7732
|
try {
|
|
7665
7733
|
return {
|
|
7666
7734
|
ok: true,
|
|
7667
|
-
diff:
|
|
7735
|
+
diff: diffDocument(asDocumentJson(request.before), asDocumentJson(request.after))
|
|
7668
7736
|
};
|
|
7669
7737
|
} catch {
|
|
7670
|
-
throw new
|
|
7738
|
+
throw new DocumentCommandError("invalid_document", "Document JSON is invalid");
|
|
7671
7739
|
}
|
|
7672
7740
|
}
|
|
7673
7741
|
if (request.action === "outline") {
|
|
7674
|
-
return { ok: true, outline:
|
|
7742
|
+
return { ok: true, outline: documentOutline(asDocumentJson(request.document)) };
|
|
7675
7743
|
}
|
|
7676
7744
|
if (request.action === "references") {
|
|
7677
|
-
return { ok: true, references:
|
|
7745
|
+
return { ok: true, references: getDocumentReferences(resolveDocument2(request.document)) };
|
|
7678
7746
|
}
|
|
7679
7747
|
if (request.action === "reference_index") {
|
|
7680
|
-
return { ok: true, reference_index:
|
|
7748
|
+
return { ok: true, reference_index: getDocumentReferenceIndex(resolveDocument2(request.document)) };
|
|
7681
7749
|
}
|
|
7682
7750
|
if (request.action === "export") {
|
|
7683
7751
|
const document2 = resolveDocument2(request.document);
|
|
7684
7752
|
return {
|
|
7685
7753
|
ok: true,
|
|
7686
|
-
latex:
|
|
7754
|
+
latex: documentLatex2(document2, { twocolumn: true }),
|
|
7687
7755
|
asset_ids: collectAssetIds(document2.blocks)
|
|
7688
7756
|
};
|
|
7689
7757
|
}
|
|
7690
7758
|
return {
|
|
7691
7759
|
ok: true,
|
|
7692
|
-
document:
|
|
7760
|
+
document: applyDocumentCommand(asDocumentJson(request.document), asDocumentCommand(request.command))
|
|
7693
7761
|
};
|
|
7694
7762
|
}
|
|
7695
7763
|
|
|
7696
|
-
// src/
|
|
7697
|
-
var
|
|
7764
|
+
// src/document-cli/stdinTransport.ts
|
|
7765
|
+
var DOCUMENT_WORKER_MAX_BODY_BYTES = 5 * 1024 * 1024;
|
|
7698
7766
|
function parseJson(input) {
|
|
7699
7767
|
try {
|
|
7700
7768
|
return JSON.parse(input);
|
|
7701
7769
|
} catch {
|
|
7702
|
-
throw new
|
|
7770
|
+
throw new DocumentWorkerProtocolError("invalid_json", "Input must be valid JSON");
|
|
7703
7771
|
}
|
|
7704
7772
|
}
|
|
7705
|
-
function
|
|
7773
|
+
function executeDocumentStdinText(input) {
|
|
7706
7774
|
try {
|
|
7707
7775
|
const size = Buffer.byteLength(input);
|
|
7708
|
-
if (size >
|
|
7709
|
-
throw new
|
|
7776
|
+
if (size > DOCUMENT_WORKER_MAX_BODY_BYTES) {
|
|
7777
|
+
throw new DocumentWorkerProtocolError("payload_too_large", "Worker request exceeds the 5 MiB limit");
|
|
7710
7778
|
}
|
|
7711
|
-
const response =
|
|
7779
|
+
const response = executeDocumentWorkerRequest(parseJson(input));
|
|
7712
7780
|
return { output: `${JSON.stringify(response)}
|
|
7713
7781
|
`, exitCode: 0 };
|
|
7714
7782
|
} catch (error) {
|
|
@@ -7727,8 +7795,8 @@ async function readAll(stream) {
|
|
|
7727
7795
|
for await (const chunk of stream) {
|
|
7728
7796
|
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
7729
7797
|
size += buffer.length;
|
|
7730
|
-
if (size >
|
|
7731
|
-
throw new
|
|
7798
|
+
if (size > DOCUMENT_WORKER_MAX_BODY_BYTES) {
|
|
7799
|
+
throw new DocumentWorkerProtocolError("payload_too_large", "Worker request exceeds the 5 MiB limit");
|
|
7732
7800
|
}
|
|
7733
7801
|
chunks.push(buffer);
|
|
7734
7802
|
}
|
|
@@ -7745,10 +7813,10 @@ function write(stream, value) {
|
|
|
7745
7813
|
});
|
|
7746
7814
|
});
|
|
7747
7815
|
}
|
|
7748
|
-
async function
|
|
7816
|
+
async function runDocumentStdin(input, output, errorOutput) {
|
|
7749
7817
|
let result;
|
|
7750
7818
|
try {
|
|
7751
|
-
result =
|
|
7819
|
+
result = executeDocumentStdinText(await readAll(input));
|
|
7752
7820
|
} catch (error) {
|
|
7753
7821
|
const failure = workerFailure(error);
|
|
7754
7822
|
result = {
|
|
@@ -7767,8 +7835,8 @@ async function runDocument2Stdin(input, output, errorOutput) {
|
|
|
7767
7835
|
return result.exitCode;
|
|
7768
7836
|
}
|
|
7769
7837
|
|
|
7770
|
-
// src/
|
|
7771
|
-
var
|
|
7838
|
+
// src/document-cli/httpServer.ts
|
|
7839
|
+
var DOCUMENT_WORKER_REQUEST_TIMEOUT_MS = 15e3;
|
|
7772
7840
|
function sendJson(response, status, body) {
|
|
7773
7841
|
const json = JSON.stringify(body);
|
|
7774
7842
|
response.writeHead(status, {
|
|
@@ -7797,14 +7865,14 @@ async function readJsonBody(request, maxBodyBytes) {
|
|
|
7797
7865
|
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk));
|
|
7798
7866
|
size += buffer.length;
|
|
7799
7867
|
if (size > maxBodyBytes) {
|
|
7800
|
-
throw new
|
|
7868
|
+
throw new DocumentWorkerProtocolError("payload_too_large", "Worker request exceeds the 5 MiB limit");
|
|
7801
7869
|
}
|
|
7802
7870
|
chunks.push(buffer);
|
|
7803
7871
|
}
|
|
7804
7872
|
try {
|
|
7805
7873
|
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
7806
7874
|
} catch {
|
|
7807
|
-
throw new
|
|
7875
|
+
throw new DocumentWorkerProtocolError("invalid_json", "Request body must be valid JSON");
|
|
7808
7876
|
}
|
|
7809
7877
|
}
|
|
7810
7878
|
function requestId(request) {
|
|
@@ -7816,31 +7884,34 @@ function traceId(request) {
|
|
|
7816
7884
|
return typeof value === "string" ? value.slice(0, 128) : void 0;
|
|
7817
7885
|
}
|
|
7818
7886
|
function actionForPath(pathname) {
|
|
7819
|
-
if (pathname ===
|
|
7887
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.diagnose_contract) {
|
|
7888
|
+
return "diagnose_contract";
|
|
7889
|
+
}
|
|
7890
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.normalize) {
|
|
7820
7891
|
return "normalize";
|
|
7821
7892
|
}
|
|
7822
|
-
if (pathname ===
|
|
7893
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.outline) {
|
|
7823
7894
|
return "outline";
|
|
7824
7895
|
}
|
|
7825
|
-
if (pathname ===
|
|
7896
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.references) {
|
|
7826
7897
|
return "references";
|
|
7827
7898
|
}
|
|
7828
|
-
if (pathname ===
|
|
7899
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.reference_index) {
|
|
7829
7900
|
return "reference_index";
|
|
7830
7901
|
}
|
|
7831
|
-
if (pathname ===
|
|
7902
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.export) {
|
|
7832
7903
|
return "export";
|
|
7833
7904
|
}
|
|
7834
|
-
if (pathname ===
|
|
7905
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.diff) {
|
|
7835
7906
|
return "diff";
|
|
7836
7907
|
}
|
|
7837
|
-
if (pathname ===
|
|
7908
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.apply_command) {
|
|
7838
7909
|
return "apply_command";
|
|
7839
7910
|
}
|
|
7840
|
-
if (pathname ===
|
|
7911
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.diagnose_math_object) {
|
|
7841
7912
|
return "diagnose_math_object";
|
|
7842
7913
|
}
|
|
7843
|
-
if (pathname ===
|
|
7914
|
+
if (pathname === DOCUMENT_WORKER_ROUTES.project_math_object_english) {
|
|
7844
7915
|
return "project_math_object_english";
|
|
7845
7916
|
}
|
|
7846
7917
|
return null;
|
|
@@ -7856,11 +7927,11 @@ function defaultLogger(entry) {
|
|
|
7856
7927
|
...unexpected !== void 0 ? { unexpected } : {}
|
|
7857
7928
|
}));
|
|
7858
7929
|
}
|
|
7859
|
-
function
|
|
7930
|
+
function createDocumentHttpServer(options) {
|
|
7860
7931
|
if (options.token.length === 0) {
|
|
7861
7932
|
throw new Error("Document worker HTTP mode requires BUTEX_WORKER_TOKEN");
|
|
7862
7933
|
}
|
|
7863
|
-
const maxBodyBytes = options.maxBodyBytes ??
|
|
7934
|
+
const maxBodyBytes = options.maxBodyBytes ?? DOCUMENT_WORKER_MAX_BODY_BYTES;
|
|
7864
7935
|
const logger = options.logger ?? defaultLogger;
|
|
7865
7936
|
const server = (0, import_node_http.createServer)(async (request, response) => {
|
|
7866
7937
|
const startedAt = Date.now();
|
|
@@ -7871,21 +7942,21 @@ function createDocument2HttpServer(options) {
|
|
|
7871
7942
|
try {
|
|
7872
7943
|
if (request.method === "GET" && pathname === "/health") {
|
|
7873
7944
|
status = 200;
|
|
7874
|
-
sendJson(response, status, { ok: true, service: "butex-
|
|
7945
|
+
sendJson(response, status, { ok: true, service: "butex-document" });
|
|
7875
7946
|
return;
|
|
7876
7947
|
}
|
|
7877
7948
|
const action = request.method === "POST" ? actionForPath(pathname) : null;
|
|
7878
7949
|
if (!action) {
|
|
7879
|
-
throw new
|
|
7950
|
+
throw new DocumentWorkerProtocolError("not_found", "Worker endpoint was not found");
|
|
7880
7951
|
}
|
|
7881
7952
|
if (!tokenMatches(options.token, bearerToken(request))) {
|
|
7882
|
-
throw new
|
|
7953
|
+
throw new DocumentWorkerProtocolError("unauthorized", "Worker service token is invalid");
|
|
7883
7954
|
}
|
|
7884
7955
|
const body = await readJsonBody(request, maxBodyBytes);
|
|
7885
7956
|
if (typeof body !== "object" || body === null || Array.isArray(body)) {
|
|
7886
|
-
throw new
|
|
7957
|
+
throw new DocumentWorkerProtocolError("invalid_request", "Request body must be an object");
|
|
7887
7958
|
}
|
|
7888
|
-
const responseBody =
|
|
7959
|
+
const responseBody = executeDocumentWorkerRequest({ ...body, action });
|
|
7889
7960
|
status = 200;
|
|
7890
7961
|
sendJson(response, status, responseBody);
|
|
7891
7962
|
} catch (error) {
|
|
@@ -7908,13 +7979,13 @@ function createDocument2HttpServer(options) {
|
|
|
7908
7979
|
});
|
|
7909
7980
|
}
|
|
7910
7981
|
});
|
|
7911
|
-
server.requestTimeout =
|
|
7912
|
-
server.headersTimeout =
|
|
7982
|
+
server.requestTimeout = DOCUMENT_WORKER_REQUEST_TIMEOUT_MS;
|
|
7983
|
+
server.headersTimeout = DOCUMENT_WORKER_REQUEST_TIMEOUT_MS;
|
|
7913
7984
|
server.keepAliveTimeout = 5e3;
|
|
7914
7985
|
return server;
|
|
7915
7986
|
}
|
|
7916
7987
|
|
|
7917
|
-
// src/
|
|
7988
|
+
// src/document-cli/main.ts
|
|
7918
7989
|
function parseArgs(args) {
|
|
7919
7990
|
const options = { serve: false, host: "127.0.0.1", help: false };
|
|
7920
7991
|
for (let index = 0; index < args.length; index += 1) {
|
|
@@ -7939,8 +8010,8 @@ function parseArgs(args) {
|
|
|
7939
8010
|
function usage() {
|
|
7940
8011
|
return [
|
|
7941
8012
|
"Usage:",
|
|
7942
|
-
" butex-
|
|
7943
|
-
" butex-
|
|
8013
|
+
" butex-document Read one JSON request from stdin",
|
|
8014
|
+
" butex-document --serve [--host] Run the stateless HTTP worker",
|
|
7944
8015
|
"",
|
|
7945
8016
|
"HTTP mode requires BUTEX_WORKER_TOKEN and honors Railway PORT.",
|
|
7946
8017
|
""
|
|
@@ -7982,14 +8053,14 @@ async function main() {
|
|
|
7982
8053
|
return 0;
|
|
7983
8054
|
}
|
|
7984
8055
|
if (!options.serve) {
|
|
7985
|
-
return
|
|
8056
|
+
return runDocumentStdin(process.stdin, process.stdout, process.stderr);
|
|
7986
8057
|
}
|
|
7987
8058
|
const token = process.env.BUTEX_WORKER_TOKEN ?? "";
|
|
7988
8059
|
const port = portFromEnvironment();
|
|
7989
|
-
const server =
|
|
8060
|
+
const server = createDocumentHttpServer({ token });
|
|
7990
8061
|
installShutdownHandlers(server);
|
|
7991
8062
|
await listen(server, port, options.host);
|
|
7992
|
-
console.error(JSON.stringify({ service: "butex-
|
|
8063
|
+
console.error(JSON.stringify({ service: "butex-document", status: "listening", host: options.host, port }));
|
|
7993
8064
|
return 0;
|
|
7994
8065
|
}
|
|
7995
8066
|
main().then((exitCode) => {
|
|
@@ -8000,4 +8071,4 @@ main().then((exitCode) => {
|
|
|
8000
8071
|
`);
|
|
8001
8072
|
process.exitCode = 1;
|
|
8002
8073
|
});
|
|
8003
|
-
//# sourceMappingURL=
|
|
8074
|
+
//# sourceMappingURL=document-cli.js.map
|