@slidev/parser 51.6.1 → 51.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/fs.mjs CHANGED
@@ -1,146 +1,107 @@
1
- import {
2
- detectFeatures,
3
- getDefaultConfig,
4
- parse,
5
- parseSlide,
6
- parseSync,
7
- prettify,
8
- prettifySlide,
9
- resolveConfig,
10
- resolveFonts,
11
- stringify,
12
- stringifySlide,
13
- verifyConfig
14
- } from "./chunk-SIS7SDEP.mjs";
15
- import {
16
- parseAspectRatio,
17
- parseRangeString
18
- } from "./chunk-47YLTX76.mjs";
19
-
20
- // src/fs.ts
21
- import fs from "node:fs";
22
- import { dirname, resolve } from "node:path";
1
+ import { parseAspectRatio, parseRangeString } from "./utils-CLLoIRbA.mjs";
2
+ import { detectFeatures, getDefaultConfig, parse, parseSlide, parseSync, prettify, prettifySlide, resolveConfig, resolveFonts, stringify, stringifySlide, verifyConfig } from "./core-CHLjwsGZ.mjs";
23
3
  import { slash } from "@antfu/utils";
24
4
  import YAML from "yaml";
25
- var preparserExtensionLoader = null;
5
+ import fs from "node:fs";
6
+ import { dirname, resolve } from "node:path";
7
+
8
+ //#region src/fs.ts
9
+ let preparserExtensionLoader = null;
26
10
  function injectPreparserExtensionLoader(fn) {
27
- preparserExtensionLoader = fn;
11
+ preparserExtensionLoader = fn;
28
12
  }
29
13
  async function load(userRoot, filepath, loadedSource = {}, mode) {
30
- const markdown = loadedSource[filepath] ?? fs.readFileSync(filepath, "utf-8");
31
- let extensions;
32
- if (preparserExtensionLoader) {
33
- const lines = markdown.split(/\r?\n/g);
34
- let hm = "";
35
- if (lines[0].match(/^---([^-].*)?$/) && !lines[1]?.match(/^\s*$/)) {
36
- let hEnd = 1;
37
- while (hEnd < lines.length && !lines[hEnd].trimEnd().match(/^---$/))
38
- hEnd++;
39
- hm = lines.slice(1, hEnd).join("\n");
40
- }
41
- const o = YAML.parse(hm) ?? {};
42
- extensions = await preparserExtensionLoader(o, filepath, mode);
43
- }
44
- const markdownFiles = {};
45
- const watchFiles = {};
46
- const slides = [];
47
- async function loadMarkdown(path, range, frontmatterOverride, importers) {
48
- let md = markdownFiles[path];
49
- if (!md) {
50
- const raw = loadedSource[path] ?? fs.readFileSync(path, "utf-8");
51
- md = await parse(raw, path, extensions);
52
- markdownFiles[path] = md;
53
- watchFiles[path] = /* @__PURE__ */ new Set();
54
- }
55
- const directImporter = importers?.at(-1);
56
- for (const index of parseRangeString(md.slides.length, range)) {
57
- const subSlide = md.slides[index - 1];
58
- try {
59
- await loadSlide(md, subSlide, frontmatterOverride, importers);
60
- } catch (e) {
61
- md.errors ??= [];
62
- md.errors.push({
63
- row: subSlide.start,
64
- message: `Error when loading slide: ${e}`
65
- });
66
- continue;
67
- }
68
- if (directImporter)
69
- (directImporter.imports ??= []).push(subSlide);
70
- }
71
- return md;
72
- }
73
- async function loadSlide(md, slide, frontmatterOverride, importChain) {
74
- if (slide.frontmatter.disabled || slide.frontmatter.hide)
75
- return;
76
- if (slide.frontmatter.src) {
77
- const [rawPath, rangeRaw] = slide.frontmatter.src.split("#");
78
- const path = slash(
79
- rawPath.startsWith("/") ? resolve(userRoot, rawPath.substring(1)) : resolve(dirname(slide.filepath), rawPath)
80
- );
81
- frontmatterOverride = {
82
- ...slide.frontmatter,
83
- ...frontmatterOverride
84
- };
85
- delete frontmatterOverride.src;
86
- if (!fs.existsSync(path)) {
87
- md.errors ??= [];
88
- md.errors.push({
89
- row: slide.start,
90
- message: `Imported markdown file not found: ${path}`
91
- });
92
- } else {
93
- await loadMarkdown(path, rangeRaw, frontmatterOverride, importChain ? [...importChain, slide] : [slide]);
94
- }
95
- } else {
96
- slides.push({
97
- frontmatter: { ...slide.frontmatter, ...frontmatterOverride },
98
- content: slide.content,
99
- revision: slide.revision,
100
- frontmatterRaw: slide.frontmatterRaw,
101
- note: slide.note,
102
- title: slide.title,
103
- level: slide.level,
104
- index: slides.length,
105
- importChain,
106
- source: slide
107
- });
108
- }
109
- }
110
- const entry = await loadMarkdown(slash(filepath));
111
- const headmatter = { ...entry.slides[0]?.frontmatter };
112
- if (slides[0]?.title)
113
- headmatter.title ??= slides[0].title;
114
- return {
115
- slides,
116
- entry,
117
- headmatter,
118
- features: detectFeatures(slides.map((s) => s.source.raw).join("")),
119
- markdownFiles,
120
- watchFiles
121
- };
14
+ const markdown = loadedSource[filepath] ?? fs.readFileSync(filepath, "utf-8");
15
+ let extensions;
16
+ if (preparserExtensionLoader) {
17
+ const lines = markdown.split(/\r?\n/g);
18
+ let hm = "";
19
+ if (lines[0].match(/^---([^-].*)?$/) && !lines[1]?.match(/^\s*$/)) {
20
+ let hEnd = 1;
21
+ while (hEnd < lines.length && !lines[hEnd].trimEnd().match(/^---$/)) hEnd++;
22
+ hm = lines.slice(1, hEnd).join("\n");
23
+ }
24
+ const o = YAML.parse(hm) ?? {};
25
+ extensions = await preparserExtensionLoader(o, filepath, mode);
26
+ }
27
+ const markdownFiles = {};
28
+ const watchFiles = {};
29
+ const slides = [];
30
+ async function loadMarkdown(path, range$1, frontmatterOverride, importers) {
31
+ let md = markdownFiles[path];
32
+ if (!md) {
33
+ const raw = loadedSource[path] ?? fs.readFileSync(path, "utf-8");
34
+ md = await parse(raw, path, extensions);
35
+ markdownFiles[path] = md;
36
+ watchFiles[path] = new Set();
37
+ }
38
+ const directImporter = importers?.at(-1);
39
+ for (const index of parseRangeString(md.slides.length, range$1)) {
40
+ const subSlide = md.slides[index - 1];
41
+ try {
42
+ await loadSlide(md, subSlide, frontmatterOverride, importers);
43
+ } catch (e) {
44
+ md.errors ??= [];
45
+ md.errors.push({
46
+ row: subSlide.start,
47
+ message: `Error when loading slide: ${e}`
48
+ });
49
+ continue;
50
+ }
51
+ if (directImporter) (directImporter.imports ??= []).push(subSlide);
52
+ }
53
+ return md;
54
+ }
55
+ async function loadSlide(md, slide, frontmatterOverride, importChain) {
56
+ if (slide.frontmatter.disabled || slide.frontmatter.hide) return;
57
+ if (slide.frontmatter.src) {
58
+ const [rawPath, rangeRaw] = slide.frontmatter.src.split("#");
59
+ const path = slash(rawPath.startsWith("/") ? resolve(userRoot, rawPath.substring(1)) : resolve(dirname(slide.filepath), rawPath));
60
+ frontmatterOverride = {
61
+ ...slide.frontmatter,
62
+ ...frontmatterOverride
63
+ };
64
+ delete frontmatterOverride.src;
65
+ if (!fs.existsSync(path)) {
66
+ md.errors ??= [];
67
+ md.errors.push({
68
+ row: slide.start,
69
+ message: `Imported markdown file not found: ${path}`
70
+ });
71
+ } else await loadMarkdown(path, rangeRaw, frontmatterOverride, importChain ? [...importChain, slide] : [slide]);
72
+ } else slides.push({
73
+ frontmatter: {
74
+ ...slide.frontmatter,
75
+ ...frontmatterOverride
76
+ },
77
+ content: slide.content,
78
+ revision: slide.revision,
79
+ frontmatterRaw: slide.frontmatterRaw,
80
+ note: slide.note,
81
+ title: slide.title,
82
+ level: slide.level,
83
+ index: slides.length,
84
+ importChain,
85
+ source: slide
86
+ });
87
+ }
88
+ const entry = await loadMarkdown(slash(filepath));
89
+ const headmatter = { ...entry.slides[0]?.frontmatter };
90
+ if (slides[0]?.title) headmatter.title ??= slides[0].title;
91
+ return {
92
+ slides,
93
+ entry,
94
+ headmatter,
95
+ features: detectFeatures(slides.map((s) => s.source.raw).join("")),
96
+ markdownFiles,
97
+ watchFiles
98
+ };
122
99
  }
123
100
  async function save(markdown) {
124
- const fileContent = stringify(markdown);
125
- fs.writeFileSync(markdown.filepath, fileContent, "utf-8");
126
- return fileContent;
101
+ const fileContent = stringify(markdown);
102
+ fs.writeFileSync(markdown.filepath, fileContent, "utf-8");
103
+ return fileContent;
127
104
  }
128
- export {
129
- detectFeatures,
130
- getDefaultConfig,
131
- injectPreparserExtensionLoader,
132
- load,
133
- parse,
134
- parseAspectRatio,
135
- parseRangeString,
136
- parseSlide,
137
- parseSync,
138
- prettify,
139
- prettifySlide,
140
- resolveConfig,
141
- resolveFonts,
142
- save,
143
- stringify,
144
- stringifySlide,
145
- verifyConfig
146
- };
105
+
106
+ //#endregion
107
+ export { detectFeatures, getDefaultConfig, injectPreparserExtensionLoader, load, parse, parseAspectRatio, parseRangeString, parseSlide, parseSync, prettify, prettifySlide, resolveConfig, resolveFonts, save, stringify, stringifySlide, verifyConfig };
package/dist/index.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- export { SlidevParserOptions, detectFeatures, getDefaultConfig, parse, parseSlide, parseSync, prettify, prettifySlide, resolveConfig, resolveFonts, stringify, stringifySlide, verifyConfig } from './core.mjs';
2
- export { parseAspectRatio, parseRangeString } from './utils.mjs';
3
- import '@slidev/types';
1
+ import { SlidevParserOptions, detectFeatures$1 as detectFeatures, getDefaultConfig$1 as getDefaultConfig, parse$1 as parse, parseSlide$1 as parseSlide, parseSync$1 as parseSync, prettify$1 as prettify, prettifySlide$1 as prettifySlide, resolveConfig$1 as resolveConfig, resolveFonts$1 as resolveFonts, stringify$1 as stringify, stringifySlide$1 as stringifySlide, verifyConfig$1 as verifyConfig } from "./core-S2rX0Zs8.mjs";
2
+ import { parseAspectRatio$1 as parseAspectRatio, parseRangeString$1 as parseRangeString } from "./utils-DHlj88Gz.mjs";
3
+ export { SlidevParserOptions, detectFeatures, getDefaultConfig, parse, parseAspectRatio, parseRangeString, parseSlide, parseSync, prettify, prettifySlide, resolveConfig, resolveFonts, stringify, stringifySlide, verifyConfig };
package/dist/index.mjs CHANGED
@@ -1,34 +1,4 @@
1
- import {
2
- detectFeatures,
3
- getDefaultConfig,
4
- parse,
5
- parseSlide,
6
- parseSync,
7
- prettify,
8
- prettifySlide,
9
- resolveConfig,
10
- resolveFonts,
11
- stringify,
12
- stringifySlide,
13
- verifyConfig
14
- } from "./chunk-SIS7SDEP.mjs";
15
- import {
16
- parseAspectRatio,
17
- parseRangeString
18
- } from "./chunk-47YLTX76.mjs";
19
- export {
20
- detectFeatures,
21
- getDefaultConfig,
22
- parse,
23
- parseAspectRatio,
24
- parseRangeString,
25
- parseSlide,
26
- parseSync,
27
- prettify,
28
- prettifySlide,
29
- resolveConfig,
30
- resolveFonts,
31
- stringify,
32
- stringifySlide,
33
- verifyConfig
34
- };
1
+ import { parseAspectRatio, parseRangeString } from "./utils-CLLoIRbA.mjs";
2
+ import { detectFeatures, getDefaultConfig, parse, parseSlide, parseSync, prettify, prettifySlide, resolveConfig, resolveFonts, stringify, stringifySlide, verifyConfig } from "./core-CHLjwsGZ.mjs";
3
+
4
+ export { detectFeatures, getDefaultConfig, parse, parseAspectRatio, parseRangeString, parseSlide, parseSync, prettify, prettifySlide, resolveConfig, resolveFonts, stringify, stringifySlide, verifyConfig };
@@ -0,0 +1,32 @@
1
+ import { isNumber, range, uniq } from "@antfu/utils";
2
+
3
+ //#region src/utils.ts
4
+ /**
5
+ * 1,3-5,8 => [1, 3, 4, 5, 8]
6
+ */
7
+ function parseRangeString(total, rangeStr) {
8
+ if (!rangeStr || rangeStr === "all" || rangeStr === "*") return range(1, total + 1);
9
+ if (rangeStr === "none") return [];
10
+ const indexes = [];
11
+ for (const part of rangeStr.split(/[,;]/g)) if (!part.includes("-")) indexes.push(+part);
12
+ else {
13
+ const [start, end] = part.split("-", 2);
14
+ indexes.push(...range(+start, !end ? total + 1 : +end + 1));
15
+ }
16
+ return uniq(indexes).filter((i) => i <= total).sort((a, b) => a - b);
17
+ }
18
+ /**
19
+ * Accepts `16/9` `1:1` `3x4`
20
+ */
21
+ function parseAspectRatio(str) {
22
+ if (isNumber(str)) return str;
23
+ if (!Number.isNaN(+str)) return +str;
24
+ const [wStr = "", hStr = ""] = str.split(/[:/x|]/);
25
+ const w = Number.parseFloat(wStr.trim());
26
+ const h = Number.parseFloat(hStr.trim());
27
+ if (Number.isNaN(w) || Number.isNaN(h) || h === 0) throw new Error(`Invalid aspect ratio "${str}"`);
28
+ return w / h;
29
+ }
30
+
31
+ //#endregion
32
+ export { parseAspectRatio, parseRangeString };
@@ -0,0 +1,12 @@
1
+ //#region src/utils.d.ts
2
+ /**
3
+ * 1,3-5,8 => [1, 3, 4, 5, 8]
4
+ */
5
+ declare function parseRangeString(total: number, rangeStr?: string): number[];
6
+ /**
7
+ * Accepts `16/9` `1:1` `3x4`
8
+ */
9
+ declare function parseAspectRatio(str: string | number): number;
10
+
11
+ //#endregion
12
+ export { parseAspectRatio as parseAspectRatio$1, parseRangeString as parseRangeString$1 };
package/dist/utils.d.mts CHANGED
@@ -1,10 +1,2 @@
1
- /**
2
- * 1,3-5,8 => [1, 3, 4, 5, 8]
3
- */
4
- declare function parseRangeString(total: number, rangeStr?: string): number[];
5
- /**
6
- * Accepts `16/9` `1:1` `3x4`
7
- */
8
- declare function parseAspectRatio(str: string | number): number;
9
-
10
- export { parseAspectRatio, parseRangeString };
1
+ import { parseAspectRatio$1 as parseAspectRatio, parseRangeString$1 as parseRangeString } from "./utils-DHlj88Gz.mjs";
2
+ export { parseAspectRatio, parseRangeString };
package/dist/utils.mjs CHANGED
@@ -1,8 +1,3 @@
1
- import {
2
- parseAspectRatio,
3
- parseRangeString
4
- } from "./chunk-47YLTX76.mjs";
5
- export {
6
- parseAspectRatio,
7
- parseRangeString
8
- };
1
+ import { parseAspectRatio, parseRangeString } from "./utils-CLLoIRbA.mjs";
2
+
3
+ export { parseAspectRatio, parseRangeString };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@slidev/parser",
3
- "version": "51.6.1",
3
+ "version": "51.7.0",
4
4
  "description": "Markdown parser for Slidev",
5
- "author": "antfu <anthonyfu117@hotmail.com>",
5
+ "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/antfu",
8
8
  "homepage": "https://sli.dev",
@@ -42,10 +42,10 @@
42
42
  "dependencies": {
43
43
  "@antfu/utils": "^9.2.0",
44
44
  "yaml": "^2.8.0",
45
- "@slidev/types": "51.6.1"
45
+ "@slidev/types": "51.7.0"
46
46
  },
47
47
  "scripts": {
48
- "build": "rimraf dist && tsup src/index.ts src/core.ts src/fs.ts src/utils.ts",
48
+ "build": "tsdown src/index.ts src/core.ts src/fs.ts src/utils.ts",
49
49
  "dev": "nr build --watch"
50
50
  }
51
51
  }
@@ -1,37 +0,0 @@
1
- // src/utils.ts
2
- import { isNumber, range, uniq } from "@antfu/utils";
3
- function parseRangeString(total, rangeStr) {
4
- if (!rangeStr || rangeStr === "all" || rangeStr === "*")
5
- return range(1, total + 1);
6
- if (rangeStr === "none")
7
- return [];
8
- const indexes = [];
9
- for (const part of rangeStr.split(/[,;]/g)) {
10
- if (!part.includes("-")) {
11
- indexes.push(+part);
12
- } else {
13
- const [start, end] = part.split("-", 2);
14
- indexes.push(
15
- ...range(+start, !end ? total + 1 : +end + 1)
16
- );
17
- }
18
- }
19
- return uniq(indexes).filter((i) => i <= total).sort((a, b) => a - b);
20
- }
21
- function parseAspectRatio(str) {
22
- if (isNumber(str))
23
- return str;
24
- if (!Number.isNaN(+str))
25
- return +str;
26
- const [wStr = "", hStr = ""] = str.split(/[:/x|]/);
27
- const w = Number.parseFloat(wStr.trim());
28
- const h = Number.parseFloat(hStr.trim());
29
- if (Number.isNaN(w) || Number.isNaN(h) || h === 0)
30
- throw new Error(`Invalid aspect ratio "${str}"`);
31
- return w / h;
32
- }
33
-
34
- export {
35
- parseRangeString,
36
- parseAspectRatio
37
- };