@openpresentation/opf 0.26.3 → 0.26.4
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 +1 -1
- package/dist/browser.js +13 -11
- package/dist/{chunk-34LIUOT7.js → chunk-6IXSHIAL.js} +6 -6
- package/dist/{chunk-PXOXTJTN.js → chunk-6NBTZYYD.js} +1 -1
- package/dist/{chunk-FXNUM2PN.js → chunk-EDCRBTJF.js} +4 -4
- package/dist/chunk-GWJBZ4LC.js +639 -0
- package/dist/{chunk-YVNFACTC.js → chunk-H5QC3X3K.js} +1 -1
- package/dist/{chunk-WASENX2R.js → chunk-MN7DRK57.js} +40 -648
- package/dist/{chunk-GLLR3AQ5.js → chunk-NZLW6FBQ.js} +1 -1
- package/dist/{chunk-7RY7QWWJ.js → chunk-O6BY3AU2.js} +1 -1
- package/dist/{chunk-PB5X2VSV.js → chunk-PIO73BQ6.js} +4 -3
- package/dist/{chunk-DEI2M2H6.js → chunk-QAEBYDKY.js} +2 -4
- package/dist/{chunk-54OV2WQC.js → chunk-QH2W6HIM.js} +1 -1
- package/dist/chunk-RYZPBJLB.js +4 -0
- package/dist/{chunk-NJWI6MPT.js → chunk-XFOOKVRI.js} +16 -1
- package/dist/{chunk-2ZQBMZTZ.js → chunk-ZSZDVIDV.js} +2 -2
- package/dist/{conversion-BjsKgxBZ.d.ts → conversion-DSafOGzh.d.ts} +2 -2
- package/dist/conversion-QIAJM6DQ.js +20 -0
- package/dist/convert.js +5 -5
- package/dist/deck.js +9 -7
- package/dist/diff.js +2 -2
- package/dist/docs.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +17 -15
- package/dist/markdown.js +7 -5
- package/dist/node-engine.d.ts +2 -2
- package/dist/node-engine.js +17 -15
- package/dist/pagination.js +2 -2
- package/dist/patch.js +1 -1
- package/dist/schema.js +1 -1
- package/dist/source.d.ts +102 -4
- package/dist/source.js +388 -9
- package/dist/validator.js +1 -1
- package/dist/yaml.js +5 -4
- package/package.json +5 -5
- package/dist/conversion-F4UCOGR7.js +0 -18
|
@@ -0,0 +1,639 @@
|
|
|
1
|
+
import { NOT_CHECKED } from './chunk-RYZPBJLB.js';
|
|
2
|
+
import { readMarkdown, parseSlideText, escapeInline, serializeRuns } from './chunk-MN7DRK57.js';
|
|
3
|
+
import { fenceCode } from './chunk-O6BY3AU2.js';
|
|
4
|
+
import { OPFMarkdownError, writeYaml, Ctx, readYamlMapping, csvField, scalarText } from './chunk-QAEBYDKY.js';
|
|
5
|
+
import { same, isRecord, runText, isPlainRun, FORMATTING } from './chunk-6NBTZYYD.js';
|
|
6
|
+
import { validate } from './chunk-XFOOKVRI.js';
|
|
7
|
+
import { checkCatalogsOption } from './chunk-XRMV2GMB.js';
|
|
8
|
+
|
|
9
|
+
// src/markdown/canon.ts
|
|
10
|
+
var FORMAT_KEYS = FORMATTING;
|
|
11
|
+
function canonRun(run) {
|
|
12
|
+
if (typeof run === "string") return run;
|
|
13
|
+
if (!isRecord(run) || typeof run.text !== "string") return void 0;
|
|
14
|
+
const out = { text: run.text };
|
|
15
|
+
for (const [key, value] of Object.entries(run)) {
|
|
16
|
+
if (key === "text") continue;
|
|
17
|
+
if (!FORMAT_KEYS.includes(key)) return void 0;
|
|
18
|
+
if (value === false || value === void 0) continue;
|
|
19
|
+
out[key] = value;
|
|
20
|
+
}
|
|
21
|
+
return Object.keys(out).length === 1 ? out.text : out;
|
|
22
|
+
}
|
|
23
|
+
var sameFormat = (a, b) => {
|
|
24
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
25
|
+
keys.delete("text");
|
|
26
|
+
for (const key of keys) if (a[key] !== b[key]) return false;
|
|
27
|
+
return true;
|
|
28
|
+
};
|
|
29
|
+
function canonText(value) {
|
|
30
|
+
if (typeof value === "string") return value;
|
|
31
|
+
if (!Array.isArray(value)) return void 0;
|
|
32
|
+
const merged = [];
|
|
33
|
+
for (const raw of value) {
|
|
34
|
+
const run = canonRun(raw);
|
|
35
|
+
if (run === void 0) return void 0;
|
|
36
|
+
if (runText(run) === "") continue;
|
|
37
|
+
const last = merged.at(-1);
|
|
38
|
+
if (last !== void 0) {
|
|
39
|
+
if (typeof last === "string" && typeof run === "string") {
|
|
40
|
+
merged[merged.length - 1] = last + run;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (typeof last === "object" && typeof run === "object" && sameFormat(last, run)) {
|
|
44
|
+
merged[merged.length - 1] = { ...last, text: last.text + run.text };
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
merged.push(run);
|
|
49
|
+
}
|
|
50
|
+
if (merged.length === 0) return "";
|
|
51
|
+
if (merged.every(isPlainRun)) return merged.map(runText).join("");
|
|
52
|
+
return merged;
|
|
53
|
+
}
|
|
54
|
+
function canonItem(item) {
|
|
55
|
+
if (typeof item === "string" || Array.isArray(item)) return canonText(item);
|
|
56
|
+
if (!isRecord(item)) return void 0;
|
|
57
|
+
for (const key of Object.keys(item)) if (key !== "text" && key !== "level" && key !== "description") return void 0;
|
|
58
|
+
const text = canonText(item.text);
|
|
59
|
+
if (text === void 0) return void 0;
|
|
60
|
+
const level = item.level === void 0 ? 0 : item.level;
|
|
61
|
+
if (!Number.isInteger(level) || level < 0) return void 0;
|
|
62
|
+
const description = item.description === void 0 ? void 0 : canonText(item.description);
|
|
63
|
+
if (item.description !== void 0 && description === void 0) return void 0;
|
|
64
|
+
if (level === 0 && description === void 0) return text;
|
|
65
|
+
const out = { text };
|
|
66
|
+
if (description !== void 0) out.description = description;
|
|
67
|
+
if (level !== 0) out.level = level;
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
var levelOf = (item) => isRecord(item) && typeof item.level === "number" ? item.level : 0;
|
|
71
|
+
function canonItems(value) {
|
|
72
|
+
if (!Array.isArray(value) || value.length === 0) return void 0;
|
|
73
|
+
const items = [];
|
|
74
|
+
let previous = -1;
|
|
75
|
+
for (const raw of value) {
|
|
76
|
+
const item = canonItem(raw);
|
|
77
|
+
if (item === void 0) return void 0;
|
|
78
|
+
const level = levelOf(item);
|
|
79
|
+
if (level > previous + 1 || previous < 0 && level !== 0) return void 0;
|
|
80
|
+
previous = level;
|
|
81
|
+
items.push(item);
|
|
82
|
+
}
|
|
83
|
+
return items;
|
|
84
|
+
}
|
|
85
|
+
var only = (value, keys) => Object.keys(value).every((key) => keys.includes(key));
|
|
86
|
+
function canonQuote(value) {
|
|
87
|
+
if (typeof value === "string") return value;
|
|
88
|
+
if (!isRecord(value) || !only(value, ["text", "attribution", "source"])) return void 0;
|
|
89
|
+
const text = canonText(value.text);
|
|
90
|
+
if (text === void 0) return void 0;
|
|
91
|
+
for (const key of ["attribution", "source"]) if (value[key] !== void 0 && typeof value[key] !== "string") return void 0;
|
|
92
|
+
if (value.source !== void 0 && value.attribution === void 0) return void 0;
|
|
93
|
+
if (value.attribution === void 0) return typeof text === "string" ? text : { text };
|
|
94
|
+
const out = { text, attribution: value.attribution };
|
|
95
|
+
if (value.source !== void 0) out.source = value.source;
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
function canonCode(value) {
|
|
99
|
+
if (typeof value === "string") return value;
|
|
100
|
+
if (!isRecord(value) || !only(value, ["source", "language", "filename"]) || typeof value.source !== "string") return void 0;
|
|
101
|
+
for (const key of ["language", "filename"]) if (value[key] !== void 0 && (typeof value[key] !== "string" || value[key] === "")) return void 0;
|
|
102
|
+
if (value.language === void 0 && value.filename === void 0) return value.source;
|
|
103
|
+
const out = { source: value.source };
|
|
104
|
+
if (value.language !== void 0) out.language = value.language;
|
|
105
|
+
if (value.filename !== void 0) out.filename = value.filename;
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
function canonMetric(value) {
|
|
109
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
110
|
+
if (!isRecord(value) || !only(value, ["value", "label", "description", "unit", "delta", "trend", "sentiment"])) return void 0;
|
|
111
|
+
if (typeof value.value !== "string" && typeof value.value !== "number") return void 0;
|
|
112
|
+
const out = { value: value.value };
|
|
113
|
+
for (const key of ["label", "description", "unit", "delta", "trend", "sentiment"]) {
|
|
114
|
+
const entry = value[key];
|
|
115
|
+
if (entry === void 0) continue;
|
|
116
|
+
if (typeof entry !== "string" && !(key === "delta" && typeof entry === "number")) return void 0;
|
|
117
|
+
out[key] = entry;
|
|
118
|
+
}
|
|
119
|
+
return Object.keys(out).length === 1 ? out.value : out;
|
|
120
|
+
}
|
|
121
|
+
function canonAsset(value) {
|
|
122
|
+
if (typeof value === "string") return value;
|
|
123
|
+
if (!isRecord(value) || !only(value, ["src", "alt", "title"]) || typeof value.src !== "string") return void 0;
|
|
124
|
+
for (const key of ["alt", "title"]) if (value[key] !== void 0 && typeof value[key] !== "string") return void 0;
|
|
125
|
+
const out = { src: value.src };
|
|
126
|
+
if (value.alt !== void 0) out.alt = value.alt;
|
|
127
|
+
if (value.title !== void 0) out.title = value.title;
|
|
128
|
+
return Object.keys(out).length === 1 ? out.src : out;
|
|
129
|
+
}
|
|
130
|
+
function canonCell(cell) {
|
|
131
|
+
if (cell === null) return cell;
|
|
132
|
+
if (typeof cell === "string" || Array.isArray(cell)) return canonText(cell);
|
|
133
|
+
return void 0;
|
|
134
|
+
}
|
|
135
|
+
function canonTable(value) {
|
|
136
|
+
if (!isRecord(value) || !only(value, ["columns", "rows"]) || !Array.isArray(value.rows)) return void 0;
|
|
137
|
+
const out = {};
|
|
138
|
+
if (value.columns !== void 0) {
|
|
139
|
+
if (!Array.isArray(value.columns) || value.columns.length === 0) return void 0;
|
|
140
|
+
const columns = value.columns.map((cell) => cell === null ? null : typeof cell === "string" || Array.isArray(cell) ? canonText(cell) : void 0);
|
|
141
|
+
if (columns.includes(void 0) || columns.every((cell) => cell === null || cell === "")) return void 0;
|
|
142
|
+
out.columns = columns;
|
|
143
|
+
}
|
|
144
|
+
const rows = [];
|
|
145
|
+
for (const row of value.rows) {
|
|
146
|
+
if (!Array.isArray(row)) return void 0;
|
|
147
|
+
const cells = row.map(canonCell);
|
|
148
|
+
if (cells.includes(void 0)) return void 0;
|
|
149
|
+
rows.push(cells);
|
|
150
|
+
}
|
|
151
|
+
out.rows = rows;
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
function canonChart(value) {
|
|
155
|
+
if (!isRecord(value) || !only(value, ["type", "alt", "data"]) || typeof value.type !== "string" || !isRecord(value.data)) return void 0;
|
|
156
|
+
if (value.alt !== void 0 && typeof value.alt !== "string") return void 0;
|
|
157
|
+
const alt = value.alt === void 0 ? {} : { alt: value.alt };
|
|
158
|
+
const data = value.data;
|
|
159
|
+
if (Array.isArray(data.columns) && Array.isArray(data.rows) && only(data, ["columns", "rows"])) {
|
|
160
|
+
if (!data.columns.every((column) => typeof column === "string") || data.columns.length === 0 || data.rows.length === 0) return void 0;
|
|
161
|
+
for (const row of data.rows) if (!Array.isArray(row) || !row.every((cell) => cell === null || ["string", "number", "boolean"].includes(typeof cell))) return void 0;
|
|
162
|
+
return { type: value.type, ...alt, data: { columns: data.columns, rows: data.rows } };
|
|
163
|
+
}
|
|
164
|
+
return void 0;
|
|
165
|
+
}
|
|
166
|
+
function canonEvent(event) {
|
|
167
|
+
if (!isRecord(event) || !only(event, ["when", "what", "description", "status"]) || typeof event.what !== "string") return void 0;
|
|
168
|
+
for (const key of ["when", "description"]) if (event[key] !== void 0 && typeof event[key] !== "string") return void 0;
|
|
169
|
+
if (event.status !== void 0 && !["done", "current", "planned"].includes(event.status)) return void 0;
|
|
170
|
+
const out = {};
|
|
171
|
+
if (event.when !== void 0) out.when = event.when;
|
|
172
|
+
out.what = event.what;
|
|
173
|
+
if (event.description !== void 0) out.description = event.description;
|
|
174
|
+
if (event.status !== void 0) out.status = event.status;
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
function canonTimeline(value) {
|
|
178
|
+
const events = Array.isArray(value) ? value : isRecord(value) && only(value, ["name", "description", "events"]) ? value.events : void 0;
|
|
179
|
+
if (!Array.isArray(events) || events.length === 0) return void 0;
|
|
180
|
+
const canon = events.map(canonEvent);
|
|
181
|
+
if (canon.includes(void 0)) return void 0;
|
|
182
|
+
if (Array.isArray(value)) return canon;
|
|
183
|
+
for (const key of ["name", "description"]) if (value[key] !== void 0 && typeof value[key] !== "string") return void 0;
|
|
184
|
+
const { name, description } = value;
|
|
185
|
+
if (name === void 0 && description === void 0) return canon;
|
|
186
|
+
const out = {};
|
|
187
|
+
if (name !== void 0) out.name = name;
|
|
188
|
+
if (description !== void 0) out.description = description;
|
|
189
|
+
out.events = canon;
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
function canonContent(key, value) {
|
|
193
|
+
switch (key) {
|
|
194
|
+
case "text":
|
|
195
|
+
return canonText(value);
|
|
196
|
+
case "items":
|
|
197
|
+
case "bullets":
|
|
198
|
+
return canonItems(value);
|
|
199
|
+
case "quote":
|
|
200
|
+
return canonQuote(value);
|
|
201
|
+
case "code":
|
|
202
|
+
return canonCode(value);
|
|
203
|
+
case "metric":
|
|
204
|
+
return canonMetric(value);
|
|
205
|
+
case "image":
|
|
206
|
+
case "video":
|
|
207
|
+
return canonAsset(value);
|
|
208
|
+
case "table":
|
|
209
|
+
return canonTable(value);
|
|
210
|
+
case "chart":
|
|
211
|
+
return canonChart(value);
|
|
212
|
+
case "timeline":
|
|
213
|
+
return canonTimeline(value);
|
|
214
|
+
default:
|
|
215
|
+
return void 0;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/markdown/emit.ts
|
|
220
|
+
var CONTENT = ["text", "items", "bullets", "image", "video", "chart", "table", "code", "metric", "quote", "timeline"];
|
|
221
|
+
var isContent = (key) => CONTENT.includes(key);
|
|
222
|
+
var ROWS = "(?:top|middle|bottom|top\\+middle|middle\\+bottom|top\\+middle\\+bottom)";
|
|
223
|
+
var COLUMNS = "(?:left|center|right|left\\+center|center\\+right|left\\+center\\+right)";
|
|
224
|
+
var REGION = new RegExp(`^(?:${ROWS}(?::${COLUMNS})?|${COLUMNS})$`);
|
|
225
|
+
var NATIVE_SLIDE = /* @__PURE__ */ new Set(["id", "type", "layout", "section", "tag", "hidden", "beat", "title", "subtitle", "notes", "blocks", ...CONTENT]);
|
|
226
|
+
function fenceFor(body, char = "`") {
|
|
227
|
+
let longest = 2;
|
|
228
|
+
for (const line of body) for (const run of line.matchAll(char === "`" ? /`+/g : /~+/g)) longest = Math.max(longest, run[0].length);
|
|
229
|
+
return char.repeat(longest + 1);
|
|
230
|
+
}
|
|
231
|
+
var fenced = (info, body) => {
|
|
232
|
+
const fence = fenceFor(body);
|
|
233
|
+
return [`${fence}${info}`, ...body, fence];
|
|
234
|
+
};
|
|
235
|
+
var optionValue = (value) => {
|
|
236
|
+
if (value.includes("--") || /[\r\n]/.test(value)) return void 0;
|
|
237
|
+
return /^[A-Za-z0-9_][A-Za-z0-9_.:+/@#-]*$/.test(value) && value !== "true" && value !== "false" ? value : JSON.stringify(value);
|
|
238
|
+
};
|
|
239
|
+
var oneLine = (value) => typeof value === "string" && value !== "" && !/[\r\n]/.test(value) && value === value.trim();
|
|
240
|
+
function listLines(items, marker) {
|
|
241
|
+
const lines = [];
|
|
242
|
+
for (const raw of items) {
|
|
243
|
+
const item = raw;
|
|
244
|
+
const text = isRecord(item) && !Array.isArray(item) ? item.text : item;
|
|
245
|
+
const level = isRecord(item) && !Array.isArray(item) && typeof item.level === "number" ? item.level : 0;
|
|
246
|
+
const rows = serializeRuns(text);
|
|
247
|
+
if (!rows || rows.length !== 1) return void 0;
|
|
248
|
+
const indent = " ".repeat(level);
|
|
249
|
+
lines.push(rows[0] === "" ? `${indent}${marker}` : `${indent}${marker} ${rows[0]}`);
|
|
250
|
+
if (isRecord(item) && !Array.isArray(item) && item.description !== void 0) {
|
|
251
|
+
const description = serializeRuns(item.description);
|
|
252
|
+
if (!description || description.length !== 1 || description[0] === "") return void 0;
|
|
253
|
+
lines.push(`${indent} : ${description[0]}`);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return lines;
|
|
257
|
+
}
|
|
258
|
+
function destination(url) {
|
|
259
|
+
if (!url || /[\r\n<>]/.test(url)) return void 0;
|
|
260
|
+
return /^[^\s()]+$/.test(url) ? url : `<${url}>`;
|
|
261
|
+
}
|
|
262
|
+
function imageLines(value) {
|
|
263
|
+
const asset = typeof value === "string" ? { src: value } : value;
|
|
264
|
+
const dest = destination(String(asset.src));
|
|
265
|
+
if (dest === void 0) return void 0;
|
|
266
|
+
const alt = asset.alt === void 0 ? "" : String(asset.alt);
|
|
267
|
+
if (/[\r\n]/.test(alt)) return void 0;
|
|
268
|
+
let title = "";
|
|
269
|
+
if (asset.title !== void 0) {
|
|
270
|
+
if (/[\r\n]/.test(String(asset.title))) return void 0;
|
|
271
|
+
title = ` "${String(asset.title).replace(/[\\"]/g, "\\$&")}"`;
|
|
272
|
+
}
|
|
273
|
+
return [``];
|
|
274
|
+
}
|
|
275
|
+
function tableLines(table) {
|
|
276
|
+
const rows = table.rows;
|
|
277
|
+
const columns = table.columns;
|
|
278
|
+
const width = columns ? columns.length : Math.max(0, ...rows.map((row) => row.length));
|
|
279
|
+
if (!width) return void 0;
|
|
280
|
+
const cell = (value) => {
|
|
281
|
+
if (value === null) return "";
|
|
282
|
+
if (typeof value === "number") return String(value);
|
|
283
|
+
const rows2 = serializeRuns(value, "cell");
|
|
284
|
+
return rows2?.[0]?.replaceAll("|", "\\|");
|
|
285
|
+
};
|
|
286
|
+
const render = (cells) => {
|
|
287
|
+
const out = cells.map(cell);
|
|
288
|
+
return out.includes(void 0) ? void 0 : `| ${out.join(" | ")} |`;
|
|
289
|
+
};
|
|
290
|
+
const header = render(columns ?? Array.from({ length: width }, () => null));
|
|
291
|
+
const body = rows.map(render);
|
|
292
|
+
if (header === void 0 || body.includes(void 0)) return void 0;
|
|
293
|
+
return [header, `| ${Array.from({ length: width }, () => "---").join(" | ")} |`, ...body];
|
|
294
|
+
}
|
|
295
|
+
function chartJson(data) {
|
|
296
|
+
const entries = Object.entries(data).map(
|
|
297
|
+
([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)}`]
|
|
298
|
+
);
|
|
299
|
+
return ["{", ...entries.flatMap((lines, index) => lines.map((line, at) => at === lines.length - 1 && index < entries.length - 1 ? `${line},` : line)), "}"];
|
|
300
|
+
}
|
|
301
|
+
function chartCandidates(chart) {
|
|
302
|
+
if (!/^\S+$/.test(chart.type)) return [];
|
|
303
|
+
const data = chart.data;
|
|
304
|
+
if (typeof chart.alt === "string" && chart.alt.includes("`")) return [];
|
|
305
|
+
const info = `chart ${chart.type}${typeof chart.alt === "string" ? ` alt=${JSON.stringify(chart.alt)}` : ""}`;
|
|
306
|
+
const json = fenced(info, chartJson(data));
|
|
307
|
+
if (Array.isArray(data.columns) && Array.isArray(data.rows)) {
|
|
308
|
+
const body = [data.columns.map((column) => csvField(column, true)).join(","), ...data.rows.map((row) => row.map((value, index) => csvField(value, index === 0)).join(","))];
|
|
309
|
+
return [fenced(info, body), json];
|
|
310
|
+
}
|
|
311
|
+
return [json];
|
|
312
|
+
}
|
|
313
|
+
var TIMELINE_STATUS_MARKS = { done: "[x] ", current: "[>] ", planned: "[ ] " };
|
|
314
|
+
function timelineLines(timeline) {
|
|
315
|
+
const events = Array.isArray(timeline) ? timeline : timeline.events;
|
|
316
|
+
const meta = Array.isArray(timeline) ? {} : timeline;
|
|
317
|
+
const info = ["timeline", ...["name", "description"].filter((key) => meta[key] !== void 0).map((key) => `${key}=${JSON.stringify(meta[key])}`)].join(" ");
|
|
318
|
+
const body = [];
|
|
319
|
+
for (const event of events) {
|
|
320
|
+
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;
|
|
321
|
+
const line = event.when === void 0 ? event.what : `${event.when} \u2014 ${event.what}`;
|
|
322
|
+
if (event.status === void 0 && /^\[[x> ]\]\s+\S/.test(line)) return void 0;
|
|
323
|
+
body.push(event.status === void 0 ? line : `${TIMELINE_STATUS_MARKS[event.status]}${line}`);
|
|
324
|
+
if (event.description !== void 0) body.push(` ${event.description}`);
|
|
325
|
+
}
|
|
326
|
+
return fenced(info, body);
|
|
327
|
+
}
|
|
328
|
+
function metricLines(metric) {
|
|
329
|
+
const value = typeof metric === "object" ? metric : { value: metric };
|
|
330
|
+
const body = [];
|
|
331
|
+
for (const key of ["value", "label", "description", "unit", "delta", "trend", "sentiment"]) if (value[key] !== void 0) body.push(`${key}: ${scalarText(value[key])}`);
|
|
332
|
+
return fenced("metric", body);
|
|
333
|
+
}
|
|
334
|
+
function splitParagraphs(runs) {
|
|
335
|
+
const out = [[]];
|
|
336
|
+
for (const run of runs) {
|
|
337
|
+
runText(run).split("\n\n").forEach((part, index) => {
|
|
338
|
+
if (index > 0) out.push([]);
|
|
339
|
+
if (part !== "") out.at(-1).push(typeof run === "string" ? part : { ...run, text: part });
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
return out;
|
|
343
|
+
}
|
|
344
|
+
function quoteLines(quote) {
|
|
345
|
+
const value = typeof quote === "string" ? { text: quote } : quote;
|
|
346
|
+
const lines = [];
|
|
347
|
+
const paragraphs = Array.isArray(value.text) ? splitParagraphs(value.text) : String(value.text).split("\n\n");
|
|
348
|
+
for (const [index, paragraph] of paragraphs.entries()) {
|
|
349
|
+
const rows = serializeRuns(paragraph);
|
|
350
|
+
if (!rows) return void 0;
|
|
351
|
+
if (index > 0) lines.push(">");
|
|
352
|
+
for (const row of rows) lines.push(row === "" ? ">" : `> ${row}`);
|
|
353
|
+
}
|
|
354
|
+
for (const key of ["attribution", "source"]) {
|
|
355
|
+
if (value[key] === void 0) continue;
|
|
356
|
+
if (!oneLine(value[key])) return void 0;
|
|
357
|
+
lines.push(`> \u2014 ${escapeInline(value[key])}`);
|
|
358
|
+
}
|
|
359
|
+
return lines;
|
|
360
|
+
}
|
|
361
|
+
function nativeCandidates(key, canon, marker) {
|
|
362
|
+
if (key === "chart") return chartCandidates(canon);
|
|
363
|
+
const lines = nativeLines(key, canon, marker);
|
|
364
|
+
return lines?.length ? [lines] : [];
|
|
365
|
+
}
|
|
366
|
+
function nativeLines(key, canon, marker) {
|
|
367
|
+
switch (key) {
|
|
368
|
+
case "text": {
|
|
369
|
+
if (canon === "") return void 0;
|
|
370
|
+
return serializeRuns(canon);
|
|
371
|
+
}
|
|
372
|
+
case "items":
|
|
373
|
+
case "bullets":
|
|
374
|
+
return listLines(canon, marker);
|
|
375
|
+
case "quote":
|
|
376
|
+
return quoteLines(canon);
|
|
377
|
+
case "code": {
|
|
378
|
+
const code = typeof canon === "string" ? { source: canon } : canon;
|
|
379
|
+
const language = code.language;
|
|
380
|
+
if (language !== void 0 && (!/^[A-Za-z0-9_+#.-]+$/.test(language) || ["chart", "metric", "timeline", "opf-slide", "opf-block"].includes(language))) return void 0;
|
|
381
|
+
return fenceCode(code.source, language, code.filename);
|
|
382
|
+
}
|
|
383
|
+
case "metric":
|
|
384
|
+
return metricLines(canon);
|
|
385
|
+
case "image":
|
|
386
|
+
case "video":
|
|
387
|
+
return imageLines(canon);
|
|
388
|
+
case "table":
|
|
389
|
+
return tableLines(canon);
|
|
390
|
+
case "timeline":
|
|
391
|
+
return timelineLines(canon);
|
|
392
|
+
default:
|
|
393
|
+
return void 0;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function blockOptionsLine(spec, as) {
|
|
397
|
+
const parts = [];
|
|
398
|
+
for (const [key, value] of [["id", spec.id], ["type", spec.type], ["as", as], ["region", spec.region ?? spec.pin]]) {
|
|
399
|
+
if (value === void 0) continue;
|
|
400
|
+
const text = typeof value === "string" ? optionValue(value) : void 0;
|
|
401
|
+
if (text === void 0) return void 0;
|
|
402
|
+
parts.push(`${key}=${text}`);
|
|
403
|
+
}
|
|
404
|
+
return parts.length ? `<!-- block: ${parts.join(" ")} -->` : "";
|
|
405
|
+
}
|
|
406
|
+
function nativeBlock(spec, marker) {
|
|
407
|
+
const canon = canonContent(spec.key, spec.value);
|
|
408
|
+
if (canon === void 0) return void 0;
|
|
409
|
+
const as = spec.key === "bullets" ? "bullets" : spec.key === "video" ? "video" : void 0;
|
|
410
|
+
const logical = spec.key === "bullets" ? "items" : spec.key === "video" ? "image" : spec.key;
|
|
411
|
+
const options = blockOptionsLine(spec, as);
|
|
412
|
+
if (options === void 0) return void 0;
|
|
413
|
+
for (const lines of nativeCandidates(logical, canon, marker)) {
|
|
414
|
+
const all = options ? [options, ...lines] : lines;
|
|
415
|
+
const { slide, clean } = parseSlideText(all.join("\n"));
|
|
416
|
+
if (!slide || !clean) continue;
|
|
417
|
+
const target = spec.region !== void 0 ? slide[spec.region] : spec.id !== void 0 || spec.type !== void 0 || spec.pin !== void 0 ? slide.blocks?.[0] : slide;
|
|
418
|
+
if (target && same(target[spec.key], canon)) return all;
|
|
419
|
+
}
|
|
420
|
+
return void 0;
|
|
421
|
+
}
|
|
422
|
+
function embed(value) {
|
|
423
|
+
const text = writeYaml(value);
|
|
424
|
+
return text === void 0 ? void 0 : fenced("opf-block", text.trimEnd().split("\n"));
|
|
425
|
+
}
|
|
426
|
+
function emitSlide(slide, index, mode) {
|
|
427
|
+
const path = `/slides/${index}`;
|
|
428
|
+
const embedded = [];
|
|
429
|
+
const loss = [];
|
|
430
|
+
const extras = {};
|
|
431
|
+
const sections = [];
|
|
432
|
+
const exp = {};
|
|
433
|
+
const expBlocks = [];
|
|
434
|
+
const handle = (where, reason, keep) => {
|
|
435
|
+
if (mode === "embed") {
|
|
436
|
+
embedded.push({ path: where, reason });
|
|
437
|
+
keep();
|
|
438
|
+
} else loss.push({ path: where, message: reason });
|
|
439
|
+
};
|
|
440
|
+
const options = [];
|
|
441
|
+
const optionFields = [["id", slide.id], ["type", slide.type], ["beat", slide.beat], ["layout", slide.layout], ["section", slide.section], ["tag", slide.tag]];
|
|
442
|
+
const failed = [];
|
|
443
|
+
for (const [key, value] of optionFields) {
|
|
444
|
+
if (value === void 0) continue;
|
|
445
|
+
if (key === "beat" && Array.isArray(value) && value.length >= 2 && value.every((entry) => typeof entry === "string")) {
|
|
446
|
+
const texts = value.map(optionValue);
|
|
447
|
+
if (texts.every((entry) => entry !== void 0)) options.push(...texts.map((entry) => ["beat", entry]));
|
|
448
|
+
else failed.push(key);
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
const text = typeof value === "string" ? optionValue(value) : void 0;
|
|
452
|
+
if (text === void 0) failed.push(key);
|
|
453
|
+
else options.push([key, text]);
|
|
454
|
+
}
|
|
455
|
+
if (slide.hidden === true) options.push(["hidden", "true"]);
|
|
456
|
+
else if (slide.hidden === false) options.push(["hidden", "false"]);
|
|
457
|
+
else if (slide.hidden !== void 0) failed.push("hidden");
|
|
458
|
+
const optionsText = options.map(([key, text]) => key === "hidden" && text === "true" ? "hidden" : `${key}=${text}`).join(" ");
|
|
459
|
+
const optionsLine = options.length ? `<!-- slide: ${optionsText} -->` : "";
|
|
460
|
+
if (optionsLine) {
|
|
461
|
+
const { slide: back, clean } = parseSlideText(optionsLine);
|
|
462
|
+
const expected = {};
|
|
463
|
+
for (const [key] of options) expected[key] = slide[key];
|
|
464
|
+
if (!back || !clean || !same(back, expected)) {
|
|
465
|
+
for (const [key] of options) failed.push(key);
|
|
466
|
+
options.length = 0;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
for (const key of new Set(failed)) handle(`${path}/${key}`, `slide option ${key} has no <!-- slide: --> form`, () => extras[key] = slide[key]);
|
|
470
|
+
const kept = options.length ? `<!-- slide: ${options.map(([key, text]) => key === "hidden" && text === "true" ? "hidden" : `${key}=${text}`).join(" ")} -->` : "";
|
|
471
|
+
if (kept) {
|
|
472
|
+
sections.push({ lines: [kept] });
|
|
473
|
+
for (const [key] of options) exp[key] = slide[key];
|
|
474
|
+
}
|
|
475
|
+
for (const [key, mark] of [["title", "#"], ["subtitle", "##"]]) {
|
|
476
|
+
const value = slide[key];
|
|
477
|
+
if (value === void 0) continue;
|
|
478
|
+
const canon = canonText(value);
|
|
479
|
+
const row = canon === void 0 ? void 0 : typeof canon === "string" ? oneLine(canon) ? escapeInline(canon) : void 0 : serializeRuns(canon)?.length === 1 ? serializeRuns(canon)[0] : void 0;
|
|
480
|
+
if (canon !== void 0 && row) {
|
|
481
|
+
const line = `${mark} ${row.replace(/(^|\s)(#+)$/, "$1\\$2")}`;
|
|
482
|
+
const { slide: back, clean } = parseSlideText(line);
|
|
483
|
+
if (back && clean && same(back[key], canon)) {
|
|
484
|
+
sections.push({ lines: [line] });
|
|
485
|
+
exp[key] = canon;
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
handle(`${path}/${key}`, `the ${key} has no single-line Markdown form`, () => extras[key] = value);
|
|
490
|
+
}
|
|
491
|
+
const specs = [];
|
|
492
|
+
const handledRegions = /* @__PURE__ */ new Set();
|
|
493
|
+
if (Array.isArray(slide.blocks)) {
|
|
494
|
+
slide.blocks.forEach((block, i) => {
|
|
495
|
+
const where = `${path}/blocks/${i}`;
|
|
496
|
+
if (!isRecord(block)) return;
|
|
497
|
+
const pin = typeof block.region === "string" ? block.region : void 0;
|
|
498
|
+
const keys = Object.keys(block).filter((key) => key !== "id" && key !== "type" && !(key === "region" && pin !== void 0));
|
|
499
|
+
const only3 = keys.length === 1 && isContent(keys[0]) ? keys[0] : void 0;
|
|
500
|
+
specs.push({ spec: { key: only3 ?? "", value: only3 ? block[only3] : void 0, id: block.id, type: block.type, ...pin !== void 0 ? { pin } : {} }, where, payload: block });
|
|
501
|
+
});
|
|
502
|
+
for (const key of CONTENT) if (slide[key] !== void 0) extras[key] = slide[key];
|
|
503
|
+
} else {
|
|
504
|
+
for (const key of CONTENT) if (slide[key] !== void 0) specs.push({ spec: { key, value: slide[key] }, where: `${path}/${key}` });
|
|
505
|
+
}
|
|
506
|
+
for (const [key, value] of Object.entries(slide)) {
|
|
507
|
+
if (!REGION.test(key) || !isRecord(value)) continue;
|
|
508
|
+
const rest = Object.keys(value).filter((entry) => entry !== "id" && entry !== "type");
|
|
509
|
+
const only3 = rest.length === 1 && isContent(rest[0]) ? rest[0] : void 0;
|
|
510
|
+
if (only3 === void 0) continue;
|
|
511
|
+
handledRegions.add(key);
|
|
512
|
+
specs.push({ spec: { key: only3, value: value[only3], id: value.id, type: value.type, region: key }, where: `${path}/${key}`, payload: value });
|
|
513
|
+
}
|
|
514
|
+
let marker = "-";
|
|
515
|
+
for (const { spec, where, payload } of specs) {
|
|
516
|
+
const lines2 = spec.key ? nativeBlock(spec, marker) : void 0;
|
|
517
|
+
if (lines2) {
|
|
518
|
+
sections.push({ lines: lines2 });
|
|
519
|
+
const written = { ...spec.id !== void 0 ? { id: spec.id } : {}, ...spec.type !== void 0 ? { type: spec.type } : {}, ...spec.pin !== void 0 ? { region: spec.pin } : {}, [spec.key]: canonContent(spec.key, spec.value) };
|
|
520
|
+
if (spec.region !== void 0) exp[spec.region] = written;
|
|
521
|
+
else expBlocks.push(written);
|
|
522
|
+
const list = spec.key === "items" || spec.key === "bullets";
|
|
523
|
+
marker = list ? marker === "-" ? "*" : "-" : "-";
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
marker = "-";
|
|
527
|
+
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", () => {
|
|
528
|
+
if (spec.region !== void 0) {
|
|
529
|
+
extras[spec.region] = slide[spec.region];
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
const payloadValue = payload ?? { [spec.key]: spec.value };
|
|
533
|
+
const lines3 = embed(payloadValue);
|
|
534
|
+
if (!lines3) throw new OPFMarkdownError("not-representable", `The block at ${where} holds a value that YAML cannot carry exactly.`, { path: where });
|
|
535
|
+
sections.push({ lines: lines3 });
|
|
536
|
+
if (spec.region !== void 0) exp[spec.region] = structuredClone(payloadValue);
|
|
537
|
+
else expBlocks.push(structuredClone(payloadValue));
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
let notesLines;
|
|
541
|
+
if (slide.notes !== void 0) {
|
|
542
|
+
const notes = slide.notes;
|
|
543
|
+
if (typeof notes === "string" && notes !== "" && !/\r/.test(notes)) {
|
|
544
|
+
const rows = notes.split("\n");
|
|
545
|
+
const candidate = [`Note: ${rows[0]}`, ...rows.slice(1)];
|
|
546
|
+
const { slide: back, clean } = parseSlideText(candidate.join("\n"));
|
|
547
|
+
if (back && clean && back.notes === notes) {
|
|
548
|
+
notesLines = candidate;
|
|
549
|
+
exp.notes = notes;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (!notesLines) handle(`${path}/notes`, "the speaker notes have no Note: form (blank edges, a --- line or a carriage return)", () => extras.notes = notes);
|
|
553
|
+
}
|
|
554
|
+
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);
|
|
555
|
+
if (Object.keys(extras).length) {
|
|
556
|
+
const text = writeYaml(extras);
|
|
557
|
+
if (text === void 0) throw new OPFMarkdownError("not-representable", `The slide at ${path} holds a value that YAML cannot carry exactly.`, { path });
|
|
558
|
+
sections.push({ lines: fenced("opf-slide", text.trimEnd().split("\n")) });
|
|
559
|
+
Object.assign(exp, structuredClone(extras));
|
|
560
|
+
}
|
|
561
|
+
const [only2] = expBlocks;
|
|
562
|
+
const onlyKey = only2 && Object.keys(only2).length === 1 ? Object.keys(only2)[0] : void 0;
|
|
563
|
+
if (expBlocks.length === 1 && onlyKey !== void 0 && isContent(onlyKey)) exp[onlyKey] = only2[onlyKey];
|
|
564
|
+
else if (expBlocks.length) exp.blocks = expBlocks;
|
|
565
|
+
if (notesLines) sections.push({ lines: notesLines });
|
|
566
|
+
if (!sections.length) sections.push({ lines: ["<!-- slide -->"] });
|
|
567
|
+
const lines = sections.flatMap((section, i) => i === 0 || i === 1 && kept ? section.lines : ["", ...section.lines]);
|
|
568
|
+
if (mode === "embed") {
|
|
569
|
+
const { slide: back, clean } = parseSlideText(lines.join("\n"));
|
|
570
|
+
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 });
|
|
571
|
+
}
|
|
572
|
+
return { lines, embedded, loss };
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// src/markdown.ts
|
|
576
|
+
function fromMarkdown(markdown, options = {}) {
|
|
577
|
+
if (typeof markdown !== "string") throw new TypeError("fromMarkdown expects a string.");
|
|
578
|
+
checkCatalogsOption(options.catalogs, "fromMarkdown");
|
|
579
|
+
const { ctx, document } = readMarkdown(markdown, options);
|
|
580
|
+
const source = markdown;
|
|
581
|
+
const slides = document.slides;
|
|
582
|
+
let report = { schemaValid: null, checks: NOT_CHECKED };
|
|
583
|
+
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");
|
|
584
|
+
else if (options.validate !== false) {
|
|
585
|
+
const checked = validate(document, options.validate === true || options.validate === void 0 ? { only: ["format", "references"], ...options.catalogs ? { catalogs: options.catalogs } : {} } : options.validate);
|
|
586
|
+
report = {
|
|
587
|
+
schemaValid: checked.schemaValid,
|
|
588
|
+
checks: { ...checked.checks, syntax: "checked" },
|
|
589
|
+
...checked.template === void 0 ? {} : { template: checked.template },
|
|
590
|
+
...checked.unfilledVariables === void 0 ? {} : { unfilledVariables: checked.unfilledVariables }
|
|
591
|
+
};
|
|
592
|
+
for (const found of checked.findings) {
|
|
593
|
+
const range = ctx.rangeOf(found.path) ?? ctx.rangeOf("") ?? { start: 0, end: 0 };
|
|
594
|
+
ctx.findings.push({ ...found, location: ctx.location(range) });
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
const findings = [...ctx.findings].sort((a, b) => a.location.offset - b.location.offset);
|
|
598
|
+
const counts = { error: 0, warning: 0, info: 0 };
|
|
599
|
+
for (const entry of findings) counts[entry.severity]++;
|
|
600
|
+
return { presentation: document, valid: counts.error === 0, findings, counts, ...report };
|
|
601
|
+
}
|
|
602
|
+
function toMarkdown(document, options = {}) {
|
|
603
|
+
const checked = validate(document, { only: ["format"] });
|
|
604
|
+
if (!checked.valid) {
|
|
605
|
+
const errors = checked.findings.filter((entry) => entry.severity === "error");
|
|
606
|
+
const first = errors[0];
|
|
607
|
+
throw new OPFMarkdownError("invalid-document", `The document is not valid OPF: ${first?.message ?? "unknown error"}${first?.path ? ` (${first.path})` : ""}.`, { issues: errors });
|
|
608
|
+
}
|
|
609
|
+
const mode = options.unsupported === "drop" ? "drop" : "embed";
|
|
610
|
+
const { slides, ...rest } = document;
|
|
611
|
+
const parts = [];
|
|
612
|
+
if (Object.keys(rest).length) {
|
|
613
|
+
const yaml = writeYaml(rest);
|
|
614
|
+
if (yaml === void 0) throw new OPFMarkdownError("not-representable", "The deck properties hold a value that YAML cannot carry exactly.");
|
|
615
|
+
if (!same(readBack(yaml), rest)) throw new OPFMarkdownError("not-representable", "The deck properties do not read back from their front matter.");
|
|
616
|
+
parts.push(`---
|
|
617
|
+
${yaml}---
|
|
618
|
+
`);
|
|
619
|
+
}
|
|
620
|
+
const embedded = [];
|
|
621
|
+
const loss = [];
|
|
622
|
+
const bodies = [];
|
|
623
|
+
slides.forEach((slide, index) => {
|
|
624
|
+
const out = emitSlide(slide, index, mode);
|
|
625
|
+
embedded.push(...out.embedded);
|
|
626
|
+
loss.push(...out.loss.map((part) => `${part.path}: ${part.message}`));
|
|
627
|
+
bodies.push(out.lines.join("\n"));
|
|
628
|
+
});
|
|
629
|
+
const markdown = `${parts.length ? `${parts[0]}
|
|
630
|
+
` : ""}${bodies.join("\n\n---\n\n")}
|
|
631
|
+
`;
|
|
632
|
+
return { markdown, report: { lossless: loss.length === 0, native: loss.length === 0 && embedded.length === 0, embedded, loss } };
|
|
633
|
+
}
|
|
634
|
+
function readBack(yaml) {
|
|
635
|
+
const ctx = new Ctx(yaml);
|
|
636
|
+
return readYamlMapping(yaml, 0, ctx, "front-matter", { start: 0, end: yaml.length }).value;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
export { fromMarkdown, toMarkdown };
|