Skip to content

Commit

Permalink
✨ Frontmatter now parses with empty body (#278)
Browse files Browse the repository at this point in the history
* ✅ TDD for #148

* 🐛 fixes #148

* 📝 adding changelog for #148
  • Loading branch information
carbontwelve authored Dec 20, 2017
1 parent da0e8d1 commit dcc27bf
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions src/Modules/Content/FrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
53 changes: 53 additions & 0 deletions tests/FrontmatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Tapestry\Tests;

use Symfony\Component\Finder\SplFileInfo;
use Tapestry\Entities\File;
use Tapestry\Modules\Content\FrontMatter;

class FrontmatterTest extends CommandTestBase
{
/**
* Written for issue #148
* @link https://github.com/carbontwelve/tapestry/issues/148
*/
function testFrontmatterParsedWhenBodyEmpty()
{
$file = new File(new SplFileInfo(__DIR__ . '/Mocks/TestFileNoBody.md', '', ''));
$frontMatter = new FrontMatter($file->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());
}
}
5 changes: 5 additions & 0 deletions tests/Mocks/TestFileNoBody.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Test File Title
draft: false
date: 1986-02-01
---

0 comments on commit dcc27bf

Please sign in to comment.