@openpresentation/opf 0.11.4 → 0.12.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 +16 -0
- package/dist/audit.d.ts +156 -0
- package/dist/audit.js +7 -0
- package/dist/{catalogs-CUClB3nl.d.ts → catalogs-BnI7JKcn.d.ts} +3 -3
- package/dist/catalogs.d.ts +2 -2
- package/dist/catalogs.js +2 -1
- package/dist/{chunk-KJKASEPE.js → chunk-4LAM3N6S.js} +753 -42
- package/dist/chunk-6LZK2IZB.js +1690 -0
- package/dist/{chunk-NSVVQ52O.js → chunk-AUM5MXBR.js} +8 -6
- package/dist/{chunk-AEUSVEUY.js → chunk-E34DJ5LL.js} +50 -14
- package/dist/chunk-FYLJVF7X.js +18448 -0
- package/dist/{chunk-HJL64ETN.js → chunk-GJKJL4YY.js} +14 -0
- package/dist/{chunk-3H2U7FDR.js → chunk-HOBSL7AJ.js} +1867 -247
- package/dist/chunk-JKVKYVWL.js +1332 -0
- package/dist/{chunk-3OJN7QAW.js → chunk-MWPMJE6P.js} +18 -4
- package/dist/chunk-RUC7L5DJ.js +261 -0
- package/dist/chunk-S5W34SIJ.js +1 -0
- package/dist/chunk-V2537ENL.js +198 -0
- package/dist/{chunk-RXNFDGPC.js → chunk-WXSPETQ6.js} +311 -40
- package/dist/{composition-Dl-d9FRj.d.ts → composition-DWbCiMwF.d.ts} +559 -10
- package/dist/composition.d.ts +1 -1
- package/dist/composition.js +2 -1
- package/dist/convert.d.ts +261 -0
- package/dist/convert.js +1107 -0
- package/dist/diff.d.ts +145 -0
- package/dist/diff.js +604 -0
- package/dist/docs.js +72 -18
- package/dist/examples.d.ts +1 -1
- package/dist/examples.js +112 -112
- package/dist/font-policy.d.ts +15 -0
- package/dist/font-policy.js +1 -1
- package/dist/format.d.ts +20 -0
- package/dist/format.js +83 -0
- package/dist/index.d.ts +173 -24
- package/dist/index.js +926 -262
- package/dist/lint.d.ts +4 -4
- package/dist/lint.js +6 -5
- package/dist/markdown.d.ts +74 -0
- package/dist/markdown.js +1941 -0
- package/dist/pagination.d.ts +1 -1
- package/dist/pagination.js +6 -5
- package/dist/patch.d.ts +99 -0
- package/dist/patch.js +6 -0
- package/dist/{presentation-j5PVrqfV.d.ts → presentation-DcF-MZcz.d.ts} +452 -17
- package/dist/repo-readme.js +1 -1
- package/dist/{schemas-X_NniU4A.d.ts → schemas-BSjlG8-R.d.ts} +28 -0
- package/dist/schemas.d.ts +1 -1
- package/dist/schemas.js +1 -1
- package/dist/spec/catalogs/audiences/customers.json +5 -1
- package/dist/spec/catalogs/audiences/executives.json +5 -1
- package/dist/spec/catalogs/audiences/index.json +20 -8
- package/dist/spec/catalogs/audiences/investors.json +5 -1
- package/dist/spec/catalogs/audiences/marketing-team.json +5 -1
- package/dist/spec/catalogs/audiences/regulators.json +5 -1
- package/dist/spec/catalogs/audiences/sales-team.json +5 -1
- package/dist/spec/catalogs/manifest.json +2 -2
- package/dist/spec/reference/engine-defaults.json +1 -1
- package/dist/spec/reference/font-policy.json +311 -40
- package/dist/spec/reference/font-policy.schema.json +40 -0
- package/dist/spec/reference/symbol-font-encodings.json +18354 -0
- package/dist/spec/reference/symbol-font-encodings.schema.json +156 -0
- package/dist/spec/schemas/audience.schema.json +7 -7
- package/dist/spec/schemas/narrative.schema.json +1 -1
- package/dist/spec/schemas/opf.schema.json +597 -28
- package/dist/spec-files.d.ts +1 -1
- package/dist/spec-files.js +1 -1
- package/dist/symbol-font-encodings.d.ts +128 -0
- package/dist/symbol-font-encodings.js +1 -0
- package/dist/types.d.ts +4 -4
- package/dist/validator-DfG9yxba.d.ts +172 -0
- package/dist/validator.d.ts +4 -36
- package/dist/validator.js +5 -4
- package/package.json +32 -3
- package/dist/chunk-FDCWQHWO.js +0 -1366
package/dist/markdown.js
ADDED
|
@@ -0,0 +1,1941 @@
|
|
|
1
|
+
import { same, isRecord, parseQuoteLines, levelsFromIndents, parseCodeFence, splitWhen, runText, isPlainRun, FORMATTING, fenceCode } from './chunk-V2537ENL.js';
|
|
2
|
+
import { lintPresentation } from './chunk-MWPMJE6P.js';
|
|
3
|
+
import { validatePresentation } from './chunk-JKVKYVWL.js';
|
|
4
|
+
import './chunk-HOBSL7AJ.js';
|
|
5
|
+
import './chunk-S5W34SIJ.js';
|
|
6
|
+
import './chunk-E34DJ5LL.js';
|
|
7
|
+
import './chunk-4LAM3N6S.js';
|
|
8
|
+
import { parseDocument, YAMLMap, Scalar, stringify } from 'yaml';
|
|
9
|
+
|
|
10
|
+
// src/markdown/canon.ts
|
|
11
|
+
var FORMAT_KEYS = FORMATTING;
|
|
12
|
+
function canonRun(run) {
|
|
13
|
+
if (typeof run === "string") return run;
|
|
14
|
+
if (!isRecord(run) || typeof run.text !== "string") return void 0;
|
|
15
|
+
const out = { text: run.text };
|
|
16
|
+
for (const [key, value] of Object.entries(run)) {
|
|
17
|
+
if (key === "text") continue;
|
|
18
|
+
if (!FORMAT_KEYS.includes(key)) return void 0;
|
|
19
|
+
if (value === false || value === void 0) continue;
|
|
20
|
+
out[key] = value;
|
|
21
|
+
}
|
|
22
|
+
return Object.keys(out).length === 1 ? out.text : out;
|
|
23
|
+
}
|
|
24
|
+
var sameFormat = (a, b) => {
|
|
25
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
26
|
+
keys.delete("text");
|
|
27
|
+
for (const key of keys) if (a[key] !== b[key]) return false;
|
|
28
|
+
return true;
|
|
29
|
+
};
|
|
30
|
+
function canonText(value) {
|
|
31
|
+
if (typeof value === "string") return value;
|
|
32
|
+
if (!Array.isArray(value)) return void 0;
|
|
33
|
+
const merged = [];
|
|
34
|
+
for (const raw of value) {
|
|
35
|
+
const run = canonRun(raw);
|
|
36
|
+
if (run === void 0) return void 0;
|
|
37
|
+
if (runText(run) === "") continue;
|
|
38
|
+
const last = merged.at(-1);
|
|
39
|
+
if (last !== void 0) {
|
|
40
|
+
if (typeof last === "string" && typeof run === "string") {
|
|
41
|
+
merged[merged.length - 1] = last + run;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (typeof last === "object" && typeof run === "object" && sameFormat(last, run)) {
|
|
45
|
+
merged[merged.length - 1] = { ...last, text: last.text + run.text };
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
merged.push(run);
|
|
50
|
+
}
|
|
51
|
+
if (merged.length === 0) return "";
|
|
52
|
+
if (merged.every(isPlainRun)) return merged.map(runText).join("");
|
|
53
|
+
return merged;
|
|
54
|
+
}
|
|
55
|
+
function canonItem(item) {
|
|
56
|
+
if (typeof item === "string" || Array.isArray(item)) return canonText(item);
|
|
57
|
+
if (!isRecord(item)) return void 0;
|
|
58
|
+
for (const key of Object.keys(item)) if (key !== "text" && key !== "level" && key !== "description") return void 0;
|
|
59
|
+
const text = canonText(item.text);
|
|
60
|
+
if (text === void 0) return void 0;
|
|
61
|
+
const level = item.level === void 0 ? 0 : item.level;
|
|
62
|
+
if (!Number.isInteger(level) || level < 0) return void 0;
|
|
63
|
+
const description = item.description === void 0 ? void 0 : canonText(item.description);
|
|
64
|
+
if (item.description !== void 0 && description === void 0) return void 0;
|
|
65
|
+
if (level === 0 && description === void 0) return text;
|
|
66
|
+
const out = { text };
|
|
67
|
+
if (description !== void 0) out.description = description;
|
|
68
|
+
if (level !== 0) out.level = level;
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
var levelOf = (item) => isRecord(item) && typeof item.level === "number" ? item.level : 0;
|
|
72
|
+
function canonItems(value) {
|
|
73
|
+
if (!Array.isArray(value) || value.length === 0) return void 0;
|
|
74
|
+
const items = [];
|
|
75
|
+
let previous = -1;
|
|
76
|
+
for (const raw of value) {
|
|
77
|
+
const item = canonItem(raw);
|
|
78
|
+
if (item === void 0) return void 0;
|
|
79
|
+
const level = levelOf(item);
|
|
80
|
+
if (level > previous + 1 || previous < 0 && level !== 0) return void 0;
|
|
81
|
+
previous = level;
|
|
82
|
+
items.push(item);
|
|
83
|
+
}
|
|
84
|
+
return items;
|
|
85
|
+
}
|
|
86
|
+
var only = (value, keys) => Object.keys(value).every((key) => keys.includes(key));
|
|
87
|
+
function canonQuote(value) {
|
|
88
|
+
if (typeof value === "string") return value;
|
|
89
|
+
if (!isRecord(value) || !only(value, ["text", "attribution", "source"]) || typeof value.text !== "string") return void 0;
|
|
90
|
+
for (const key of ["attribution", "source"]) if (value[key] !== void 0 && typeof value[key] !== "string") return void 0;
|
|
91
|
+
if (value.source !== void 0 && value.attribution === void 0) return void 0;
|
|
92
|
+
if (value.attribution === void 0) return value.text;
|
|
93
|
+
const out = { text: value.text, attribution: value.attribution };
|
|
94
|
+
if (value.source !== void 0) out.source = value.source;
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
function canonCode(value) {
|
|
98
|
+
if (typeof value === "string") return value;
|
|
99
|
+
if (!isRecord(value) || !only(value, ["source", "language", "filename"]) || typeof value.source !== "string") return void 0;
|
|
100
|
+
for (const key of ["language", "filename"]) if (value[key] !== void 0 && (typeof value[key] !== "string" || value[key] === "")) return void 0;
|
|
101
|
+
if (value.language === void 0 && value.filename === void 0) return value.source;
|
|
102
|
+
const out = { source: value.source };
|
|
103
|
+
if (value.language !== void 0) out.language = value.language;
|
|
104
|
+
if (value.filename !== void 0) out.filename = value.filename;
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
function canonMetric(value) {
|
|
108
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
109
|
+
if (!isRecord(value) || !only(value, ["value", "label", "description", "unit", "delta", "trend"])) return void 0;
|
|
110
|
+
if (typeof value.value !== "string" && typeof value.value !== "number") return void 0;
|
|
111
|
+
const out = { value: value.value };
|
|
112
|
+
for (const key of ["label", "description", "unit", "delta", "trend"]) {
|
|
113
|
+
const entry = value[key];
|
|
114
|
+
if (entry === void 0) continue;
|
|
115
|
+
if (typeof entry !== "string" && !(key === "delta" && typeof entry === "number")) return void 0;
|
|
116
|
+
out[key] = entry;
|
|
117
|
+
}
|
|
118
|
+
return Object.keys(out).length === 1 ? out.value : out;
|
|
119
|
+
}
|
|
120
|
+
function canonAsset(value) {
|
|
121
|
+
if (typeof value === "string") return value;
|
|
122
|
+
if (!isRecord(value) || !only(value, ["src", "alt", "title"]) || typeof value.src !== "string") return void 0;
|
|
123
|
+
for (const key of ["alt", "title"]) if (value[key] !== void 0 && typeof value[key] !== "string") return void 0;
|
|
124
|
+
const out = { src: value.src };
|
|
125
|
+
if (value.alt !== void 0) out.alt = value.alt;
|
|
126
|
+
if (value.title !== void 0) out.title = value.title;
|
|
127
|
+
return Object.keys(out).length === 1 ? out.src : out;
|
|
128
|
+
}
|
|
129
|
+
function canonCell(cell) {
|
|
130
|
+
if (cell === null) return cell;
|
|
131
|
+
if (typeof cell === "string" || Array.isArray(cell)) return canonText(cell);
|
|
132
|
+
return void 0;
|
|
133
|
+
}
|
|
134
|
+
function canonTable(value) {
|
|
135
|
+
if (!isRecord(value) || !only(value, ["columns", "rows"]) || !Array.isArray(value.rows)) return void 0;
|
|
136
|
+
const out = {};
|
|
137
|
+
if (value.columns !== void 0) {
|
|
138
|
+
if (!Array.isArray(value.columns) || value.columns.length === 0) return void 0;
|
|
139
|
+
const columns = value.columns.map((cell) => cell === null ? null : typeof cell === "string" || Array.isArray(cell) ? canonText(cell) : void 0);
|
|
140
|
+
if (columns.includes(void 0) || columns.every((cell) => cell === null || cell === "")) return void 0;
|
|
141
|
+
out.columns = columns;
|
|
142
|
+
}
|
|
143
|
+
const rows = [];
|
|
144
|
+
for (const row of value.rows) {
|
|
145
|
+
if (!Array.isArray(row)) return void 0;
|
|
146
|
+
const cells = row.map(canonCell);
|
|
147
|
+
if (cells.includes(void 0)) return void 0;
|
|
148
|
+
rows.push(cells);
|
|
149
|
+
}
|
|
150
|
+
out.rows = rows;
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
function canonChart(value) {
|
|
154
|
+
if (!isRecord(value) || !only(value, ["type", "data"]) || typeof value.type !== "string" || !isRecord(value.data)) return void 0;
|
|
155
|
+
const data = value.data;
|
|
156
|
+
if (Array.isArray(data.columns) && Array.isArray(data.rows) && only(data, ["columns", "rows"])) {
|
|
157
|
+
if (!data.columns.every((column) => typeof column === "string") || data.columns.length === 0 || data.rows.length === 0) return void 0;
|
|
158
|
+
for (const row of data.rows) if (!Array.isArray(row) || !row.every((cell) => cell === null || ["string", "number", "boolean"].includes(typeof cell))) return void 0;
|
|
159
|
+
return { type: value.type, data: { columns: data.columns, rows: data.rows } };
|
|
160
|
+
}
|
|
161
|
+
if (typeof data.src === "string" && only(data, ["src", "sheet", "range", "columns"])) return { type: value.type, data };
|
|
162
|
+
return void 0;
|
|
163
|
+
}
|
|
164
|
+
function canonEvent(event) {
|
|
165
|
+
if (!isRecord(event) || !only(event, ["when", "what", "description"]) || typeof event.what !== "string") return void 0;
|
|
166
|
+
for (const key of ["when", "description"]) if (event[key] !== void 0 && typeof event[key] !== "string") return void 0;
|
|
167
|
+
const out = {};
|
|
168
|
+
if (event.when !== void 0) out.when = event.when;
|
|
169
|
+
out.what = event.what;
|
|
170
|
+
if (event.description !== void 0) out.description = event.description;
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
function canonTimeline(value) {
|
|
174
|
+
const events = Array.isArray(value) ? value : isRecord(value) && only(value, ["name", "description", "events"]) ? value.events : void 0;
|
|
175
|
+
if (!Array.isArray(events) || events.length === 0) return void 0;
|
|
176
|
+
const canon = events.map(canonEvent);
|
|
177
|
+
if (canon.includes(void 0)) return void 0;
|
|
178
|
+
if (Array.isArray(value)) return canon;
|
|
179
|
+
for (const key of ["name", "description"]) if (value[key] !== void 0 && typeof value[key] !== "string") return void 0;
|
|
180
|
+
const { name, description } = value;
|
|
181
|
+
if (name === void 0 && description === void 0) return canon;
|
|
182
|
+
const out = {};
|
|
183
|
+
if (name !== void 0) out.name = name;
|
|
184
|
+
if (description !== void 0) out.description = description;
|
|
185
|
+
out.events = canon;
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
function canonContent(key, value) {
|
|
189
|
+
switch (key) {
|
|
190
|
+
case "text":
|
|
191
|
+
return canonText(value);
|
|
192
|
+
case "items":
|
|
193
|
+
case "bullets":
|
|
194
|
+
return canonItems(value);
|
|
195
|
+
case "quote":
|
|
196
|
+
return canonQuote(value);
|
|
197
|
+
case "code":
|
|
198
|
+
return canonCode(value);
|
|
199
|
+
case "metric":
|
|
200
|
+
return canonMetric(value);
|
|
201
|
+
case "image":
|
|
202
|
+
case "video":
|
|
203
|
+
return canonAsset(value);
|
|
204
|
+
case "table":
|
|
205
|
+
return canonTable(value);
|
|
206
|
+
case "chart":
|
|
207
|
+
return canonChart(value);
|
|
208
|
+
case "timeline":
|
|
209
|
+
return canonTimeline(value);
|
|
210
|
+
default:
|
|
211
|
+
return void 0;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// src/markdown/inline.ts
|
|
216
|
+
var PUNCT = /[\p{P}\p{S}]/u;
|
|
217
|
+
var WS = /\s/u;
|
|
218
|
+
var ALNUM = /[\p{L}\p{N}]/u;
|
|
219
|
+
var ASCII_PUNCT = /^[!-/:-@[-`{-~]$/;
|
|
220
|
+
var MAX_DEPTH = 16;
|
|
221
|
+
var MAX_TARGET = 2e3;
|
|
222
|
+
var MAX_SPAN = 300;
|
|
223
|
+
var SPAN_FLAGS = {
|
|
224
|
+
bold: "bold",
|
|
225
|
+
italic: "italic",
|
|
226
|
+
underline: "underline",
|
|
227
|
+
strike: "strikethrough",
|
|
228
|
+
strikethrough: "strikethrough",
|
|
229
|
+
sup: "superscript",
|
|
230
|
+
superscript: "superscript",
|
|
231
|
+
sub: "subscript",
|
|
232
|
+
subscript: "subscript"
|
|
233
|
+
};
|
|
234
|
+
var SPAN_VALUES = { color: "color", size: "fontSize", fontsize: "fontSize", font: "fontFamily", fontfamily: "fontFamily" };
|
|
235
|
+
function parseAttributes(source) {
|
|
236
|
+
const pairs = [];
|
|
237
|
+
let i = 0;
|
|
238
|
+
while (i < source.length) {
|
|
239
|
+
while (i < source.length && /[\s,]/.test(source[i])) i++;
|
|
240
|
+
if (i >= source.length) break;
|
|
241
|
+
const key = /^[A-Za-z][\w-]*/.exec(source.slice(i))?.[0];
|
|
242
|
+
if (!key) return { pairs, error: `Expected an attribute name at ${JSON.stringify(source.slice(i, i + 12))}.` };
|
|
243
|
+
i += key.length;
|
|
244
|
+
if (source[i] !== "=") {
|
|
245
|
+
pairs.push([key, true]);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
i++;
|
|
249
|
+
if (source[i] === '"') {
|
|
250
|
+
let j = i + 1;
|
|
251
|
+
while (j < source.length && source[j] !== '"') j += source[j] === "\\" ? 2 : 1;
|
|
252
|
+
if (j >= source.length) return { pairs, error: `Unterminated quoted value for ${key}.` };
|
|
253
|
+
try {
|
|
254
|
+
pairs.push([key, JSON.parse(source.slice(i, j + 1))]);
|
|
255
|
+
} catch {
|
|
256
|
+
return { pairs, error: `Invalid quoted value for ${key}; use JSON string escapes.` };
|
|
257
|
+
}
|
|
258
|
+
i = j + 1;
|
|
259
|
+
} else {
|
|
260
|
+
const value = /^[^\s,"]+/.exec(source.slice(i))?.[0];
|
|
261
|
+
if (!value) return { pairs, error: `Missing value for ${key}.` };
|
|
262
|
+
pairs.push([key, value]);
|
|
263
|
+
i += value.length;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return { pairs };
|
|
267
|
+
}
|
|
268
|
+
function spanStyle(source, issue) {
|
|
269
|
+
const style = {};
|
|
270
|
+
const { pairs, error } = parseAttributes(source);
|
|
271
|
+
if (error) issue("markdown/span-attributes", `${error} A span reads {bold italic color=#ff0000 size=24 font="Open Sans"}.`);
|
|
272
|
+
for (const [rawKey, value] of pairs) {
|
|
273
|
+
const key = rawKey.toLowerCase();
|
|
274
|
+
const flag = SPAN_FLAGS[key];
|
|
275
|
+
const field = SPAN_VALUES[key];
|
|
276
|
+
if (flag) {
|
|
277
|
+
if (value === true || value === "true") style[flag] = true;
|
|
278
|
+
else if (value !== "false") issue("markdown/span-attributes", `Span flag ${rawKey} takes no value (or =true/false).`);
|
|
279
|
+
} else if (field === "fontSize") {
|
|
280
|
+
const size = value === true ? Number.NaN : Number(value);
|
|
281
|
+
if (Number.isFinite(size) && size > 0) style.fontSize = size;
|
|
282
|
+
else issue("markdown/span-attributes", `Span size must be a positive number, not ${JSON.stringify(value)}.`);
|
|
283
|
+
} else if (field) {
|
|
284
|
+
if (typeof value === "string" && value) style[field] = value;
|
|
285
|
+
else issue("markdown/span-attributes", `Span ${rawKey} needs a value.`);
|
|
286
|
+
} else issue("markdown/span-attributes", `Unknown span attribute ${JSON.stringify(rawKey)}. Known: bold, italic, underline, strike, sup, sub, color, size, font.`);
|
|
287
|
+
}
|
|
288
|
+
return style;
|
|
289
|
+
}
|
|
290
|
+
var TAGS = { u: "underline", sup: "superscript", sub: "subscript" };
|
|
291
|
+
function matchBracket(s, start) {
|
|
292
|
+
let depth = 0;
|
|
293
|
+
for (let i = start; i < s.length; i++) {
|
|
294
|
+
const c = s[i];
|
|
295
|
+
if (c === "\\") i++;
|
|
296
|
+
else if (c === "[") depth++;
|
|
297
|
+
else if (c === "]" && --depth === 0) return i;
|
|
298
|
+
}
|
|
299
|
+
return -1;
|
|
300
|
+
}
|
|
301
|
+
var ANGLE_AT = /<([^<>\n]*)>/y;
|
|
302
|
+
function readDestination(s, open) {
|
|
303
|
+
let i = open + 1;
|
|
304
|
+
while (s[i] === " ") i++;
|
|
305
|
+
let url = "";
|
|
306
|
+
if (s[i] === "<") {
|
|
307
|
+
ANGLE_AT.lastIndex = i;
|
|
308
|
+
const angle = ANGLE_AT.exec(s);
|
|
309
|
+
if (!angle) return void 0;
|
|
310
|
+
url = unescapeInline(angle[1]);
|
|
311
|
+
i += angle[0].length;
|
|
312
|
+
} else {
|
|
313
|
+
let depth = 0;
|
|
314
|
+
const from = i;
|
|
315
|
+
for (; i < s.length && i - from <= MAX_TARGET; i++) {
|
|
316
|
+
const c = s[i];
|
|
317
|
+
if (c === "\\" && ASCII_PUNCT.test(s[i + 1] ?? "")) {
|
|
318
|
+
i++;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (/\s/.test(c)) break;
|
|
322
|
+
if (c === "(") depth++;
|
|
323
|
+
else if (c === ")") {
|
|
324
|
+
if (depth === 0) break;
|
|
325
|
+
depth--;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (i - from > MAX_TARGET) return void 0;
|
|
329
|
+
url = unescapeInline(s.slice(from, i));
|
|
330
|
+
}
|
|
331
|
+
while (s[i] === " ") i++;
|
|
332
|
+
let title;
|
|
333
|
+
if (s[i] === '"' || s[i] === "'") {
|
|
334
|
+
const quote = s[i];
|
|
335
|
+
let j = i + 1;
|
|
336
|
+
while (j < s.length && j - i <= MAX_TARGET && s[j] !== quote) j += s[j] === "\\" ? 2 : 1;
|
|
337
|
+
if (j >= s.length || j - i > MAX_TARGET) return void 0;
|
|
338
|
+
title = unescapeInline(s.slice(i + 1, j));
|
|
339
|
+
i = j + 1;
|
|
340
|
+
while (s[i] === " ") i++;
|
|
341
|
+
}
|
|
342
|
+
if (s[i] !== ")") return void 0;
|
|
343
|
+
return title === void 0 ? { url, end: i + 1 } : { url, end: i + 1, title };
|
|
344
|
+
}
|
|
345
|
+
function unescapeInline(text) {
|
|
346
|
+
return text.replace(/\\([!-/:-@[-`{-~])/g, "$1");
|
|
347
|
+
}
|
|
348
|
+
function flanking(prev, next, ch) {
|
|
349
|
+
const left = !WS.test(next) && (!PUNCT.test(next) || WS.test(prev) || PUNCT.test(prev));
|
|
350
|
+
const right = !WS.test(prev) && (!PUNCT.test(prev) || WS.test(next) || PUNCT.test(next));
|
|
351
|
+
if (ch === "_") return { canOpen: left && (!right || PUNCT.test(prev)), canClose: right && (!left || PUNCT.test(next)) };
|
|
352
|
+
return { canOpen: left, canClose: right };
|
|
353
|
+
}
|
|
354
|
+
function bracketPairs(s) {
|
|
355
|
+
const pairs = /* @__PURE__ */ new Map();
|
|
356
|
+
const open = [];
|
|
357
|
+
for (let i = 0; i < s.length; i++) {
|
|
358
|
+
const c = s[i];
|
|
359
|
+
if (c === "\\") i++;
|
|
360
|
+
else if (c === "[") open.push(i);
|
|
361
|
+
else if (c === "]" && open.length) pairs.set(open.pop(), i);
|
|
362
|
+
}
|
|
363
|
+
return pairs;
|
|
364
|
+
}
|
|
365
|
+
var TAG_AT = /<(\/?)(u|sup|sub)>/iy;
|
|
366
|
+
var BR_AT = /<br\s*\/?>/iy;
|
|
367
|
+
var AUTOLINK_AT = /<([A-Za-z][A-Za-z0-9+.-]{1,31}:[^\s<>]*)>/y;
|
|
368
|
+
function tokenize(s, issue, depth, budget) {
|
|
369
|
+
const tokens = [];
|
|
370
|
+
const pairs = bracketPairs(s);
|
|
371
|
+
let text = "";
|
|
372
|
+
const flush = () => {
|
|
373
|
+
if (text) tokens.push({ t: "text", v: text });
|
|
374
|
+
text = "";
|
|
375
|
+
};
|
|
376
|
+
const before = (i) => i > 0 ? s[i - 1] : " ";
|
|
377
|
+
for (let i = 0; i < s.length; ) {
|
|
378
|
+
const c = s[i];
|
|
379
|
+
if (c === "\\" && i + 1 < s.length && ASCII_PUNCT.test(s[i + 1])) {
|
|
380
|
+
text += s[i + 1];
|
|
381
|
+
i += 2;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (c === "[") {
|
|
385
|
+
const close = depth < MAX_DEPTH ? pairs.get(i) ?? -1 : -1;
|
|
386
|
+
if (close > 0) {
|
|
387
|
+
const next = s[close + 1];
|
|
388
|
+
if (next === "(" && budget.left > 0) {
|
|
389
|
+
const dest = readDestination(s, close + 1);
|
|
390
|
+
budget.left -= dest ? dest.end - close : Math.min(MAX_TARGET, s.length - close);
|
|
391
|
+
if (dest) {
|
|
392
|
+
flush();
|
|
393
|
+
tokens.push({ t: "node", children: parseSegs(s.slice(i + 1, close), issue, depth + 1, budget), apply: dest.url ? { link: dest.url } : {} });
|
|
394
|
+
i = dest.end;
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
} else if (next === "{") {
|
|
398
|
+
const end = s.slice(close + 2, close + 2 + MAX_SPAN).indexOf("}") + close + 2;
|
|
399
|
+
if (end > close + 1 && !s.slice(close + 2, end).includes("\n")) {
|
|
400
|
+
flush();
|
|
401
|
+
tokens.push({ t: "node", children: parseSegs(s.slice(i + 1, close), issue, depth + 1, budget), apply: spanStyle(s.slice(close + 2, end), issue) });
|
|
402
|
+
i = end + 1;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
text += c;
|
|
408
|
+
i++;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (c === "<") {
|
|
412
|
+
TAG_AT.lastIndex = i;
|
|
413
|
+
const tag = TAG_AT.exec(s);
|
|
414
|
+
if (tag) {
|
|
415
|
+
flush();
|
|
416
|
+
tokens.push({ t: "tag", name: TAGS[tag[2].toLowerCase()], close: tag[1] === "/", matched: false });
|
|
417
|
+
i += tag[0].length;
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
BR_AT.lastIndex = i;
|
|
421
|
+
const br = BR_AT.exec(s);
|
|
422
|
+
if (br) {
|
|
423
|
+
text += "\n";
|
|
424
|
+
i += br[0].length;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
AUTOLINK_AT.lastIndex = i;
|
|
428
|
+
const auto = AUTOLINK_AT.exec(s);
|
|
429
|
+
if (auto) {
|
|
430
|
+
flush();
|
|
431
|
+
tokens.push({ t: "node", children: [{ text: auto[1], style: {} }], apply: { link: auto[1] } });
|
|
432
|
+
i += auto[0].length;
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if (c === "*" || c === "_" || c === "~") {
|
|
437
|
+
let j = i;
|
|
438
|
+
while (s[j] === c) j++;
|
|
439
|
+
const n = j - i;
|
|
440
|
+
if (n <= 3 && (c !== "~" || n === 2)) {
|
|
441
|
+
flush();
|
|
442
|
+
const { canOpen, canClose } = flanking(before(i), s[j] ?? " ", c);
|
|
443
|
+
tokens.push({ t: "delim", ch: c, n, orig: n, canOpen, canClose, opens: [], closes: [] });
|
|
444
|
+
i = j;
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
text += s.slice(i, j);
|
|
448
|
+
i = j;
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
text += c;
|
|
452
|
+
i++;
|
|
453
|
+
}
|
|
454
|
+
flush();
|
|
455
|
+
return tokens;
|
|
456
|
+
}
|
|
457
|
+
function resolve(tokens) {
|
|
458
|
+
const stack = [];
|
|
459
|
+
for (let k = 0; k < tokens.length; k++) {
|
|
460
|
+
const token = tokens[k];
|
|
461
|
+
if (token.t === "tag") {
|
|
462
|
+
if (!token.close) stack.push(k);
|
|
463
|
+
else {
|
|
464
|
+
for (let s = stack.length - 1; s >= 0; s--) {
|
|
465
|
+
const other = tokens[stack[s]];
|
|
466
|
+
if (other?.t === "tag" && other.name === token.name && !other.matched) {
|
|
467
|
+
other.matched = token.matched = true;
|
|
468
|
+
stack.length = s;
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
if (token.t !== "delim") continue;
|
|
476
|
+
if (token.canClose) {
|
|
477
|
+
while (token.n > 0) {
|
|
478
|
+
let found = -1;
|
|
479
|
+
for (let s = stack.length - 1; s >= 0; s--) {
|
|
480
|
+
const other = tokens[stack[s]];
|
|
481
|
+
if (other?.t === "delim" && other.ch === token.ch && other.n > 0 && other.canOpen) {
|
|
482
|
+
if ((other.canClose || token.canOpen) && (other.orig + token.orig) % 3 === 0 && !(other.orig % 3 === 0 && token.orig % 3 === 0)) continue;
|
|
483
|
+
found = s;
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
if (found < 0) break;
|
|
488
|
+
const opener = tokens[stack[found]];
|
|
489
|
+
const use = token.ch === "~" ? 2 : Math.min(opener.n, token.n) >= 2 ? 2 : 1;
|
|
490
|
+
const style = token.ch === "~" ? "strikethrough" : use === 2 ? "bold" : "italic";
|
|
491
|
+
opener.n -= use;
|
|
492
|
+
token.n -= use;
|
|
493
|
+
opener.opens.push(style);
|
|
494
|
+
token.closes.push(style);
|
|
495
|
+
stack.length = opener.n > 0 ? found + 1 : found;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
if (token.n > 0 && token.canOpen) stack.push(k);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
function addStyle(base, add) {
|
|
502
|
+
return { ...add, ...base };
|
|
503
|
+
}
|
|
504
|
+
function parseSegs(s, issue, depth, budget) {
|
|
505
|
+
const tokens = tokenize(s, issue, depth, budget);
|
|
506
|
+
resolve(tokens);
|
|
507
|
+
const segs = [];
|
|
508
|
+
const counts = {};
|
|
509
|
+
const current = () => {
|
|
510
|
+
const style = {};
|
|
511
|
+
for (const key of ["bold", "italic", "strikethrough", "underline", "superscript", "subscript"]) if ((counts[key] ?? 0) > 0) style[key] = true;
|
|
512
|
+
return style;
|
|
513
|
+
};
|
|
514
|
+
const push = (text, style = current()) => {
|
|
515
|
+
if (text) segs.push({ text, style });
|
|
516
|
+
};
|
|
517
|
+
for (const token of tokens) {
|
|
518
|
+
if (token.t === "text") push(token.v);
|
|
519
|
+
else if (token.t === "node") for (const child of token.children) segs.push({ text: child.text, style: addStyle(child.style, addStyle(token.apply, current())) });
|
|
520
|
+
else if (token.t === "tag") {
|
|
521
|
+
if (!token.matched) push(`<${token.close ? "/" : ""}${token.name === "underline" ? "u" : token.name === "superscript" ? "sup" : "sub"}>`);
|
|
522
|
+
else counts[token.name] = (counts[token.name] ?? 0) + (token.close ? -1 : 1);
|
|
523
|
+
} else {
|
|
524
|
+
for (const style of token.closes) counts[style] = (counts[style] ?? 0) - 1;
|
|
525
|
+
push(token.ch.repeat(token.n));
|
|
526
|
+
for (const style of [...token.opens].reverse()) counts[style] = (counts[style] ?? 0) + 1;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return segs;
|
|
530
|
+
}
|
|
531
|
+
var sameStyle = (a, b) => {
|
|
532
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
533
|
+
for (const key of keys) if (a[key] !== b[key]) return false;
|
|
534
|
+
return true;
|
|
535
|
+
};
|
|
536
|
+
function parseInlineSegments(text, issue = () => {
|
|
537
|
+
}) {
|
|
538
|
+
const merged = [];
|
|
539
|
+
const budget = { left: 40 * text.length + 1e3 };
|
|
540
|
+
for (const seg of parseSegs(text, issue, 0, budget)) {
|
|
541
|
+
const last = merged.at(-1);
|
|
542
|
+
if (last && sameStyle(last.style, seg.style)) last.text += seg.text;
|
|
543
|
+
else merged.push({ text: seg.text, style: seg.style });
|
|
544
|
+
}
|
|
545
|
+
return merged;
|
|
546
|
+
}
|
|
547
|
+
var toRun = (seg) => {
|
|
548
|
+
const keys = Object.keys(seg.style);
|
|
549
|
+
if (!keys.length) return seg.text;
|
|
550
|
+
const run = { text: seg.text };
|
|
551
|
+
for (const key of FORMATTING) if (seg.style[key] !== void 0) run[key] = seg.style[key];
|
|
552
|
+
return run;
|
|
553
|
+
};
|
|
554
|
+
function parseInline(text, issue = () => {
|
|
555
|
+
}) {
|
|
556
|
+
const segs = parseInlineSegments(text, issue);
|
|
557
|
+
if (!segs.length) return "";
|
|
558
|
+
if (segs.every((seg) => !Object.keys(seg.style).length)) return segs.map((seg) => seg.text).join("");
|
|
559
|
+
return segs.map(toRun);
|
|
560
|
+
}
|
|
561
|
+
function parsePlainInline(text, issue = () => {
|
|
562
|
+
}) {
|
|
563
|
+
const segs = parseInlineSegments(text, issue);
|
|
564
|
+
if (segs.some((seg) => Object.keys(seg.style).length)) issue("markdown/formatting-dropped", "This field holds plain text, so bold, italic, links and other formatting in it were dropped.");
|
|
565
|
+
return segs.map((seg) => seg.text).join("");
|
|
566
|
+
}
|
|
567
|
+
var BLOCK_START = [
|
|
568
|
+
/^#{1,6}(?:\s|$)/,
|
|
569
|
+
// heading
|
|
570
|
+
/^>/,
|
|
571
|
+
// quote
|
|
572
|
+
/^[-+*](?:\s|$)/,
|
|
573
|
+
// bullet
|
|
574
|
+
/^\d{1,9}[.)](?:\s|$)/,
|
|
575
|
+
// numbered item
|
|
576
|
+
/^(?:`{3,}|~{3,})/,
|
|
577
|
+
// fence
|
|
578
|
+
/^-{3,}\s*$/,
|
|
579
|
+
// slide separator
|
|
580
|
+
/^\|/,
|
|
581
|
+
// table
|
|
582
|
+
/^!\[/,
|
|
583
|
+
// image
|
|
584
|
+
/^Notes?:/,
|
|
585
|
+
// speaker notes
|
|
586
|
+
/^<!--/,
|
|
587
|
+
// comment
|
|
588
|
+
/^:(?:\s|$)/
|
|
589
|
+
// list item description
|
|
590
|
+
];
|
|
591
|
+
function escapeLineStart(line) {
|
|
592
|
+
if (!BLOCK_START.some((pattern) => pattern.test(line))) return line;
|
|
593
|
+
const match = /^(\d{1,9})([.)])/.exec(line);
|
|
594
|
+
if (match) return `${match[1]}\\${line.slice(match[1].length)}`;
|
|
595
|
+
const notes = /^(Notes?)(:)/.exec(line);
|
|
596
|
+
if (notes) return `${notes[1]}\\${line.slice(notes[1].length)}`;
|
|
597
|
+
return `\\${line}`;
|
|
598
|
+
}
|
|
599
|
+
function escapeInline(text) {
|
|
600
|
+
let out = "";
|
|
601
|
+
for (let i = 0; i < text.length; i++) {
|
|
602
|
+
const c = text[i];
|
|
603
|
+
const next = text[i + 1];
|
|
604
|
+
if (c === "\\") out += next === void 0 || ASCII_PUNCT.test(next) ? "\\\\" : c;
|
|
605
|
+
else if (c === "*" || c === "[" || c === "]") out += `\\${c}`;
|
|
606
|
+
else if (c === "<") out += next !== void 0 && /[A-Za-z/!]/.test(next) ? "\\<" : c;
|
|
607
|
+
else if (c === "_") out += ALNUM.test(text[i - 1] ?? "") && ALNUM.test(text[i + 1] ?? "") ? c : "\\_";
|
|
608
|
+
else if (c === "~") out += text[i - 1] === "~" || text[i + 1] === "~" ? "\\~" : c;
|
|
609
|
+
else out += c;
|
|
610
|
+
}
|
|
611
|
+
return out;
|
|
612
|
+
}
|
|
613
|
+
function serializeRun(run, breakText) {
|
|
614
|
+
const text = runText(run);
|
|
615
|
+
if (typeof run === "string") return text.split("\n").map(escapeInline).join(breakText);
|
|
616
|
+
if (!isRecord(run) || typeof run.text !== "string") return void 0;
|
|
617
|
+
for (const key of Object.keys(run)) if (key !== "text" && !FORMATTING.includes(key)) return void 0;
|
|
618
|
+
const lead = /^\s*/.exec(text)[0];
|
|
619
|
+
const trail = /\s*$/.exec(text.slice(lead.length))[0];
|
|
620
|
+
const core = text.slice(lead.length, text.length - trail.length);
|
|
621
|
+
const edge = (s) => s.split("\n").map(escapeInline).join(breakText);
|
|
622
|
+
if (!core) return edge(text);
|
|
623
|
+
let out = core.split("\n").map(escapeInline).join(breakText);
|
|
624
|
+
if (run.color !== void 0 || run.fontSize !== void 0 || run.fontFamily !== void 0) {
|
|
625
|
+
const attrs = [];
|
|
626
|
+
if (run.color !== void 0) attrs.push(`color=${attrValue(String(run.color))}`);
|
|
627
|
+
if (run.fontSize !== void 0) attrs.push(`size=${String(run.fontSize)}`);
|
|
628
|
+
if (run.fontFamily !== void 0) attrs.push(`font=${attrValue(String(run.fontFamily))}`);
|
|
629
|
+
out = `[${out}]{${attrs.join(" ")}}`;
|
|
630
|
+
}
|
|
631
|
+
if (run.subscript === true) out = `<sub>${out}</sub>`;
|
|
632
|
+
if (run.superscript === true) out = `<sup>${out}</sup>`;
|
|
633
|
+
if (run.underline === true) out = `<u>${out}</u>`;
|
|
634
|
+
if (run.strikethrough === true) out = `~~${out}~~`;
|
|
635
|
+
if (run.italic === true) out = `*${out}*`;
|
|
636
|
+
if (run.bold === true) out = `**${out}**`;
|
|
637
|
+
if (run.link !== void 0) {
|
|
638
|
+
const url = String(run.link);
|
|
639
|
+
if (!url || /[\n\r<>]/.test(url)) return void 0;
|
|
640
|
+
out = `[${out}](${/^[^\s()]+$/.test(url) ? url : `<${url}>`})`;
|
|
641
|
+
}
|
|
642
|
+
return edge(lead) + out + edge(trail);
|
|
643
|
+
}
|
|
644
|
+
var attrValue = (value) => /^[^\s,"}]+$/.test(value) ? value : JSON.stringify(value);
|
|
645
|
+
function serializeRuns(value, mode = "paragraph") {
|
|
646
|
+
const runs = typeof value === "string" ? [value] : Array.isArray(value) ? value : void 0;
|
|
647
|
+
if (!runs) return void 0;
|
|
648
|
+
const parts = [];
|
|
649
|
+
for (const run of runs) {
|
|
650
|
+
const part = serializeRun(run, mode === "cell" ? "<br>" : "\\\n");
|
|
651
|
+
if (part === void 0) return void 0;
|
|
652
|
+
parts.push(part);
|
|
653
|
+
}
|
|
654
|
+
const lines = parts.join("").split("\n");
|
|
655
|
+
return mode === "cell" ? [lines.join("")] : lines.map(escapeLineStart);
|
|
656
|
+
}
|
|
657
|
+
function splitLines(source) {
|
|
658
|
+
const lines = [];
|
|
659
|
+
let start = 0;
|
|
660
|
+
let no = 1;
|
|
661
|
+
let i = 0;
|
|
662
|
+
while (i < source.length) {
|
|
663
|
+
const c = source[i];
|
|
664
|
+
if (c === "\n" || c === "\r") {
|
|
665
|
+
lines.push({ text: source.slice(start, i), start, end: i, no: no++ });
|
|
666
|
+
if (c === "\r" && source[i + 1] === "\n") i++;
|
|
667
|
+
i++;
|
|
668
|
+
start = i;
|
|
669
|
+
} else i++;
|
|
670
|
+
}
|
|
671
|
+
if (start < source.length) lines.push({ text: source.slice(start), start, end: source.length, no });
|
|
672
|
+
return lines;
|
|
673
|
+
}
|
|
674
|
+
var Ctx = class {
|
|
675
|
+
constructor(source) {
|
|
676
|
+
this.source = source;
|
|
677
|
+
for (let i = 0; i < source.length; i++) {
|
|
678
|
+
if (source[i] === "\r") {
|
|
679
|
+
if (source[i + 1] === "\n") i++;
|
|
680
|
+
this.lineStarts.push(i + 1);
|
|
681
|
+
} else if (source[i] === "\n") this.lineStarts.push(i + 1);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
source;
|
|
685
|
+
diagnostics = [];
|
|
686
|
+
/** JSON Pointer in the converted document to the source range that produced it. */
|
|
687
|
+
ranges = /* @__PURE__ */ new Map();
|
|
688
|
+
lineStarts = [0];
|
|
689
|
+
location(range) {
|
|
690
|
+
let lo = 0;
|
|
691
|
+
let hi = this.lineStarts.length;
|
|
692
|
+
while (lo + 1 < hi) {
|
|
693
|
+
const mid = lo + hi >> 1;
|
|
694
|
+
if ((this.lineStarts[mid] ?? 0) <= range.start) lo = mid;
|
|
695
|
+
else hi = mid;
|
|
696
|
+
}
|
|
697
|
+
return { offset: range.start, length: Math.max(0, range.end - range.start), line: lo + 1, column: range.start - (this.lineStarts[lo] ?? 0) + 1 };
|
|
698
|
+
}
|
|
699
|
+
report(ruleId, severity, message, help, range, path = "") {
|
|
700
|
+
this.diagnostics.push({ ruleId, severity, scope: "document", path, message, help, location: this.location(range) });
|
|
701
|
+
}
|
|
702
|
+
error(rule, message, help, range, path = "") {
|
|
703
|
+
this.report(`markdown/${rule}`, "error", message, help, range, path);
|
|
704
|
+
}
|
|
705
|
+
warn(rule, message, help, range, path = "") {
|
|
706
|
+
this.report(`markdown/${rule}`, "warning", message, help, range, path);
|
|
707
|
+
}
|
|
708
|
+
get errors() {
|
|
709
|
+
return this.diagnostics.filter((d) => d.severity === "error").length;
|
|
710
|
+
}
|
|
711
|
+
/** The source range of a JSON Pointer, falling back to the nearest ancestor that has one. */
|
|
712
|
+
rangeOf(pointer) {
|
|
713
|
+
for (let path = pointer; ; path = path.slice(0, path.lastIndexOf("/"))) {
|
|
714
|
+
const found = this.ranges.get(path);
|
|
715
|
+
if (found) return found;
|
|
716
|
+
if (!path) return void 0;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
};
|
|
720
|
+
var lineRange = (first, last = first) => ({ start: first.start, end: last.end });
|
|
721
|
+
function readYamlMapping(text, base, ctx, rule, context) {
|
|
722
|
+
const keys = /* @__PURE__ */ new Map();
|
|
723
|
+
const doc = parseDocument(text, { schema: "core", uniqueKeys: true, merge: false, prettyErrors: false });
|
|
724
|
+
let ok = true;
|
|
725
|
+
for (const error of doc.errors) {
|
|
726
|
+
ok = false;
|
|
727
|
+
const [start, end] = error.pos;
|
|
728
|
+
ctx.error(rule, `YAML: ${error.message.split("\n")[0]}`, "Fix the YAML syntax. Front matter and embedded blocks hold JSON-compatible YAML: mappings, sequences and scalars, no anchors or tags.", { start: base + start, end: base + Math.max(end, start) });
|
|
729
|
+
}
|
|
730
|
+
if (!ok) return { value: {}, keys, ok };
|
|
731
|
+
if (doc.contents === null) return { value: {}, keys, ok };
|
|
732
|
+
if (!(doc.contents instanceof YAMLMap)) {
|
|
733
|
+
ctx.error(`${rule}-not-mapping`, "Expected a YAML mapping of keys and values.", "Write `key: value` lines; a list or a bare value is not accepted here.", context);
|
|
734
|
+
return { value: {}, keys, ok: false };
|
|
735
|
+
}
|
|
736
|
+
for (const pair of doc.contents.items) {
|
|
737
|
+
const key = pair.key;
|
|
738
|
+
const name = key instanceof Scalar ? String(key.value) : void 0;
|
|
739
|
+
if (name !== void 0 && key?.range) keys.set(name, { start: base + key.range[0], end: base + (pair.value?.range?.[1] ?? key.range[1]) });
|
|
740
|
+
}
|
|
741
|
+
let value;
|
|
742
|
+
try {
|
|
743
|
+
value = doc.toJS({ maxAliasCount: 0 });
|
|
744
|
+
} catch (error) {
|
|
745
|
+
ctx.error(rule, `YAML: ${error.message}`, "Anchors and aliases are not accepted; write each value out.", context);
|
|
746
|
+
return { value: {}, keys, ok: false };
|
|
747
|
+
}
|
|
748
|
+
const bad = nonJson(value);
|
|
749
|
+
if (bad) {
|
|
750
|
+
ctx.error(rule, `YAML value is not JSON-compatible (${bad}).`, "Quote special numbers such as .inf or .nan to keep them as strings.", context);
|
|
751
|
+
return { value: {}, keys, ok: false };
|
|
752
|
+
}
|
|
753
|
+
return { value, keys, ok };
|
|
754
|
+
}
|
|
755
|
+
function nonJson(value) {
|
|
756
|
+
if (typeof value === "number") return Number.isFinite(value) ? void 0 : "a number that is not finite";
|
|
757
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return void 0;
|
|
758
|
+
if (Array.isArray(value)) return value.map(nonJson).find(Boolean);
|
|
759
|
+
if (isRecord(value)) return Object.values(value).map(nonJson).find(Boolean);
|
|
760
|
+
return `a ${typeof value}`;
|
|
761
|
+
}
|
|
762
|
+
function writeYaml(value) {
|
|
763
|
+
for (const blockQuote of [true, false]) {
|
|
764
|
+
const text = stringify(value, { indent: 2, lineWidth: 0, minContentWidth: 0, blockQuote, defaultStringType: "PLAIN", defaultKeyType: "PLAIN" });
|
|
765
|
+
const doc = parseDocument(text, { schema: "core", uniqueKeys: true, merge: false });
|
|
766
|
+
if (!doc.errors.length && same(doc.toJS({ maxAliasCount: 0 }), value)) return text.endsWith("\n") ? text : `${text}
|
|
767
|
+
`;
|
|
768
|
+
}
|
|
769
|
+
return void 0;
|
|
770
|
+
}
|
|
771
|
+
var DECIMAL = /^-?(?:0|[1-9]\d*)(?:\.\d+)?$/;
|
|
772
|
+
function scalarText(value) {
|
|
773
|
+
if (typeof value === "number") return DECIMAL.test(String(value)) ? String(value) : JSON.stringify(String(value));
|
|
774
|
+
const plain = value !== "" && value === value.trim() && !value.startsWith('"') && !/[\r\n]/.test(value) && !DECIMAL.test(value);
|
|
775
|
+
return plain ? value : JSON.stringify(value);
|
|
776
|
+
}
|
|
777
|
+
function readCsv(text) {
|
|
778
|
+
const rows = [];
|
|
779
|
+
let row = [];
|
|
780
|
+
let i = 0;
|
|
781
|
+
const n = text.length;
|
|
782
|
+
const endRecord = () => {
|
|
783
|
+
rows.push(row);
|
|
784
|
+
row = [];
|
|
785
|
+
};
|
|
786
|
+
while (i < n) {
|
|
787
|
+
while (text[i] === " " || text[i] === " ") i++;
|
|
788
|
+
if (text[i] === '"') {
|
|
789
|
+
let value = "";
|
|
790
|
+
i++;
|
|
791
|
+
for (; ; ) {
|
|
792
|
+
if (i >= n) return { rows, error: "A quoted field is not closed." };
|
|
793
|
+
if (text[i] === '"') {
|
|
794
|
+
if (text[i + 1] === '"') {
|
|
795
|
+
value += '"';
|
|
796
|
+
i += 2;
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
799
|
+
i++;
|
|
800
|
+
break;
|
|
801
|
+
}
|
|
802
|
+
value += text[i++];
|
|
803
|
+
}
|
|
804
|
+
while (text[i] === " " || text[i] === " ") i++;
|
|
805
|
+
if (i < n && text[i] !== "," && text[i] !== "\n" && text[i] !== "\r") return { rows, error: "Text follows a closing quote." };
|
|
806
|
+
row.push({ value, quoted: true });
|
|
807
|
+
} else {
|
|
808
|
+
let j = i;
|
|
809
|
+
while (j < n && text[j] !== "," && text[j] !== "\n" && text[j] !== "\r") j++;
|
|
810
|
+
row.push({ value: text.slice(i, j).trim(), quoted: false });
|
|
811
|
+
i = j;
|
|
812
|
+
}
|
|
813
|
+
if (text[i] === ",") {
|
|
814
|
+
i++;
|
|
815
|
+
if (i >= n || text[i] === "\n" || text[i] === "\r") row.push({ value: "", quoted: false });
|
|
816
|
+
continue;
|
|
817
|
+
}
|
|
818
|
+
if (text[i] === "\r" && text[i + 1] === "\n") i++;
|
|
819
|
+
i++;
|
|
820
|
+
endRecord();
|
|
821
|
+
}
|
|
822
|
+
if (row.length) endRecord();
|
|
823
|
+
return { rows };
|
|
824
|
+
}
|
|
825
|
+
var needsQuotes = (value) => /[",\r\n]/.test(value) || value !== value.trim() || value === "";
|
|
826
|
+
function csvField(cell, header = false) {
|
|
827
|
+
if (cell === null) return "";
|
|
828
|
+
if (typeof cell === "number") return String(cell);
|
|
829
|
+
if (typeof cell === "boolean") return String(cell);
|
|
830
|
+
const text = String(cell);
|
|
831
|
+
const ambiguous = !header && (DECIMAL.test(text) || /^(?:true|false)$/.test(text));
|
|
832
|
+
return needsQuotes(text) || ambiguous ? `"${text.replaceAll('"', '""')}"` : text;
|
|
833
|
+
}
|
|
834
|
+
var OPFMarkdownError = class extends Error {
|
|
835
|
+
code;
|
|
836
|
+
details;
|
|
837
|
+
constructor(code, message, details = {}) {
|
|
838
|
+
super(message);
|
|
839
|
+
this.name = "OPFMarkdownError";
|
|
840
|
+
this.code = code;
|
|
841
|
+
this.details = details;
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
// src/markdown/parse.ts
|
|
846
|
+
var SLIDE_OPTION_KEYS = ["id", "type", "layout", "section", "tag", "hidden", "beat"];
|
|
847
|
+
var BLOCK_OPTION_KEYS = ["id", "type", "as", "region"];
|
|
848
|
+
var BLANK = /^\s*$/;
|
|
849
|
+
var FENCE_OPEN = /^( {0,3})(`{3,}|~{3,})(.*)$/;
|
|
850
|
+
var SEPARATOR = /^-{3,}[ \t]*$/;
|
|
851
|
+
var HEADING = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?[ \t]*$/;
|
|
852
|
+
var QUOTE = /^ {0,3}>/;
|
|
853
|
+
var LIST_ITEM = /^([ \t]*)([-+*]|\d{1,9}[.)])(?:[ \t]+(.*))?$/;
|
|
854
|
+
var TABLE_ROW = /^ {0,3}\|/;
|
|
855
|
+
var TABLE_DELIMITER = /^ {0,3}\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/;
|
|
856
|
+
var IMAGE = /^ {0,3}!\[/;
|
|
857
|
+
var COMMENT = /^ {0,3}<!--/;
|
|
858
|
+
var NOTES = /^(Notes?):[ \t]*(.*)$/;
|
|
859
|
+
var indentWidth = (space) => [...space].reduce((width, char) => width + (char === " " ? 4 : 1), 0);
|
|
860
|
+
function fenceClose(open) {
|
|
861
|
+
return new RegExp(`^ {0,3}${open[0] === "`" ? "`" : "~"}{${open.length},}[ \\t]*$`);
|
|
862
|
+
}
|
|
863
|
+
function fenceStart(text) {
|
|
864
|
+
const match = FENCE_OPEN.exec(text);
|
|
865
|
+
if (!match) return void 0;
|
|
866
|
+
if (match[2][0] === "`" && match[3].includes("`")) return void 0;
|
|
867
|
+
return { fence: match[2], info: match[3].trim() };
|
|
868
|
+
}
|
|
869
|
+
var tableStart = (lines, i) => TABLE_ROW.test(lines[i].text) && lines[i + 1] !== void 0 && TABLE_DELIMITER.test(lines[i + 1].text) && lines[i + 1].text.includes("-");
|
|
870
|
+
function listMarker(text) {
|
|
871
|
+
const match = LIST_ITEM.exec(text);
|
|
872
|
+
if (!match) return void 0;
|
|
873
|
+
const marker = match[2];
|
|
874
|
+
const ordered = /^\d/.test(marker);
|
|
875
|
+
return { indent: indentWidth(match[1]), marker, content: match[3] ?? "", ordered, family: ordered ? marker.slice(-1) : marker };
|
|
876
|
+
}
|
|
877
|
+
function interrupts(lines, i) {
|
|
878
|
+
const text = lines[i].text;
|
|
879
|
+
if (BLANK.test(text) || fenceStart(text) || HEADING.test(text) && !/^ {0,3}#{7}/.test(text) || QUOTE.test(text) || COMMENT.test(text) || NOTES.test(text) || SEPARATOR.test(text)) return true;
|
|
880
|
+
if (tableStart(lines, i)) return true;
|
|
881
|
+
if (IMAGE.test(text) && /^ {0,3}!\[(?:[^\]\\]|\\.)*\]\(.*\)[ \t]*$/.test(text)) return true;
|
|
882
|
+
const item = listMarker(text);
|
|
883
|
+
return !!item && item.indent < 4 && item.content.trim() !== "" && (!item.ordered || /^0*1[.)]$/.test(item.marker));
|
|
884
|
+
}
|
|
885
|
+
function splitSegments(lines, split) {
|
|
886
|
+
const segments = [[]];
|
|
887
|
+
let fence;
|
|
888
|
+
let comment = false;
|
|
889
|
+
let notes = false;
|
|
890
|
+
const hasContent = (segment) => segment.some((line) => !BLANK.test(line.text) && !/^\s*<!--.*-->\s*$/.test(line.text));
|
|
891
|
+
for (const line of lines) {
|
|
892
|
+
const text = line.text;
|
|
893
|
+
const current = segments.at(-1);
|
|
894
|
+
if (fence !== void 0) {
|
|
895
|
+
if (fenceClose(fence).test(text)) fence = void 0;
|
|
896
|
+
current.push(line);
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
if (comment) {
|
|
900
|
+
if (text.includes("-->")) comment = false;
|
|
901
|
+
current.push(line);
|
|
902
|
+
continue;
|
|
903
|
+
}
|
|
904
|
+
if (SEPARATOR.test(text)) {
|
|
905
|
+
segments.push([]);
|
|
906
|
+
notes = false;
|
|
907
|
+
continue;
|
|
908
|
+
}
|
|
909
|
+
if (split === "headings" && /^#(?:[ \t]|$)/.test(text) && hasContent(current)) {
|
|
910
|
+
segments.push([line]);
|
|
911
|
+
notes = false;
|
|
912
|
+
continue;
|
|
913
|
+
}
|
|
914
|
+
if (!notes) {
|
|
915
|
+
const open = fenceStart(text);
|
|
916
|
+
if (open) fence = open.fence;
|
|
917
|
+
else if (COMMENT.test(text) && !text.includes("-->", text.indexOf("<!--") + 4)) comment = true;
|
|
918
|
+
else if (NOTES.test(text)) notes = true;
|
|
919
|
+
}
|
|
920
|
+
current.push(line);
|
|
921
|
+
}
|
|
922
|
+
return segments;
|
|
923
|
+
}
|
|
924
|
+
function joinParagraph(lines) {
|
|
925
|
+
let out = "";
|
|
926
|
+
lines.forEach((raw, k) => {
|
|
927
|
+
let text = raw.trim();
|
|
928
|
+
let separator = " ";
|
|
929
|
+
if (k < lines.length - 1) {
|
|
930
|
+
const slashes = /\\+$/.exec(text)?.[0].length ?? 0;
|
|
931
|
+
if (slashes % 2 === 1) {
|
|
932
|
+
text = text.slice(0, -1);
|
|
933
|
+
separator = "\n";
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
out += text + (k < lines.length - 1 ? separator : "");
|
|
937
|
+
});
|
|
938
|
+
return out;
|
|
939
|
+
}
|
|
940
|
+
var bold = (value) => typeof value === "string" ? [{ text: value, bold: true }] : value.map((run) => typeof run === "string" ? { text: run, bold: true } : { ...run, bold: true });
|
|
941
|
+
function parseOptions(text, keys, kind, ctx, range) {
|
|
942
|
+
const { pairs, error } = parseAttributes(text);
|
|
943
|
+
const help = `A ${kind} options comment reads <!-- ${kind}: ${kind === "slide" ? 'id=cover layout=title section="Part 1" hidden' : "id=chart-1 as=bullets region=top:left"} -->. Keys: ${keys.join(", ")}.`;
|
|
944
|
+
if (error) {
|
|
945
|
+
ctx.error("options-syntax", error, help, range);
|
|
946
|
+
return void 0;
|
|
947
|
+
}
|
|
948
|
+
const out = {};
|
|
949
|
+
let ok = true;
|
|
950
|
+
for (const [key, value] of pairs) {
|
|
951
|
+
if (!keys.includes(key)) {
|
|
952
|
+
ctx.error("options-unknown-key", `Unknown ${kind} option ${JSON.stringify(key)}.`, `${help} Anything else goes in a fenced opf-${kind} block.`, range);
|
|
953
|
+
ok = false;
|
|
954
|
+
} else if (key === "hidden") {
|
|
955
|
+
if (value === true || value === "true") out.hidden = true;
|
|
956
|
+
else if (value === "false") out.hidden = false;
|
|
957
|
+
else {
|
|
958
|
+
ctx.error("options-value", `hidden takes true or false, not ${JSON.stringify(value)}.`, help, range);
|
|
959
|
+
ok = false;
|
|
960
|
+
}
|
|
961
|
+
} else if (value === true) {
|
|
962
|
+
ctx.error("options-value", `Option ${key} needs a value: ${key}=value.`, help, range);
|
|
963
|
+
ok = false;
|
|
964
|
+
} else if (key === "beat" && out.beat !== void 0) out.beat = [...Array.isArray(out.beat) ? out.beat : [out.beat], value];
|
|
965
|
+
else if (key in out) {
|
|
966
|
+
ctx.error("options-duplicate", `Option ${key} is given twice.`, help, range);
|
|
967
|
+
ok = false;
|
|
968
|
+
} else if (key === "as" && value !== "bullets" && value !== "video") {
|
|
969
|
+
ctx.error("options-value", `as takes bullets (a list stored as bullets) or video (an image line stored as video), not ${JSON.stringify(value)}.`, help, range);
|
|
970
|
+
ok = false;
|
|
971
|
+
} else out[key] = value;
|
|
972
|
+
}
|
|
973
|
+
return ok ? out : void 0;
|
|
974
|
+
}
|
|
975
|
+
var SlideParser = class {
|
|
976
|
+
constructor(ctx, lines, path) {
|
|
977
|
+
this.ctx = ctx;
|
|
978
|
+
this.lines = lines;
|
|
979
|
+
this.path = path;
|
|
980
|
+
}
|
|
981
|
+
ctx;
|
|
982
|
+
lines;
|
|
983
|
+
path;
|
|
984
|
+
slide = {};
|
|
985
|
+
blocks = [];
|
|
986
|
+
slideOptions;
|
|
987
|
+
slideOptionsRange;
|
|
988
|
+
pending;
|
|
989
|
+
title;
|
|
990
|
+
subtitle;
|
|
991
|
+
notes;
|
|
992
|
+
extras;
|
|
993
|
+
notesRange;
|
|
994
|
+
i = 0;
|
|
995
|
+
issueAt(range) {
|
|
996
|
+
return (rule, message) => rule === "markdown/formatting-dropped" ? this.ctx.warn("formatting-dropped", message, "Use a plain string here, or put the field in a fenced opf-slide block.", range) : this.ctx.error(rule.replace(/^markdown\//, ""), message, "Fix the inline syntax.", range);
|
|
997
|
+
}
|
|
998
|
+
inline(text, range) {
|
|
999
|
+
return parseInline(text, this.issueAt(range));
|
|
1000
|
+
}
|
|
1001
|
+
plain(text, range) {
|
|
1002
|
+
return parsePlainInline(text, this.issueAt(range));
|
|
1003
|
+
}
|
|
1004
|
+
run() {
|
|
1005
|
+
const { lines, ctx } = this;
|
|
1006
|
+
while (this.i < lines.length) {
|
|
1007
|
+
const start = this.i;
|
|
1008
|
+
const line = lines[start];
|
|
1009
|
+
if (BLANK.test(line.text)) {
|
|
1010
|
+
this.i++;
|
|
1011
|
+
continue;
|
|
1012
|
+
}
|
|
1013
|
+
if (COMMENT.test(line.text)) this.comment();
|
|
1014
|
+
else if (NOTES.test(line.text)) this.parseNotes();
|
|
1015
|
+
else if (fenceStart(line.text)) this.fence();
|
|
1016
|
+
else if (HEADING.test(line.text) && !/^ {0,3}#{7}/.test(line.text)) this.heading();
|
|
1017
|
+
else if (QUOTE.test(line.text)) this.quote();
|
|
1018
|
+
else if (tableStart(lines, start)) this.table();
|
|
1019
|
+
else if (this.imageLine()) ; else if (listMarker(line.text) && listMarker(line.text).indent < 4) this.list();
|
|
1020
|
+
else this.paragraph();
|
|
1021
|
+
if (this.i === start) this.i++;
|
|
1022
|
+
}
|
|
1023
|
+
if (this.pending) ctx.error("options-orphan", "A block options comment is not followed by a block.", "Put the <!-- block: ... --> comment directly before the block it describes.", this.pending.range, this.path);
|
|
1024
|
+
return this.finish();
|
|
1025
|
+
}
|
|
1026
|
+
add(block) {
|
|
1027
|
+
if (this.pending) {
|
|
1028
|
+
block.options = this.pending.options;
|
|
1029
|
+
this.pending = void 0;
|
|
1030
|
+
}
|
|
1031
|
+
this.blocks.push(block);
|
|
1032
|
+
}
|
|
1033
|
+
native(key, value, range) {
|
|
1034
|
+
const block = { payload: { [key]: value }, range, key };
|
|
1035
|
+
this.add(block);
|
|
1036
|
+
if (block.options) {
|
|
1037
|
+
const as = block.options.as;
|
|
1038
|
+
const stored = as === "bullets" && key === "items" ? "bullets" : as === "video" && key === "image" ? "video" : key;
|
|
1039
|
+
if (as !== void 0 && stored === key) this.ctx.error("options-value", `as=${as} does not apply to this block.`, "as=bullets goes before a list, as=video before an image line.", range, this.path);
|
|
1040
|
+
block.payload = { [stored]: value };
|
|
1041
|
+
block.key = stored;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
// --- comments --------------------------------------------------------------------------------
|
|
1045
|
+
comment() {
|
|
1046
|
+
const { lines, ctx } = this;
|
|
1047
|
+
const first = lines[this.i];
|
|
1048
|
+
let end = this.i;
|
|
1049
|
+
while (end < lines.length && !lines[end].text.includes("-->", end === this.i ? lines[end].text.indexOf("<!--") + 4 : 0)) end++;
|
|
1050
|
+
if (end >= lines.length) {
|
|
1051
|
+
end = lines.length - 1;
|
|
1052
|
+
ctx.error("comment-unterminated", "An HTML comment is not closed.", "End the comment with -->.", lineRange(first, lines[end]), this.path);
|
|
1053
|
+
}
|
|
1054
|
+
const last = lines[end];
|
|
1055
|
+
const range = lineRange(first, last);
|
|
1056
|
+
const body = lines.slice(this.i, end + 1).map((line) => line.text).join(" ");
|
|
1057
|
+
this.i = end + 1;
|
|
1058
|
+
const match = /^\s*<!--\s*(slide|block)(?::|(?=\s*-->))([\s\S]*?)-->([\s\S]*)$/.exec(body);
|
|
1059
|
+
if (!match) return;
|
|
1060
|
+
const kind = match[1];
|
|
1061
|
+
if (match[3].trim()) ctx.error("options-trailing", "Text follows a closing --> on the same line.", "Put content on its own line after the options comment.", range, this.path);
|
|
1062
|
+
const options = parseOptions(match[2], kind === "slide" ? SLIDE_OPTION_KEYS : BLOCK_OPTION_KEYS, kind, ctx, range);
|
|
1063
|
+
if (!options) return;
|
|
1064
|
+
if (kind === "slide") {
|
|
1065
|
+
if (this.slideOptions) ctx.error("options-duplicate", "A slide has more than one <!-- slide: ... --> comment.", "Merge the options into one comment.", range, this.path);
|
|
1066
|
+
else {
|
|
1067
|
+
this.slideOptions = options;
|
|
1068
|
+
this.slideOptionsRange = range;
|
|
1069
|
+
}
|
|
1070
|
+
} else if (this.pending) ctx.error("options-duplicate", "Two block options comments in a row.", "Merge them into one comment.", range, this.path);
|
|
1071
|
+
else this.pending = { options, range };
|
|
1072
|
+
}
|
|
1073
|
+
// --- notes -----------------------------------------------------------------------------------
|
|
1074
|
+
parseNotes() {
|
|
1075
|
+
const { lines } = this;
|
|
1076
|
+
const first = lines[this.i];
|
|
1077
|
+
const match = NOTES.exec(first.text);
|
|
1078
|
+
const rows = [match[2], ...lines.slice(this.i + 1).map((line) => line.text)];
|
|
1079
|
+
while (rows.length && BLANK.test(rows[0])) rows.shift();
|
|
1080
|
+
while (rows.length && BLANK.test(rows.at(-1))) rows.pop();
|
|
1081
|
+
const range = lineRange(first, lines.at(-1));
|
|
1082
|
+
this.notesRange = range;
|
|
1083
|
+
this.notes = rows.join("\n");
|
|
1084
|
+
this.i = lines.length;
|
|
1085
|
+
}
|
|
1086
|
+
// --- headings --------------------------------------------------------------------------------
|
|
1087
|
+
heading() {
|
|
1088
|
+
const { lines, ctx } = this;
|
|
1089
|
+
const line = lines[this.i++];
|
|
1090
|
+
const match = HEADING.exec(line.text);
|
|
1091
|
+
const level = match[1].length;
|
|
1092
|
+
const text = (match[2] ?? "").replace(/(?:^|[ \t]+)#+[ \t]*$/, "").trim();
|
|
1093
|
+
const range = lineRange(line);
|
|
1094
|
+
if (!text) {
|
|
1095
|
+
ctx.error("empty-heading", "A heading has no text.", "Write the title after the #, or remove the line.", range, this.path);
|
|
1096
|
+
return;
|
|
1097
|
+
}
|
|
1098
|
+
if (level === 1) {
|
|
1099
|
+
if (this.title !== void 0) ctx.error("duplicate-title", "A slide has more than one # title.", "Use --- to start a new slide, or `##` for the subtitle.", range, `${this.path}/title`);
|
|
1100
|
+
else {
|
|
1101
|
+
this.title = this.plain(text, range);
|
|
1102
|
+
ctx.ranges.set(`${this.path}/title`, range);
|
|
1103
|
+
}
|
|
1104
|
+
} else if (level === 2) {
|
|
1105
|
+
if (this.subtitle !== void 0) ctx.error("duplicate-subtitle", "A slide has more than one ## subtitle.", "A slide has one subtitle. Use a paragraph for further lines.", range, `${this.path}/subtitle`);
|
|
1106
|
+
else {
|
|
1107
|
+
this.subtitle = this.plain(text, range);
|
|
1108
|
+
ctx.ranges.set(`${this.path}/subtitle`, range);
|
|
1109
|
+
}
|
|
1110
|
+
} else {
|
|
1111
|
+
ctx.warn("heading-demoted", `A level-${level} heading has no OPF form, so it became a bold paragraph.`, "Only # (title) and ## (subtitle) are slide fields. Use a bold paragraph on purpose to silence this.", range, this.path);
|
|
1112
|
+
this.native("text", bold(this.inline(text, range)), range);
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
// --- paragraph -------------------------------------------------------------------------------
|
|
1116
|
+
paragraph() {
|
|
1117
|
+
const { lines } = this;
|
|
1118
|
+
const first = this.i;
|
|
1119
|
+
const rows = [];
|
|
1120
|
+
do
|
|
1121
|
+
rows.push(lines[this.i++].text);
|
|
1122
|
+
while (this.i < lines.length && !interrupts(lines, this.i));
|
|
1123
|
+
const range = lineRange(lines[first], lines[this.i - 1]);
|
|
1124
|
+
this.native("text", this.inline(joinParagraph(rows), range), range);
|
|
1125
|
+
}
|
|
1126
|
+
// --- quote -----------------------------------------------------------------------------------
|
|
1127
|
+
quote() {
|
|
1128
|
+
const { lines, ctx } = this;
|
|
1129
|
+
const first = this.i;
|
|
1130
|
+
const rows = [];
|
|
1131
|
+
while (this.i < lines.length && QUOTE.test(lines[this.i].text)) rows.push(lines[this.i++].text.replace(/^ {0,3}> ?/, ""));
|
|
1132
|
+
const range = lineRange(lines[first], lines[this.i - 1]);
|
|
1133
|
+
while (rows.length && BLANK.test(rows[0])) rows.shift();
|
|
1134
|
+
while (rows.length && BLANK.test(rows.at(-1))) rows.pop();
|
|
1135
|
+
if (!rows.length) {
|
|
1136
|
+
ctx.error("empty-quote", "A block quote has no text.", "Write the quote after >.", range, this.path);
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1139
|
+
const parsed = parseQuoteLines(rows);
|
|
1140
|
+
const paragraphs = [[]];
|
|
1141
|
+
for (const row of parsed.text) {
|
|
1142
|
+
if (BLANK.test(row)) paragraphs.push([]);
|
|
1143
|
+
else paragraphs.at(-1).push(row);
|
|
1144
|
+
}
|
|
1145
|
+
const text = this.plain(
|
|
1146
|
+
paragraphs.filter((paragraph) => paragraph.length).map(joinParagraph).join("\n\n"),
|
|
1147
|
+
range
|
|
1148
|
+
);
|
|
1149
|
+
const value = { text };
|
|
1150
|
+
if (parsed.attribution !== void 0) value.attribution = this.plain(parsed.attribution, range);
|
|
1151
|
+
if (parsed.source !== void 0) value.source = this.plain(parsed.source, range);
|
|
1152
|
+
this.native("quote", Object.keys(value).length === 1 ? value.text : value, range);
|
|
1153
|
+
}
|
|
1154
|
+
// --- lists -----------------------------------------------------------------------------------
|
|
1155
|
+
list() {
|
|
1156
|
+
const { lines, ctx } = this;
|
|
1157
|
+
const first = this.i;
|
|
1158
|
+
const entries = [];
|
|
1159
|
+
const head = listMarker(lines[first].text);
|
|
1160
|
+
let topIndent = head.indent;
|
|
1161
|
+
let numbered = false;
|
|
1162
|
+
let i = first;
|
|
1163
|
+
while (i < lines.length) {
|
|
1164
|
+
const text = lines[i].text;
|
|
1165
|
+
if (BLANK.test(text)) {
|
|
1166
|
+
let j = i + 1;
|
|
1167
|
+
while (j < lines.length && BLANK.test(lines[j].text)) j++;
|
|
1168
|
+
const item2 = j < lines.length ? listMarker(lines[j].text) : void 0;
|
|
1169
|
+
if (!item2) break;
|
|
1170
|
+
if (item2.indent <= topIndent && item2.family !== head.family) break;
|
|
1171
|
+
i = j;
|
|
1172
|
+
continue;
|
|
1173
|
+
}
|
|
1174
|
+
const item = listMarker(text);
|
|
1175
|
+
if (item) {
|
|
1176
|
+
if (item.indent <= topIndent) {
|
|
1177
|
+
if (item.family !== head.family) break;
|
|
1178
|
+
topIndent = Math.min(topIndent, item.indent);
|
|
1179
|
+
}
|
|
1180
|
+
if (item.ordered) numbered = true;
|
|
1181
|
+
entries.push({ indent: item.indent, rows: [item.content.trim()] });
|
|
1182
|
+
i++;
|
|
1183
|
+
continue;
|
|
1184
|
+
}
|
|
1185
|
+
const current = entries.at(-1);
|
|
1186
|
+
if (!current || interrupts(lines, i)) break;
|
|
1187
|
+
const description = /^([ \t]+):[ \t]+(.*)$/.exec(text);
|
|
1188
|
+
if (description && indentWidth(description[1]) > current.indent && !current.description) current.description = [description[2].trim()];
|
|
1189
|
+
else if (current.description) current.description.push(text.trim());
|
|
1190
|
+
else current.rows.push(text);
|
|
1191
|
+
i++;
|
|
1192
|
+
}
|
|
1193
|
+
this.i = i;
|
|
1194
|
+
const last = Math.max(first, i - 1);
|
|
1195
|
+
let end = last;
|
|
1196
|
+
while (end > first && BLANK.test(lines[end].text)) end--;
|
|
1197
|
+
const range = lineRange(lines[first], lines[end]);
|
|
1198
|
+
if (numbered) ctx.warn("numbered-list", "Numbers in a list are not kept: an OPF list has no numbering, so each item became a bullet.", "Write the numbers into the text if they matter, or use a bulleted list.", range, this.path);
|
|
1199
|
+
const levels = levelsFromIndents(entries.map((entry) => entry.indent));
|
|
1200
|
+
const items = entries.map((entry, k) => {
|
|
1201
|
+
const text = this.inline(joinParagraph(entry.rows), range);
|
|
1202
|
+
const description = entry.description ? this.inline(joinParagraph(entry.description), range) : void 0;
|
|
1203
|
+
const level = levels[k];
|
|
1204
|
+
if (!level && description === void 0) return text;
|
|
1205
|
+
const out = { text };
|
|
1206
|
+
if (description !== void 0) out.description = description;
|
|
1207
|
+
if (level) out.level = level;
|
|
1208
|
+
return out;
|
|
1209
|
+
});
|
|
1210
|
+
this.native("items", items, range);
|
|
1211
|
+
}
|
|
1212
|
+
// --- tables ----------------------------------------------------------------------------------
|
|
1213
|
+
table() {
|
|
1214
|
+
const { lines, ctx } = this;
|
|
1215
|
+
const first = this.i;
|
|
1216
|
+
this.i += 2;
|
|
1217
|
+
while (this.i < lines.length && TABLE_ROW.test(lines[this.i].text)) this.i++;
|
|
1218
|
+
const range = lineRange(lines[first], lines[this.i - 1]);
|
|
1219
|
+
const header = splitRow(lines[first].text);
|
|
1220
|
+
const rows = lines.slice(first + 2, this.i).map((line) => splitRow(line.text));
|
|
1221
|
+
const cell = (text) => this.inline(text, range);
|
|
1222
|
+
const out = {};
|
|
1223
|
+
if (header.some((text) => text !== "")) out.columns = header.map((text) => text === "" ? null : cell(text));
|
|
1224
|
+
out.rows = rows.map((row) => {
|
|
1225
|
+
if (header.length && row.length !== header.length) ctx.warn("table-ragged", `A table row has ${row.length} cells but the header has ${header.length}.`, "Give every row the same number of cells.", range, this.path);
|
|
1226
|
+
const cells = row.map((text) => text === "" ? null : cell(text));
|
|
1227
|
+
while (header.length && cells.length < header.length) cells.push(null);
|
|
1228
|
+
return cells;
|
|
1229
|
+
});
|
|
1230
|
+
this.native("table", out, range);
|
|
1231
|
+
}
|
|
1232
|
+
// --- images ----------------------------------------------------------------------------------
|
|
1233
|
+
imageLine() {
|
|
1234
|
+
const { lines, ctx } = this;
|
|
1235
|
+
const line = lines[this.i];
|
|
1236
|
+
const text = line.text.replace(/^ {0,3}/, "");
|
|
1237
|
+
if (!text.startsWith("![")) return false;
|
|
1238
|
+
const close = matchBracket(text, 1);
|
|
1239
|
+
if (close < 0 || text[close + 1] !== "(") return false;
|
|
1240
|
+
const dest = readDestination(text, close + 1);
|
|
1241
|
+
if (!dest || text.slice(dest.end).trim() !== "") return false;
|
|
1242
|
+
this.i++;
|
|
1243
|
+
const range = lineRange(line);
|
|
1244
|
+
if (!dest.url) {
|
|
1245
|
+
ctx.error("image-source", "An image has no source.", "Write the file or URL inside the parentheses: .", range, this.path);
|
|
1246
|
+
return true;
|
|
1247
|
+
}
|
|
1248
|
+
const alt = unescapeInline(text.slice(2, close));
|
|
1249
|
+
const value = { src: dest.url };
|
|
1250
|
+
if (alt) value.alt = alt;
|
|
1251
|
+
if (dest.title !== void 0) value.title = dest.title;
|
|
1252
|
+
this.native("image", Object.keys(value).length === 1 ? value.src : value, range);
|
|
1253
|
+
return true;
|
|
1254
|
+
}
|
|
1255
|
+
// --- fences ----------------------------------------------------------------------------------
|
|
1256
|
+
fence() {
|
|
1257
|
+
const { lines, ctx } = this;
|
|
1258
|
+
const first = this.i;
|
|
1259
|
+
const open = fenceStart(lines[first].text);
|
|
1260
|
+
const close = fenceClose(open.fence);
|
|
1261
|
+
let end = first + 1;
|
|
1262
|
+
while (end < lines.length && !close.test(lines[end].text)) end++;
|
|
1263
|
+
const terminated = end < lines.length;
|
|
1264
|
+
const lastLine = terminated ? lines[end] : lines.at(-1);
|
|
1265
|
+
const range = lineRange(lines[first], lastLine);
|
|
1266
|
+
if (!terminated) ctx.error("fence-unterminated", "A fenced block is not closed.", `Add a closing ${open.fence} line.`, range, this.path);
|
|
1267
|
+
this.i = terminated ? end + 1 : lines.length;
|
|
1268
|
+
const body = lines.slice(first + 1, terminated ? end : lines.length);
|
|
1269
|
+
const bodyText = body.map((line) => line.text).join("\n");
|
|
1270
|
+
const bodyStart = body[0]?.start ?? lines[first].end;
|
|
1271
|
+
const info = open.info;
|
|
1272
|
+
const word = /^[^\s{]*/.exec(info)[0];
|
|
1273
|
+
const rest = info.slice(word.length).trim();
|
|
1274
|
+
if (word === "chart") this.chart(rest, bodyText, range);
|
|
1275
|
+
else if (word === "metric") this.metric(body, range);
|
|
1276
|
+
else if (word === "timeline") this.timeline(rest, body, range);
|
|
1277
|
+
else if (word === "opf-slide") this.embeddedSlide(bodyText, bodyStart, range);
|
|
1278
|
+
else if (word === "opf-block") this.embeddedBlock(bodyText, bodyStart, range);
|
|
1279
|
+
else {
|
|
1280
|
+
const parsed = parseCodeFence([lines[first].text, ...body.map((line) => line.text), open.fence]);
|
|
1281
|
+
if (!parsed) {
|
|
1282
|
+
ctx.error("code-fence", "This fenced block could not be read as code.", "Close the fence with at least as many backticks as opened it.", range, this.path);
|
|
1283
|
+
return;
|
|
1284
|
+
}
|
|
1285
|
+
const hasMeta = parsed.language !== void 0 || parsed.filename !== void 0;
|
|
1286
|
+
const value = { source: parsed.source };
|
|
1287
|
+
if (parsed.language) value.language = parsed.language;
|
|
1288
|
+
if (parsed.filename) value.filename = parsed.filename;
|
|
1289
|
+
this.native("code", hasMeta ? value : parsed.source, range);
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
chart(rest, text, range) {
|
|
1293
|
+
const { ctx } = this;
|
|
1294
|
+
const type = /^[^\s]+/.exec(rest)?.[0];
|
|
1295
|
+
if (!type) {
|
|
1296
|
+
ctx.error("chart-type", "A chart block names its type after `chart`.", "Write ```chart column (or line, pie, bar, ...), then the data.", range, this.path);
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
const trimmed = text.trim();
|
|
1300
|
+
if (!trimmed) {
|
|
1301
|
+
ctx.error("chart-data", "A chart block has no data.", "Write CSV with a header row, or a JSON object with columns and rows or a data source.", range, this.path);
|
|
1302
|
+
return;
|
|
1303
|
+
}
|
|
1304
|
+
if (trimmed.startsWith("{")) {
|
|
1305
|
+
try {
|
|
1306
|
+
const data = JSON.parse(trimmed);
|
|
1307
|
+
if (!isRecord(data)) throw new Error("not an object");
|
|
1308
|
+
this.native("chart", { type, data }, range);
|
|
1309
|
+
} catch (error) {
|
|
1310
|
+
ctx.error("chart-data", `Chart JSON is invalid: ${error.message}.`, 'Write a JSON object: {"columns": [...], "rows": [[...]]} or {"src": ...}.', range, this.path);
|
|
1311
|
+
}
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
const csv = readCsv(text);
|
|
1315
|
+
if (csv.error) {
|
|
1316
|
+
ctx.error("chart-data", `Chart CSV is invalid: ${csv.error}`, "Quote fields that contain commas, quotes or line breaks.", range, this.path);
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
const [head, ...body] = csv.rows.filter((row) => row.some((field) => field.value !== "" || field.quoted));
|
|
1320
|
+
if (!head || !body.length) {
|
|
1321
|
+
ctx.error("chart-data", "Chart CSV needs a header row and at least one data row.", "The first line names the columns.", range, this.path);
|
|
1322
|
+
return;
|
|
1323
|
+
}
|
|
1324
|
+
const typed = (field) => field.quoted ? field.value : field.value === "" ? null : DECIMAL.test(field.value) ? Number(field.value) : field.value === "true" ? true : field.value === "false" ? false : field.value;
|
|
1325
|
+
const rows = body.map((row) => row.map((field, index) => index === 0 ? field.value : typed(field)));
|
|
1326
|
+
if (rows.some((row) => row.length !== head.length)) ctx.warn("chart-ragged", `A chart row has a different number of values than the ${head.length} columns.`, "Give every row one value per column.", range, this.path);
|
|
1327
|
+
this.native("chart", { type, data: { columns: head.map((field) => field.value), rows } }, range);
|
|
1328
|
+
}
|
|
1329
|
+
metric(body, range) {
|
|
1330
|
+
const { ctx } = this;
|
|
1331
|
+
const allowed = ["value", "label", "description", "unit", "delta", "trend"];
|
|
1332
|
+
const help = `A metric block is key: value lines. Keys: ${allowed.join(", ")}. A value is plain text, or a JSON string in double quotes.`;
|
|
1333
|
+
const out = {};
|
|
1334
|
+
for (const line of body) {
|
|
1335
|
+
if (BLANK.test(line.text)) continue;
|
|
1336
|
+
const match = /^([A-Za-z]+):[ \t]*(.*?)[ \t]*$/.exec(line.text);
|
|
1337
|
+
const where = lineRange(line);
|
|
1338
|
+
if (!match) {
|
|
1339
|
+
ctx.error("metric-block", "Expected a key: value line.", help, where, this.path);
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
const [, key, raw] = match;
|
|
1343
|
+
if (!allowed.includes(key) || key in out || raw === "") {
|
|
1344
|
+
ctx.error("metric-block", !allowed.includes(key) ? `Unknown metric key ${JSON.stringify(key)}.` : key in out ? `Metric key ${key} is given twice.` : `Metric key ${key} has no value.`, help, where, this.path);
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1347
|
+
if (raw.startsWith('"')) {
|
|
1348
|
+
try {
|
|
1349
|
+
const value = JSON.parse(raw);
|
|
1350
|
+
if (typeof value !== "string") throw new Error("not a string");
|
|
1351
|
+
out[key] = value;
|
|
1352
|
+
} catch {
|
|
1353
|
+
ctx.error("metric-block", `The value of ${key} starts with a quote but is not a JSON string.`, help, where, this.path);
|
|
1354
|
+
return;
|
|
1355
|
+
}
|
|
1356
|
+
} else out[key] = (key === "value" || key === "delta") && DECIMAL.test(raw) ? Number(raw) : raw;
|
|
1357
|
+
}
|
|
1358
|
+
if (out.value === void 0) {
|
|
1359
|
+
ctx.error("metric-block", "A metric needs a value.", help, range, this.path);
|
|
1360
|
+
return;
|
|
1361
|
+
}
|
|
1362
|
+
const ordered = {};
|
|
1363
|
+
for (const key of allowed) if (out[key] !== void 0) ordered[key] = out[key];
|
|
1364
|
+
this.native("metric", Object.keys(ordered).length === 1 ? ordered.value : ordered, range);
|
|
1365
|
+
}
|
|
1366
|
+
timeline(rest, body, range) {
|
|
1367
|
+
const { ctx } = this;
|
|
1368
|
+
const { pairs, error } = parseAttributes(rest);
|
|
1369
|
+
const meta = {};
|
|
1370
|
+
if (error) ctx.error("timeline-attributes", error, 'Write ```timeline name="Roadmap" description="..." (both optional).', range, this.path);
|
|
1371
|
+
for (const [key, value] of pairs) {
|
|
1372
|
+
if ((key === "name" || key === "description") && typeof value === "string") meta[key] = value;
|
|
1373
|
+
else ctx.error("timeline-attributes", `Unknown timeline attribute ${JSON.stringify(key)}.`, "A timeline fence takes name and description.", range, this.path);
|
|
1374
|
+
}
|
|
1375
|
+
const events = [];
|
|
1376
|
+
for (const line of body) {
|
|
1377
|
+
if (BLANK.test(line.text)) continue;
|
|
1378
|
+
if (/^[ \t]+\S/.test(line.text)) {
|
|
1379
|
+
const event = events.at(-1);
|
|
1380
|
+
if (!event) {
|
|
1381
|
+
ctx.error("timeline-description", "An indented line must follow an event.", "Put the event first, then its description indented below it.", lineRange(line), this.path);
|
|
1382
|
+
continue;
|
|
1383
|
+
}
|
|
1384
|
+
event.description = event.description === void 0 ? line.text.trim() : `${event.description} ${line.text.trim()}`;
|
|
1385
|
+
continue;
|
|
1386
|
+
}
|
|
1387
|
+
const text = line.text.trim();
|
|
1388
|
+
const dash = text.indexOf(" \u2014 ");
|
|
1389
|
+
const split = dash > 0 && text.slice(dash + 3).trim() ? { when: text.slice(0, dash).trim(), what: text.slice(dash + 3).trim() } : splitWhen(text);
|
|
1390
|
+
events.push(split ? { when: split.when, what: split.what } : { what: text });
|
|
1391
|
+
}
|
|
1392
|
+
if (!events.length) {
|
|
1393
|
+
ctx.error("timeline-events", "A timeline has no events.", "Write one event per line: `2024 Q1 \u2014 Pilot`.", range, this.path);
|
|
1394
|
+
return;
|
|
1395
|
+
}
|
|
1396
|
+
this.native("timeline", Object.keys(meta).length ? { ...meta, events } : events, range);
|
|
1397
|
+
}
|
|
1398
|
+
embeddedBlock(text, base, range) {
|
|
1399
|
+
const { ctx } = this;
|
|
1400
|
+
const parsed = readYamlMapping(text, base, ctx, "opf-block", range);
|
|
1401
|
+
if (!parsed.ok) return;
|
|
1402
|
+
if (this.pending) {
|
|
1403
|
+
ctx.error("options-embedded", "A block options comment cannot precede an opf-block fence.", "Put id and type inside the opf-block YAML.", this.pending.range, this.path);
|
|
1404
|
+
this.pending = void 0;
|
|
1405
|
+
}
|
|
1406
|
+
const keys = Object.keys(parsed.value);
|
|
1407
|
+
this.blocks.push({ payload: parsed.value, range, key: keys.length === 1 ? keys[0] : void 0 });
|
|
1408
|
+
}
|
|
1409
|
+
embeddedSlide(text, base, range) {
|
|
1410
|
+
const { ctx } = this;
|
|
1411
|
+
const parsed = readYamlMapping(text, base, ctx, "opf-slide", range);
|
|
1412
|
+
if (!parsed.ok) return;
|
|
1413
|
+
if (this.extras) ctx.error("options-duplicate", "A slide has more than one opf-slide block.", "Merge them into one.", range, this.path);
|
|
1414
|
+
else {
|
|
1415
|
+
this.extras = parsed.value;
|
|
1416
|
+
for (const [key, found] of parsed.keys) ctx.ranges.set(`${this.path}/${key.replaceAll("~", "~0").replaceAll("/", "~1")}`, found);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
// --- assembly --------------------------------------------------------------------------------
|
|
1420
|
+
finish() {
|
|
1421
|
+
const { slide, ctx, path } = this;
|
|
1422
|
+
const set = (key, value, range) => {
|
|
1423
|
+
if (value === void 0) return;
|
|
1424
|
+
if (Object.hasOwn(slide, key)) ctx.error("slide-property-conflict", `The slide property ${JSON.stringify(key)} is set twice.`, "Set it either in Markdown or in the opf-slide block, not both.", this.slideOptionsRange ?? range ?? { start: 0, end: 0 }, `${path}/${key}`);
|
|
1425
|
+
else Object.defineProperty(slide, key, { value, enumerable: true, writable: true, configurable: true });
|
|
1426
|
+
};
|
|
1427
|
+
const options = this.slideOptions ?? {};
|
|
1428
|
+
if (this.slideOptionsRange) for (const key of Object.keys(options)) ctx.ranges.set(`${path}/${key}`, this.slideOptionsRange);
|
|
1429
|
+
set("id", options.id);
|
|
1430
|
+
set("type", options.type);
|
|
1431
|
+
set("beat", options.beat);
|
|
1432
|
+
set("layout", options.layout);
|
|
1433
|
+
set("section", options.section);
|
|
1434
|
+
set("tag", options.tag);
|
|
1435
|
+
set("title", this.title);
|
|
1436
|
+
set("subtitle", this.subtitle);
|
|
1437
|
+
const content = this.blocks.filter((block) => block.options?.region === void 0);
|
|
1438
|
+
const regions = this.blocks.filter((block) => block.options?.region !== void 0);
|
|
1439
|
+
const [only2] = content;
|
|
1440
|
+
if (content.length === 1 && only2?.key !== void 0 && only2.options?.id === void 0 && only2.options?.type === void 0 && isContentKey(only2.key) && Object.keys(only2.payload).length === 1) {
|
|
1441
|
+
slide[only2.key] = only2.payload[only2.key];
|
|
1442
|
+
ctx.ranges.set(`${path}/${only2.key}`, only2.range);
|
|
1443
|
+
} else if (content.length) {
|
|
1444
|
+
slide.blocks = content.map((block, index) => {
|
|
1445
|
+
ctx.ranges.set(`${path}/blocks/${index}`, block.range);
|
|
1446
|
+
const out = {};
|
|
1447
|
+
if (block.options?.id !== void 0) out.id = block.options.id;
|
|
1448
|
+
if (block.options?.type !== void 0) out.type = block.options.type;
|
|
1449
|
+
return Object.assign(out, block.payload);
|
|
1450
|
+
});
|
|
1451
|
+
}
|
|
1452
|
+
for (const block of regions) {
|
|
1453
|
+
const region = block.options.region;
|
|
1454
|
+
const payload = {};
|
|
1455
|
+
if (block.options?.id !== void 0) payload.id = block.options.id;
|
|
1456
|
+
if (block.options?.type !== void 0) payload.type = block.options.type;
|
|
1457
|
+
Object.assign(payload, block.payload);
|
|
1458
|
+
ctx.ranges.set(`${path}/${region}`, block.range);
|
|
1459
|
+
set(region, payload, block.range);
|
|
1460
|
+
}
|
|
1461
|
+
set("notes", this.notes || void 0, this.notesRange);
|
|
1462
|
+
if (this.notesRange && this.notes) ctx.ranges.set(`${path}/notes`, this.notesRange);
|
|
1463
|
+
set("hidden", options.hidden);
|
|
1464
|
+
for (const [key, value] of Object.entries(this.extras ?? {})) set(key, value);
|
|
1465
|
+
return slide;
|
|
1466
|
+
}
|
|
1467
|
+
};
|
|
1468
|
+
var CONTENT = /* @__PURE__ */ new Set(["text", "items", "bullets", "image", "video", "chart", "table", "code", "metric", "quote", "timeline"]);
|
|
1469
|
+
var isContentKey = (key) => CONTENT.has(key);
|
|
1470
|
+
function splitRow(text) {
|
|
1471
|
+
let s = text.trim();
|
|
1472
|
+
if (s.startsWith("|")) s = s.slice(1);
|
|
1473
|
+
const cells = [];
|
|
1474
|
+
let cell = "";
|
|
1475
|
+
for (let i = 0; i < s.length; i++) {
|
|
1476
|
+
const c = s[i];
|
|
1477
|
+
if (c === "\\" && i + 1 < s.length) {
|
|
1478
|
+
cell += c + s[i + 1];
|
|
1479
|
+
i++;
|
|
1480
|
+
} else if (c === "|") {
|
|
1481
|
+
cells.push(cell.trim());
|
|
1482
|
+
cell = "";
|
|
1483
|
+
} else cell += c;
|
|
1484
|
+
}
|
|
1485
|
+
if (cell.trim() !== "" || !/\|\s*$/.test(text)) cells.push(cell.trim());
|
|
1486
|
+
return cells;
|
|
1487
|
+
}
|
|
1488
|
+
function parseSlide(ctx, lines, index) {
|
|
1489
|
+
const path = `/slides/${index}`;
|
|
1490
|
+
const first = lines.find((line) => !BLANK.test(line.text));
|
|
1491
|
+
const last = [...lines].reverse().find((line) => !BLANK.test(line.text));
|
|
1492
|
+
if (first && last) ctx.ranges.set(path, lineRange(first, last));
|
|
1493
|
+
return new SlideParser(ctx, lines, path).run();
|
|
1494
|
+
}
|
|
1495
|
+
function emptySegment(lines) {
|
|
1496
|
+
return lines.every((line) => BLANK.test(line.text));
|
|
1497
|
+
}
|
|
1498
|
+
function parseSlideText(text) {
|
|
1499
|
+
const ctx = new Ctx(text);
|
|
1500
|
+
const segments = splitSegments(splitLines(text), "rules").filter((segment) => !emptySegment(segment));
|
|
1501
|
+
if (segments.length !== 1) return { slide: void 0, clean: false };
|
|
1502
|
+
const slide = parseSlide(ctx, segments[0], 0);
|
|
1503
|
+
return { slide, clean: ctx.diagnostics.length === 0 };
|
|
1504
|
+
}
|
|
1505
|
+
function frontMatter(lines) {
|
|
1506
|
+
if (!lines.length || lines[0].text.trimEnd() !== "---") return void 0;
|
|
1507
|
+
for (let i = 1; i < lines.length; i++) {
|
|
1508
|
+
const text = lines[i].text.trimEnd();
|
|
1509
|
+
if (text === "---" || text === "...") return { yaml: { start: lines[0].end, end: lines[i].start }, end: i + 1 };
|
|
1510
|
+
}
|
|
1511
|
+
return "unterminated";
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
// src/markdown/emit.ts
|
|
1515
|
+
var CONTENT2 = ["text", "items", "bullets", "image", "video", "chart", "table", "code", "metric", "quote", "timeline"];
|
|
1516
|
+
var isContent = (key) => CONTENT2.includes(key);
|
|
1517
|
+
var ROWS = "(?:top|middle|bottom|top\\+middle|middle\\+bottom|top\\+middle\\+bottom)";
|
|
1518
|
+
var COLUMNS = "(?:left|center|right|left\\+center|center\\+right|left\\+center\\+right)";
|
|
1519
|
+
var REGION = new RegExp(`^(?:${ROWS}(?::${COLUMNS})?|${COLUMNS})$`);
|
|
1520
|
+
var NATIVE_SLIDE = /* @__PURE__ */ new Set(["id", "type", "layout", "section", "tag", "hidden", "beat", "title", "subtitle", "notes", "blocks", ...CONTENT2]);
|
|
1521
|
+
function fenceFor(body, char = "`") {
|
|
1522
|
+
let longest = 2;
|
|
1523
|
+
for (const line of body) for (const run of line.matchAll(char === "`" ? /`+/g : /~+/g)) longest = Math.max(longest, run[0].length);
|
|
1524
|
+
return char.repeat(longest + 1);
|
|
1525
|
+
}
|
|
1526
|
+
var fenced = (info, body) => {
|
|
1527
|
+
const fence = fenceFor(body);
|
|
1528
|
+
return [`${fence}${info}`, ...body, fence];
|
|
1529
|
+
};
|
|
1530
|
+
var optionValue = (value) => {
|
|
1531
|
+
if (value.includes("--") || /[\r\n]/.test(value)) return void 0;
|
|
1532
|
+
return /^[A-Za-z0-9_][A-Za-z0-9_.:+/@#-]*$/.test(value) && value !== "true" && value !== "false" ? value : JSON.stringify(value);
|
|
1533
|
+
};
|
|
1534
|
+
var oneLine = (value) => typeof value === "string" && value !== "" && !/[\r\n]/.test(value) && value === value.trim();
|
|
1535
|
+
function listLines(items, marker) {
|
|
1536
|
+
const lines = [];
|
|
1537
|
+
for (const raw of items) {
|
|
1538
|
+
const item = raw;
|
|
1539
|
+
const text = isRecord(item) && !Array.isArray(item) ? item.text : item;
|
|
1540
|
+
const level = isRecord(item) && !Array.isArray(item) && typeof item.level === "number" ? item.level : 0;
|
|
1541
|
+
const rows = serializeRuns(text);
|
|
1542
|
+
if (!rows || rows.length !== 1) return void 0;
|
|
1543
|
+
const indent = " ".repeat(level);
|
|
1544
|
+
lines.push(rows[0] === "" ? `${indent}${marker}` : `${indent}${marker} ${rows[0]}`);
|
|
1545
|
+
if (isRecord(item) && !Array.isArray(item) && item.description !== void 0) {
|
|
1546
|
+
const description = serializeRuns(item.description);
|
|
1547
|
+
if (!description || description.length !== 1 || description[0] === "") return void 0;
|
|
1548
|
+
lines.push(`${indent} : ${description[0]}`);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
return lines;
|
|
1552
|
+
}
|
|
1553
|
+
function destination(url) {
|
|
1554
|
+
if (!url || /[\r\n<>]/.test(url)) return void 0;
|
|
1555
|
+
return /^[^\s()]+$/.test(url) ? url : `<${url}>`;
|
|
1556
|
+
}
|
|
1557
|
+
function imageLines(value) {
|
|
1558
|
+
const asset = typeof value === "string" ? { src: value } : value;
|
|
1559
|
+
const dest = destination(String(asset.src));
|
|
1560
|
+
if (dest === void 0) return void 0;
|
|
1561
|
+
const alt = asset.alt === void 0 ? "" : String(asset.alt);
|
|
1562
|
+
if (/[\r\n]/.test(alt)) return void 0;
|
|
1563
|
+
let title = "";
|
|
1564
|
+
if (asset.title !== void 0) {
|
|
1565
|
+
if (/[\r\n]/.test(String(asset.title))) return void 0;
|
|
1566
|
+
title = ` "${String(asset.title).replace(/[\\"]/g, "\\$&")}"`;
|
|
1567
|
+
}
|
|
1568
|
+
return [``];
|
|
1569
|
+
}
|
|
1570
|
+
function tableLines(table) {
|
|
1571
|
+
const rows = table.rows;
|
|
1572
|
+
const columns = table.columns;
|
|
1573
|
+
const width = columns ? columns.length : Math.max(0, ...rows.map((row) => row.length));
|
|
1574
|
+
if (!width) return void 0;
|
|
1575
|
+
const cell = (value) => {
|
|
1576
|
+
if (value === null) return "";
|
|
1577
|
+
if (typeof value === "number") return String(value);
|
|
1578
|
+
const rows2 = serializeRuns(value, "cell");
|
|
1579
|
+
return rows2?.[0]?.replaceAll("|", "\\|");
|
|
1580
|
+
};
|
|
1581
|
+
const render = (cells) => {
|
|
1582
|
+
const out = cells.map(cell);
|
|
1583
|
+
return out.includes(void 0) ? void 0 : `| ${out.join(" | ")} |`;
|
|
1584
|
+
};
|
|
1585
|
+
const header = render(columns ?? Array.from({ length: width }, () => null));
|
|
1586
|
+
const body = rows.map(render);
|
|
1587
|
+
if (header === void 0 || body.includes(void 0)) return void 0;
|
|
1588
|
+
return [header, `| ${Array.from({ length: width }, () => "---").join(" | ")} |`, ...body];
|
|
1589
|
+
}
|
|
1590
|
+
function chartJson(data) {
|
|
1591
|
+
const entries = Object.entries(data).map(
|
|
1592
|
+
([key, value]) => key === "rows" && Array.isArray(value) ? [' "rows": [', ...value.map((row, index) => ` ${JSON.stringify(row)}${index < value.length - 1 ? "," : ""}`), " ]"] : [` ${JSON.stringify(key)}: ${JSON.stringify(value)}`]
|
|
1593
|
+
);
|
|
1594
|
+
return ["{", ...entries.flatMap((lines, index) => lines.map((line, at) => at === lines.length - 1 && index < entries.length - 1 ? `${line},` : line)), "}"];
|
|
1595
|
+
}
|
|
1596
|
+
function chartCandidates(chart) {
|
|
1597
|
+
if (!/^\S+$/.test(chart.type)) return [];
|
|
1598
|
+
const data = chart.data;
|
|
1599
|
+
const info = `chart ${chart.type}`;
|
|
1600
|
+
const json = fenced(info, chartJson(data));
|
|
1601
|
+
if (Array.isArray(data.columns) && Array.isArray(data.rows)) {
|
|
1602
|
+
const body = [data.columns.map((column) => csvField(column, true)).join(","), ...data.rows.map((row) => row.map((value, index) => csvField(value, index === 0)).join(","))];
|
|
1603
|
+
return [fenced(info, body), json];
|
|
1604
|
+
}
|
|
1605
|
+
return [json];
|
|
1606
|
+
}
|
|
1607
|
+
function timelineLines(timeline) {
|
|
1608
|
+
const events = Array.isArray(timeline) ? timeline : timeline.events;
|
|
1609
|
+
const meta = Array.isArray(timeline) ? {} : timeline;
|
|
1610
|
+
const info = ["timeline", ...["name", "description"].filter((key) => meta[key] !== void 0).map((key) => `${key}=${JSON.stringify(meta[key])}`)].join(" ");
|
|
1611
|
+
const body = [];
|
|
1612
|
+
for (const event of events) {
|
|
1613
|
+
for (const key of ["when", "what", "description"]) if (typeof event[key] === "string" && (/[\r\n]/.test(event[key]) || event[key] !== event[key].trim() || event[key] === "")) return void 0;
|
|
1614
|
+
body.push(event.when === void 0 ? event.what : `${event.when} \u2014 ${event.what}`);
|
|
1615
|
+
if (event.description !== void 0) body.push(` ${event.description}`);
|
|
1616
|
+
}
|
|
1617
|
+
return fenced(info, body);
|
|
1618
|
+
}
|
|
1619
|
+
function metricLines(metric) {
|
|
1620
|
+
const value = typeof metric === "object" ? metric : { value: metric };
|
|
1621
|
+
const body = [];
|
|
1622
|
+
for (const key of ["value", "label", "description", "unit", "delta", "trend"]) if (value[key] !== void 0) body.push(`${key}: ${scalarText(value[key])}`);
|
|
1623
|
+
return fenced("metric", body);
|
|
1624
|
+
}
|
|
1625
|
+
function quoteLines(quote) {
|
|
1626
|
+
const value = typeof quote === "string" ? { text: quote } : quote;
|
|
1627
|
+
const lines = [];
|
|
1628
|
+
const paragraphs = String(value.text).split("\n\n");
|
|
1629
|
+
for (const [index, paragraph] of paragraphs.entries()) {
|
|
1630
|
+
const rows = serializeRuns(paragraph);
|
|
1631
|
+
if (!rows) return void 0;
|
|
1632
|
+
if (index > 0) lines.push(">");
|
|
1633
|
+
for (const row of rows) lines.push(row === "" ? ">" : `> ${row}`);
|
|
1634
|
+
}
|
|
1635
|
+
for (const key of ["attribution", "source"]) {
|
|
1636
|
+
if (value[key] === void 0) continue;
|
|
1637
|
+
if (!oneLine(value[key])) return void 0;
|
|
1638
|
+
lines.push(`> \u2014 ${escapeInline(value[key])}`);
|
|
1639
|
+
}
|
|
1640
|
+
return lines;
|
|
1641
|
+
}
|
|
1642
|
+
function nativeCandidates(key, canon, marker) {
|
|
1643
|
+
if (key === "chart") return chartCandidates(canon);
|
|
1644
|
+
const lines = nativeLines(key, canon, marker);
|
|
1645
|
+
return lines?.length ? [lines] : [];
|
|
1646
|
+
}
|
|
1647
|
+
function nativeLines(key, canon, marker) {
|
|
1648
|
+
switch (key) {
|
|
1649
|
+
case "text": {
|
|
1650
|
+
if (canon === "") return void 0;
|
|
1651
|
+
return serializeRuns(canon);
|
|
1652
|
+
}
|
|
1653
|
+
case "items":
|
|
1654
|
+
case "bullets":
|
|
1655
|
+
return listLines(canon, marker);
|
|
1656
|
+
case "quote":
|
|
1657
|
+
return quoteLines(canon);
|
|
1658
|
+
case "code": {
|
|
1659
|
+
const code = typeof canon === "string" ? { source: canon } : canon;
|
|
1660
|
+
const language = code.language;
|
|
1661
|
+
if (language !== void 0 && (!/^[A-Za-z0-9_+#.-]+$/.test(language) || ["chart", "metric", "timeline", "opf-slide", "opf-block"].includes(language))) return void 0;
|
|
1662
|
+
return fenceCode(code.source, language, code.filename);
|
|
1663
|
+
}
|
|
1664
|
+
case "metric":
|
|
1665
|
+
return metricLines(canon);
|
|
1666
|
+
case "image":
|
|
1667
|
+
case "video":
|
|
1668
|
+
return imageLines(canon);
|
|
1669
|
+
case "table":
|
|
1670
|
+
return tableLines(canon);
|
|
1671
|
+
case "timeline":
|
|
1672
|
+
return timelineLines(canon);
|
|
1673
|
+
default:
|
|
1674
|
+
return void 0;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
function blockOptionsLine(spec, as) {
|
|
1678
|
+
const parts = [];
|
|
1679
|
+
for (const [key, value] of [["id", spec.id], ["type", spec.type], ["as", as], ["region", spec.region]]) {
|
|
1680
|
+
if (value === void 0) continue;
|
|
1681
|
+
const text = typeof value === "string" ? optionValue(value) : void 0;
|
|
1682
|
+
if (text === void 0) return void 0;
|
|
1683
|
+
parts.push(`${key}=${text}`);
|
|
1684
|
+
}
|
|
1685
|
+
return parts.length ? `<!-- block: ${parts.join(" ")} -->` : "";
|
|
1686
|
+
}
|
|
1687
|
+
function nativeBlock(spec, marker) {
|
|
1688
|
+
const canon = canonContent(spec.key, spec.value);
|
|
1689
|
+
if (canon === void 0) return void 0;
|
|
1690
|
+
const as = spec.key === "bullets" ? "bullets" : spec.key === "video" ? "video" : void 0;
|
|
1691
|
+
const logical = spec.key === "bullets" ? "items" : spec.key === "video" ? "image" : spec.key;
|
|
1692
|
+
const options = blockOptionsLine(spec, as);
|
|
1693
|
+
if (options === void 0) return void 0;
|
|
1694
|
+
for (const lines of nativeCandidates(logical, canon, marker)) {
|
|
1695
|
+
const all = options ? [options, ...lines] : lines;
|
|
1696
|
+
const { slide, clean } = parseSlideText(all.join("\n"));
|
|
1697
|
+
if (!slide || !clean) continue;
|
|
1698
|
+
const target = spec.region !== void 0 ? slide[spec.region] : spec.id !== void 0 || spec.type !== void 0 ? slide.blocks?.[0] : slide;
|
|
1699
|
+
if (target && same(target[spec.key], canon)) return all;
|
|
1700
|
+
}
|
|
1701
|
+
return void 0;
|
|
1702
|
+
}
|
|
1703
|
+
function embed(value) {
|
|
1704
|
+
const text = writeYaml(value);
|
|
1705
|
+
return text === void 0 ? void 0 : fenced("opf-block", text.trimEnd().split("\n"));
|
|
1706
|
+
}
|
|
1707
|
+
function emitSlide(slide, index, mode) {
|
|
1708
|
+
const path = `/slides/${index}`;
|
|
1709
|
+
const embedded = [];
|
|
1710
|
+
const loss = [];
|
|
1711
|
+
const extras = {};
|
|
1712
|
+
const sections = [];
|
|
1713
|
+
const exp = {};
|
|
1714
|
+
const expBlocks = [];
|
|
1715
|
+
const handle = (where, reason, keep) => {
|
|
1716
|
+
if (mode === "embed") {
|
|
1717
|
+
embedded.push({ path: where, reason });
|
|
1718
|
+
keep();
|
|
1719
|
+
} else loss.push({ path: where, message: reason });
|
|
1720
|
+
};
|
|
1721
|
+
const options = [];
|
|
1722
|
+
const optionFields = [["id", slide.id], ["type", slide.type], ["beat", slide.beat], ["layout", slide.layout], ["section", slide.section], ["tag", slide.tag]];
|
|
1723
|
+
const failed = [];
|
|
1724
|
+
for (const [key, value] of optionFields) {
|
|
1725
|
+
if (value === void 0) continue;
|
|
1726
|
+
if (key === "beat" && Array.isArray(value) && value.length >= 2 && value.every((entry) => typeof entry === "string")) {
|
|
1727
|
+
const texts = value.map(optionValue);
|
|
1728
|
+
if (texts.every((entry) => entry !== void 0)) options.push(...texts.map((entry) => ["beat", entry]));
|
|
1729
|
+
else failed.push(key);
|
|
1730
|
+
continue;
|
|
1731
|
+
}
|
|
1732
|
+
const text = typeof value === "string" ? optionValue(value) : void 0;
|
|
1733
|
+
if (text === void 0) failed.push(key);
|
|
1734
|
+
else options.push([key, text]);
|
|
1735
|
+
}
|
|
1736
|
+
if (slide.hidden === true) options.push(["hidden", "true"]);
|
|
1737
|
+
else if (slide.hidden === false) options.push(["hidden", "false"]);
|
|
1738
|
+
else if (slide.hidden !== void 0) failed.push("hidden");
|
|
1739
|
+
const optionsText = options.map(([key, text]) => key === "hidden" && text === "true" ? "hidden" : `${key}=${text}`).join(" ");
|
|
1740
|
+
const optionsLine = options.length ? `<!-- slide: ${optionsText} -->` : "";
|
|
1741
|
+
if (optionsLine) {
|
|
1742
|
+
const { slide: back, clean } = parseSlideText(optionsLine);
|
|
1743
|
+
const expected = {};
|
|
1744
|
+
for (const [key] of options) expected[key] = slide[key];
|
|
1745
|
+
if (!back || !clean || !same(back, expected)) {
|
|
1746
|
+
for (const [key] of options) failed.push(key);
|
|
1747
|
+
options.length = 0;
|
|
1748
|
+
}
|
|
1749
|
+
}
|
|
1750
|
+
for (const key of new Set(failed)) handle(`${path}/${key}`, `slide option ${key} has no <!-- slide: --> form`, () => extras[key] = slide[key]);
|
|
1751
|
+
const kept = options.length ? `<!-- slide: ${options.map(([key, text]) => key === "hidden" && text === "true" ? "hidden" : `${key}=${text}`).join(" ")} -->` : "";
|
|
1752
|
+
if (kept) {
|
|
1753
|
+
sections.push({ lines: [kept] });
|
|
1754
|
+
for (const [key] of options) exp[key] = slide[key];
|
|
1755
|
+
}
|
|
1756
|
+
for (const [key, mark] of [["title", "#"], ["subtitle", "##"]]) {
|
|
1757
|
+
const value = slide[key];
|
|
1758
|
+
if (value === void 0) continue;
|
|
1759
|
+
if (oneLine(value)) {
|
|
1760
|
+
const line = `${mark} ${escapeInline(value).replace(/(^|\s)(#+)$/, "$1\\$2")}`;
|
|
1761
|
+
const { slide: back, clean } = parseSlideText(line);
|
|
1762
|
+
if (back && clean && back[key] === value) {
|
|
1763
|
+
sections.push({ lines: [line] });
|
|
1764
|
+
exp[key] = value;
|
|
1765
|
+
continue;
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
handle(`${path}/${key}`, `the ${key} is not a single line of plain text`, () => extras[key] = value);
|
|
1769
|
+
}
|
|
1770
|
+
const specs = [];
|
|
1771
|
+
const handledRegions = /* @__PURE__ */ new Set();
|
|
1772
|
+
if (Array.isArray(slide.blocks)) {
|
|
1773
|
+
slide.blocks.forEach((block, i) => {
|
|
1774
|
+
const where = `${path}/blocks/${i}`;
|
|
1775
|
+
if (!isRecord(block)) return;
|
|
1776
|
+
const keys = Object.keys(block).filter((key) => key !== "id" && key !== "type");
|
|
1777
|
+
const only3 = keys.length === 1 && isContent(keys[0]) ? keys[0] : void 0;
|
|
1778
|
+
specs.push({ spec: { key: only3 ?? "", value: only3 ? block[only3] : void 0, id: block.id, type: block.type }, where, payload: block });
|
|
1779
|
+
});
|
|
1780
|
+
for (const key of CONTENT2) if (slide[key] !== void 0) extras[key] = slide[key];
|
|
1781
|
+
} else {
|
|
1782
|
+
for (const key of CONTENT2) if (slide[key] !== void 0) specs.push({ spec: { key, value: slide[key] }, where: `${path}/${key}` });
|
|
1783
|
+
}
|
|
1784
|
+
for (const [key, value] of Object.entries(slide)) {
|
|
1785
|
+
if (!REGION.test(key) || !isRecord(value)) continue;
|
|
1786
|
+
const rest = Object.keys(value).filter((entry) => entry !== "id" && entry !== "type");
|
|
1787
|
+
const only3 = rest.length === 1 && isContent(rest[0]) ? rest[0] : void 0;
|
|
1788
|
+
if (only3 === void 0) continue;
|
|
1789
|
+
handledRegions.add(key);
|
|
1790
|
+
specs.push({ spec: { key: only3, value: value[only3], id: value.id, type: value.type, region: key }, where: `${path}/${key}`, payload: value });
|
|
1791
|
+
}
|
|
1792
|
+
let marker = "-";
|
|
1793
|
+
for (const { spec, where, payload } of specs) {
|
|
1794
|
+
const lines2 = spec.key ? nativeBlock(spec, marker) : void 0;
|
|
1795
|
+
if (lines2) {
|
|
1796
|
+
sections.push({ lines: lines2 });
|
|
1797
|
+
const written = { ...spec.id !== void 0 ? { id: spec.id } : {}, ...spec.type !== void 0 ? { type: spec.type } : {}, [spec.key]: canonContent(spec.key, spec.value) };
|
|
1798
|
+
if (spec.region !== void 0) exp[spec.region] = written;
|
|
1799
|
+
else expBlocks.push(written);
|
|
1800
|
+
const list = spec.key === "items" || spec.key === "bullets";
|
|
1801
|
+
marker = list ? marker === "-" ? "*" : "-" : "-";
|
|
1802
|
+
continue;
|
|
1803
|
+
}
|
|
1804
|
+
marker = "-";
|
|
1805
|
+
handle(where, spec.key ? `${spec.key} content has a part with no Markdown form` : "a block with several fields or a nested group has no Markdown form", () => {
|
|
1806
|
+
if (spec.region !== void 0) {
|
|
1807
|
+
extras[spec.region] = slide[spec.region];
|
|
1808
|
+
return;
|
|
1809
|
+
}
|
|
1810
|
+
const payloadValue = payload ?? { [spec.key]: spec.value };
|
|
1811
|
+
const lines3 = embed(payloadValue);
|
|
1812
|
+
if (!lines3) throw new OPFMarkdownError("not-representable", `The block at ${where} holds a value that YAML cannot carry exactly.`, { path: where });
|
|
1813
|
+
sections.push({ lines: lines3 });
|
|
1814
|
+
if (spec.region !== void 0) exp[spec.region] = structuredClone(payloadValue);
|
|
1815
|
+
else expBlocks.push(structuredClone(payloadValue));
|
|
1816
|
+
});
|
|
1817
|
+
}
|
|
1818
|
+
let notesLines;
|
|
1819
|
+
if (slide.notes !== void 0) {
|
|
1820
|
+
const notes = slide.notes;
|
|
1821
|
+
if (typeof notes === "string" && notes !== "" && !/\r/.test(notes)) {
|
|
1822
|
+
const rows = notes.split("\n");
|
|
1823
|
+
const candidate = [`Note: ${rows[0]}`, ...rows.slice(1)];
|
|
1824
|
+
const { slide: back, clean } = parseSlideText(candidate.join("\n"));
|
|
1825
|
+
if (back && clean && back.notes === notes) {
|
|
1826
|
+
notesLines = candidate;
|
|
1827
|
+
exp.notes = notes;
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
if (!notesLines) handle(`${path}/notes`, "the speaker notes have no Note: form (blank edges, a --- line or a carriage return)", () => extras.notes = notes);
|
|
1831
|
+
}
|
|
1832
|
+
for (const [key, value] of Object.entries(slide)) if (!NATIVE_SLIDE.has(key) && !handledRegions.has(key)) handle(`${path}/${key}`, `${JSON.stringify(key)} has no Markdown form`, () => extras[key] = value);
|
|
1833
|
+
if (Object.keys(extras).length) {
|
|
1834
|
+
const text = writeYaml(extras);
|
|
1835
|
+
if (text === void 0) throw new OPFMarkdownError("not-representable", `The slide at ${path} holds a value that YAML cannot carry exactly.`, { path });
|
|
1836
|
+
sections.push({ lines: fenced("opf-slide", text.trimEnd().split("\n")) });
|
|
1837
|
+
Object.assign(exp, structuredClone(extras));
|
|
1838
|
+
}
|
|
1839
|
+
const [only2] = expBlocks;
|
|
1840
|
+
const onlyKey = only2 && Object.keys(only2).length === 1 ? Object.keys(only2)[0] : void 0;
|
|
1841
|
+
if (expBlocks.length === 1 && onlyKey !== void 0 && isContent(onlyKey)) exp[onlyKey] = only2[onlyKey];
|
|
1842
|
+
else if (expBlocks.length) exp.blocks = expBlocks;
|
|
1843
|
+
if (notesLines) sections.push({ lines: notesLines });
|
|
1844
|
+
if (!sections.length) sections.push({ lines: ["<!-- slide -->"] });
|
|
1845
|
+
const lines = sections.flatMap((section, i) => i === 0 || i === 1 && kept ? section.lines : ["", ...section.lines]);
|
|
1846
|
+
if (mode === "embed") {
|
|
1847
|
+
const { slide: back, clean } = parseSlideText(lines.join("\n"));
|
|
1848
|
+
if (!back || !clean || !same(back, exp)) throw new OPFMarkdownError("not-representable", `The slide at ${path} does not read back from its Markdown.`, { path, expected: exp, actual: back });
|
|
1849
|
+
}
|
|
1850
|
+
return { lines, embedded, loss };
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
// src/markdown.ts
|
|
1854
|
+
function markdownToOpf(markdown, options = {}) {
|
|
1855
|
+
if (typeof markdown !== "string") throw new TypeError("markdownToOpf expects a string.");
|
|
1856
|
+
const source = markdown;
|
|
1857
|
+
const ctx = new Ctx(source);
|
|
1858
|
+
let lines = splitLines(source);
|
|
1859
|
+
if (lines[0]?.text.startsWith("\uFEFF")) lines = [{ ...lines[0], text: lines[0].text.slice(1), start: lines[0].start + 1 }, ...lines.slice(1)];
|
|
1860
|
+
const front = {};
|
|
1861
|
+
let body = lines;
|
|
1862
|
+
const matter = frontMatter(lines);
|
|
1863
|
+
if (matter === "unterminated") {
|
|
1864
|
+
ctx.error("front-matter-unterminated", "The front matter starting on line 1 is not closed.", "Close it with a line containing only ---, or remove the opening --- (a deck without front matter starts with its first slide).", lineRange(lines[0]));
|
|
1865
|
+
body = lines.slice(1);
|
|
1866
|
+
} else if (matter) {
|
|
1867
|
+
const range = { start: matter.yaml.start, end: matter.yaml.end };
|
|
1868
|
+
const parsed = readYamlMapping(source.slice(range.start, range.end), range.start, ctx, "front-matter", range);
|
|
1869
|
+
ctx.ranges.set("", range);
|
|
1870
|
+
if (parsed.ok && !Object.keys(parsed.value).length && source.slice(range.start, range.end).trim()) ctx.warn("front-matter-comments", "The text between the first two --- lines is only YAML comments, so it was not read as slide content.", "A deck that starts with a slide must not start with a --- line: only front matter may open the file. Remove the first --- or add the deck properties.", range);
|
|
1871
|
+
for (const [key, value] of Object.entries(parsed.value)) {
|
|
1872
|
+
if (key === "slides") ctx.error("front-matter-slides", "The front matter cannot hold `slides`: slides are the Markdown that follows it.", "Remove the slides key and write the slides below the front matter, separated by ---.", parsed.keys.get("slides") ?? range, "/slides");
|
|
1873
|
+
else Object.defineProperty(front, key, { value, enumerable: true, writable: true, configurable: true });
|
|
1874
|
+
}
|
|
1875
|
+
for (const [key, found] of parsed.keys) ctx.ranges.set(`/${key.replaceAll("~", "~0").replaceAll("/", "~1")}`, found);
|
|
1876
|
+
body = lines.slice(matter.end);
|
|
1877
|
+
}
|
|
1878
|
+
const document = { ...options.defaults ?? {}, ...front };
|
|
1879
|
+
const segments = splitSegments(body, options.split ?? "rules");
|
|
1880
|
+
const slides = [];
|
|
1881
|
+
segments.forEach((segment, index) => {
|
|
1882
|
+
if (emptySegment(segment)) {
|
|
1883
|
+
const edge = index === 0 || index === segments.length - 1;
|
|
1884
|
+
if (!edge) {
|
|
1885
|
+
const around = segment[0] ?? segments[index + 1]?.[0] ?? segments[index - 1]?.at(-1);
|
|
1886
|
+
if (around) ctx.warn("empty-slide", "Two --- lines in a row give an empty slide, which was skipped.", "Remove the extra ---, or write <!-- slide --> to keep an empty slide.", lineRange(around));
|
|
1887
|
+
}
|
|
1888
|
+
return;
|
|
1889
|
+
}
|
|
1890
|
+
slides.push(parseSlide(ctx, segment, slides.length));
|
|
1891
|
+
});
|
|
1892
|
+
document.slides = slides;
|
|
1893
|
+
if (!slides.length) ctx.error("no-slides", "The Markdown has no slides.", "Write at least one slide: a # title or any content, with --- between slides.", { start: 0, end: Math.min(1, source.length) }, "/slides");
|
|
1894
|
+
else if (options.validate !== false) {
|
|
1895
|
+
for (const found of lintPresentation(document).diagnostics) {
|
|
1896
|
+
const range = ctx.rangeOf(found.path) ?? ctx.rangeOf("") ?? { start: 0, end: 0 };
|
|
1897
|
+
ctx.diagnostics.push({ ...found, location: ctx.location(range) });
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
const diagnostics = [...ctx.diagnostics].sort((a, b) => a.location.offset - b.location.offset);
|
|
1901
|
+
const counts = { error: 0, warning: 0, info: 0 };
|
|
1902
|
+
for (const diagnostic of diagnostics) counts[diagnostic.severity]++;
|
|
1903
|
+
return { document, valid: counts.error === 0, diagnostics, counts };
|
|
1904
|
+
}
|
|
1905
|
+
function opfToMarkdown(document, options = {}) {
|
|
1906
|
+
const checked = validatePresentation(document);
|
|
1907
|
+
if (!checked.valid) {
|
|
1908
|
+
const first = checked.errors[0];
|
|
1909
|
+
throw new OPFMarkdownError("invalid-document", `The document is not valid OPF: ${first?.message ?? "unknown error"}${first?.path ? ` (${first.path})` : ""}.`, { issues: checked.errors });
|
|
1910
|
+
}
|
|
1911
|
+
const mode = options.unsupported === "drop" ? "drop" : "embed";
|
|
1912
|
+
const { slides, ...rest } = document;
|
|
1913
|
+
const parts = [];
|
|
1914
|
+
if (Object.keys(rest).length) {
|
|
1915
|
+
const yaml = writeYaml(rest);
|
|
1916
|
+
if (yaml === void 0) throw new OPFMarkdownError("not-representable", "The deck properties hold a value that YAML cannot carry exactly.");
|
|
1917
|
+
if (!same(readBack(yaml), rest)) throw new OPFMarkdownError("not-representable", "The deck properties do not read back from their front matter.");
|
|
1918
|
+
parts.push(`---
|
|
1919
|
+
${yaml}---
|
|
1920
|
+
`);
|
|
1921
|
+
}
|
|
1922
|
+
const embedded = [];
|
|
1923
|
+
const loss = [];
|
|
1924
|
+
const bodies = [];
|
|
1925
|
+
slides.forEach((slide, index) => {
|
|
1926
|
+
const out = emitSlide(slide, index, mode);
|
|
1927
|
+
embedded.push(...out.embedded);
|
|
1928
|
+
loss.push(...out.loss.map((part) => `${part.path}: ${part.message}`));
|
|
1929
|
+
bodies.push(out.lines.join("\n"));
|
|
1930
|
+
});
|
|
1931
|
+
const markdown = `${parts.length ? `${parts[0]}
|
|
1932
|
+
` : ""}${bodies.join("\n\n---\n\n")}
|
|
1933
|
+
`;
|
|
1934
|
+
return { markdown, report: { lossless: loss.length === 0, native: loss.length === 0 && embedded.length === 0, embedded, loss } };
|
|
1935
|
+
}
|
|
1936
|
+
function readBack(yaml) {
|
|
1937
|
+
const ctx = new Ctx(yaml);
|
|
1938
|
+
return readYamlMapping(yaml, 0, ctx, "front-matter", { start: 0, end: yaml.length }).value;
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
export { OPFMarkdownError, markdownToOpf, opfToMarkdown };
|