@drghaliasri/butex 7.0.0 → 7.0.2
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 +2 -0
- package/dist/document2-cli.js +530 -1
- package/dist/document2-cli.js.map +1 -1
- package/dist/document2.d.mts +61 -2
- package/dist/document2.d.ts +61 -2
- package/dist/document2.js +454 -47
- package/dist/document2.js.map +1 -1
- package/dist/document2.mjs +454 -47
- package/dist/document2.mjs.map +1 -1
- package/dist/react-document2.js +46 -46
- package/dist/react-document2.js.map +1 -1
- package/dist/react-document2.mjs +46 -46
- package/dist/react-document2.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -298,6 +298,8 @@ const updated = applyDocument2Command(canonical, {
|
|
|
298
298
|
|
|
299
299
|
Block insertions accept `{ end: true }`, `{ before_block_id }`, or `{ after_block_id }`; missing targets fail explicitly. The command union also includes stable-ID inline token operations, list/item operations, and table cell/row/column operations. List and table targets are found recursively, while row and column coordinates are zero-based. Column shape changes require the complete resulting LaTeX `columns` specification.
|
|
300
300
|
|
|
301
|
+
Article authoring commands can move top-level blocks, patch `meta`, manage the ordered reference catalog by normalized key, insert the derived bibliography block, revise figure/table captions and labels, and update an existing figure's host asset association or include value. Reference anchors use `{ before_reference_key }`, `{ after_reference_key }`, or `{ end: true }`. Asset upload/storage and session revisions remain host-owned; see [`docs/fastapi-document2-worker-contract.md`](docs/fastapi-document2-worker-contract.md) for the complete command and error contract.
|
|
302
|
+
|
|
301
303
|
Whole-field replacement rejects formatted text, citations, references, and math so an agent cannot silently discard structured content. Inline math insertion/replacement requires one complete delimited `source` plus a matching structured `MathObject`; BuTeX does not parse raw LaTeX into an equation AST.
|
|
302
304
|
|
|
303
305
|
### Document worker CLI
|
package/dist/document2-cli.js
CHANGED
|
@@ -587,6 +587,86 @@ function refTokenLatex(keys, refCommand) {
|
|
|
587
587
|
const command = refCommand === "eqref" ? "\\eqref" : "\\ref";
|
|
588
588
|
return `${command}{${keys.join(",")}}`;
|
|
589
589
|
}
|
|
590
|
+
function collectDocument2Labels(document2) {
|
|
591
|
+
const entries = [];
|
|
592
|
+
const counters = { fig: 0, tab: 0, eq: 0 };
|
|
593
|
+
function pushEqFromField(tokens) {
|
|
594
|
+
for (const token of tokens) {
|
|
595
|
+
if (token.kind !== "math") {
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
if (token.display && token.labelEnabled && token.label && token.label.trim().length > 0) {
|
|
599
|
+
counters.eq += 1;
|
|
600
|
+
entries.push({
|
|
601
|
+
key: token.label.trim(),
|
|
602
|
+
kind: "eq",
|
|
603
|
+
caption: "",
|
|
604
|
+
ownerId: token.id,
|
|
605
|
+
number: counters.eq
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
function walk(blocks) {
|
|
611
|
+
for (const block of blocks) {
|
|
612
|
+
if (block.kind === "image" && block.labelEnabled && block.label.trim().length > 0) {
|
|
613
|
+
counters.fig += 1;
|
|
614
|
+
entries.push({
|
|
615
|
+
key: block.label.trim(),
|
|
616
|
+
kind: "fig",
|
|
617
|
+
caption: block.captionEnabled ? block.caption : "",
|
|
618
|
+
ownerId: block.id,
|
|
619
|
+
number: counters.fig
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
if (block.kind === "table") {
|
|
623
|
+
if (block.labelEnabled && block.label.trim().length > 0) {
|
|
624
|
+
counters.tab += 1;
|
|
625
|
+
entries.push({
|
|
626
|
+
key: block.label.trim(),
|
|
627
|
+
kind: "tab",
|
|
628
|
+
caption: block.captionEnabled ? block.caption : "",
|
|
629
|
+
ownerId: block.id,
|
|
630
|
+
number: counters.tab
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
for (const row of block.rows) {
|
|
634
|
+
for (const cell of row) {
|
|
635
|
+
pushEqFromField(cell.tokens);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
if (block.kind === "textBlock") {
|
|
640
|
+
pushEqFromField(block.field.tokens);
|
|
641
|
+
}
|
|
642
|
+
if (block.kind === "list") {
|
|
643
|
+
for (const item of block.items) {
|
|
644
|
+
pushEqFromField(item.field.tokens);
|
|
645
|
+
walk(item.blocks);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
walk(document2.blocks);
|
|
651
|
+
return entries;
|
|
652
|
+
}
|
|
653
|
+
function applyFloatMetaPatch(block, patch) {
|
|
654
|
+
if (typeof patch.centered === "boolean") {
|
|
655
|
+
block.centered = patch.centered;
|
|
656
|
+
}
|
|
657
|
+
if (typeof patch.captionEnabled === "boolean") {
|
|
658
|
+
block.captionEnabled = patch.captionEnabled;
|
|
659
|
+
}
|
|
660
|
+
if (typeof patch.caption === "string") {
|
|
661
|
+
block.caption = patch.caption;
|
|
662
|
+
}
|
|
663
|
+
if (typeof patch.labelEnabled === "boolean") {
|
|
664
|
+
block.labelEnabled = patch.labelEnabled;
|
|
665
|
+
}
|
|
666
|
+
if (typeof patch.label === "string") {
|
|
667
|
+
block.label = patch.label;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
590
670
|
|
|
591
671
|
// src/document2/inlineIdentity.ts
|
|
592
672
|
function createDocument2IdentityState() {
|
|
@@ -2578,6 +2658,12 @@ function requireBoolean(value, field) {
|
|
|
2578
2658
|
}
|
|
2579
2659
|
return value;
|
|
2580
2660
|
}
|
|
2661
|
+
function optionalBoolean(value, field) {
|
|
2662
|
+
if (!(field in value)) {
|
|
2663
|
+
return void 0;
|
|
2664
|
+
}
|
|
2665
|
+
return requireBoolean(value[field], field);
|
|
2666
|
+
}
|
|
2581
2667
|
function requireIndex(value, field) {
|
|
2582
2668
|
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
|
|
2583
2669
|
throw new Document2CommandError("invalid_command", `${field} must be a non-negative integer`);
|
|
@@ -2808,6 +2894,297 @@ function compactTextTokens2(tokens, emptyTokenId = document2Id("text")) {
|
|
|
2808
2894
|
return compacted.length > 0 ? compacted : [{ id: emptyTokenId, kind: "text", text: "" }];
|
|
2809
2895
|
}
|
|
2810
2896
|
|
|
2897
|
+
// src/document2/keys.ts
|
|
2898
|
+
var DOCUMENT2_KEY_PATTERN = /^[\p{L}\p{M}\p{Nd}:._-]+$/u;
|
|
2899
|
+
function normalizeDocument2Key(key) {
|
|
2900
|
+
return key.normalize("NFC").trim();
|
|
2901
|
+
}
|
|
2902
|
+
function isDuplicateDocument2Key(key, namespace) {
|
|
2903
|
+
const normalized = normalizeDocument2Key(key);
|
|
2904
|
+
if (normalized.length === 0) {
|
|
2905
|
+
return false;
|
|
2906
|
+
}
|
|
2907
|
+
for (const reference of namespace.references ?? []) {
|
|
2908
|
+
if (namespace.excludeReferenceId && reference.id === namespace.excludeReferenceId) {
|
|
2909
|
+
continue;
|
|
2910
|
+
}
|
|
2911
|
+
if (normalizeDocument2Key(reference.key) === normalized) {
|
|
2912
|
+
return true;
|
|
2913
|
+
}
|
|
2914
|
+
}
|
|
2915
|
+
for (const label of namespace.labels ?? []) {
|
|
2916
|
+
if (namespace.excludeOwnerId && label.ownerId === namespace.excludeOwnerId) {
|
|
2917
|
+
continue;
|
|
2918
|
+
}
|
|
2919
|
+
if (normalizeDocument2Key(label.key) === normalized) {
|
|
2920
|
+
return true;
|
|
2921
|
+
}
|
|
2922
|
+
}
|
|
2923
|
+
return false;
|
|
2924
|
+
}
|
|
2925
|
+
function document2KeyError(key, namespace = {}) {
|
|
2926
|
+
const normalized = normalizeDocument2Key(key);
|
|
2927
|
+
if (normalized.length === 0) {
|
|
2928
|
+
return "empty";
|
|
2929
|
+
}
|
|
2930
|
+
if (!DOCUMENT2_KEY_PATTERN.test(normalized)) {
|
|
2931
|
+
return "invalid";
|
|
2932
|
+
}
|
|
2933
|
+
if (isDuplicateDocument2Key(normalized, namespace)) {
|
|
2934
|
+
return "duplicate";
|
|
2935
|
+
}
|
|
2936
|
+
return null;
|
|
2937
|
+
}
|
|
2938
|
+
|
|
2939
|
+
// src/document2/articleJsonCommands.ts
|
|
2940
|
+
var REFERENCE_TEXT_FIELDS = ["authors", "title", "year", "venue", "url"];
|
|
2941
|
+
function hasOwn(value, field) {
|
|
2942
|
+
return Object.prototype.hasOwnProperty.call(value, field);
|
|
2943
|
+
}
|
|
2944
|
+
function parseDatePatch(value) {
|
|
2945
|
+
if (!isObject3(value)) {
|
|
2946
|
+
throw new Document2CommandError("invalid_command", "date must be an object");
|
|
2947
|
+
}
|
|
2948
|
+
const patch = {};
|
|
2949
|
+
if (hasOwn(value, "day")) {
|
|
2950
|
+
if (typeof value.day !== "number" || !Number.isInteger(value.day) || value.day < 1 || value.day > 30) {
|
|
2951
|
+
throw new Document2CommandError("invalid_command", "date.day must be an integer from 1 to 30");
|
|
2952
|
+
}
|
|
2953
|
+
patch.day = value.day;
|
|
2954
|
+
}
|
|
2955
|
+
if (hasOwn(value, "month")) {
|
|
2956
|
+
if (typeof value.month !== "string" || !HIJRI_MONTH_IDS.includes(value.month)) {
|
|
2957
|
+
throw new Document2CommandError("invalid_command", "date.month must be a supported Hijri month");
|
|
2958
|
+
}
|
|
2959
|
+
patch.month = value.month;
|
|
2960
|
+
}
|
|
2961
|
+
if (hasOwn(value, "year")) {
|
|
2962
|
+
if (typeof value.year !== "number" || !Number.isInteger(value.year) || value.year < HIJRI_YEAR_MIN || value.year > HIJRI_YEAR_MAX) {
|
|
2963
|
+
throw new Document2CommandError(
|
|
2964
|
+
"invalid_command",
|
|
2965
|
+
`date.year must be an integer from ${String(HIJRI_YEAR_MIN)} to ${String(HIJRI_YEAR_MAX)}`
|
|
2966
|
+
);
|
|
2967
|
+
}
|
|
2968
|
+
patch.year = value.year;
|
|
2969
|
+
}
|
|
2970
|
+
if (Object.keys(patch).length === 0) {
|
|
2971
|
+
throw new Document2CommandError("invalid_command", "date requires at least one field");
|
|
2972
|
+
}
|
|
2973
|
+
return patch;
|
|
2974
|
+
}
|
|
2975
|
+
function parseReferenceSeparator(value) {
|
|
2976
|
+
if (!hasOwn(value, "field_separator")) {
|
|
2977
|
+
return void 0;
|
|
2978
|
+
}
|
|
2979
|
+
if (value.field_separator !== "," && value.field_separator !== "\u060C") {
|
|
2980
|
+
throw new Document2CommandError("invalid_command", "field_separator must be , or \u060C");
|
|
2981
|
+
}
|
|
2982
|
+
return value.field_separator;
|
|
2983
|
+
}
|
|
2984
|
+
function parseReferenceAnchor(value) {
|
|
2985
|
+
if (!isObject3(value)) {
|
|
2986
|
+
throw new Document2CommandError("invalid_anchor", "anchor must be an object");
|
|
2987
|
+
}
|
|
2988
|
+
const before = typeof value.before_reference_key === "string" ? normalizeDocument2Key(value.before_reference_key) : "";
|
|
2989
|
+
const after = typeof value.after_reference_key === "string" ? normalizeDocument2Key(value.after_reference_key) : "";
|
|
2990
|
+
const choices = Number(before.length > 0) + Number(after.length > 0) + Number(value.end === true);
|
|
2991
|
+
if (choices !== 1) {
|
|
2992
|
+
throw new Document2CommandError("invalid_anchor", "anchor requires exactly one reference position");
|
|
2993
|
+
}
|
|
2994
|
+
if (before) {
|
|
2995
|
+
return { before_reference_key: before };
|
|
2996
|
+
}
|
|
2997
|
+
if (after) {
|
|
2998
|
+
return { after_reference_key: after };
|
|
2999
|
+
}
|
|
3000
|
+
return { end: true };
|
|
3001
|
+
}
|
|
3002
|
+
function parseUpdateDocumentMeta(value) {
|
|
3003
|
+
const title = optionalString(value, "title");
|
|
3004
|
+
const authors = optionalString(value, "authors");
|
|
3005
|
+
const abstract = optionalString(value, "abstract");
|
|
3006
|
+
const date = hasOwn(value, "date") ? parseDatePatch(value.date) : void 0;
|
|
3007
|
+
if (title === void 0 && authors === void 0 && abstract === void 0 && date === void 0) {
|
|
3008
|
+
throw new Document2CommandError("invalid_command", "update_document_meta requires at least one field");
|
|
3009
|
+
}
|
|
3010
|
+
return {
|
|
3011
|
+
op: "update_document_meta",
|
|
3012
|
+
...title !== void 0 ? { title } : {},
|
|
3013
|
+
...authors !== void 0 ? { authors } : {},
|
|
3014
|
+
...abstract !== void 0 ? { abstract } : {},
|
|
3015
|
+
...date !== void 0 ? { date } : {}
|
|
3016
|
+
};
|
|
3017
|
+
}
|
|
3018
|
+
function referenceFields(value) {
|
|
3019
|
+
const fields = {};
|
|
3020
|
+
for (const field of REFERENCE_TEXT_FIELDS) {
|
|
3021
|
+
const parsed = optionalString(value, field);
|
|
3022
|
+
if (parsed !== void 0) {
|
|
3023
|
+
fields[field] = parsed;
|
|
3024
|
+
}
|
|
3025
|
+
}
|
|
3026
|
+
const separator = parseReferenceSeparator(value);
|
|
3027
|
+
if (separator !== void 0) {
|
|
3028
|
+
fields.field_separator = separator;
|
|
3029
|
+
}
|
|
3030
|
+
return fields;
|
|
3031
|
+
}
|
|
3032
|
+
function parseInsertReference(value) {
|
|
3033
|
+
const key = requireString2(value.key, "key", false);
|
|
3034
|
+
return {
|
|
3035
|
+
op: "insert_reference",
|
|
3036
|
+
key,
|
|
3037
|
+
...referenceFields(value),
|
|
3038
|
+
anchor: parseReferenceAnchor(value.anchor)
|
|
3039
|
+
};
|
|
3040
|
+
}
|
|
3041
|
+
function parseUpdateReference(value) {
|
|
3042
|
+
const nextKey = optionalString(value, "key");
|
|
3043
|
+
const fields = referenceFields(value);
|
|
3044
|
+
if (nextKey === void 0 && Object.keys(fields).length === 0) {
|
|
3045
|
+
throw new Document2CommandError("invalid_command", "update_reference requires at least one field");
|
|
3046
|
+
}
|
|
3047
|
+
return {
|
|
3048
|
+
op: "update_reference",
|
|
3049
|
+
reference_key: requireString2(value.reference_key, "reference_key", false),
|
|
3050
|
+
...nextKey !== void 0 ? { key: nextKey } : {},
|
|
3051
|
+
...fields
|
|
3052
|
+
};
|
|
3053
|
+
}
|
|
3054
|
+
function parseArticleCommand(value) {
|
|
3055
|
+
if (value.op === "update_document_meta") {
|
|
3056
|
+
return parseUpdateDocumentMeta(value);
|
|
3057
|
+
}
|
|
3058
|
+
if (value.op === "insert_reference") {
|
|
3059
|
+
return parseInsertReference(value);
|
|
3060
|
+
}
|
|
3061
|
+
if (value.op === "update_reference") {
|
|
3062
|
+
return parseUpdateReference(value);
|
|
3063
|
+
}
|
|
3064
|
+
if (value.op === "remove_reference") {
|
|
3065
|
+
return {
|
|
3066
|
+
op: value.op,
|
|
3067
|
+
reference_key: requireString2(value.reference_key, "reference_key", false)
|
|
3068
|
+
};
|
|
3069
|
+
}
|
|
3070
|
+
if (value.op === "move_reference") {
|
|
3071
|
+
return {
|
|
3072
|
+
op: value.op,
|
|
3073
|
+
reference_key: requireString2(value.reference_key, "reference_key", false),
|
|
3074
|
+
anchor: parseReferenceAnchor(value.anchor)
|
|
3075
|
+
};
|
|
3076
|
+
}
|
|
3077
|
+
throw new Document2CommandError("invalid_command", "Unsupported article command");
|
|
3078
|
+
}
|
|
3079
|
+
function referenceIndexes(document2, key) {
|
|
3080
|
+
const normalized = normalizeDocument2Key(key);
|
|
3081
|
+
const indexes = [];
|
|
3082
|
+
document2.references.forEach((reference, index) => {
|
|
3083
|
+
if (normalizeDocument2Key(reference.key) === normalized) {
|
|
3084
|
+
indexes.push(index);
|
|
3085
|
+
}
|
|
3086
|
+
});
|
|
3087
|
+
return indexes;
|
|
3088
|
+
}
|
|
3089
|
+
function referenceTargetIndex(document2, key) {
|
|
3090
|
+
const indexes = referenceIndexes(document2, key);
|
|
3091
|
+
if (indexes.length === 0) {
|
|
3092
|
+
throw new Document2CommandError("reference_not_found", "Document reference was not found");
|
|
3093
|
+
}
|
|
3094
|
+
if (indexes.length > 1) {
|
|
3095
|
+
throw new Document2CommandError("duplicate_key", "Reference key is ambiguous");
|
|
3096
|
+
}
|
|
3097
|
+
return indexes[0];
|
|
3098
|
+
}
|
|
3099
|
+
function validateReferenceKey(document2, key, excludeReferenceId) {
|
|
3100
|
+
const normalized = normalizeDocument2Key(key);
|
|
3101
|
+
const error = document2KeyError(normalized, {
|
|
3102
|
+
references: document2.references,
|
|
3103
|
+
labels: collectDocument2Labels(document2),
|
|
3104
|
+
...excludeReferenceId ? { excludeReferenceId } : {}
|
|
3105
|
+
});
|
|
3106
|
+
if (error === "duplicate") {
|
|
3107
|
+
throw new Document2CommandError("duplicate_key", "Reference key is already in use");
|
|
3108
|
+
}
|
|
3109
|
+
if (error !== null) {
|
|
3110
|
+
throw new Document2CommandError("invalid_key", "Reference key is invalid");
|
|
3111
|
+
}
|
|
3112
|
+
return normalized;
|
|
3113
|
+
}
|
|
3114
|
+
function referenceInsertionIndex(document2, anchor) {
|
|
3115
|
+
if ("end" in anchor) {
|
|
3116
|
+
return document2.references.length;
|
|
3117
|
+
}
|
|
3118
|
+
const key = "before_reference_key" in anchor ? anchor.before_reference_key : anchor.after_reference_key;
|
|
3119
|
+
const indexes = referenceIndexes(document2, key);
|
|
3120
|
+
if (indexes.length === 0) {
|
|
3121
|
+
throw new Document2CommandError("anchor_not_found", "Anchor reference was not found");
|
|
3122
|
+
}
|
|
3123
|
+
if (indexes.length > 1) {
|
|
3124
|
+
throw new Document2CommandError("duplicate_key", "Anchor reference key is ambiguous");
|
|
3125
|
+
}
|
|
3126
|
+
return "before_reference_key" in anchor ? indexes[0] : indexes[0] + 1;
|
|
3127
|
+
}
|
|
3128
|
+
function applyReferenceFields(reference, command) {
|
|
3129
|
+
for (const field of REFERENCE_TEXT_FIELDS) {
|
|
3130
|
+
const next = command[field];
|
|
3131
|
+
if (next !== void 0) {
|
|
3132
|
+
reference[field] = next;
|
|
3133
|
+
}
|
|
3134
|
+
}
|
|
3135
|
+
if (command.field_separator !== void 0) {
|
|
3136
|
+
reference.fieldSeparator = command.field_separator;
|
|
3137
|
+
}
|
|
3138
|
+
}
|
|
3139
|
+
function applyArticleCommand(document2, command) {
|
|
3140
|
+
if (command.op === "update_document_meta") {
|
|
3141
|
+
document2.meta = {
|
|
3142
|
+
title: command.title ?? document2.meta.title,
|
|
3143
|
+
authors: command.authors ?? document2.meta.authors,
|
|
3144
|
+
abstract: command.abstract ?? document2.meta.abstract,
|
|
3145
|
+
date: command.date ? normalizeDocument2HijriDate({ ...document2.meta.date, ...command.date }) : { ...document2.meta.date }
|
|
3146
|
+
};
|
|
3147
|
+
return;
|
|
3148
|
+
}
|
|
3149
|
+
if (command.op === "insert_reference") {
|
|
3150
|
+
const key = validateReferenceKey(document2, command.key);
|
|
3151
|
+
const reference2 = referenceFromJson({
|
|
3152
|
+
key,
|
|
3153
|
+
authors: command.authors,
|
|
3154
|
+
title: command.title,
|
|
3155
|
+
year: command.year,
|
|
3156
|
+
venue: command.venue,
|
|
3157
|
+
url: command.url,
|
|
3158
|
+
field_separator: command.field_separator
|
|
3159
|
+
});
|
|
3160
|
+
document2.references.splice(referenceInsertionIndex(document2, command.anchor), 0, reference2);
|
|
3161
|
+
return;
|
|
3162
|
+
}
|
|
3163
|
+
const targetIndex = referenceTargetIndex(document2, command.reference_key);
|
|
3164
|
+
const target = document2.references[targetIndex];
|
|
3165
|
+
if (command.op === "update_reference") {
|
|
3166
|
+
if (command.key !== void 0) {
|
|
3167
|
+
target.key = validateReferenceKey(document2, command.key, target.id);
|
|
3168
|
+
}
|
|
3169
|
+
applyReferenceFields(target, command);
|
|
3170
|
+
return;
|
|
3171
|
+
}
|
|
3172
|
+
if (command.op === "remove_reference") {
|
|
3173
|
+
document2.references.splice(targetIndex, 1);
|
|
3174
|
+
return;
|
|
3175
|
+
}
|
|
3176
|
+
const targetKey = normalizeDocument2Key(target.key);
|
|
3177
|
+
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;
|
|
3178
|
+
if (anchorKey !== null && normalizeDocument2Key(anchorKey) === targetKey) {
|
|
3179
|
+
throw new Document2CommandError("invalid_anchor", "A reference cannot be moved relative to itself");
|
|
3180
|
+
}
|
|
3181
|
+
if (anchorKey !== null) {
|
|
3182
|
+
referenceInsertionIndex(document2, command.anchor);
|
|
3183
|
+
}
|
|
3184
|
+
const [reference] = document2.references.splice(targetIndex, 1);
|
|
3185
|
+
document2.references.splice(referenceInsertionIndex(document2, command.anchor), 0, reference);
|
|
3186
|
+
}
|
|
3187
|
+
|
|
2811
3188
|
// src/document2/inlineJsonCommands.ts
|
|
2812
3189
|
function parseInlineAnchor(value) {
|
|
2813
3190
|
if (!isObject3(value)) {
|
|
@@ -3482,6 +3859,9 @@ function isListCommand(command) {
|
|
|
3482
3859
|
function isTableCommand(command) {
|
|
3483
3860
|
return command.op === "insert_table" || command.op === "replace_table_cell" || command.op === "insert_table_row" || command.op === "remove_table_row" || command.op === "move_table_row" || command.op === "insert_table_column" || command.op === "remove_table_column" || command.op === "move_table_column";
|
|
3484
3861
|
}
|
|
3862
|
+
function isArticleCommand(command) {
|
|
3863
|
+
return command.op === "update_document_meta" || command.op === "insert_reference" || command.op === "update_reference" || command.op === "remove_reference" || command.op === "move_reference";
|
|
3864
|
+
}
|
|
3485
3865
|
function parseTextBlockKind(value) {
|
|
3486
3866
|
if (value === "section" || value === "subsection" || value === "subsubsection" || value === "paragraph") {
|
|
3487
3867
|
return value;
|
|
@@ -3509,6 +3889,62 @@ function parseBasicCommand(value) {
|
|
|
3509
3889
|
if (value.op === "remove_block") {
|
|
3510
3890
|
return { op: value.op, block_id: requireString2(value.block_id, "block_id", false).trim() };
|
|
3511
3891
|
}
|
|
3892
|
+
if (value.op === "move_block") {
|
|
3893
|
+
return {
|
|
3894
|
+
op: value.op,
|
|
3895
|
+
block_id: requireString2(value.block_id, "block_id", false).trim(),
|
|
3896
|
+
anchor: parseBlockAnchor(value.anchor)
|
|
3897
|
+
};
|
|
3898
|
+
}
|
|
3899
|
+
if (value.op === "insert_bibliography") {
|
|
3900
|
+
const metadata = parseMetadata(value.metadata);
|
|
3901
|
+
return {
|
|
3902
|
+
op: value.op,
|
|
3903
|
+
anchor: parseBlockAnchor(value.anchor),
|
|
3904
|
+
...metadata ? { metadata } : {}
|
|
3905
|
+
};
|
|
3906
|
+
}
|
|
3907
|
+
if (value.op === "update_figure") {
|
|
3908
|
+
const hasAssetId = Object.prototype.hasOwnProperty.call(value, "asset_id");
|
|
3909
|
+
const hasValue = Object.prototype.hasOwnProperty.call(value, "value");
|
|
3910
|
+
if (!hasAssetId && !hasValue) {
|
|
3911
|
+
throw new Document2CommandError("invalid_command", "update_figure requires asset_id or value");
|
|
3912
|
+
}
|
|
3913
|
+
let assetId;
|
|
3914
|
+
if (hasAssetId) {
|
|
3915
|
+
if (value.asset_id === null) {
|
|
3916
|
+
assetId = null;
|
|
3917
|
+
} else {
|
|
3918
|
+
assetId = requireString2(value.asset_id, "asset_id", false).trim();
|
|
3919
|
+
}
|
|
3920
|
+
}
|
|
3921
|
+
const figureValue = hasValue ? requireString2(value.value, "value") : void 0;
|
|
3922
|
+
return {
|
|
3923
|
+
op: value.op,
|
|
3924
|
+
block_id: requireString2(value.block_id, "block_id", false).trim(),
|
|
3925
|
+
...hasAssetId ? { asset_id: assetId } : {},
|
|
3926
|
+
...hasValue ? { value: figureValue } : {}
|
|
3927
|
+
};
|
|
3928
|
+
}
|
|
3929
|
+
if (value.op === "update_float_meta") {
|
|
3930
|
+
const centered = optionalBoolean(value, "centered");
|
|
3931
|
+
const captionEnabled = optionalBoolean(value, "caption_enabled");
|
|
3932
|
+
const caption = optionalString(value, "caption");
|
|
3933
|
+
const labelEnabled = optionalBoolean(value, "label_enabled");
|
|
3934
|
+
const label = optionalString(value, "label");
|
|
3935
|
+
if (centered === void 0 && captionEnabled === void 0 && caption === void 0 && labelEnabled === void 0 && label === void 0) {
|
|
3936
|
+
throw new Document2CommandError("invalid_command", "update_float_meta requires at least one field");
|
|
3937
|
+
}
|
|
3938
|
+
return {
|
|
3939
|
+
op: value.op,
|
|
3940
|
+
block_id: requireString2(value.block_id, "block_id", false).trim(),
|
|
3941
|
+
...centered !== void 0 ? { centered } : {},
|
|
3942
|
+
...captionEnabled !== void 0 ? { caption_enabled: captionEnabled } : {},
|
|
3943
|
+
...caption !== void 0 ? { caption } : {},
|
|
3944
|
+
...labelEnabled !== void 0 ? { label_enabled: labelEnabled } : {},
|
|
3945
|
+
...label !== void 0 ? { label } : {}
|
|
3946
|
+
};
|
|
3947
|
+
}
|
|
3512
3948
|
if (value.op === "insert_figure") {
|
|
3513
3949
|
const figureValue = optionalString(value, "value");
|
|
3514
3950
|
const caption = optionalString(value, "caption");
|
|
@@ -3539,7 +3975,10 @@ function parseCommand2(value) {
|
|
|
3539
3975
|
if (value.op === "insert_table" || value.op === "replace_table_cell" || value.op === "insert_table_row" || value.op === "remove_table_row" || value.op === "move_table_row" || value.op === "insert_table_column" || value.op === "remove_table_column" || value.op === "move_table_column") {
|
|
3540
3976
|
return parseTableCommand(value);
|
|
3541
3977
|
}
|
|
3542
|
-
if (value.op === "
|
|
3978
|
+
if (value.op === "update_document_meta" || value.op === "insert_reference" || value.op === "update_reference" || value.op === "remove_reference" || value.op === "move_reference") {
|
|
3979
|
+
return parseArticleCommand(value);
|
|
3980
|
+
}
|
|
3981
|
+
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") {
|
|
3543
3982
|
return parseBasicCommand(value);
|
|
3544
3983
|
}
|
|
3545
3984
|
throw new Document2CommandError("invalid_command", "Unsupported document command");
|
|
@@ -3592,6 +4031,94 @@ function applyBasicCommand(document2, command) {
|
|
|
3592
4031
|
document2.blocks.splice(index, 1);
|
|
3593
4032
|
return;
|
|
3594
4033
|
}
|
|
4034
|
+
if (command.op === "move_block") {
|
|
4035
|
+
const index = document2.blocks.findIndex((block3) => block3.id === command.block_id);
|
|
4036
|
+
if (index < 0) {
|
|
4037
|
+
throw new Document2CommandError("block_not_found", "Document block was not found");
|
|
4038
|
+
}
|
|
4039
|
+
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;
|
|
4040
|
+
if (anchorId === command.block_id) {
|
|
4041
|
+
throw new Document2CommandError("invalid_anchor", "A block cannot be moved relative to itself");
|
|
4042
|
+
}
|
|
4043
|
+
if (anchorId !== null) {
|
|
4044
|
+
blockInsertionIndex(document2, command.anchor);
|
|
4045
|
+
}
|
|
4046
|
+
const [block2] = document2.blocks.splice(index, 1);
|
|
4047
|
+
document2.blocks.splice(blockInsertionIndex(document2, command.anchor), 0, block2);
|
|
4048
|
+
return;
|
|
4049
|
+
}
|
|
4050
|
+
if (command.op === "insert_bibliography") {
|
|
4051
|
+
let hasBibliography2 = function(blocks) {
|
|
4052
|
+
return blocks.some((block3) => block3.kind === "bibliography" || block3.kind === "list" && block3.items.some((item) => hasBibliography2(item.blocks)));
|
|
4053
|
+
};
|
|
4054
|
+
var hasBibliography = hasBibliography2;
|
|
4055
|
+
if (hasBibliography2(document2.blocks)) {
|
|
4056
|
+
throw new Document2CommandError("bibliography_exists", "Document already contains a bibliography block");
|
|
4057
|
+
}
|
|
4058
|
+
const block2 = {
|
|
4059
|
+
id: newBlockId(document2),
|
|
4060
|
+
kind: "bibliography",
|
|
4061
|
+
command: "\\begin{thebibliography}",
|
|
4062
|
+
closing: "\\end{thebibliography}",
|
|
4063
|
+
...command.metadata ? { metadata: { ...command.metadata } } : {}
|
|
4064
|
+
};
|
|
4065
|
+
document2.blocks.splice(blockInsertionIndex(document2, command.anchor), 0, block2);
|
|
4066
|
+
return;
|
|
4067
|
+
}
|
|
4068
|
+
if (command.op === "update_figure") {
|
|
4069
|
+
const block2 = findBlock(document2.blocks, command.block_id);
|
|
4070
|
+
if (!block2) {
|
|
4071
|
+
throw new Document2CommandError("block_not_found", "Document block was not found");
|
|
4072
|
+
}
|
|
4073
|
+
if (block2.kind !== "image") {
|
|
4074
|
+
throw new Document2CommandError("block_kind_mismatch", "Target block is not a figure");
|
|
4075
|
+
}
|
|
4076
|
+
if ("asset_id" in command) {
|
|
4077
|
+
if (command.asset_id === null) {
|
|
4078
|
+
delete block2.assetId;
|
|
4079
|
+
} else {
|
|
4080
|
+
block2.assetId = command.asset_id;
|
|
4081
|
+
}
|
|
4082
|
+
}
|
|
4083
|
+
if (command.value !== void 0) {
|
|
4084
|
+
block2.value = command.value;
|
|
4085
|
+
}
|
|
4086
|
+
return;
|
|
4087
|
+
}
|
|
4088
|
+
if (command.op === "update_float_meta") {
|
|
4089
|
+
const block2 = findBlock(document2.blocks, command.block_id);
|
|
4090
|
+
if (!block2) {
|
|
4091
|
+
throw new Document2CommandError("block_not_found", "Document block was not found");
|
|
4092
|
+
}
|
|
4093
|
+
if (block2.kind !== "image" && block2.kind !== "table") {
|
|
4094
|
+
throw new Document2CommandError("block_kind_mismatch", "Target block is not a figure or table");
|
|
4095
|
+
}
|
|
4096
|
+
let label = command.label;
|
|
4097
|
+
if (label !== void 0) {
|
|
4098
|
+
label = label.trim().length > 0 ? normalizeDocument2Key(label) : "";
|
|
4099
|
+
}
|
|
4100
|
+
if (label !== void 0 && label.length > 0) {
|
|
4101
|
+
const keyError = document2KeyError(label, {
|
|
4102
|
+
references: document2.references,
|
|
4103
|
+
labels: collectDocument2Labels(document2),
|
|
4104
|
+
excludeOwnerId: block2.id
|
|
4105
|
+
});
|
|
4106
|
+
if (keyError === "duplicate") {
|
|
4107
|
+
throw new Document2CommandError("duplicate_key", "Float label is already in use");
|
|
4108
|
+
}
|
|
4109
|
+
if (keyError !== null) {
|
|
4110
|
+
throw new Document2CommandError("invalid_key", "Float label is invalid");
|
|
4111
|
+
}
|
|
4112
|
+
}
|
|
4113
|
+
applyFloatMetaPatch(block2, {
|
|
4114
|
+
...command.centered !== void 0 ? { centered: command.centered } : {},
|
|
4115
|
+
...command.caption_enabled !== void 0 ? { captionEnabled: command.caption_enabled } : {},
|
|
4116
|
+
...command.caption !== void 0 ? { caption: command.caption } : {},
|
|
4117
|
+
...command.label_enabled !== void 0 ? { labelEnabled: command.label_enabled } : {},
|
|
4118
|
+
...label !== void 0 ? { label } : {}
|
|
4119
|
+
});
|
|
4120
|
+
return;
|
|
4121
|
+
}
|
|
3595
4122
|
const block = {
|
|
3596
4123
|
id: newBlockId(document2),
|
|
3597
4124
|
kind: "image",
|
|
@@ -3615,6 +4142,8 @@ function applyDocument2Command(json, commandInput) {
|
|
|
3615
4142
|
applyListCommand(document2, command);
|
|
3616
4143
|
} else if (isTableCommand(command)) {
|
|
3617
4144
|
applyTableCommand(document2, command);
|
|
4145
|
+
} else if (isArticleCommand(command)) {
|
|
4146
|
+
applyArticleCommand(document2, command);
|
|
3618
4147
|
} else {
|
|
3619
4148
|
applyBasicCommand(document2, command);
|
|
3620
4149
|
}
|