From d377964782ae4fbcce5560043843f4b0e6c58a2b Mon Sep 17 00:00:00 2001 From: _Kerman Date: Thu, 7 Mar 2024 12:36:18 +0800 Subject: [PATCH 1/3] fix: slide formatter --- packages/parser/src/core.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/parser/src/core.ts b/packages/parser/src/core.ts index 9727774d44..157f0ab8fa 100644 --- a/packages/parser/src/core.ts +++ b/packages/parser/src/core.ts @@ -3,7 +3,7 @@ import { isObject, objectMap } from '@antfu/utils' import type { FrontmatterStyle, SlidevFeatureFlags, SlidevMarkdown, SlidevPreparserExtension, SourceSlideInfo } from '@slidev/types' export function stringify(data: SlidevMarkdown) { - return `${data.slides.map(stringifySlide).join('\n').trim()}\n` + return `${data.slides.map(stringifySlide).join('').trim()}\n` } export function stringifySlide(data: SourceSlideInfo, idx = 0) { @@ -13,14 +13,15 @@ export function stringifySlide(data: SourceSlideInfo, idx = 0) { } 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.raw += `\n\n\n` else data.raw += '\n' return data From dcad59d0a8c747c20550c848a527ef32aaea1914 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Thu, 7 Mar 2024 13:17:31 +0800 Subject: [PATCH 2/3] fix: slide formatter --- packages/parser/src/core.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/parser/src/core.ts b/packages/parser/src/core.ts index 157f0ab8fa..ebef1e65a7 100644 --- a/packages/parser/src/core.ts +++ b/packages/parser/src/core.ts @@ -1,15 +1,15 @@ 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) { - return `${data.slides.map(stringifySlide).join('').trim()}\n` + return `${data.slides.map(stringifySlide).join('\n').trim()}\n` } 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) { @@ -21,9 +21,7 @@ export function prettifySlide(data: SourceSlideInfo) { : `---\n${data.frontmatterRaw.trim()}\n---\n${data.content}` : data.content if (data.note) - data.raw += `\n\n\n` - else - data.raw += '\n' + data.raw += `\n\n` return data } From 75fd798657d2d222feb93ce49b3edf7c1a84f451 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Thu, 7 Mar 2024 15:48:53 +0800 Subject: [PATCH 3/3] test: update snaps --- test/__snapshots__/parser.test.ts.snap | 12 +----------- test/parser.test.ts | 6 +++++- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/test/__snapshots__/parser.test.ts.snap b/test/__snapshots__/parser.test.ts.snap index eebeeb4b2a..8226d1375f 100644 --- a/test/__snapshots__/parser.test.ts.snap +++ b/test/__snapshots__/parser.test.ts.snap @@ -123,7 +123,6 @@ fonts: --- # Hi - ", "start": 0, "title": "Hi", @@ -200,7 +199,6 @@ This is note "note": undefined, "raw": " # Morning - ", "start": 24, "title": "Morning", @@ -290,7 +288,6 @@ this should be treated as code block Also part of the code block \`\`\` - ", "start": 39, "title": undefined, @@ -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, @@ -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", @@ -422,7 +418,6 @@ layout: should not from yaml 2 \`\`\` Content 3 - ", "start": 71, "title": "Title", @@ -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"}", @@ -655,7 +649,6 @@ Sample Text \`\`\`ts console.log('Hello World') \`\`\` - ", "start": 0, "title": "H1", @@ -696,7 +689,6 @@ console.log('Hello World') - Hi - Hey - Yo - ", "start": 11, "title": "Hello", @@ -722,7 +714,6 @@ Nice to meet you "note": undefined, "raw": " Nice to meet you - ", "start": 20, "title": undefined, @@ -1072,7 +1063,6 @@ $x+2$ # Inline Page $x+2$ - ", "start": 20, "title": "Inline Page", diff --git a/test/parser.test.ts b/test/parser.test.ts index 5fa9c0e399..1da641be41 100644 --- a/test/parser.test.ts +++ b/test/parser.test.ts @@ -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', { @@ -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)