Skip to content

Commit a0ee374

Browse files
committed
feat(parser): infer title for each page
1 parent 2317998 commit a0ee374

File tree

6 files changed

+293
-2
lines changed

6 files changed

+293
-2
lines changed

.vscode/extensions.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"recommendations": [
33
"antfu.vite",
44
"johnsoncodehk.volar",
5-
"lokalise.i18n-ally",
65
"antfu.iconify",
76
"dbaeumer.vscode-eslint",
87
"voorjaar.windicss-intellisense",

packages/parser/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ export function parse(
7272
note = v.trim()
7373
return ''
7474
})
75+
76+
const title = content.match(/^#+ (.*)$/m)?.[1]?.trim()
77+
7578
return {
7679
raw,
80+
title,
7781
content,
7882
frontmatter: result.data || {},
7983
note,
@@ -87,6 +91,7 @@ export function parse(
8791
return
8892
const raw = lines.slice(start, end).join('\n')
8993
slides.push({
94+
index: slides.length,
9095
start,
9196
end,
9297
...parseContent(raw),
@@ -137,7 +142,7 @@ export function parse(
137142
)
138143

139144
config.theme ??= headmatter.theme ?? 'default'
140-
config.title ??= headmatter.title ?? (slides[0].content.match(/^# (.*)$/m)?.[1] || '').trim()
145+
config.title ??= headmatter.title ?? slides[0].title ?? 'Slidev'
141146
config.remoteAssets ??= headmatter.remoteAssets ?? true
142147
config.monaco ??= headmatter.monaco ?? 'dev'
143148
config.download ??= headmatter.download ?? false

packages/types/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
export interface SlideInfo {
2+
index: number
23
start: number
34
end: number
45
raw: string
56
content: string
67
note?: string
78
frontmatter: Record<string, any>
9+
title?: string
810
}
911

1012
export interface SlideInfoExtended extends SlideInfo {

test/__snapshots__/parser.test.ts.snap

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,162 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`md parser frontmatter.md: data 1`] = `
4+
Object {
5+
"config": Object {
6+
"download": false,
7+
"info": true,
8+
"monaco": "dev",
9+
"remoteAssets": true,
10+
"theme": "default",
11+
"title": "Hi",
12+
},
13+
"filepath": "/Users/antfu/i/slidev/test/fixtures/markdown/frontmatter.md",
14+
"raw": "---
15+
layout: cover
16+
---
17+
# Hi
18+
---
19+
meta:
20+
title: FooBar
21+
duration: 12
22+
layout: center
23+
---
24+
# Hello
25+
<!-- This is notes -->
26+
---
27+
28+
# Morning
29+
---
30+
layout: text
31+
---
32+
<!-- This is not notes -->
33+
Hey
34+
---
35+
36+
\`\`\`md
37+
---
38+
this should be treated as code block
39+
---
40+
----
41+
\`\`\`
42+
",
43+
"slides": Array [
44+
Object {
45+
"content": "
46+
# Hi
47+
",
48+
"end": 4,
49+
"frontmatter": Object {
50+
"layout": "cover",
51+
},
52+
"index": 0,
53+
"note": undefined,
54+
"raw": "---
55+
layout: cover
56+
---
57+
58+
# Hi
59+
60+
",
61+
"start": 0,
62+
"title": "Hi",
63+
},
64+
Object {
65+
"content": "
66+
# Hello
67+
",
68+
"end": 12,
69+
"frontmatter": Object {
70+
"layout": "center",
71+
"meta": Object {
72+
"duration": 12,
73+
"title": "FooBar",
74+
},
75+
},
76+
"index": 1,
77+
"note": "This is notes",
78+
"raw": "---
79+
meta:
80+
title: FooBar
81+
duration: 12
82+
layout: center
83+
---
84+
85+
# Hello
86+
87+
<!--
88+
This is notes
89+
-->
90+
",
91+
"start": 4,
92+
"title": "Hello",
93+
},
94+
Object {
95+
"content": "
96+
# Morning
97+
",
98+
"end": 15,
99+
"frontmatter": Object {},
100+
"index": 2,
101+
"note": undefined,
102+
"raw": "
103+
# Morning
104+
105+
",
106+
"start": 13,
107+
"title": "Morning",
108+
},
109+
Object {
110+
"content": "
111+
<!-- This is not notes -->
112+
Hey
113+
",
114+
"end": 20,
115+
"frontmatter": Object {
116+
"layout": "text",
117+
},
118+
"index": 3,
119+
"note": undefined,
120+
"raw": "---
121+
layout: text
122+
---
123+
124+
<!-- This is not notes -->
125+
Hey
126+
127+
",
128+
"start": 15,
129+
"title": undefined,
130+
},
131+
Object {
132+
"content": "
133+
\`\`\`md
134+
---
135+
this should be treated as code block
136+
---
137+
----
138+
\`\`\`
139+
",
140+
"end": 28,
141+
"frontmatter": Object {},
142+
"index": 4,
143+
"note": undefined,
144+
"raw": "
145+
\`\`\`md
146+
---
147+
this should be treated as code block
148+
---
149+
----
150+
\`\`\`
151+
152+
",
153+
"start": 21,
154+
"title": undefined,
155+
},
156+
],
157+
}
158+
`;
159+
3160
exports[`md parser frontmatter.md: formatted 1`] = `
4161
"---
5162
layout: cover
@@ -32,9 +189,128 @@ layout: text
32189
33190
<!-- This is not notes -->
34191
Hey
192+
193+
194+
---
195+
196+
\`\`\`md
197+
---
198+
this should be treated as code block
199+
---
200+
----
201+
\`\`\`
35202
"
36203
`;
37204
205+
exports[`md parser minimal.md: data 1`] = `
206+
Object {
207+
"config": Object {
208+
"download": false,
209+
"info": true,
210+
"monaco": "dev",
211+
"remoteAssets": true,
212+
"theme": "default",
213+
"title": "H1",
214+
},
215+
"filepath": "/Users/antfu/i/slidev/test/fixtures/markdown/minimal.md",
216+
"raw": "# H1
217+
## H2
218+
### H3
219+
220+
Sample Text
221+
222+
\`\`\`ts
223+
console.log('Hello World')
224+
\`\`\`
225+
226+
---
227+
228+
# Hello
229+
230+
- Hello
231+
- Hi
232+
- Hey
233+
- Yo
234+
---
235+
236+
Nice to meet you
237+
",
238+
"slides": Array [
239+
Object {
240+
"content": "
241+
# H1
242+
## H2
243+
### H3
244+
245+
Sample Text
246+
247+
\`\`\`ts
248+
console.log('Hello World')
249+
\`\`\`
250+
",
251+
"end": 10,
252+
"frontmatter": Object {},
253+
"index": 0,
254+
"note": undefined,
255+
"raw": "
256+
# H1
257+
## H2
258+
### H3
259+
260+
Sample Text
261+
262+
\`\`\`ts
263+
console.log('Hello World')
264+
\`\`\`
265+
266+
",
267+
"start": 0,
268+
"title": "H1",
269+
},
270+
Object {
271+
"content": "
272+
# Hello
273+
274+
- Hello
275+
- Hi
276+
- Hey
277+
- Yo
278+
",
279+
"end": 18,
280+
"frontmatter": Object {},
281+
"index": 1,
282+
"note": undefined,
283+
"raw": "
284+
# Hello
285+
286+
- Hello
287+
- Hi
288+
- Hey
289+
- Yo
290+
291+
",
292+
"start": 11,
293+
"title": "Hello",
294+
},
295+
Object {
296+
"content": "
297+
Nice to meet you
298+
",
299+
"end": 21,
300+
"frontmatter": Object {},
301+
"index": 2,
302+
"note": undefined,
303+
"raw": "
304+
Nice to meet you
305+
306+
",
307+
"start": 19,
308+
"title": undefined,
309+
},
310+
],
311+
}
312+
`;
313+
38314
exports[`md parser minimal.md: formatted 1`] = `
39315
"# H1
40316
## H2

test/fixtures/markdown/frontmatter.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ layout: text
1818
---
1919
<!-- This is not notes -->
2020
Hey
21+
---
22+
23+
```md
24+
---
25+
this should be treated as code block
26+
---
27+
----
28+
```

test/parser.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('md parser', () => {
1717
prettify(data)
1818

1919
expect(stringify(data)).toMatchSnapshot('formatted')
20+
expect(data).toMatchSnapshot('data')
2021
})
2122
}
2223

0 commit comments

Comments
 (0)