From cc972390b24c101f9d1872384cdc790aab128171 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 12 Jun 2024 14:45:24 +0200 Subject: [PATCH 1/2] Check if the array key is set before trying to trim it Fixes "undefined array key" warning when a Markdown file does only have front matter and no body. This will also fix https://github.com/hydephp/develop/issues/1705. --- src/ComplexMarkdownParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ComplexMarkdownParser.php b/src/ComplexMarkdownParser.php index a2f37dd..07a67c1 100644 --- a/src/ComplexMarkdownParser.php +++ b/src/ComplexMarkdownParser.php @@ -84,7 +84,7 @@ protected function getBody(): string protected function trimBody(array $body): array { - if (trim($body[0]) === '') { + if (isset($body[0]) && trim($body[0]) === '') { unset($body[0]); } From 11307776333b2c5b3f9b9b66e89f2b9396c4138b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 12 Jun 2024 14:47:30 +0200 Subject: [PATCH 2/2] Test it can parse Markdown file with front matter and no Markdown body --- .../YamlFrontMatterMarkdownCompatibleParseTest.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/YamlFrontMatterMarkdownCompatibleParseTest.php b/tests/YamlFrontMatterMarkdownCompatibleParseTest.php index 5088d04..62706e7 100644 --- a/tests/YamlFrontMatterMarkdownCompatibleParseTest.php +++ b/tests/YamlFrontMatterMarkdownCompatibleParseTest.php @@ -51,7 +51,7 @@ public function it_separates_the_front_matter_from_the_body() public function it_leaves_string_without_front_matter_intact() { $document = YamlFrontMatter::markdownCompatibleParse( - "Lorem ipsum." + 'Lorem ipsum.' ); $this->assertInstanceOf(Document::class, $document); @@ -97,4 +97,16 @@ public function it_can_parse_a_string_with_windows_line_endings() $this->assertEquals(['foo' => 'bar'], $document->matter()); $this->assertStringContainsString('Lorem ipsum.', $document->body()); } + + /** @test */ + public function it_can_parse_markdown_file_with_front_matter_and_no_markdown_body() + { + $document = YamlFrontMatter::markdownCompatibleParse( + "---\nfoo: bar\n---" + ); + + $this->assertInstanceOf(Document::class, $document); + $this->assertEquals(['foo' => 'bar'], $document->matter()); + $this->assertEmpty($document->body()); + } }