@slidev/parser 51.6.0 → 51.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,475 +0,0 @@
1
- import {
2
- parseAspectRatio
3
- } from "./chunk-47YLTX76.mjs";
4
-
5
- // src/core.ts
6
- import { ensurePrefix } from "@antfu/utils";
7
- import YAML from "yaml";
8
-
9
- // src/config.ts
10
- import { toArray, uniq } from "@antfu/utils";
11
- function getDefaultConfig() {
12
- return {
13
- theme: "default",
14
- title: "Slidev",
15
- titleTemplate: "%s - Slidev",
16
- addons: [],
17
- remoteAssets: false,
18
- monaco: true,
19
- monacoTypesSource: "local",
20
- monacoTypesAdditionalPackages: [],
21
- monacoTypesIgnorePackages: [],
22
- monacoRunAdditionalDeps: [],
23
- download: false,
24
- export: {},
25
- info: false,
26
- highlighter: "shiki",
27
- twoslash: true,
28
- lineNumbers: false,
29
- colorSchema: "auto",
30
- routerMode: "history",
31
- aspectRatio: 16 / 9,
32
- canvasWidth: 980,
33
- exportFilename: "",
34
- selectable: false,
35
- themeConfig: {},
36
- fonts: {},
37
- favicon: "https://cdn.jsdelivr.net/gh/slidevjs/slidev/assets/favicon.png",
38
- drawings: {},
39
- plantUmlServer: "https://www.plantuml.com/plantuml",
40
- codeCopy: true,
41
- author: "",
42
- record: "dev",
43
- css: "unocss",
44
- presenter: true,
45
- browserExporter: "dev",
46
- htmlAttrs: {},
47
- transition: null,
48
- editor: true,
49
- contextMenu: null,
50
- wakeLock: true,
51
- remote: false,
52
- mdc: false,
53
- seoMeta: {}
54
- };
55
- }
56
- function resolveConfig(headmatter, themeMeta = {}, filepath, verify = false) {
57
- const themeHightlighter = ["prism", "shiki", "shikiji"].includes(themeMeta.highlighter || "") ? themeMeta.highlighter : void 0;
58
- const themeColorSchema = ["light", "dark"].includes(themeMeta.colorSchema || "") ? themeMeta.colorSchema : void 0;
59
- const defaultConfig = getDefaultConfig();
60
- const config = {
61
- ...defaultConfig,
62
- highlighter: themeHightlighter || defaultConfig.highlighter,
63
- colorSchema: themeColorSchema || defaultConfig.colorSchema,
64
- ...themeMeta.defaults,
65
- ...headmatter.config,
66
- ...headmatter,
67
- fonts: resolveFonts({
68
- ...themeMeta.defaults?.fonts,
69
- ...headmatter.config?.fonts,
70
- ...headmatter?.fonts
71
- }),
72
- drawings: resolveDrawings(headmatter.drawings, filepath),
73
- htmlAttrs: {
74
- ...themeMeta.defaults?.htmlAttrs,
75
- ...headmatter.config?.htmlAttrs,
76
- ...headmatter?.htmlAttrs
77
- }
78
- };
79
- if (config.highlighter === "shikiji") {
80
- console.warn(`[slidev] "shikiji" is merged back to "shiki", you can safely change it "highlighter: shiki"`);
81
- config.highlighter = "shiki";
82
- }
83
- if (config.highlighter === "prism")
84
- throw new Error(`[slidev] "prism" support has been dropped. Please use "highlighter: shiki" instead`);
85
- if (config.colorSchema !== "dark" && config.colorSchema !== "light")
86
- config.colorSchema = "auto";
87
- if (themeColorSchema && config.colorSchema === "auto")
88
- config.colorSchema = themeColorSchema;
89
- config.aspectRatio = parseAspectRatio(config.aspectRatio);
90
- if (verify)
91
- verifyConfig(config, themeMeta);
92
- return config;
93
- }
94
- function verifyConfig(config, themeMeta = {}, warn = (v) => console.warn(`[slidev] ${v}`)) {
95
- const themeHightlighter = themeMeta.highlighter === "shiki" ? themeMeta.highlighter : void 0;
96
- const themeColorSchema = ["light", "dark"].includes(themeMeta.colorSchema || "") ? themeMeta.colorSchema : void 0;
97
- if (themeColorSchema && config.colorSchema !== themeColorSchema)
98
- warn(`Color schema "${config.colorSchema}" does not supported by the theme`);
99
- if (themeHightlighter && config.highlighter !== themeHightlighter)
100
- warn(`Syntax highlighter "${config.highlighter}" does not supported by the theme`);
101
- if (config.css !== "unocss") {
102
- warn(`Unsupported Atomic CSS engine "${config.css}", fallback to UnoCSS`);
103
- config.css = "unocss";
104
- }
105
- }
106
- function resolveFonts(fonts = {}) {
107
- const {
108
- fallbacks = true,
109
- italic = false,
110
- provider = "google"
111
- } = fonts;
112
- let sans = toArray(fonts.sans).flatMap((i) => i.split(/,\s*/g)).map((i) => i.trim());
113
- let serif = toArray(fonts.serif).flatMap((i) => i.split(/,\s*/g)).map((i) => i.trim());
114
- let mono = toArray(fonts.mono).flatMap((i) => i.split(/,\s*/g)).map((i) => i.trim());
115
- const weights = toArray(fonts.weights || "200,400,600").flatMap((i) => i.toString().split(/,\s*/g)).map((i) => i.trim());
116
- const custom = toArray(fonts.custom).flatMap((i) => i.split(/,\s*/g)).map((i) => i.trim());
117
- const local = toArray(fonts.local).flatMap((i) => i.split(/,\s*/g)).map((i) => i.trim());
118
- const webfonts = fonts.webfonts ? fonts.webfonts : fallbacks ? uniq([...sans, ...serif, ...mono, ...custom]) : [];
119
- webfonts.filter((i) => local.includes(i));
120
- function toQuoted(font) {
121
- if (/^(['"]).*\1$/.test(font))
122
- return font;
123
- return `"${font}"`;
124
- }
125
- if (fallbacks) {
126
- sans = uniq([
127
- ...sans.map(toQuoted),
128
- "ui-sans-serif",
129
- "system-ui",
130
- "-apple-system",
131
- "BlinkMacSystemFont",
132
- '"Segoe UI"',
133
- "Roboto",
134
- '"Helvetica Neue"',
135
- "Arial",
136
- '"Noto Sans"',
137
- "sans-serif",
138
- '"Apple Color Emoji"',
139
- '"Segoe UI Emoji"',
140
- '"Segoe UI Symbol"',
141
- '"Noto Color Emoji"'
142
- ]);
143
- serif = uniq([
144
- ...serif.map(toQuoted),
145
- "ui-serif",
146
- "Georgia",
147
- "Cambria",
148
- '"Times New Roman"',
149
- "Times",
150
- "serif"
151
- ]);
152
- mono = uniq([
153
- ...mono.map(toQuoted),
154
- "ui-monospace",
155
- "SFMono-Regular",
156
- "Menlo",
157
- "Monaco",
158
- "Consolas",
159
- '"Liberation Mono"',
160
- '"Courier New"',
161
- "monospace"
162
- ]);
163
- }
164
- return {
165
- sans,
166
- serif,
167
- mono,
168
- webfonts,
169
- provider,
170
- local,
171
- italic,
172
- weights
173
- };
174
- }
175
- function resolveDrawings(options = {}, filepath) {
176
- const {
177
- enabled = true,
178
- persist = false,
179
- presenterOnly = false,
180
- syncAll = true
181
- } = options;
182
- const persistPath = typeof persist === "string" ? persist : persist ? `.slidev/drawings${filepath ? `/${filepath.match(/([^\\/]+?)(\.\w+)?$/)?.[1]}` : ""}` : false;
183
- return {
184
- enabled,
185
- persist: persistPath,
186
- presenterOnly,
187
- syncAll
188
- };
189
- }
190
-
191
- // src/core.ts
192
- function stringify(data) {
193
- return `${data.slides.map(stringifySlide).join("\n").trim()}
194
- `;
195
- }
196
- function stringifySlide(data, idx = 0) {
197
- return data.raw.startsWith("---") || idx === 0 ? data.raw : `---
198
- ${ensurePrefix("\n", data.raw)}`;
199
- }
200
- function prettifySlide(data) {
201
- const trimed = data.content.trim();
202
- data.content = trimed ? `
203
- ${data.content.trim()}
204
- ` : "";
205
- data.raw = data.frontmatterDoc?.contents ? data.frontmatterStyle === "yaml" ? `\`\`\`yaml
206
- ${data.frontmatterDoc.toString().trim()}
207
- \`\`\`
208
- ${data.content}` : `---
209
- ${data.frontmatterDoc.toString().trim()}
210
- ---
211
- ${data.content}` : data.content;
212
- if (data.note)
213
- data.raw += `
214
- <!--
215
- ${data.note.trim()}
216
- -->
217
- `;
218
- return data;
219
- }
220
- function prettify(data) {
221
- data.slides.forEach(prettifySlide);
222
- return data;
223
- }
224
- function matter(code, options) {
225
- let type;
226
- let raw;
227
- let content = code.replace(/^---.*\r?\n([\s\S]*?)---/, (_, f) => {
228
- type = "frontmatter";
229
- raw = f;
230
- return "";
231
- });
232
- if (type !== "frontmatter") {
233
- content = content.replace(/^\s*```ya?ml([\s\S]*?)```/, (_, f) => {
234
- type = "yaml";
235
- raw = f;
236
- return "";
237
- });
238
- }
239
- const doc = raw && !options.noParseYAML ? YAML.parseDocument(raw) : void 0;
240
- return {
241
- type,
242
- raw,
243
- doc,
244
- data: doc?.toJSON(),
245
- content
246
- };
247
- }
248
- function detectFeatures(code) {
249
- return {
250
- katex: !!code.match(/\$.*?\$/) || !!code.match(/\$\$/),
251
- monaco: code.match(/\{monaco.*\}/) ? scanMonacoReferencedMods(code) : false,
252
- tweet: !!code.match(/<Tweet\b/),
253
- mermaid: !!code.match(/^```mermaid/m)
254
- };
255
- }
256
- function parseSlide(raw, options = {}) {
257
- const matterResult = matter(raw, options);
258
- let note;
259
- const frontmatter = matterResult.data || {};
260
- let content = matterResult.content.trim();
261
- const revision = hash(raw.trim());
262
- const comments = Array.from(content.matchAll(/<!--([\s\S]*?)-->/g));
263
- if (comments.length) {
264
- const last = comments[comments.length - 1];
265
- if (last.index !== void 0 && last.index + last[0].length >= content.length) {
266
- note = last[1].trim();
267
- content = content.slice(0, last.index).trim();
268
- }
269
- }
270
- let title;
271
- let level;
272
- if (frontmatter.title || frontmatter.name) {
273
- title = frontmatter.title || frontmatter.name;
274
- } else {
275
- const match = content.match(/^(#+) (.*)$/m);
276
- title = match?.[2]?.trim();
277
- level = match?.[1]?.length;
278
- }
279
- if (frontmatter.level)
280
- level = frontmatter.level || 1;
281
- return {
282
- raw,
283
- title,
284
- level,
285
- revision,
286
- content,
287
- contentRaw: content,
288
- frontmatter,
289
- frontmatterStyle: matterResult.type,
290
- frontmatterDoc: matterResult.doc,
291
- frontmatterRaw: matterResult.raw,
292
- note
293
- };
294
- }
295
- async function parse(markdown, filepath, extensions, options = {}) {
296
- const lines = markdown.split(options.preserveCR ? "\n" : /\r?\n/g);
297
- const slides = [];
298
- let start = 0;
299
- let contentStart = 0;
300
- async function slice(end) {
301
- if (start === end)
302
- return;
303
- const raw = lines.slice(start, end).join("\n");
304
- const slide = {
305
- ...parseSlide(raw, options),
306
- filepath,
307
- index: slides.length,
308
- start,
309
- contentStart,
310
- end
311
- };
312
- if (extensions) {
313
- for (const e of extensions) {
314
- if (e.transformSlide) {
315
- const newContent = await e.transformSlide(slide.content, slide.frontmatter);
316
- if (newContent !== void 0)
317
- slide.content = newContent;
318
- if (typeof slide.frontmatter.title === "string") {
319
- slide.title = slide.frontmatter.title;
320
- }
321
- if (typeof slide.frontmatter.level === "number") {
322
- slide.level = slide.frontmatter.level;
323
- }
324
- }
325
- if (e.transformNote) {
326
- const newNote = await e.transformNote(slide.note, slide.frontmatter);
327
- if (newNote !== void 0)
328
- slide.note = newNote;
329
- }
330
- }
331
- }
332
- slides.push(slide);
333
- start = end + 1;
334
- contentStart = end + 1;
335
- }
336
- if (extensions) {
337
- for (const e of extensions) {
338
- if (e.transformRawLines)
339
- await e.transformRawLines(lines);
340
- }
341
- }
342
- for (let i = 0; i < lines.length; i++) {
343
- const line = lines[i].trimEnd();
344
- if (line.startsWith("---")) {
345
- await slice(i);
346
- const next = lines[i + 1];
347
- if (line[3] !== "-" && next?.trim()) {
348
- start = i;
349
- for (i += 1; i < lines.length; i++) {
350
- if (lines[i].trimEnd() === "---")
351
- break;
352
- }
353
- contentStart = i + 1;
354
- }
355
- } else if (line.trimStart().startsWith("```")) {
356
- const codeBlockLevel = line.match(/^\s*`+/)[0];
357
- let j = i + 1;
358
- for (; j < lines.length; j++) {
359
- if (lines[j].startsWith(codeBlockLevel))
360
- break;
361
- }
362
- if (j !== lines.length)
363
- i = j;
364
- }
365
- }
366
- if (start <= lines.length - 1)
367
- await slice(lines.length);
368
- return {
369
- filepath,
370
- raw: markdown,
371
- slides
372
- };
373
- }
374
- function parseSync(markdown, filepath, options = {}) {
375
- const lines = markdown.split(options.preserveCR ? "\n" : /\r?\n/g);
376
- const slides = [];
377
- let start = 0;
378
- let contentStart = 0;
379
- function slice(end) {
380
- if (start === end)
381
- return;
382
- const raw = lines.slice(start, end).join("\n");
383
- const slide = {
384
- ...parseSlide(raw, options),
385
- filepath,
386
- index: slides.length,
387
- start,
388
- contentStart,
389
- end
390
- };
391
- slides.push(slide);
392
- start = end + 1;
393
- contentStart = end + 1;
394
- }
395
- for (let i = 0; i < lines.length; i++) {
396
- const line = lines[i].trimEnd();
397
- if (line.startsWith("---")) {
398
- slice(i);
399
- const next = lines[i + 1];
400
- if (line[3] !== "-" && next?.trim()) {
401
- start = i;
402
- for (i += 1; i < lines.length; i++) {
403
- if (lines[i].trimEnd() === "---")
404
- break;
405
- }
406
- contentStart = i + 1;
407
- }
408
- } else if (line.trimStart().startsWith("```")) {
409
- const codeBlockLevel = line.match(/^\s*`+/)[0];
410
- let j = i + 1;
411
- for (; j < lines.length; j++) {
412
- if (lines[j].startsWith(codeBlockLevel))
413
- break;
414
- }
415
- if (j !== lines.length)
416
- i = j;
417
- }
418
- }
419
- if (start <= lines.length - 1)
420
- slice(lines.length);
421
- return {
422
- filepath,
423
- raw: markdown,
424
- slides
425
- };
426
- }
427
- function scanMonacoReferencedMods(md) {
428
- const types = /* @__PURE__ */ new Set();
429
- const deps = /* @__PURE__ */ new Set();
430
- md.replace(
431
- /^```(\w+)\s*\{monaco([^}]*)\}\s*(\S[\s\S]*?)^```/gm,
432
- (full, lang = "ts", kind, code) => {
433
- lang = lang.trim();
434
- const isDep = kind === "-run";
435
- if (["js", "javascript", "ts", "typescript"].includes(lang)) {
436
- for (const [, , specifier] of code.matchAll(/\s+from\s+(["'])([/.\w@-]+)\1/g)) {
437
- if (specifier) {
438
- if (!"./".includes(specifier))
439
- types.add(specifier);
440
- if (isDep)
441
- deps.add(specifier);
442
- }
443
- }
444
- }
445
- return "";
446
- }
447
- );
448
- return {
449
- types: Array.from(types),
450
- deps: Array.from(deps)
451
- };
452
- }
453
- function hash(str) {
454
- let hash2 = 0;
455
- for (let i = 0; i < str.length; i++) {
456
- hash2 = (hash2 << 5) - hash2 + str.charCodeAt(i);
457
- hash2 |= 0;
458
- }
459
- return hash2.toString(36).slice(0, 12);
460
- }
461
-
462
- export {
463
- getDefaultConfig,
464
- resolveConfig,
465
- verifyConfig,
466
- resolveFonts,
467
- stringify,
468
- stringifySlide,
469
- prettifySlide,
470
- prettify,
471
- detectFeatures,
472
- parseSlide,
473
- parse,
474
- parseSync
475
- };