Skip to content

Commit

Permalink
feat(parser): infer title for each page
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 30, 2021
1 parent 2317998 commit a0ee374
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 2 deletions.
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"recommendations": [
"antfu.vite",
"johnsoncodehk.volar",
"lokalise.i18n-ally",
"antfu.iconify",
"dbaeumer.vscode-eslint",
"voorjaar.windicss-intellisense",
Expand Down
7 changes: 6 additions & 1 deletion packages/parser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ export function parse(
note = v.trim()
return ''
})

const title = content.match(/^#+ (.*)$/m)?.[1]?.trim()

return {
raw,
title,
content,
frontmatter: result.data || {},
note,
Expand All @@ -87,6 +91,7 @@ export function parse(
return
const raw = lines.slice(start, end).join('\n')
slides.push({
index: slides.length,
start,
end,
...parseContent(raw),
Expand Down Expand Up @@ -137,7 +142,7 @@ export function parse(
)

config.theme ??= headmatter.theme ?? 'default'
config.title ??= headmatter.title ?? (slides[0].content.match(/^# (.*)$/m)?.[1] || '').trim()
config.title ??= headmatter.title ?? slides[0].title ?? 'Slidev'
config.remoteAssets ??= headmatter.remoteAssets ?? true
config.monaco ??= headmatter.monaco ?? 'dev'
config.download ??= headmatter.download ?? false
Expand Down
2 changes: 2 additions & 0 deletions packages/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export interface SlideInfo {
index: number
start: number
end: number
raw: string
content: string
note?: string
frontmatter: Record<string, any>
title?: string
}

export interface SlideInfoExtended extends SlideInfo {
Expand Down
276 changes: 276 additions & 0 deletions test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,162 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`md parser frontmatter.md: data 1`] = `
Object {
"config": Object {
"download": false,
"info": true,
"monaco": "dev",
"remoteAssets": true,
"theme": "default",
"title": "Hi",
},
"filepath": "/Users/antfu/i/slidev/test/fixtures/markdown/frontmatter.md",
"raw": "---
layout: cover
---
# Hi
---
meta:
title: FooBar
duration: 12
layout: center
---
# Hello
<!-- This is notes -->
---
# Morning
---
layout: text
---
<!-- This is not notes -->
Hey
---
\`\`\`md
---
this should be treated as code block
---
----
\`\`\`
",
"slides": Array [
Object {
"content": "
# Hi
",
"end": 4,
"frontmatter": Object {
"layout": "cover",
},
"index": 0,
"note": undefined,
"raw": "---
layout: cover
---
# Hi
",
"start": 0,
"title": "Hi",
},
Object {
"content": "
# Hello
",
"end": 12,
"frontmatter": Object {
"layout": "center",
"meta": Object {
"duration": 12,
"title": "FooBar",
},
},
"index": 1,
"note": "This is notes",
"raw": "---
meta:
title: FooBar
duration: 12
layout: center
---
# Hello
<!--
This is notes
-->
",
"start": 4,
"title": "Hello",
},
Object {
"content": "
# Morning
",
"end": 15,
"frontmatter": Object {},
"index": 2,
"note": undefined,
"raw": "
# Morning
",
"start": 13,
"title": "Morning",
},
Object {
"content": "
<!-- This is not notes -->
Hey
",
"end": 20,
"frontmatter": Object {
"layout": "text",
},
"index": 3,
"note": undefined,
"raw": "---
layout: text
---
<!-- This is not notes -->
Hey
",
"start": 15,
"title": undefined,
},
Object {
"content": "
\`\`\`md
---
this should be treated as code block
---
----
\`\`\`
",
"end": 28,
"frontmatter": Object {},
"index": 4,
"note": undefined,
"raw": "
\`\`\`md
---
this should be treated as code block
---
----
\`\`\`
",
"start": 21,
"title": undefined,
},
],
}
`;
exports[`md parser frontmatter.md: formatted 1`] = `
"---
layout: cover
Expand Down Expand Up @@ -32,9 +189,128 @@ layout: text
<!-- This is not notes -->
Hey
---
\`\`\`md
---
this should be treated as code block
---
----
\`\`\`
"
`;
exports[`md parser minimal.md: data 1`] = `
Object {
"config": Object {
"download": false,
"info": true,
"monaco": "dev",
"remoteAssets": true,
"theme": "default",
"title": "H1",
},
"filepath": "/Users/antfu/i/slidev/test/fixtures/markdown/minimal.md",
"raw": "# H1
## H2
### H3
Sample Text
\`\`\`ts
console.log('Hello World')
\`\`\`
---
# Hello
- Hello
- Hi
- Hey
- Yo
---
Nice to meet you
",
"slides": Array [
Object {
"content": "
# H1
## H2
### H3
Sample Text
\`\`\`ts
console.log('Hello World')
\`\`\`
",
"end": 10,
"frontmatter": Object {},
"index": 0,
"note": undefined,
"raw": "
# H1
## H2
### H3
Sample Text
\`\`\`ts
console.log('Hello World')
\`\`\`
",
"start": 0,
"title": "H1",
},
Object {
"content": "
# Hello
- Hello
- Hi
- Hey
- Yo
",
"end": 18,
"frontmatter": Object {},
"index": 1,
"note": undefined,
"raw": "
# Hello
- Hello
- Hi
- Hey
- Yo
",
"start": 11,
"title": "Hello",
},
Object {
"content": "
Nice to meet you
",
"end": 21,
"frontmatter": Object {},
"index": 2,
"note": undefined,
"raw": "
Nice to meet you
",
"start": 19,
"title": undefined,
},
],
}
`;
exports[`md parser minimal.md: formatted 1`] = `
"# H1
## H2
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/markdown/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ layout: text
---
<!-- This is not notes -->
Hey
---

```md
---
this should be treated as code block
---
----
```
1 change: 1 addition & 0 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('md parser', () => {
prettify(data)

expect(stringify(data)).toMatchSnapshot('formatted')
expect(data).toMatchSnapshot('data')
})
}

Expand Down

0 comments on commit a0ee374

Please sign in to comment.