diff --git a/CHANGELOG.md b/CHANGELOG.md index 369ecb6..66c1a8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ Tapestry currently operates a one month release cycle between releases, see the ## [1.0.12] - Unreleased +### Fixed +- #148 Frontmatter is now parsed in files with an empty body. + ### Added - #157 Added lock file support so Tapestry doesn't run concurrently. diff --git a/src/Modules/Content/FrontMatter.php b/src/Modules/Content/FrontMatter.php index ba67da7..dcddb89 100644 --- a/src/Modules/Content/FrontMatter.php +++ b/src/Modules/Content/FrontMatter.php @@ -10,7 +10,7 @@ class FrontMatter /** * @var string */ - private $pattern = '/^\s*(?:---[\s]*[\r\n]+)(.*?)(?:---[\s]*[\r\n]+)(.*?)$/s'; + private $pattern = '/^\s*(?:---[\s]*[\r\n]+)(.*?)(?:---[\s]*)(.*?)$/s'; /** * @var string @@ -35,8 +35,8 @@ public function __construct($body) $this->body = $body; // If front matter is found, then we should parse it if (preg_match($this->pattern, $this->body, $matches)) { - $this->content = $matches[2]; - $this->parse($matches[1]); + $this->content = trim($matches[2]); + $this->parse(trim($matches[1])); } else { $this->content = $this->body; } diff --git a/tests/FrontmatterTest.php b/tests/FrontmatterTest.php new file mode 100644 index 0000000..27a9f95 --- /dev/null +++ b/tests/FrontmatterTest.php @@ -0,0 +1,53 @@ +getFileContent()); + $this->assertSame('', $frontMatter->getContent()); + $this->assertSame([ + 'title' => 'Test File Title', + 'draft' => false, + 'date' => 507600000 + ], $frontMatter->getData()); + } + + function testFrontMatterAndBodyParsedCorrectly() + { + $file = new File(new SplFileInfo(__DIR__ . '/Mocks/TestFile.md', '', '')); + $frontMatter = new FrontMatter($file->getFileContent()); + $this->assertSame('This is a test file...', $frontMatter->getContent()); + $this->assertSame([ + 'title' => 'Test File Title', + 'draft' => false, + 'date' => 507600000 + ], $frontMatter->getData()); + } + + function testFrontMatterParsedWhenEmpty() + { + $frontMatter = new FrontMatter("---\n---\nHello World"); + $this->assertSame('Hello World', $frontMatter->getContent()); + $this->assertSame([], $frontMatter->getData()); + + $frontMatter = new FrontMatter("---\n---\n\n\nHello World"); + $this->assertSame('Hello World', $frontMatter->getContent()); + $this->assertSame([], $frontMatter->getData()); + + $frontMatter = new FrontMatter("---\r\n---\r\nHello World"); + $this->assertSame('Hello World', $frontMatter->getContent()); + $this->assertSame([], $frontMatter->getData()); + } +} diff --git a/tests/Mocks/TestFileNoBody.md b/tests/Mocks/TestFileNoBody.md new file mode 100644 index 0000000..376c827 --- /dev/null +++ b/tests/Mocks/TestFileNoBody.md @@ -0,0 +1,5 @@ +--- +title: Test File Title +draft: false +date: 1986-02-01 +--- \ No newline at end of file