Skip to content

Commit

Permalink
fix: preserve empty headmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
KermanX committed Feb 29, 2024
1 parent 7d7a185 commit c8231d3
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import type { SlideInfoBase } from '@slidev/types'

export const astFormat = 'slidev-ast'

interface MarkdownNode {
export interface MarkdownNode {
type: 'markdown'
raw: string
slides: SlideNode[]
}

interface SlideNode {
export interface SlideInfo extends SlideInfoBase {
isFirstSlide: boolean
}

export interface SlideNode {
type: 'slide'
info: SlideInfoBase
info: SlideInfo
}

export type ASTNode = MarkdownNode | SlideNode
6 changes: 4 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ export const parser: Parser<ASTNode> = {
return {
type: 'markdown',
raw,
slides: slides.map(info => ({
slides: slides.map((info, index) => ({
type: 'slide',
info,
info: Object.assign(info, {
isFirstSlide: index === 0,
}),
})),
}
},
Expand Down
21 changes: 10 additions & 11 deletions src/printer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { SlideInfoBase } from '@slidev/types'
import type { Doc, Options, Printer } from 'prettier'
import { doc } from 'prettier'
import type { ASTNode } from './ast'
import type { ASTNode, SlideInfo } from './ast'

const { join, line, hardline } = doc.builders

Expand Down Expand Up @@ -36,7 +35,7 @@ export const printer: Printer<ASTNode> = {
}

async function printSlide(
info: SlideInfoBase,
info: SlideInfo,
textToDoc: (text: string, options: Options) => Promise<Doc>,
): Promise<Doc[]> {
return [
Expand All @@ -47,11 +46,11 @@ async function printSlide(
}

async function printFrontmatter(
info: SlideInfoBase,
info: SlideInfo,
textToDoc: (text: string, options: Options) => Promise<Doc>,
): Promise<Doc[]> {
const trimed = info.frontmatterRaw?.trim() ?? ''
if (trimed.length === 0)
if (trimed.length === 0 && !info.isFirstSlide)
return []

const formatted = await textToDoc(trimed, {
Expand All @@ -63,7 +62,7 @@ async function printFrontmatter(
}

async function printContent(
info: SlideInfoBase,
info: SlideInfo,
textToDoc: (text: string, options: Options) => Promise<Doc>,
): Promise<Doc[]> {
if (info.content.trim().length === 0)
Expand All @@ -78,31 +77,31 @@ async function printContent(
]
}

function printNote(info: SlideInfoBase): Doc[] {
function printNote(info: SlideInfo): Doc[] {
if (!info.note)
return []
return [hardline, '<!--', line, info.note.trim(), line, '-->', hardline]
}

function printSlideNoEmbed(info: SlideInfoBase): Doc[] {
function printSlideNoEmbed(info: SlideInfo): Doc[] {
return [
...(printFrontmatterNoEmbed(info)),
...(printContentNoEmbed(info)),
...printNote(info),
]
}

function printFrontmatterNoEmbed(info: SlideInfoBase): Doc[] {
function printFrontmatterNoEmbed(info: SlideInfo): Doc[] {
const trimed = info.frontmatterRaw?.trim() ?? ''
if (trimed.length === 0)
if (trimed.length === 0 && !info.isFirstSlide)
return []

return info.frontmatterStyle === 'yaml'
? [hardline, '```yaml', hardline, trimed, hardline, '```', hardline]
: [trimed, hardline, '---', hardline]
}

function printContentNoEmbed(info: SlideInfoBase): Doc[] {
function printContentNoEmbed(info: SlideInfo): Doc[] {
if (info.content.trim().length === 0)
return []

Expand Down
4 changes: 4 additions & 0 deletions test/inputs/noHeadmatter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

# Title
5 changes: 5 additions & 0 deletions test/outputs/noHeadmatter_config1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

---

# Title
5 changes: 5 additions & 0 deletions test/outputs/noHeadmatter_default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

---

# Title
5 changes: 5 additions & 0 deletions test/outputs/noHeadmatter_noEmbed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

---

# Title

0 comments on commit c8231d3

Please sign in to comment.