Skip to content

Commit

Permalink
feat: automatically strip YAML front matter (#538)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Jacobs <570070+quantizor@users.noreply.github.com>
  • Loading branch information
varnerac and quantizor authored Jan 29, 2024
1 parent 3e0815d commit bd923dc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fixture.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
draft: false
date: 2024-01-15
---
Markdown: Syntax
================

Expand Down
18 changes: 18 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,24 @@ describe('overrides', () => {
})
})

it('should remove YAML front matter', () => {
render(
compiler(theredoc`
---
key: value
other_key: different value
---
Hello.
`)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<span>
Hello.
</span>
`)
})

it('handles a holistic example', () => {
const md = fs.readFileSync(__dirname + '/fixture.md', 'utf8')
render(compiler(md))
Expand Down
3 changes: 3 additions & 0 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const CR_NEWLINE_R = /\r\n?/g
const FOOTNOTE_R = /^\[\^([^\]]+)](:.*)\n/
const FOOTNOTE_REFERENCE_R = /^\[\^([^\]]+)]/
const FORMFEED_R = /\f/g
const FRONT_MATTER_R = /^---[ \t]*\n(.|\n)*\n---[ \t]*\n/
const GFM_TASK_R = /^\s*?\[(x|\s)\]/
const HEADING_R = /^ *(#{1,6}) *([^\n]+?)(?: +#*)?(?:\n *)*(?:\n|$)/
const HEADING_ATX_COMPLIANT_R =
Expand Down Expand Up @@ -1078,6 +1079,8 @@ export function compiler(
}

function compile(input: string): JSX.Element {
input = input.replace(FRONT_MATTER_R, '')

let inline = false

if (options.forceInline) {
Expand Down

0 comments on commit bd923dc

Please sign in to comment.