@drghaliasri/butex 7.0.3 → 7.1.0
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/dist/document2-cli.js +376 -34
- package/dist/document2-cli.js.map +1 -1
- package/dist/document2.d.mts +83 -3
- package/dist/document2.d.ts +83 -3
- package/dist/document2.js +399 -37
- package/dist/document2.js.map +1 -1
- package/dist/document2.mjs +396 -37
- package/dist/document2.mjs.map +1 -1
- package/dist/react-document2-preview.d.mts +336 -0
- package/dist/react-document2-preview.d.ts +336 -0
- package/dist/react-document2-preview.js +7767 -0
- package/dist/react-document2-preview.js.map +1 -0
- package/dist/react-document2-preview.mjs +7737 -0
- package/dist/react-document2-preview.mjs.map +1 -0
- package/dist/react-document2.d.mts +53 -14
- package/dist/react-document2.d.ts +53 -14
- package/dist/react-document2.js +284 -42
- package/dist/react-document2.js.map +1 -1
- package/dist/react-document2.mjs +281 -41
- package/dist/react-document2.mjs.map +1 -1
- package/package.json +9 -2
package/dist/document2-cli.js
CHANGED
|
@@ -708,6 +708,10 @@ function defaultFloatMeta() {
|
|
|
708
708
|
centered: true,
|
|
709
709
|
captionEnabled: false,
|
|
710
710
|
caption: "",
|
|
711
|
+
captionField: {
|
|
712
|
+
id: document2Id("field"),
|
|
713
|
+
tokens: [{ id: document2Id("text"), kind: "text", text: "" }]
|
|
714
|
+
},
|
|
711
715
|
labelEnabled: false,
|
|
712
716
|
label: ""
|
|
713
717
|
};
|
|
@@ -766,15 +770,20 @@ function collectDocument2Labels(document2) {
|
|
|
766
770
|
}
|
|
767
771
|
function walk(blocks) {
|
|
768
772
|
for (const block of blocks) {
|
|
769
|
-
if (block.kind === "image"
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
773
|
+
if (block.kind === "image") {
|
|
774
|
+
if (block.labelEnabled && block.label.trim().length > 0) {
|
|
775
|
+
counters.fig += 1;
|
|
776
|
+
entries.push({
|
|
777
|
+
key: block.label.trim(),
|
|
778
|
+
kind: "fig",
|
|
779
|
+
caption: block.captionEnabled ? block.caption : "",
|
|
780
|
+
ownerId: block.id,
|
|
781
|
+
number: counters.fig
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
if (block.captionEnabled) {
|
|
785
|
+
pushEqFromField(block.captionField.tokens);
|
|
786
|
+
}
|
|
778
787
|
}
|
|
779
788
|
if (block.kind === "table") {
|
|
780
789
|
if (block.labelEnabled && block.label.trim().length > 0) {
|
|
@@ -787,6 +796,9 @@ function collectDocument2Labels(document2) {
|
|
|
787
796
|
number: counters.tab
|
|
788
797
|
});
|
|
789
798
|
}
|
|
799
|
+
if (block.captionEnabled) {
|
|
800
|
+
pushEqFromField(block.captionField.tokens);
|
|
801
|
+
}
|
|
790
802
|
for (const row of block.rows) {
|
|
791
803
|
for (const cell of row) {
|
|
792
804
|
pushEqFromField(cell.tokens);
|
|
@@ -829,6 +841,14 @@ function applyFloatMetaPatch(block, patch) {
|
|
|
829
841
|
}
|
|
830
842
|
if (typeof patch.caption === "string") {
|
|
831
843
|
block.caption = patch.caption;
|
|
844
|
+
const existingText = block.captionField.tokens.find((token) => token.kind === "text");
|
|
845
|
+
block.captionField.tokens = [
|
|
846
|
+
{
|
|
847
|
+
id: existingText?.id ?? document2Id("text"),
|
|
848
|
+
kind: "text",
|
|
849
|
+
text: patch.caption
|
|
850
|
+
}
|
|
851
|
+
];
|
|
832
852
|
}
|
|
833
853
|
if (typeof patch.labelEnabled === "boolean") {
|
|
834
854
|
block.labelEnabled = patch.labelEnabled;
|
|
@@ -1348,6 +1368,21 @@ function parseListBlock(json, options, path, diagnostics, identities) {
|
|
|
1348
1368
|
...metadataFromJson(json.metadata)
|
|
1349
1369
|
};
|
|
1350
1370
|
}
|
|
1371
|
+
function captionFieldFromJson(json, options, path, diagnostics, identities) {
|
|
1372
|
+
const caption = typeof json.caption === "string" ? json.caption : "";
|
|
1373
|
+
return applyImportedInlineIds(
|
|
1374
|
+
createInlineField2(
|
|
1375
|
+
caption,
|
|
1376
|
+
json.caption_math_objects ?? [],
|
|
1377
|
+
options,
|
|
1378
|
+
`${path}.caption`,
|
|
1379
|
+
diagnostics,
|
|
1380
|
+
json.caption_formats ?? []
|
|
1381
|
+
),
|
|
1382
|
+
json.caption_inline_ids,
|
|
1383
|
+
identities
|
|
1384
|
+
);
|
|
1385
|
+
}
|
|
1351
1386
|
function parseTableBlock(json, options, path, diagnostics, identities) {
|
|
1352
1387
|
if (!Array.isArray(json.rows)) {
|
|
1353
1388
|
throw new Error("\\begin{tabular} requires rows array");
|
|
@@ -1374,6 +1409,7 @@ function parseTableBlock(json, options, path, diagnostics, identities) {
|
|
|
1374
1409
|
if (mathObjects.length > 0 && mathObjects.length !== mathObjectIndex) {
|
|
1375
1410
|
pushDiagnostic(diagnostics, options, path, `math_objects count mismatch: detected ${String(mathObjectIndex)}, got ${String(mathObjects.length)}`);
|
|
1376
1411
|
}
|
|
1412
|
+
const floatMeta = parseFloatMetaFromJson(json);
|
|
1377
1413
|
return {
|
|
1378
1414
|
id: blockIdFromJson(json, identities),
|
|
1379
1415
|
kind: "table",
|
|
@@ -1381,13 +1417,15 @@ function parseTableBlock(json, options, path, diagnostics, identities) {
|
|
|
1381
1417
|
closing: "\\end{tabular}",
|
|
1382
1418
|
columns: typeof json.columns === "string" ? json.columns : "",
|
|
1383
1419
|
rows,
|
|
1384
|
-
...
|
|
1420
|
+
...floatMeta,
|
|
1421
|
+
captionField: captionFieldFromJson(json, options, path, diagnostics, identities),
|
|
1385
1422
|
...metadataFromJson(json.metadata)
|
|
1386
1423
|
};
|
|
1387
1424
|
}
|
|
1388
|
-
function parseImageBlock(json, identities) {
|
|
1425
|
+
function parseImageBlock(json, options, path, diagnostics, identities) {
|
|
1389
1426
|
const assetId = typeof json.asset_id === "string" && json.asset_id.length > 0 ? json.asset_id : void 0;
|
|
1390
1427
|
const value = assetId !== void 0 ? typeof json.value === "string" ? json.value : "" : requireString(json.value, "\\includegraphics requires string value");
|
|
1428
|
+
const floatMeta = parseFloatMetaFromJson(json);
|
|
1391
1429
|
return {
|
|
1392
1430
|
id: blockIdFromJson(json, identities),
|
|
1393
1431
|
kind: "image",
|
|
@@ -1395,7 +1433,8 @@ function parseImageBlock(json, identities) {
|
|
|
1395
1433
|
value,
|
|
1396
1434
|
...assetId !== void 0 ? { assetId } : {},
|
|
1397
1435
|
options: isRecordOfStrings(json.options) ? json.options : {},
|
|
1398
|
-
...
|
|
1436
|
+
...floatMeta,
|
|
1437
|
+
captionField: captionFieldFromJson(json, options, path, diagnostics, identities),
|
|
1399
1438
|
...metadataFromJson(json.metadata)
|
|
1400
1439
|
};
|
|
1401
1440
|
}
|
|
@@ -1428,7 +1467,7 @@ function parseBlock(json, options, path, diagnostics, identities) {
|
|
|
1428
1467
|
return parseTableBlock(json, options, path, diagnostics, identities);
|
|
1429
1468
|
}
|
|
1430
1469
|
if (json.command === "\\includegraphics") {
|
|
1431
|
-
return parseImageBlock(json, identities);
|
|
1470
|
+
return parseImageBlock(json, options, path, diagnostics, identities);
|
|
1432
1471
|
}
|
|
1433
1472
|
if (json.command === "\\begin{thebibliography}" || json.command === "\\bibliography") {
|
|
1434
1473
|
return parseBibliographyBlock(json, identities);
|
|
@@ -2980,6 +3019,9 @@ function findField(blocks, fieldId) {
|
|
|
2980
3019
|
if (block.kind === "textBlock" && block.field.id === fieldId) {
|
|
2981
3020
|
return block.field;
|
|
2982
3021
|
}
|
|
3022
|
+
if ((block.kind === "image" || block.kind === "table") && block.captionField.id === fieldId) {
|
|
3023
|
+
return block.captionField;
|
|
3024
|
+
}
|
|
2983
3025
|
if (block.kind === "list") {
|
|
2984
3026
|
for (const item of block.items) {
|
|
2985
3027
|
if (item.field.id === fieldId) {
|
|
@@ -3018,26 +3060,30 @@ function findBlock(blocks, blockId) {
|
|
|
3018
3060
|
}
|
|
3019
3061
|
return null;
|
|
3020
3062
|
}
|
|
3063
|
+
function collectFieldIdentity(field, sets) {
|
|
3064
|
+
sets.fields.add(field.id);
|
|
3065
|
+
field.tokens.forEach((token) => sets.tokens.add(token.id));
|
|
3066
|
+
}
|
|
3021
3067
|
function collectIdentitySets(blocks, sets) {
|
|
3022
3068
|
for (const block of blocks) {
|
|
3023
3069
|
sets.blocks.add(block.id);
|
|
3024
3070
|
if (block.kind === "textBlock") {
|
|
3025
|
-
|
|
3026
|
-
block.field.tokens.forEach((token) => sets.tokens.add(token.id));
|
|
3071
|
+
collectFieldIdentity(block.field, sets);
|
|
3027
3072
|
} else if (block.kind === "list") {
|
|
3028
3073
|
for (const item of block.items) {
|
|
3029
3074
|
sets.items.add(item.id);
|
|
3030
|
-
|
|
3031
|
-
item.field.tokens.forEach((token) => sets.tokens.add(token.id));
|
|
3075
|
+
collectFieldIdentity(item.field, sets);
|
|
3032
3076
|
collectIdentitySets(item.blocks, sets);
|
|
3033
3077
|
}
|
|
3034
3078
|
} else if (block.kind === "table") {
|
|
3079
|
+
collectFieldIdentity(block.captionField, sets);
|
|
3035
3080
|
for (const row of block.rows) {
|
|
3036
3081
|
for (const field of row) {
|
|
3037
|
-
|
|
3038
|
-
field.tokens.forEach((token) => sets.tokens.add(token.id));
|
|
3082
|
+
collectFieldIdentity(field, sets);
|
|
3039
3083
|
}
|
|
3040
3084
|
}
|
|
3085
|
+
} else if (block.kind === "image") {
|
|
3086
|
+
collectFieldIdentity(block.captionField, sets);
|
|
3041
3087
|
}
|
|
3042
3088
|
}
|
|
3043
3089
|
}
|
|
@@ -3372,6 +3418,37 @@ function applyReferenceFields(reference, command) {
|
|
|
3372
3418
|
reference.fieldSeparator = command.field_separator;
|
|
3373
3419
|
}
|
|
3374
3420
|
}
|
|
3421
|
+
function replaceCitationKey(document2, previousKey, nextKey) {
|
|
3422
|
+
function replaceInField(field) {
|
|
3423
|
+
for (const token of field.tokens) {
|
|
3424
|
+
if (token.kind === "cite") {
|
|
3425
|
+
token.keys = token.keys.map((key) => key === previousKey ? nextKey : key);
|
|
3426
|
+
}
|
|
3427
|
+
}
|
|
3428
|
+
}
|
|
3429
|
+
function visitBlocks(blocks) {
|
|
3430
|
+
for (const block of blocks) {
|
|
3431
|
+
if (block.kind === "textBlock") {
|
|
3432
|
+
replaceInField(block.field);
|
|
3433
|
+
} else if (block.kind === "table") {
|
|
3434
|
+
replaceInField(block.captionField);
|
|
3435
|
+
for (const row of block.rows) {
|
|
3436
|
+
for (const cell of row) {
|
|
3437
|
+
replaceInField(cell);
|
|
3438
|
+
}
|
|
3439
|
+
}
|
|
3440
|
+
} else if (block.kind === "image") {
|
|
3441
|
+
replaceInField(block.captionField);
|
|
3442
|
+
} else if (block.kind === "list") {
|
|
3443
|
+
for (const item of block.items) {
|
|
3444
|
+
replaceInField(item.field);
|
|
3445
|
+
visitBlocks(item.blocks);
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3448
|
+
}
|
|
3449
|
+
}
|
|
3450
|
+
visitBlocks(document2.blocks);
|
|
3451
|
+
}
|
|
3375
3452
|
function applyArticleCommand(document2, command) {
|
|
3376
3453
|
if (command.op === "update_document_meta") {
|
|
3377
3454
|
document2.meta = {
|
|
@@ -3400,7 +3477,10 @@ function applyArticleCommand(document2, command) {
|
|
|
3400
3477
|
const target = document2.references[targetIndex];
|
|
3401
3478
|
if (command.op === "update_reference") {
|
|
3402
3479
|
if (command.key !== void 0) {
|
|
3403
|
-
|
|
3480
|
+
const previousKey = target.key;
|
|
3481
|
+
const nextKey = validateReferenceKey(document2, command.key, target.id);
|
|
3482
|
+
target.key = nextKey;
|
|
3483
|
+
replaceCitationKey(document2, previousKey, nextKey);
|
|
3404
3484
|
}
|
|
3405
3485
|
applyReferenceFields(target, command);
|
|
3406
3486
|
return;
|
|
@@ -3829,10 +3909,15 @@ function insertTable(document2, command) {
|
|
|
3829
3909
|
columns: command.columns,
|
|
3830
3910
|
rows: command.rows.map((row) => row.map((cell) => createCommandInlineField(document2, cell))),
|
|
3831
3911
|
...defaultFloatMeta(),
|
|
3832
|
-
...command.caption !== void 0 ? { caption: command.caption, captionEnabled: command.caption.trim().length > 0 } : {},
|
|
3833
3912
|
...command.label !== void 0 ? { label: command.label, labelEnabled: command.label.trim().length > 0 } : {},
|
|
3834
3913
|
...command.metadata ? { metadata: { ...command.metadata } } : {}
|
|
3835
3914
|
};
|
|
3915
|
+
if (command.caption !== void 0) {
|
|
3916
|
+
applyFloatMetaPatch(block, {
|
|
3917
|
+
caption: command.caption,
|
|
3918
|
+
captionEnabled: command.caption.trim().length > 0
|
|
3919
|
+
});
|
|
3920
|
+
}
|
|
3836
3921
|
document2.blocks.splice(blockInsertionIndex(document2, command.anchor), 0, block);
|
|
3837
3922
|
}
|
|
3838
3923
|
function checkCell(table, rowIndex, columnIndex) {
|
|
@@ -3977,6 +4062,15 @@ function fieldJson(field) {
|
|
|
3977
4062
|
...serialized.hasPersistedMath ? { math_objects: serialized.mathObjects } : {}
|
|
3978
4063
|
};
|
|
3979
4064
|
}
|
|
4065
|
+
function captionJson(field) {
|
|
4066
|
+
const serialized = serializeField(field);
|
|
4067
|
+
return {
|
|
4068
|
+
caption: serialized.value,
|
|
4069
|
+
caption_inline_ids: serialized.inlineIds,
|
|
4070
|
+
...serialized.formats.length > 0 ? { caption_formats: serialized.formats } : {},
|
|
4071
|
+
...serialized.hasPersistedMath ? { caption_math_objects: serialized.mathObjects } : {}
|
|
4072
|
+
};
|
|
4073
|
+
}
|
|
3980
4074
|
function listItemJson(item) {
|
|
3981
4075
|
const field = fieldJson(item.field);
|
|
3982
4076
|
return {
|
|
@@ -4021,7 +4115,7 @@ function blockJson(block) {
|
|
|
4021
4115
|
...hasPersistedMath ? { math_objects: fields.flatMap((row) => row.flatMap((field) => field.mathObjects)) } : {},
|
|
4022
4116
|
centered: block.centered,
|
|
4023
4117
|
caption_enabled: block.captionEnabled,
|
|
4024
|
-
|
|
4118
|
+
...captionJson(block.captionField),
|
|
4025
4119
|
label_enabled: block.labelEnabled,
|
|
4026
4120
|
label: block.label,
|
|
4027
4121
|
...block.metadata ? { metadata: { ...block.metadata } } : {}
|
|
@@ -4036,7 +4130,7 @@ function blockJson(block) {
|
|
|
4036
4130
|
options: { ...block.options },
|
|
4037
4131
|
centered: block.centered,
|
|
4038
4132
|
caption_enabled: block.captionEnabled,
|
|
4039
|
-
|
|
4133
|
+
...captionJson(block.captionField),
|
|
4040
4134
|
label_enabled: block.labelEnabled,
|
|
4041
4135
|
label: block.label,
|
|
4042
4136
|
...block.metadata ? { metadata: { ...block.metadata } } : {}
|
|
@@ -4363,10 +4457,15 @@ function applyBasicCommand(document2, command) {
|
|
|
4363
4457
|
assetId: command.asset_id,
|
|
4364
4458
|
options: { width: "0.8\\columnwidth" },
|
|
4365
4459
|
...defaultFloatMeta(),
|
|
4366
|
-
...command.caption !== void 0 ? { caption: command.caption, captionEnabled: command.caption.trim().length > 0 } : {},
|
|
4367
4460
|
...command.label !== void 0 ? { label: command.label, labelEnabled: command.label.trim().length > 0 } : {},
|
|
4368
4461
|
...command.metadata ? { metadata: { ...command.metadata } } : {}
|
|
4369
4462
|
};
|
|
4463
|
+
if (command.caption !== void 0) {
|
|
4464
|
+
applyFloatMetaPatch(block, {
|
|
4465
|
+
caption: command.caption,
|
|
4466
|
+
captionEnabled: command.caption.trim().length > 0
|
|
4467
|
+
});
|
|
4468
|
+
}
|
|
4370
4469
|
document2.blocks.splice(blockInsertionIndex(document2, command.anchor), 0, block);
|
|
4371
4470
|
}
|
|
4372
4471
|
function applyDocument2Command(json, commandInput) {
|
|
@@ -4386,6 +4485,104 @@ function applyDocument2Command(json, commandInput) {
|
|
|
4386
4485
|
return toDocumentJson2(document2);
|
|
4387
4486
|
}
|
|
4388
4487
|
|
|
4488
|
+
// src/document2/diff.ts
|
|
4489
|
+
var import_diff = require("diff");
|
|
4490
|
+
var MAX_TOTAL_TEXT_CHARS = 16e3;
|
|
4491
|
+
var MAX_CHANGED_CHUNK_CHARS = 4e3;
|
|
4492
|
+
var MAX_CONTEXT_CHUNK_CHARS = 600;
|
|
4493
|
+
var TRUNCATION_MARKER = "\n\u2026\n";
|
|
4494
|
+
var IDENTITY_KEYS = /* @__PURE__ */ new Set(["id", "inline_ids", "caption_inline_ids", "cell_inline_ids"]);
|
|
4495
|
+
function comparisonValue(value, key) {
|
|
4496
|
+
if (key && IDENTITY_KEYS.has(key)) {
|
|
4497
|
+
return void 0;
|
|
4498
|
+
}
|
|
4499
|
+
if (Array.isArray(value)) {
|
|
4500
|
+
return value.map((entry) => comparisonValue(entry));
|
|
4501
|
+
}
|
|
4502
|
+
if (value && typeof value === "object") {
|
|
4503
|
+
const result = {};
|
|
4504
|
+
for (const childKey of Object.keys(value).sort()) {
|
|
4505
|
+
const child = comparisonValue(value[childKey], childKey);
|
|
4506
|
+
if (child !== void 0) {
|
|
4507
|
+
result[childKey] = child;
|
|
4508
|
+
}
|
|
4509
|
+
}
|
|
4510
|
+
return result;
|
|
4511
|
+
}
|
|
4512
|
+
return value;
|
|
4513
|
+
}
|
|
4514
|
+
function normalizedComparisonDocument(document2) {
|
|
4515
|
+
return comparisonValue(toDocumentJson2(fromDocumentJson2(document2)));
|
|
4516
|
+
}
|
|
4517
|
+
function withoutFinalNewline(value) {
|
|
4518
|
+
return value.endsWith("\n") ? value.slice(0, -1) : value;
|
|
4519
|
+
}
|
|
4520
|
+
function shorten(text, limit, mode) {
|
|
4521
|
+
if (text.length <= limit) {
|
|
4522
|
+
return { text, truncated: false };
|
|
4523
|
+
}
|
|
4524
|
+
if (limit <= TRUNCATION_MARKER.length) {
|
|
4525
|
+
return { text: text.slice(0, Math.max(0, limit)), truncated: true };
|
|
4526
|
+
}
|
|
4527
|
+
const available = limit - TRUNCATION_MARKER.length;
|
|
4528
|
+
if (mode === "head") {
|
|
4529
|
+
return { text: `${text.slice(0, available)}${TRUNCATION_MARKER}`, truncated: true };
|
|
4530
|
+
}
|
|
4531
|
+
if (mode === "tail") {
|
|
4532
|
+
return { text: `${TRUNCATION_MARKER}${text.slice(text.length - available)}`, truncated: true };
|
|
4533
|
+
}
|
|
4534
|
+
const head = Math.ceil(available / 2);
|
|
4535
|
+
const tail = available - head;
|
|
4536
|
+
return {
|
|
4537
|
+
text: `${text.slice(0, head)}${TRUNCATION_MARKER}${text.slice(text.length - tail)}`,
|
|
4538
|
+
truncated: true
|
|
4539
|
+
};
|
|
4540
|
+
}
|
|
4541
|
+
function diffDocument2(before, after) {
|
|
4542
|
+
const beforeComparable = normalizedComparisonDocument(before);
|
|
4543
|
+
const afterComparable = normalizedComparisonDocument(after);
|
|
4544
|
+
const parts = (0, import_diff.diffJson)(beforeComparable, afterComparable);
|
|
4545
|
+
const changed = parts.some((part) => part.added === true || part.removed === true);
|
|
4546
|
+
if (!changed) {
|
|
4547
|
+
return { version: 1, changed: false, truncated: false, chunks: [] };
|
|
4548
|
+
}
|
|
4549
|
+
const chunks = [];
|
|
4550
|
+
let truncated = false;
|
|
4551
|
+
let totalTextChars = 0;
|
|
4552
|
+
const append = (kind, rawText, limit, mode) => {
|
|
4553
|
+
const cleaned = withoutFinalNewline(rawText);
|
|
4554
|
+
if (cleaned.length === 0) return;
|
|
4555
|
+
const perChunk = shorten(cleaned, limit, mode);
|
|
4556
|
+
truncated ||= perChunk.truncated;
|
|
4557
|
+
const remaining = MAX_TOTAL_TEXT_CHARS - totalTextChars;
|
|
4558
|
+
if (remaining <= 0) {
|
|
4559
|
+
truncated = true;
|
|
4560
|
+
return;
|
|
4561
|
+
}
|
|
4562
|
+
const withinBudget = shorten(perChunk.text, remaining, mode);
|
|
4563
|
+
truncated ||= withinBudget.truncated;
|
|
4564
|
+
if (withinBudget.text.length === 0) return;
|
|
4565
|
+
chunks.push({ kind, text: withinBudget.text });
|
|
4566
|
+
totalTextChars += withinBudget.text.length;
|
|
4567
|
+
};
|
|
4568
|
+
parts.forEach((part, index) => {
|
|
4569
|
+
if (part.added) {
|
|
4570
|
+
append("added", part.value, MAX_CHANGED_CHUNK_CHARS, "both");
|
|
4571
|
+
return;
|
|
4572
|
+
}
|
|
4573
|
+
if (part.removed) {
|
|
4574
|
+
append("removed", part.value, MAX_CHANGED_CHUNK_CHARS, "both");
|
|
4575
|
+
return;
|
|
4576
|
+
}
|
|
4577
|
+
const previousChanged = index > 0 && (parts[index - 1]?.added === true || parts[index - 1]?.removed === true);
|
|
4578
|
+
const nextChanged = index + 1 < parts.length && (parts[index + 1]?.added === true || parts[index + 1]?.removed === true);
|
|
4579
|
+
if (!previousChanged && !nextChanged) return;
|
|
4580
|
+
const mode = previousChanged && nextChanged ? "both" : previousChanged ? "head" : "tail";
|
|
4581
|
+
append("context", part.value, MAX_CONTEXT_CHUNK_CHARS, mode);
|
|
4582
|
+
});
|
|
4583
|
+
return { version: 1, changed: true, truncated, chunks };
|
|
4584
|
+
}
|
|
4585
|
+
|
|
4389
4586
|
// src/document2/outline.ts
|
|
4390
4587
|
var EXCERPT_LIMIT = 160;
|
|
4391
4588
|
function normalizeExcerpt(value) {
|
|
@@ -4460,6 +4657,119 @@ function document2Outline(json) {
|
|
|
4460
4657
|
});
|
|
4461
4658
|
}
|
|
4462
4659
|
|
|
4660
|
+
// src/document2/introspection.ts
|
|
4661
|
+
function appendUnique(target, seen, values) {
|
|
4662
|
+
for (const value of values) {
|
|
4663
|
+
if (!seen.has(value)) {
|
|
4664
|
+
seen.add(value);
|
|
4665
|
+
target.push(value);
|
|
4666
|
+
}
|
|
4667
|
+
}
|
|
4668
|
+
}
|
|
4669
|
+
function getDocument2References(document2) {
|
|
4670
|
+
return document2.references.map((reference) => ({
|
|
4671
|
+
key: reference.key,
|
|
4672
|
+
authors: reference.authors,
|
|
4673
|
+
title: reference.title,
|
|
4674
|
+
year: reference.year,
|
|
4675
|
+
venue: reference.venue,
|
|
4676
|
+
url: reference.url,
|
|
4677
|
+
field_separator: reference.fieldSeparator
|
|
4678
|
+
}));
|
|
4679
|
+
}
|
|
4680
|
+
function getDocument2ReferenceIndex(document2) {
|
|
4681
|
+
const referenceKeys = new Set(document2.references.map((reference) => reference.key));
|
|
4682
|
+
const labelEntries = collectDocument2Labels(document2);
|
|
4683
|
+
const labelKeys = new Set(labelEntries.map((entry) => entry.key));
|
|
4684
|
+
const citations = [];
|
|
4685
|
+
const crossReferences = [];
|
|
4686
|
+
const tokenLocations = /* @__PURE__ */ new Map();
|
|
4687
|
+
const unresolvedCitationKeys = [];
|
|
4688
|
+
const unresolvedCrossReferenceKeys = [];
|
|
4689
|
+
const seenCitationKeys = /* @__PURE__ */ new Set();
|
|
4690
|
+
const seenCrossReferenceKeys = /* @__PURE__ */ new Set();
|
|
4691
|
+
function visitField(blockId, field) {
|
|
4692
|
+
for (const token of field.tokens) {
|
|
4693
|
+
tokenLocations.set(token.id, { block_id: blockId, field_id: field.id });
|
|
4694
|
+
if (token.kind === "cite") {
|
|
4695
|
+
const unresolvedKeys = token.keys.filter((key) => !referenceKeys.has(key));
|
|
4696
|
+
citations.push({
|
|
4697
|
+
kind: "cite",
|
|
4698
|
+
block_id: blockId,
|
|
4699
|
+
field_id: field.id,
|
|
4700
|
+
token_id: token.id,
|
|
4701
|
+
keys: [...token.keys],
|
|
4702
|
+
unresolved_keys: unresolvedKeys
|
|
4703
|
+
});
|
|
4704
|
+
appendUnique(unresolvedCitationKeys, seenCitationKeys, unresolvedKeys);
|
|
4705
|
+
} else if (token.kind === "ref") {
|
|
4706
|
+
const unresolvedKeys = token.keys.filter((key) => !labelKeys.has(key));
|
|
4707
|
+
crossReferences.push({
|
|
4708
|
+
kind: "ref",
|
|
4709
|
+
ref_command: token.refCommand,
|
|
4710
|
+
block_id: blockId,
|
|
4711
|
+
field_id: field.id,
|
|
4712
|
+
token_id: token.id,
|
|
4713
|
+
keys: [...token.keys],
|
|
4714
|
+
unresolved_keys: unresolvedKeys
|
|
4715
|
+
});
|
|
4716
|
+
appendUnique(unresolvedCrossReferenceKeys, seenCrossReferenceKeys, unresolvedKeys);
|
|
4717
|
+
}
|
|
4718
|
+
}
|
|
4719
|
+
}
|
|
4720
|
+
function visitBlocks(blocks) {
|
|
4721
|
+
for (const block of blocks) {
|
|
4722
|
+
if (block.kind === "textBlock") {
|
|
4723
|
+
visitField(block.id, block.field);
|
|
4724
|
+
} else if (block.kind === "table") {
|
|
4725
|
+
visitField(block.id, block.captionField);
|
|
4726
|
+
for (const row of block.rows) {
|
|
4727
|
+
for (const cell of row) {
|
|
4728
|
+
visitField(block.id, cell);
|
|
4729
|
+
}
|
|
4730
|
+
}
|
|
4731
|
+
} else if (block.kind === "image") {
|
|
4732
|
+
visitField(block.id, block.captionField);
|
|
4733
|
+
} else if (block.kind === "list") {
|
|
4734
|
+
for (const item of block.items) {
|
|
4735
|
+
visitField(block.id, item.field);
|
|
4736
|
+
visitBlocks(item.blocks);
|
|
4737
|
+
}
|
|
4738
|
+
}
|
|
4739
|
+
}
|
|
4740
|
+
}
|
|
4741
|
+
visitBlocks(document2.blocks);
|
|
4742
|
+
const labels = labelEntries.map((entry) => {
|
|
4743
|
+
if (entry.kind !== "eq") {
|
|
4744
|
+
return {
|
|
4745
|
+
key: entry.key,
|
|
4746
|
+
kind: entry.kind,
|
|
4747
|
+
caption: entry.caption,
|
|
4748
|
+
number: entry.number,
|
|
4749
|
+
block_id: entry.ownerId
|
|
4750
|
+
};
|
|
4751
|
+
}
|
|
4752
|
+
const location = tokenLocations.get(entry.ownerId);
|
|
4753
|
+
return {
|
|
4754
|
+
key: entry.key,
|
|
4755
|
+
kind: entry.kind,
|
|
4756
|
+
caption: entry.caption,
|
|
4757
|
+
number: entry.number,
|
|
4758
|
+
...location ? { block_id: location.block_id, field_id: location.field_id } : {},
|
|
4759
|
+
token_id: entry.ownerId
|
|
4760
|
+
};
|
|
4761
|
+
});
|
|
4762
|
+
return {
|
|
4763
|
+
citations,
|
|
4764
|
+
cross_references: crossReferences,
|
|
4765
|
+
labels,
|
|
4766
|
+
unresolved: {
|
|
4767
|
+
citation_keys: unresolvedCitationKeys,
|
|
4768
|
+
cross_reference_keys: unresolvedCrossReferenceKeys
|
|
4769
|
+
}
|
|
4770
|
+
};
|
|
4771
|
+
}
|
|
4772
|
+
|
|
4463
4773
|
// src/document2/arabicPreamble.ts
|
|
4464
4774
|
function fontOptions(digitsMapping, includeScript = true) {
|
|
4465
4775
|
const parts = [];
|
|
@@ -4827,11 +5137,12 @@ ${block.closing}`;
|
|
|
4827
5137
|
${rows.join(" \\\\\n")}
|
|
4828
5138
|
${block.closing}`;
|
|
4829
5139
|
}
|
|
4830
|
-
function floatCaptionLabelLines(block, counter) {
|
|
5140
|
+
function floatCaptionLabelLines(block, counter, labelKinds) {
|
|
4831
5141
|
const lines = [];
|
|
4832
|
-
const
|
|
5142
|
+
const caption = inlineFieldLatexWithLabelKinds(block.captionField, labelKinds).trim();
|
|
5143
|
+
const hasCaption = block.captionEnabled && caption.length > 0;
|
|
4833
5144
|
if (hasCaption) {
|
|
4834
|
-
lines.push(` \\caption{${
|
|
5145
|
+
lines.push(` \\caption{${caption}}`);
|
|
4835
5146
|
}
|
|
4836
5147
|
if (block.labelEnabled && block.label.trim().length > 0) {
|
|
4837
5148
|
if (!hasCaption) {
|
|
@@ -4847,7 +5158,7 @@ function tableBlockLatex(block, labelKinds, fixedFloats) {
|
|
|
4847
5158
|
if (block.centered) {
|
|
4848
5159
|
lines.push(" \\centering");
|
|
4849
5160
|
}
|
|
4850
|
-
lines.push(...floatCaptionLabelLines(block, "table"));
|
|
5161
|
+
lines.push(...floatCaptionLabelLines(block, "table", labelKinds));
|
|
4851
5162
|
lines.push(indent(tabularInnerLatex(block, labelKinds)));
|
|
4852
5163
|
lines.push("\\end{table}");
|
|
4853
5164
|
return lines.join("\n");
|
|
@@ -4867,14 +5178,14 @@ function imageOptionsLatex(block, constrainToColumn) {
|
|
|
4867
5178
|
}
|
|
4868
5179
|
return `[${entries.map(([key, value]) => `${key}=${value}`).join(",")}]`;
|
|
4869
5180
|
}
|
|
4870
|
-
function imageBlockLatex(block, fixedFloats) {
|
|
5181
|
+
function imageBlockLatex(block, labelKinds, fixedFloats) {
|
|
4871
5182
|
const placement = fixedFloats ? "H" : "htbp";
|
|
4872
5183
|
const lines = [`\\begin{figure}[${placement}]`];
|
|
4873
5184
|
if (block.centered) {
|
|
4874
5185
|
lines.push(" \\centering");
|
|
4875
5186
|
}
|
|
4876
5187
|
lines.push(` ${block.command}${imageOptionsLatex(block, true)}{${block.value}}`);
|
|
4877
|
-
lines.push(...floatCaptionLabelLines(block, "figure"));
|
|
5188
|
+
lines.push(...floatCaptionLabelLines(block, "figure", labelKinds));
|
|
4878
5189
|
lines.push("\\end{figure}");
|
|
4879
5190
|
return lines.join("\n");
|
|
4880
5191
|
}
|
|
@@ -4889,7 +5200,7 @@ function blockLatex(block, references, labelKinds, fixedFloats) {
|
|
|
4889
5200
|
return tableBlockLatex(block, labelKinds, fixedFloats);
|
|
4890
5201
|
}
|
|
4891
5202
|
if (block.kind === "image") {
|
|
4892
|
-
return imageBlockLatex(block, fixedFloats);
|
|
5203
|
+
return imageBlockLatex(block, labelKinds, fixedFloats);
|
|
4893
5204
|
}
|
|
4894
5205
|
if (block.kind === "bibliography") {
|
|
4895
5206
|
const width = String(Math.max(references.length, 9));
|
|
@@ -5082,7 +5393,7 @@ function validateBlock(context, block, path) {
|
|
|
5082
5393
|
);
|
|
5083
5394
|
});
|
|
5084
5395
|
});
|
|
5085
|
-
|
|
5396
|
+
validateInlineField(context, block.captionField, `${path}.caption`, block.id);
|
|
5086
5397
|
if (block.labelEnabled) {
|
|
5087
5398
|
registerKey(context, block.label, `${path}.label`, "tab", block.id);
|
|
5088
5399
|
}
|
|
@@ -5095,7 +5406,7 @@ function validateBlock(context, block, path) {
|
|
|
5095
5406
|
addIssue(context, "empty_image", path, block.id);
|
|
5096
5407
|
}
|
|
5097
5408
|
validateTextValue(context, block.value, `${path}.value`, block.id);
|
|
5098
|
-
|
|
5409
|
+
validateInlineField(context, block.captionField, `${path}.caption`, block.id);
|
|
5099
5410
|
if (block.labelEnabled) {
|
|
5100
5411
|
registerKey(context, block.label, `${path}.label`, "fig", block.id);
|
|
5101
5412
|
}
|
|
@@ -5279,10 +5590,16 @@ function parseDocument2WorkerRequest(value) {
|
|
|
5279
5590
|
if (!isObject4(value) || typeof value.action !== "string") {
|
|
5280
5591
|
throw new Document2WorkerProtocolError("invalid_request", "Worker request requires an action");
|
|
5281
5592
|
}
|
|
5593
|
+
if (value.action === "diff") {
|
|
5594
|
+
if (!("before" in value) || !("after" in value)) {
|
|
5595
|
+
throw new Document2WorkerProtocolError("invalid_request", "diff requires before and after documents");
|
|
5596
|
+
}
|
|
5597
|
+
return { action: "diff", before: value.before, after: value.after };
|
|
5598
|
+
}
|
|
5282
5599
|
if (!("document" in value)) {
|
|
5283
5600
|
throw new Document2WorkerProtocolError("invalid_request", "Worker request requires a document");
|
|
5284
5601
|
}
|
|
5285
|
-
if (value.action === "normalize" || value.action === "outline" || value.action === "export") {
|
|
5602
|
+
if (value.action === "normalize" || value.action === "outline" || value.action === "references" || value.action === "reference_index" || value.action === "export") {
|
|
5286
5603
|
return { action: value.action, document: value.document };
|
|
5287
5604
|
}
|
|
5288
5605
|
if (value.action === "apply_command") {
|
|
@@ -5389,9 +5706,25 @@ function executeDocument2WorkerRequest(input) {
|
|
|
5389
5706
|
if (request.action === "normalize") {
|
|
5390
5707
|
return { ok: true, document: normalizeDocument(request.document) };
|
|
5391
5708
|
}
|
|
5709
|
+
if (request.action === "diff") {
|
|
5710
|
+
try {
|
|
5711
|
+
return {
|
|
5712
|
+
ok: true,
|
|
5713
|
+
diff: diffDocument2(asDocument2Json(request.before), asDocument2Json(request.after))
|
|
5714
|
+
};
|
|
5715
|
+
} catch {
|
|
5716
|
+
throw new Document2CommandError("invalid_document", "Document JSON is invalid");
|
|
5717
|
+
}
|
|
5718
|
+
}
|
|
5392
5719
|
if (request.action === "outline") {
|
|
5393
5720
|
return { ok: true, outline: document2Outline(asDocument2Json(request.document)) };
|
|
5394
5721
|
}
|
|
5722
|
+
if (request.action === "references") {
|
|
5723
|
+
return { ok: true, references: getDocument2References(resolveDocument2(request.document)) };
|
|
5724
|
+
}
|
|
5725
|
+
if (request.action === "reference_index") {
|
|
5726
|
+
return { ok: true, reference_index: getDocument2ReferenceIndex(resolveDocument2(request.document)) };
|
|
5727
|
+
}
|
|
5395
5728
|
if (request.action === "export") {
|
|
5396
5729
|
const document2 = resolveDocument2(request.document);
|
|
5397
5730
|
return {
|
|
@@ -5531,9 +5864,18 @@ function actionForPath(pathname) {
|
|
|
5531
5864
|
if (pathname === "/v1/document2/outline") {
|
|
5532
5865
|
return "outline";
|
|
5533
5866
|
}
|
|
5867
|
+
if (pathname === "/v1/document2/references") {
|
|
5868
|
+
return "references";
|
|
5869
|
+
}
|
|
5870
|
+
if (pathname === "/v1/document2/reference-index") {
|
|
5871
|
+
return "reference_index";
|
|
5872
|
+
}
|
|
5534
5873
|
if (pathname === "/v1/document2/export") {
|
|
5535
5874
|
return "export";
|
|
5536
5875
|
}
|
|
5876
|
+
if (pathname === "/v1/document2/diff") {
|
|
5877
|
+
return "diff";
|
|
5878
|
+
}
|
|
5537
5879
|
if (pathname === "/v1/document2/commands") {
|
|
5538
5880
|
return "apply_command";
|
|
5539
5881
|
}
|