Skip to content

Commit ff1ca70

Browse files
authored
fix: slide formatter (#1379)
1 parent f41603e commit ff1ca70

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

packages/parser/src/core.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import YAML from 'js-yaml'
2-
import { isObject, objectMap } from '@antfu/utils'
2+
import { ensurePrefix, isObject, objectMap } from '@antfu/utils'
33
import type { FrontmatterStyle, SlidevFeatureFlags, SlidevMarkdown, SlidevPreparserExtension, SourceSlideInfo } from '@slidev/types'
44

55
export function stringify(data: SlidevMarkdown) {
@@ -9,20 +9,19 @@ export function stringify(data: SlidevMarkdown) {
99
export function stringifySlide(data: SourceSlideInfo, idx = 0) {
1010
return (data.raw.startsWith('---') || idx === 0)
1111
? data.raw
12-
: `---\n${data.raw.startsWith('\n') ? data.raw : `\n${data.raw}`}`
12+
: `---\n${ensurePrefix('\n', data.raw)}`
1313
}
1414

1515
export function prettifySlide(data: SourceSlideInfo) {
16-
data.content = `\n${data.content.trim()}\n`
17-
data.raw = Object.keys(data.frontmatter || {}).length
16+
const trimed = data.content.trim()
17+
data.content = trimed ? `\n${data.content.trim()}\n` : ''
18+
data.raw = data.frontmatterRaw
1819
? data.frontmatterStyle === 'yaml'
19-
? `\`\`\`yaml\n${YAML.dump(data.frontmatter).trim()}\n\`\`\`\n${data.content}`
20-
: `---\n${YAML.dump(data.frontmatter).trim()}\n---\n${data.content}`
20+
? `\`\`\`yaml\n${data.frontmatterRaw.trim()}\n\`\`\`\n${data.content}`
21+
: `---\n${data.frontmatterRaw.trim()}\n---\n${data.content}`
2122
: data.content
2223
if (data.note)
2324
data.raw += `\n<!--\n${data.note.trim()}\n-->\n`
24-
else
25-
data.raw += '\n'
2625
return data
2726
}
2827

test/__snapshots__/parser.test.ts.snap

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ fonts:
123123
---
124124
125125
# Hi
126-
127126
",
128127
"start": 0,
129128
"title": "Hi",
@@ -200,7 +199,6 @@ This is note
200199
"note": undefined,
201200
"raw": "
202201
# Morning
203-
204202
",
205203
"start": 24,
206204
"title": "Morning",
@@ -290,7 +288,6 @@ this should be treated as code block
290288
291289
Also part of the code block
292290
\`\`\`
293-
294291
",
295292
"start": 39,
296293
"title": undefined,
@@ -322,11 +319,11 @@ layout: from yaml
322319
"level": undefined,
323320
"note": undefined,
324321
"raw": "\`\`\`yaml
322+
# The first yaml block should be treated as frontmatter
325323
layout: from yaml
326324
\`\`\`
327325
328326
Content 1
329-
330327
",
331328
"start": 51,
332329
"title": undefined,
@@ -375,7 +372,6 @@ layout: should not from yaml 1
375372
\`\`\`
376373
377374
Content 2
378-
379375
",
380376
"start": 59,
381377
"title": "When there is already a frontmatter, the first yaml block should be treated as content",
@@ -422,7 +418,6 @@ layout: should not from yaml 2
422418
\`\`\`
423419
424420
Content 3
425-
426421
",
427422
"start": 71,
428423
"title": "Title",
@@ -534,7 +529,6 @@ mdc: true
534529
# MDC{style="color:red"}
535530
536531
:arrow{x1=1 y1=1 x2=2 y2=2}
537-
538532
",
539533
"start": 0,
540534
"title": "MDC{style="color:red"}",
@@ -655,7 +649,6 @@ Sample Text
655649
\`\`\`ts
656650
console.log('Hello World')
657651
\`\`\`
658-
659652
",
660653
"start": 0,
661654
"title": "H1",
@@ -696,7 +689,6 @@ console.log('Hello World')
696689
- Hi
697690
- Hey
698691
- Yo
699-
700692
",
701693
"start": 11,
702694
"title": "Hello",
@@ -722,7 +714,6 @@ Nice to meet you
722714
"note": undefined,
723715
"raw": "
724716
Nice to meet you
725-
726717
",
727718
"start": 20,
728719
"title": undefined,
@@ -1072,7 +1063,6 @@ $x+2$
10721063
# Inline Page
10731064
10741065
$x+2$
1075-
10761066
",
10771067
"start": 20,
10781068
"title": "Inline Page",

test/parser.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function configDiff(v: SlidevConfig) {
1414
return res
1515
}
1616

17+
function replaceCRLF(s: string) {
18+
return s.replace(/\r\n/g, '\n')
19+
}
20+
1721
describe('md parser', () => {
1822
const userRoot = resolve(__dirname, 'fixtures/markdown')
1923
const files = fg.sync('*.md', {
@@ -25,7 +29,7 @@ describe('md parser', () => {
2529
it(basename(file), async () => {
2630
const data = await load(userRoot, file)
2731

28-
expect(stringify(data.entry).trim()).toEqual(data.entry.raw.trim())
32+
expect(stringify(data.entry).trim()).toEqual(replaceCRLF(data.entry.raw.trim()))
2933

3034
prettify(data.entry)
3135

0 commit comments

Comments
 (0)