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

Automatically strip YAML front matter #538

Merged
merged 2 commits into from
Jan 29, 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
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
Loading