Skip to content

Commit 75bc2ea

Browse files
authored
feat(parser): transform slide notes (#2136)
* Add new transformNote function, update docs * Update transform notes documentation
1 parent b6cd347 commit 75bc2ea

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

docs/custom/config-parser.md

+55
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This example systematically replaces any `@@@` line with a line with `hello`. It
5151
- An extension can contain:
5252
- a `transformRawLines(lines)` function that runs just after parsing the headmatter of the md file and receives a list of all lines (from the md file). The function can mutate the list arbitrarily.
5353
- a `transformSlide(content, frontmatter)` function that is called for each slide, just after splitting the file, and receives the slide content as a string and the frontmatter of the slide as an object. The function can mutate the frontmatter and must return the content string (possibly modified, possibly `undefined` if no modifications have been done).
54+
- a `transformNote(note, frontmatter)` function that is called for each slide, just after splitting the file, and receives the slide note as a string or undefined and the frontmatter of the slide as an object. The function can mutate the frontmatter and must return the note string (possibly modified, possibly `undefined` if no modifications have been done).
5455
- a `name`
5556

5657
## Example Preparser Extensions
@@ -179,3 +180,57 @@ export default definePreparserSetup(() => {
179180
```
180181

181182
And that's it.
183+
184+
185+
186+
### Use case 3: using custom frontmatter to transform note
187+
188+
Imagine a case where you want to replace the slides default notes with custom notes.
189+
For instance, you might want to write your `slides.md` as follows:
190+
191+
<!-- eslint-skip -->
192+
193+
```md
194+
---
195+
layout: quote
196+
_note: notes/note.md
197+
---
198+
199+
# Welcome
200+
201+
> great!
202+
203+
<!--
204+
Default slide notes
205+
-->
206+
```
207+
208+
Here we used an underscore in `_note` to avoid possible conflicts with existing frontmatter properties.
209+
210+
To handle this `_note: ...` syntax in the frontmatter, create a `./setup/preparser.ts` file with the following content:
211+
212+
```ts twoslash
213+
import { definePreparserSetup } from '@slidev/types'
214+
import fs from 'fs'
215+
import { promises as fsp } from 'fs'
216+
217+
export default definePreparserSetup(() => {
218+
return [
219+
{
220+
async transformNote(note, frontmatter) {
221+
if ('_note' in frontmatter && fs.existsSync(frontmatter._note)) {
222+
try {
223+
const newNote = await fsp.readFile(frontmatter._note, 'utf8')
224+
return newNote
225+
} catch (err) {
226+
}
227+
}
228+
229+
return note
230+
},
231+
},
232+
]
233+
})
234+
```
235+
236+
And that's it.

packages/parser/src/core.ts

+6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ export async function parse(
155155
slide.level = slide.frontmatter.level
156156
}
157157
}
158+
159+
if (e.transformNote) {
160+
const newNote = await e.transformNote(slide.note, slide.frontmatter)
161+
if (newNote !== undefined)
162+
slide.note = newNote
163+
}
158164
}
159165
}
160166
slides.push(slide)

packages/types/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export interface SlidevPreparserExtension {
122122
name?: string
123123
transformRawLines?: (lines: string[]) => Promise<void> | void
124124
transformSlide?: (content: string, frontmatter: any) => Promise<string | undefined>
125+
transformNote?: (note: string | undefined, frontmatter: any) => Promise<string | undefined>
125126
}
126127

127128
export type PreparserExtensionLoader = (headmatter: Record<string, unknown>, filepath: string, mode?: string) => Promise<SlidevPreparserExtension[]>

0 commit comments

Comments
 (0)