@folijs/core 0.2.0 → 0.3.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/CHANGELOG.md +23 -0
- package/README.md +1 -1
- package/dist/document.d.ts +10 -16
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +22 -22
- package/dist/document.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/model/fragment.d.ts +5 -2
- package/dist/model/fragment.d.ts.map +1 -1
- package/dist/model/fragment.js +5 -2
- package/dist/model/fragment.js.map +1 -1
- package/dist/model/node.d.ts +1 -1
- package/dist/model/node.d.ts.map +1 -1
- package/dist/model/node.js.map +1 -1
- package/dist/schema.d.ts +10 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +11 -1
- package/dist/schema.js.map +1 -1
- package/dist/state/commands.d.ts +7 -6
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +134 -97
- package/dist/state/commands.js.map +1 -1
- package/dist/state/editor.d.ts +25 -15
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +44 -16
- package/dist/state/editor.js.map +1 -1
- package/dist/state/format.d.ts +8 -22
- package/dist/state/format.d.ts.map +1 -1
- package/dist/state/format.js +16 -140
- package/dist/state/format.js.map +1 -1
- package/dist/state/index.d.ts +8 -4
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +8 -4
- package/dist/state/index.js.map +1 -1
- package/dist/state/links.d.ts.map +1 -1
- package/dist/state/links.js +10 -2
- package/dist/state/links.js.map +1 -1
- package/dist/state/lists.d.ts +19 -32
- package/dist/state/lists.d.ts.map +1 -1
- package/dist/state/lists.js +23 -372
- package/dist/state/lists.js.map +1 -1
- package/dist/state/mark.d.ts +25 -0
- package/dist/state/mark.d.ts.map +1 -0
- package/dist/state/mark.js +40 -0
- package/dist/state/mark.js.map +1 -0
- package/dist/state/state.d.ts.map +1 -1
- package/dist/state/state.js +11 -0
- package/dist/state/state.js.map +1 -1
- package/dist/state/word.d.ts +72 -0
- package/dist/state/word.d.ts.map +1 -0
- package/dist/state/word.js +173 -0
- package/dist/state/word.js.map +1 -0
- package/dist/state/wordclip.d.ts +43 -0
- package/dist/state/wordclip.d.ts.map +1 -0
- package/dist/state/wordclip.js +412 -0
- package/dist/state/wordclip.js.map +1 -0
- package/dist/state/wordformat.d.ts +136 -0
- package/dist/state/wordformat.d.ts.map +1 -0
- package/dist/state/wordformat.js +846 -0
- package/dist/state/wordformat.js.map +1 -0
- package/dist/v3/check.d.ts +33 -0
- package/dist/v3/check.d.ts.map +1 -0
- package/dist/v3/check.js +90 -0
- package/dist/v3/check.js.map +1 -0
- package/dist/v3/document.d.ts +217 -0
- package/dist/v3/document.d.ts.map +1 -0
- package/dist/v3/document.js +225 -0
- package/dist/v3/document.js.map +1 -0
- package/dist/v3/index.d.ts +6 -0
- package/dist/v3/index.d.ts.map +1 -0
- package/dist/v3/index.js +6 -0
- package/dist/v3/index.js.map +1 -0
- package/dist/v3/migrate.d.ts +19 -0
- package/dist/v3/migrate.d.ts.map +1 -0
- package/dist/v3/migrate.js +227 -0
- package/dist/v3/migrate.js.map +1 -0
- package/dist/v3/resolve.d.ts +129 -0
- package/dist/v3/resolve.d.ts.map +1 -0
- package/dist/v3/resolve.js +534 -0
- package/dist/v3/resolve.js.map +1 -0
- package/dist/v3/schema.d.ts +27 -0
- package/dist/v3/schema.d.ts.map +1 -0
- package/dist/v3/schema.js +429 -0
- package/dist/v3/schema.js.map +1 -0
- package/dist/v3/units.d.ts +16 -0
- package/dist/v3/units.d.ts.map +1 -0
- package/dist/v3/units.js +28 -0
- package/dist/v3/units.js.map +1 -0
- package/package.json +1 -1
- package/src/document.ts +26 -26
- package/src/index.ts +3 -6
- package/src/model/fragment.ts +11 -3
- package/src/model/node.ts +6 -1
- package/src/schema.ts +13 -1
- package/src/state/commands.ts +147 -91
- package/src/state/editor.ts +81 -35
- package/src/state/format.ts +24 -122
- package/src/state/index.ts +34 -3
- package/src/state/links.ts +10 -2
- package/src/state/lists.ts +31 -408
- package/src/state/mark.ts +47 -0
- package/src/state/state.ts +11 -0
- package/src/state/word.ts +245 -0
- package/src/state/wordclip.ts +445 -0
- package/src/state/wordformat.ts +982 -0
- package/src/v3/check.ts +119 -0
- package/src/v3/document.ts +456 -0
- package/src/v3/index.ts +33 -0
- package/src/v3/migrate.ts +267 -0
- package/src/v3/resolve.ts +646 -0
- package/src/v3/schema.ts +479 -0
- package/src/v3/units.ts +34 -0
package/src/v3/check.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small validators for schema v3's JSON-valued attributes and envelope. A check returns an
|
|
3
|
+
* error message, or undefined when the value is valid. They are the trust boundary for
|
|
4
|
+
* untrusted documents: only own keys are looked up, so `__proto__`, `constructor` and other
|
|
5
|
+
* inherited names are unknown keys, never checks.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type Check = (value: unknown) => string | undefined;
|
|
9
|
+
|
|
10
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
11
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const bool: Check = (v) => (typeof v === "boolean" ? undefined : "must be true or false");
|
|
15
|
+
|
|
16
|
+
export const int =
|
|
17
|
+
(min: number, max: number): Check =>
|
|
18
|
+
(v) =>
|
|
19
|
+
Number.isInteger(v) && (v as number) >= min && (v as number) <= max
|
|
20
|
+
? undefined
|
|
21
|
+
: `must be an integer from ${min} to ${max}`;
|
|
22
|
+
|
|
23
|
+
/** A string of at most `max` characters, empty allowed (Word's ST_String: authors, initials). */
|
|
24
|
+
export const str =
|
|
25
|
+
(max = 255): Check =>
|
|
26
|
+
(v) =>
|
|
27
|
+
typeof v === "string" && v.length <= max
|
|
28
|
+
? undefined
|
|
29
|
+
: `must be a string of at most ${max} characters`;
|
|
30
|
+
|
|
31
|
+
/** A non-empty string: ids and names things are referenced by. */
|
|
32
|
+
export const text =
|
|
33
|
+
(max = 255): Check =>
|
|
34
|
+
(v) =>
|
|
35
|
+
typeof v === "string" && v.length > 0 && v.length <= max
|
|
36
|
+
? undefined
|
|
37
|
+
: `must be a non-empty string of at most ${max} characters`;
|
|
38
|
+
|
|
39
|
+
export const oneOf =
|
|
40
|
+
(values: readonly string[]): Check =>
|
|
41
|
+
(v) =>
|
|
42
|
+
values.includes(v as string) ? undefined : `must be one of ${values.join(", ")}`;
|
|
43
|
+
|
|
44
|
+
/** A token of one of Word's long enumerations (border styles, number formats…). */
|
|
45
|
+
export const token: Check = (v) =>
|
|
46
|
+
typeof v === "string" && /^[A-Za-z][A-Za-z0-9-]{0,63}$/.test(v)
|
|
47
|
+
? undefined
|
|
48
|
+
: "must be a Word token";
|
|
49
|
+
|
|
50
|
+
/** `RRGGBB` (any case, as Word writes it) or `auto`. */
|
|
51
|
+
export const color: Check = (v) =>
|
|
52
|
+
typeof v === "string" && /^([0-9A-Fa-f]{6}|auto)$/.test(v) ? undefined : "must be RRGGBB or auto";
|
|
53
|
+
|
|
54
|
+
/** An ISO 8601 date-time, as Word stores revision and comment dates. */
|
|
55
|
+
export const date: Check = (v) =>
|
|
56
|
+
typeof v === "string" &&
|
|
57
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})?$/.test(v)
|
|
58
|
+
? undefined
|
|
59
|
+
: "must be an ISO 8601 date-time";
|
|
60
|
+
|
|
61
|
+
/** Verbatim XML kept for export: never parsed by the engine. */
|
|
62
|
+
export const xml: Check = (v) =>
|
|
63
|
+
typeof v === "string" && v.length > 0 && v.length <= 16_777_216
|
|
64
|
+
? undefined
|
|
65
|
+
: "must be an XML string";
|
|
66
|
+
|
|
67
|
+
/** An element's own attributes Foli does not model, by qualified name (`w14:paraId`). */
|
|
68
|
+
export const attributes: Check = (v) => {
|
|
69
|
+
if (!isRecord(v)) return "must be an object";
|
|
70
|
+
const entries = Object.entries(v);
|
|
71
|
+
if (entries.length === 0) return "must not be empty (use null)";
|
|
72
|
+
if (entries.length > 64) return "must have at most 64 attributes";
|
|
73
|
+
for (const [name, value] of entries) {
|
|
74
|
+
if (!/^[A-Za-z_][\w.-]*(:[A-Za-z_][\w.-]*)?$/.test(name))
|
|
75
|
+
return `${name} is not an attribute name`;
|
|
76
|
+
if (typeof value !== "string" || value.length > 65_535) return `${name} must be a string`;
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const nullable =
|
|
82
|
+
(check: Check): Check =>
|
|
83
|
+
(v) =>
|
|
84
|
+
v === null ? undefined : check(v);
|
|
85
|
+
|
|
86
|
+
export const listOf =
|
|
87
|
+
(check: Check, max = 100_000): Check =>
|
|
88
|
+
(v) => {
|
|
89
|
+
if (!Array.isArray(v)) return "must be an array";
|
|
90
|
+
if (v.length > max) return `must have at most ${max} items`;
|
|
91
|
+
for (let i = 0; i < v.length; i++) {
|
|
92
|
+
const error = check(v[i]);
|
|
93
|
+
if (error) return `[${i}] ${error}`;
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* An object with only the given keys, each optional unless listed in `required`. Unless
|
|
100
|
+
* `allowEmpty`, at least one key is present (an empty object means "not set": use null).
|
|
101
|
+
*/
|
|
102
|
+
export const shape =
|
|
103
|
+
(
|
|
104
|
+
fields: Readonly<Record<string, Check>>,
|
|
105
|
+
required: readonly string[] = [],
|
|
106
|
+
allowEmpty = false,
|
|
107
|
+
): Check =>
|
|
108
|
+
(v) => {
|
|
109
|
+
if (!isRecord(v)) return "must be an object";
|
|
110
|
+
const keys = Object.keys(v);
|
|
111
|
+
if (keys.length === 0 && !allowEmpty) return "must not be empty (use null)";
|
|
112
|
+
for (const key of keys) {
|
|
113
|
+
if (!Object.hasOwn(fields, key)) return `unknown key ${key}`;
|
|
114
|
+
const error = (fields[key] as Check)(v[key]);
|
|
115
|
+
if (error) return `${key} ${error}`;
|
|
116
|
+
}
|
|
117
|
+
for (const key of required) if (!Object.hasOwn(v, key)) return `${key} is required`;
|
|
118
|
+
return undefined;
|
|
119
|
+
};
|
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { DEFAULT_SETTINGS, parseDocumentV2 } from "../document.js";
|
|
2
|
+
import { FoliDocumentError, type MarkJSON, type Node, type NodeJSON } from "../model/index.js";
|
|
3
|
+
import {
|
|
4
|
+
bool,
|
|
5
|
+
type Check,
|
|
6
|
+
color,
|
|
7
|
+
date,
|
|
8
|
+
int,
|
|
9
|
+
isRecord,
|
|
10
|
+
listOf,
|
|
11
|
+
oneOf,
|
|
12
|
+
shape,
|
|
13
|
+
str,
|
|
14
|
+
text,
|
|
15
|
+
token,
|
|
16
|
+
xml,
|
|
17
|
+
} from "./check.js";
|
|
18
|
+
import { migrateToV3 } from "./migrate.js";
|
|
19
|
+
import {
|
|
20
|
+
checkSection,
|
|
21
|
+
checkParagraphProperties as paragraphProperties,
|
|
22
|
+
checkRunProperties as runProperties,
|
|
23
|
+
schema,
|
|
24
|
+
} from "./schema.js";
|
|
25
|
+
|
|
26
|
+
/** The schema version of documents in the Word model (ADR-0005). */
|
|
27
|
+
export const SCHEMA_VERSION = 3;
|
|
28
|
+
|
|
29
|
+
/** Page setup of a section, in twips (1/1440 inch). Missing parts take Word's defaults. */
|
|
30
|
+
export interface SectionJSON {
|
|
31
|
+
pageSize?: { width?: number; height?: number; orientation?: "portrait" | "landscape" };
|
|
32
|
+
margins?: {
|
|
33
|
+
top?: number;
|
|
34
|
+
right?: number;
|
|
35
|
+
bottom?: number;
|
|
36
|
+
left?: number;
|
|
37
|
+
header?: number;
|
|
38
|
+
footer?: number;
|
|
39
|
+
gutter?: number;
|
|
40
|
+
};
|
|
41
|
+
type?: "nextPage" | "continuous" | "evenPage" | "oddPage" | "nextColumn";
|
|
42
|
+
titlePage?: boolean;
|
|
43
|
+
/** Relationship ids of the header and footer parts (their content stays in the package until Phase 11). */
|
|
44
|
+
headers?: { default?: string; first?: string; even?: string };
|
|
45
|
+
footers?: { default?: string; first?: string; even?: string };
|
|
46
|
+
/** The `w:sectPr` this was read from, verbatim. */
|
|
47
|
+
ext?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface BorderJSON {
|
|
51
|
+
style: string;
|
|
52
|
+
size?: number;
|
|
53
|
+
space?: number;
|
|
54
|
+
color?: string;
|
|
55
|
+
themeColor?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Paragraph properties (`w:pPr`) as a paragraph's attributes, a style, a numbering level or the defaults give them. */
|
|
59
|
+
export interface ParagraphPropertiesJSON {
|
|
60
|
+
style?: string;
|
|
61
|
+
justification?: string;
|
|
62
|
+
/** Twips; with both `firstLine` and `hanging`, Word applies `hanging`. */
|
|
63
|
+
indent?: { left?: number; right?: number; firstLine?: number; hanging?: number };
|
|
64
|
+
spacing?: {
|
|
65
|
+
before?: number;
|
|
66
|
+
after?: number;
|
|
67
|
+
line?: number;
|
|
68
|
+
lineRule?: "auto" | "exact" | "atLeast";
|
|
69
|
+
beforeAutospacing?: boolean;
|
|
70
|
+
afterAutospacing?: boolean;
|
|
71
|
+
};
|
|
72
|
+
keepNext?: boolean;
|
|
73
|
+
keepLines?: boolean;
|
|
74
|
+
widowControl?: boolean;
|
|
75
|
+
pageBreakBefore?: boolean;
|
|
76
|
+
contextualSpacing?: boolean;
|
|
77
|
+
outlineLevel?: number;
|
|
78
|
+
numbering?: { id?: number; level?: number };
|
|
79
|
+
tabs?: { position: number; kind: string; leader?: string }[];
|
|
80
|
+
borders?: Partial<Record<"top" | "bottom" | "left" | "right" | "between" | "bar", BorderJSON>>;
|
|
81
|
+
shading?: { pattern: string; color?: string; fill?: string; themeFill?: string };
|
|
82
|
+
markFormat?: MarkJSON[];
|
|
83
|
+
ext?: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface StyleJSON {
|
|
87
|
+
/** `w:styleId`, what paragraphs and runs reference. With duplicates, the first wins, as in Word. */
|
|
88
|
+
id: string;
|
|
89
|
+
type: "paragraph" | "character" | "table" | "numbering";
|
|
90
|
+
name?: string;
|
|
91
|
+
basedOn?: string;
|
|
92
|
+
next?: string;
|
|
93
|
+
link?: string;
|
|
94
|
+
/** The default style of its type (Normal, Default Paragraph Font…). */
|
|
95
|
+
isDefault?: boolean;
|
|
96
|
+
paragraph?: ParagraphPropertiesJSON;
|
|
97
|
+
run?: MarkJSON[];
|
|
98
|
+
/** The `w:style` this was read from, verbatim (table properties, UI priority…). */
|
|
99
|
+
ext?: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface NumberingLevelJSON {
|
|
103
|
+
level: number;
|
|
104
|
+
start?: number;
|
|
105
|
+
/** `w:numFmt`: decimal (Word's default), bullet, lowerLetter, upperRoman, none… */
|
|
106
|
+
format?: string;
|
|
107
|
+
/** `w:lvlText`: "%1.", "%1.%2", "•"… */
|
|
108
|
+
text?: string;
|
|
109
|
+
justification?: "left" | "center" | "right" | "start" | "end";
|
|
110
|
+
/** `w:lvlRestart`: restart after this level (0 never). */
|
|
111
|
+
restart?: number;
|
|
112
|
+
/** `w:isLgl`: legal numbering (arabic numbers in a multi-level number). */
|
|
113
|
+
legal?: boolean;
|
|
114
|
+
suffix?: "tab" | "space" | "nothing";
|
|
115
|
+
/** `w:pStyle`: the style this level numbers (numbered headings). */
|
|
116
|
+
style?: string;
|
|
117
|
+
paragraph?: ParagraphPropertiesJSON;
|
|
118
|
+
run?: MarkJSON[];
|
|
119
|
+
/** The `w:lvl` this was read from, verbatim. */
|
|
120
|
+
ext?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface AbstractNumberingJSON {
|
|
124
|
+
/** `w:abstractNumId`. */
|
|
125
|
+
id: number;
|
|
126
|
+
multiLevelType?: "singleLevel" | "multilevel" | "hybridMultilevel";
|
|
127
|
+
levels: NumberingLevelJSON[];
|
|
128
|
+
styleLink?: string;
|
|
129
|
+
numStyleLink?: string;
|
|
130
|
+
ext?: string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface NumberingInstanceJSON {
|
|
134
|
+
/** `w:numId`, what paragraphs reference. */
|
|
135
|
+
id: number;
|
|
136
|
+
/** May name a definition that does not exist: the paragraphs are then not numbered, as in Word. */
|
|
137
|
+
abstract: number;
|
|
138
|
+
overrides?: { level: number; start?: number; definition?: NumberingLevelJSON }[];
|
|
139
|
+
ext?: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export interface ThemeJSON {
|
|
143
|
+
fonts: {
|
|
144
|
+
major: { latin: string; eastAsia?: string; cs?: string };
|
|
145
|
+
minor: { latin: string; eastAsia?: string; cs?: string };
|
|
146
|
+
};
|
|
147
|
+
/** dk1, lt1, dk2, lt2, accent1–6, hlink, folHlink: RRGGBB. */
|
|
148
|
+
colors: Record<string, string>;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface CommentJSON {
|
|
152
|
+
id: number;
|
|
153
|
+
author: string;
|
|
154
|
+
initials?: string;
|
|
155
|
+
date?: string;
|
|
156
|
+
/** The comment's paragraphs (schema v3 blocks). */
|
|
157
|
+
content: NodeJSON[];
|
|
158
|
+
/** The comment this one replies to (a missing one makes it a thread of its own). */
|
|
159
|
+
parent?: number;
|
|
160
|
+
/** Resolved. */
|
|
161
|
+
done?: boolean;
|
|
162
|
+
/** The `w:comment` start tag, verbatim: its attributes as the file has them. */
|
|
163
|
+
ext?: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A document in the Word model: the content tree and what Word keeps beside it. */
|
|
167
|
+
export interface FoliDocument {
|
|
168
|
+
schemaVersion: typeof SCHEMA_VERSION;
|
|
169
|
+
/** The last section's page setup (`w:body/w:sectPr`); earlier sections end at paragraphs. */
|
|
170
|
+
section: SectionJSON;
|
|
171
|
+
/** Document defaults (`w:docDefaults`). */
|
|
172
|
+
defaults: {
|
|
173
|
+
paragraph?: ParagraphPropertiesJSON;
|
|
174
|
+
run?: MarkJSON[];
|
|
175
|
+
/** The `w:docDefaults` these were read from, verbatim. */
|
|
176
|
+
ext?: string;
|
|
177
|
+
};
|
|
178
|
+
styles: StyleJSON[];
|
|
179
|
+
numbering: { abstracts: AbstractNumberingJSON[]; instances: NumberingInstanceJSON[] };
|
|
180
|
+
theme?: ThemeJSON;
|
|
181
|
+
comments: CommentJSON[];
|
|
182
|
+
content: NodeJSON;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* A new document: `content` (schema v3; default: one empty paragraph) on A4 with 25.4 mm
|
|
187
|
+
* margins, with the styles and list definitions the editor's commands use (Normal, Heading 1–6,
|
|
188
|
+
* List Paragraph, Hyperlink; bullets and numbers) and 11 pt Georgia, 1.5 lines: the look a
|
|
189
|
+
* migrated 0.2.0 document has. `section` (in twips: `mmToTwips`) is merged over that page:
|
|
190
|
+
* `pageSize` and `margins` part by part. Not validated: `parseDocument` (or `createEditor`) does
|
|
191
|
+
* that.
|
|
192
|
+
*/
|
|
193
|
+
export function createDocument(
|
|
194
|
+
content: NodeJSON = { type: "doc", content: [{ type: "paragraph" }] },
|
|
195
|
+
section?: SectionJSON,
|
|
196
|
+
): FoliDocument {
|
|
197
|
+
const empty = { type: "doc", content: [] } as const satisfies NodeJSON;
|
|
198
|
+
const blank = migrateToV3({
|
|
199
|
+
schemaVersion: 2,
|
|
200
|
+
settings: DEFAULT_SETTINGS,
|
|
201
|
+
content: empty as never,
|
|
202
|
+
});
|
|
203
|
+
const base = blank.section;
|
|
204
|
+
return {
|
|
205
|
+
...blank,
|
|
206
|
+
// The default (A4) section has only pageSize and margins: each merged part by part.
|
|
207
|
+
section: {
|
|
208
|
+
...section,
|
|
209
|
+
pageSize: { ...base.pageSize, ...section?.pageSize },
|
|
210
|
+
margins: { ...base.margins, ...section?.margins },
|
|
211
|
+
},
|
|
212
|
+
content,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Validates untrusted input against schema v3. Returns a copy that shares nothing with the
|
|
218
|
+
* input, with the content and the comments' content in canonical form. Documents of schema 1
|
|
219
|
+
* and 2 are validated as such and migrated (`migrateToV3`). Throws FoliDocumentError instead
|
|
220
|
+
* of dropping anything.
|
|
221
|
+
*
|
|
222
|
+
* What Word opens is accepted: duplicate ids (the first wins), a numbering instance or a reply
|
|
223
|
+
* whose target is missing, references to styles that do not exist. Rejected is only what the
|
|
224
|
+
* engine cannot work with: wrong types, out-of-range values, and cycles (a style based on
|
|
225
|
+
* itself, comments replying to each other) that would never end when followed.
|
|
226
|
+
*/
|
|
227
|
+
export function parseDocument(input: unknown): FoliDocument {
|
|
228
|
+
if (!isRecord(input)) throw new FoliDocumentError("Document must be an object");
|
|
229
|
+
const version = input.schemaVersion;
|
|
230
|
+
if (version === 1 || version === 2) return parseDocument(migrateToV3(parseDocumentV2(input)));
|
|
231
|
+
if (version !== SCHEMA_VERSION) {
|
|
232
|
+
const newer = Number.isInteger(version) && (version as number) > SCHEMA_VERSION;
|
|
233
|
+
throw new FoliDocumentError(
|
|
234
|
+
`Unsupported schemaVersion: ${String(version)} (this version of @folijs/core reads 1 to ` +
|
|
235
|
+
`${SCHEMA_VERSION}${newer ? "; the document was saved by a newer one" : ""})`,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
for (const key of Object.keys(input)) {
|
|
239
|
+
if (!ENVELOPE_KEYS.includes(key)) throw new FoliDocumentError(`${key}: unknown key`);
|
|
240
|
+
}
|
|
241
|
+
check("section", input.section, checkSection);
|
|
242
|
+
check(
|
|
243
|
+
"defaults",
|
|
244
|
+
input.defaults,
|
|
245
|
+
shape({ paragraph: paragraphProperties, run: runProperties, ext: xml }, [], true),
|
|
246
|
+
);
|
|
247
|
+
check("styles", input.styles, listOf(checkStyle));
|
|
248
|
+
check(
|
|
249
|
+
"numbering",
|
|
250
|
+
input.numbering,
|
|
251
|
+
shape({ abstracts: listOf(checkAbstract), instances: listOf(checkInstance) }, [
|
|
252
|
+
"abstracts",
|
|
253
|
+
"instances",
|
|
254
|
+
]),
|
|
255
|
+
);
|
|
256
|
+
if (input.theme !== undefined) check("theme", input.theme, checkTheme);
|
|
257
|
+
check("comments", input.comments, listOf(checkComment));
|
|
258
|
+
const styles = input.styles as StyleJSON[];
|
|
259
|
+
const comments = input.comments as CommentJSON[];
|
|
260
|
+
noCycles(
|
|
261
|
+
"styles",
|
|
262
|
+
styles,
|
|
263
|
+
(s) => s.id,
|
|
264
|
+
(s) => s.basedOn,
|
|
265
|
+
(s) => `${s.id} is based on itself`,
|
|
266
|
+
);
|
|
267
|
+
noCycles(
|
|
268
|
+
"comments",
|
|
269
|
+
comments,
|
|
270
|
+
(c) => c.id,
|
|
271
|
+
(c) => c.parent,
|
|
272
|
+
(c) => `comment ${c.id} replies to itself`,
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
const content = parseContent(input.content);
|
|
276
|
+
// A copy: the validated document must not change when the caller's input does.
|
|
277
|
+
const envelope = structuredClone({
|
|
278
|
+
section: input.section,
|
|
279
|
+
defaults: input.defaults,
|
|
280
|
+
styles,
|
|
281
|
+
numbering: input.numbering,
|
|
282
|
+
theme: input.theme,
|
|
283
|
+
}) as Pick<FoliDocument, "section" | "defaults" | "styles" | "numbering" | "theme">;
|
|
284
|
+
return {
|
|
285
|
+
schemaVersion: SCHEMA_VERSION,
|
|
286
|
+
section: envelope.section,
|
|
287
|
+
defaults: envelope.defaults,
|
|
288
|
+
styles: envelope.styles,
|
|
289
|
+
numbering: envelope.numbering,
|
|
290
|
+
...(envelope.theme === undefined ? {} : { theme: envelope.theme }),
|
|
291
|
+
comments: comments.map((c, i) => ({
|
|
292
|
+
...structuredClone({ ...c, content: [] }),
|
|
293
|
+
content: blocks(c.content, `comments[${i}].content`).map((block) => block.toJSON()),
|
|
294
|
+
})),
|
|
295
|
+
content: content.toJSON(),
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Parses untrusted schema v3 content into a `doc` node. */
|
|
300
|
+
export function parseContent(value: unknown, path = "content"): Node {
|
|
301
|
+
const doc = schema.nodeFromJSON(value, path);
|
|
302
|
+
if (doc.type !== schema.topNodeType) fail(path, 'root node must be of type "doc"');
|
|
303
|
+
if (doc.marks.length) fail(`${path}.marks`, "doc cannot have marks");
|
|
304
|
+
return doc;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const ENVELOPE_KEYS = [
|
|
308
|
+
"schemaVersion",
|
|
309
|
+
"section",
|
|
310
|
+
"defaults",
|
|
311
|
+
"styles",
|
|
312
|
+
"numbering",
|
|
313
|
+
"theme",
|
|
314
|
+
"comments",
|
|
315
|
+
"content",
|
|
316
|
+
];
|
|
317
|
+
|
|
318
|
+
function fail(path: string, message: string): never {
|
|
319
|
+
throw new FoliDocumentError(`${path}: ${message}`);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function check(path: string, value: unknown, checker: Check): void {
|
|
323
|
+
const error = checker(value);
|
|
324
|
+
if (error) fail(path, error);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Following `next` from any item ends (no item reaches itself). Linear: each item is walked
|
|
329
|
+
* once. Ids that occur twice resolve to the first, as Word does.
|
|
330
|
+
*/
|
|
331
|
+
function noCycles<T>(
|
|
332
|
+
path: string,
|
|
333
|
+
items: readonly T[],
|
|
334
|
+
id: (item: T) => unknown,
|
|
335
|
+
next: (item: T) => unknown,
|
|
336
|
+
message: (item: T) => string,
|
|
337
|
+
): void {
|
|
338
|
+
const byId = new Map<unknown, number>();
|
|
339
|
+
items.forEach((item, i) => {
|
|
340
|
+
if (!byId.has(id(item))) byId.set(id(item), i);
|
|
341
|
+
});
|
|
342
|
+
const state = new Uint8Array(items.length); // 0 unseen, 1 on the current path, 2 ends
|
|
343
|
+
items.forEach((_, start) => {
|
|
344
|
+
const path_: number[] = [];
|
|
345
|
+
let at: number | undefined = start;
|
|
346
|
+
while (at !== undefined && state[at] === 0) {
|
|
347
|
+
state[at] = 1;
|
|
348
|
+
path_.push(at);
|
|
349
|
+
const target = next(items[at] as T);
|
|
350
|
+
at = target === undefined ? undefined : byId.get(target);
|
|
351
|
+
}
|
|
352
|
+
if (at !== undefined && state[at] === 1) fail(`${path}[${at}]`, message(items[at] as T));
|
|
353
|
+
for (const i of path_) state[i] = 2;
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function blocks(content: NodeJSON[], path: string): Node[] {
|
|
358
|
+
const doc = parseContent({ type: "doc", content }, path);
|
|
359
|
+
const list: Node[] = [];
|
|
360
|
+
doc.forEach((block) => {
|
|
361
|
+
list.push(block);
|
|
362
|
+
});
|
|
363
|
+
return list;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const checkStyle: Check = shape(
|
|
367
|
+
{
|
|
368
|
+
id: text(),
|
|
369
|
+
type: oneOf(["paragraph", "character", "table", "numbering"]),
|
|
370
|
+
name: str(),
|
|
371
|
+
basedOn: text(),
|
|
372
|
+
next: text(),
|
|
373
|
+
link: text(),
|
|
374
|
+
isDefault: bool,
|
|
375
|
+
paragraph: paragraphProperties,
|
|
376
|
+
run: runProperties,
|
|
377
|
+
ext: xml,
|
|
378
|
+
},
|
|
379
|
+
["id", "type"],
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
const checkLevel: Check = shape(
|
|
383
|
+
{
|
|
384
|
+
level: int(0, 8),
|
|
385
|
+
start: int(0, 2_147_483_647),
|
|
386
|
+
format: token,
|
|
387
|
+
text: str(),
|
|
388
|
+
justification: oneOf(["left", "center", "right", "start", "end"]),
|
|
389
|
+
restart: int(0, 9),
|
|
390
|
+
legal: bool,
|
|
391
|
+
suffix: oneOf(["tab", "space", "nothing"]),
|
|
392
|
+
style: text(),
|
|
393
|
+
paragraph: paragraphProperties,
|
|
394
|
+
run: runProperties,
|
|
395
|
+
ext: xml,
|
|
396
|
+
},
|
|
397
|
+
["level"],
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
const checkAbstract: Check = shape(
|
|
401
|
+
{
|
|
402
|
+
id: int(0, 2_147_483_647),
|
|
403
|
+
multiLevelType: oneOf(["singleLevel", "multilevel", "hybridMultilevel"]),
|
|
404
|
+
levels: listOf(checkLevel, 9),
|
|
405
|
+
styleLink: text(),
|
|
406
|
+
numStyleLink: text(),
|
|
407
|
+
ext: xml,
|
|
408
|
+
},
|
|
409
|
+
["id", "levels"],
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
const checkInstance: Check = shape(
|
|
413
|
+
{
|
|
414
|
+
id: int(1, 2_147_483_647),
|
|
415
|
+
abstract: int(0, 2_147_483_647),
|
|
416
|
+
overrides: listOf(
|
|
417
|
+
shape({ level: int(0, 8), start: int(0, 2_147_483_647), definition: checkLevel }, ["level"]),
|
|
418
|
+
9,
|
|
419
|
+
),
|
|
420
|
+
ext: xml,
|
|
421
|
+
},
|
|
422
|
+
["id", "abstract"],
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
const font = shape({ latin: str(), eastAsia: str(), cs: str() }, ["latin"]);
|
|
426
|
+
|
|
427
|
+
const checkTheme: Check = shape(
|
|
428
|
+
{
|
|
429
|
+
fonts: shape({ major: font, minor: font }, ["major", "minor"]),
|
|
430
|
+
colors: (v) => {
|
|
431
|
+
if (!isRecord(v)) return "must be an object";
|
|
432
|
+
for (const [key, value] of Object.entries(v)) {
|
|
433
|
+
if (!/^(dk[12]|lt[12]|accent[1-6]|hlink|folHlink)$/.test(key))
|
|
434
|
+
return `unknown color ${key}`;
|
|
435
|
+
const error = color(value);
|
|
436
|
+
if (error) return `${key} ${error}`;
|
|
437
|
+
}
|
|
438
|
+
return undefined;
|
|
439
|
+
},
|
|
440
|
+
},
|
|
441
|
+
["fonts", "colors"],
|
|
442
|
+
);
|
|
443
|
+
|
|
444
|
+
const checkComment: Check = shape(
|
|
445
|
+
{
|
|
446
|
+
id: int(0, 2_147_483_647),
|
|
447
|
+
author: str(),
|
|
448
|
+
initials: str(9),
|
|
449
|
+
date: date,
|
|
450
|
+
content: (v) => (Array.isArray(v) ? undefined : "must be an array of blocks"),
|
|
451
|
+
parent: int(0, 2_147_483_647),
|
|
452
|
+
done: bool,
|
|
453
|
+
ext: xml,
|
|
454
|
+
},
|
|
455
|
+
["id", "author", "content"],
|
|
456
|
+
);
|
package/src/v3/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export {
|
|
2
|
+
type AbstractNumberingJSON,
|
|
3
|
+
type BorderJSON,
|
|
4
|
+
type CommentJSON,
|
|
5
|
+
createDocument,
|
|
6
|
+
type FoliDocument,
|
|
7
|
+
type NumberingInstanceJSON,
|
|
8
|
+
type NumberingLevelJSON,
|
|
9
|
+
type ParagraphPropertiesJSON,
|
|
10
|
+
parseContent,
|
|
11
|
+
parseDocument,
|
|
12
|
+
SCHEMA_VERSION,
|
|
13
|
+
type SectionJSON,
|
|
14
|
+
type StyleJSON,
|
|
15
|
+
type ThemeJSON,
|
|
16
|
+
} from "./document.js";
|
|
17
|
+
export { migrateToV3 } from "./migrate.js";
|
|
18
|
+
export {
|
|
19
|
+
formatNumber,
|
|
20
|
+
type ListMarker,
|
|
21
|
+
type ParagraphFormat,
|
|
22
|
+
Resolver,
|
|
23
|
+
type RunFormat,
|
|
24
|
+
} from "./resolve.js";
|
|
25
|
+
export {
|
|
26
|
+
checkSection,
|
|
27
|
+
HIGHLIGHT_COLORS,
|
|
28
|
+
JUSTIFICATIONS,
|
|
29
|
+
type Justification,
|
|
30
|
+
RUN_PROPERTY_MARKS,
|
|
31
|
+
schema,
|
|
32
|
+
} from "./schema.js";
|
|
33
|
+
export * from "./units.js";
|