Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: slide formatter #1379

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions packages/parser/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import YAML from 'js-yaml'
import { isObject, objectMap } from '@antfu/utils'
import { ensurePrefix, isObject, objectMap } from '@antfu/utils'
import type { FrontmatterStyle, SlidevFeatureFlags, SlidevMarkdown, SlidevPreparserExtension, SourceSlideInfo } from '@slidev/types'

export function stringify(data: SlidevMarkdown) {
Expand All @@ -9,20 +9,19 @@ export function stringify(data: SlidevMarkdown) {
export function stringifySlide(data: SourceSlideInfo, idx = 0) {
return (data.raw.startsWith('---') || idx === 0)
? data.raw
: `---\n${data.raw.startsWith('\n') ? data.raw : `\n${data.raw}`}`
: `---\n${ensurePrefix('\n', data.raw)}`
}

export function prettifySlide(data: SourceSlideInfo) {
data.content = `\n${data.content.trim()}\n`
data.raw = Object.keys(data.frontmatter || {}).length
const trimed = data.content.trim()
data.content = trimed ? `\n${data.content.trim()}\n` : ''
data.raw = data.frontmatterRaw
? data.frontmatterStyle === 'yaml'
? `\`\`\`yaml\n${YAML.dump(data.frontmatter).trim()}\n\`\`\`\n${data.content}`
: `---\n${YAML.dump(data.frontmatter).trim()}\n---\n${data.content}`
? `\`\`\`yaml\n${data.frontmatterRaw.trim()}\n\`\`\`\n${data.content}`
: `---\n${data.frontmatterRaw.trim()}\n---\n${data.content}`
: data.content
if (data.note)
data.raw += `\n<!--\n${data.note.trim()}\n-->\n`
else
data.raw += '\n'
return data
}

Expand Down
12 changes: 1 addition & 11 deletions test/__snapshots__/parser.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ fonts:
---

# Hi

",
"start": 0,
"title": "Hi",
Expand Down Expand Up @@ -200,7 +199,6 @@ This is note
"note": undefined,
"raw": "
# Morning

",
"start": 24,
"title": "Morning",
Expand Down Expand Up @@ -290,7 +288,6 @@ this should be treated as code block

Also part of the code block
\`\`\`

",
"start": 39,
"title": undefined,
Expand Down Expand Up @@ -322,11 +319,11 @@ layout: from yaml
"level": undefined,
"note": undefined,
"raw": "\`\`\`yaml
# The first yaml block should be treated as frontmatter
layout: from yaml
\`\`\`

Content 1

",
"start": 51,
"title": undefined,
Expand Down Expand Up @@ -375,7 +372,6 @@ layout: should not from yaml 1
\`\`\`

Content 2

",
"start": 59,
"title": "When there is already a frontmatter, the first yaml block should be treated as content",
Expand Down Expand Up @@ -422,7 +418,6 @@ layout: should not from yaml 2
\`\`\`

Content 3

",
"start": 71,
"title": "Title",
Expand Down Expand Up @@ -534,7 +529,6 @@ mdc: true
# MDC{style="color:red"}

:arrow{x1=1 y1=1 x2=2 y2=2}

",
"start": 0,
"title": "MDC{style="color:red"}",
Expand Down Expand Up @@ -655,7 +649,6 @@ Sample Text
\`\`\`ts
console.log('Hello World')
\`\`\`

",
"start": 0,
"title": "H1",
Expand Down Expand Up @@ -696,7 +689,6 @@ console.log('Hello World')
- Hi
- Hey
- Yo

",
"start": 11,
"title": "Hello",
Expand All @@ -722,7 +714,6 @@ Nice to meet you
"note": undefined,
"raw": "
Nice to meet you

",
"start": 20,
"title": undefined,
Expand Down Expand Up @@ -1072,7 +1063,6 @@ $x+2$
# Inline Page

$x+2$

",
"start": 20,
"title": "Inline Page",
Expand Down
6 changes: 5 additions & 1 deletion test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function configDiff(v: SlidevConfig) {
return res
}

function replaceCRLF(s: string) {
return s.replace(/\r\n/g, '\n')
}

describe('md parser', () => {
const userRoot = resolve(__dirname, 'fixtures/markdown')
const files = fg.sync('*.md', {
Expand All @@ -25,7 +29,7 @@ describe('md parser', () => {
it(basename(file), async () => {
const data = await load(userRoot, file)

expect(stringify(data.entry).trim()).toEqual(data.entry.raw.trim())
expect(stringify(data.entry).trim()).toEqual(replaceCRLF(data.entry.raw.trim()))

prettify(data.entry)

Expand Down
Loading