From 4bf809dc7443cbd3238ae41784e8539e7fd98b79 Mon Sep 17 00:00:00 2001 From: Drew Varner Date: Mon, 15 Jan 2024 15:52:21 -0500 Subject: [PATCH] Automatically strip YAML front matter --- fixture.md | 4 ++++ index.compiler.spec.tsx | 18 ++++++++++++++++++ index.tsx | 3 +++ 3 files changed, 25 insertions(+) diff --git a/fixture.md b/fixture.md index eedf163d..9434ec56 100644 --- a/fixture.md +++ b/fixture.md @@ -1,3 +1,7 @@ +--- +draft: false +date: 2024-01-15 +--- Markdown: Syntax ================ diff --git a/index.compiler.spec.tsx b/index.compiler.spec.tsx index 714317b2..05453f2c 100644 --- a/index.compiler.spec.tsx +++ b/index.compiler.spec.tsx @@ -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(` + + Hello. + +`) +}) + it('handles a holistic example', () => { const md = fs.readFileSync(__dirname + '/fixture.md', 'utf8') render(compiler(md)) diff --git a/index.tsx b/index.tsx index a4f06b54..fbcd42c6 100644 --- a/index.tsx +++ b/index.tsx @@ -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 = @@ -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) {