Skip to content

Commit

Permalink
Merge branch '2.0' into 2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Feb 26, 2022
2 parents 34712ca + 39031ad commit 3b0fb78
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

## [Unreleased][unreleased]

## [2.0.4] - 2022-02-26

### Fixed

- Fixed front matter parsing with Windows line endings (#821)

## [2.1.2] - 2022-02-13

### Fixed
Expand Down Expand Up @@ -386,6 +392,7 @@ No changes were introduced since the previous release.
[2.1.2]: https://github.com/thephpleague/commonmark/compare/2.1.1...2.1.2
[2.1.1]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.1.1
[2.1.0]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.1.0
[2.0.4]: https://github.com/thephpleague/commonmark/compare/2.0.3...2.0.4
[2.0.3]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.0.3
[2.0.2]: https://github.com/thephpleague/commonmark/compare/2.0.1...2.0.2
[2.0.1]: https://github.com/thephpleague/commonmark/compare/2.0.0...2.0.1
Expand Down
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down
13 changes: 8 additions & 5 deletions src/Extension/FrontMatter/FrontMatterParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class FrontMatterParser implements FrontMatterParserInterface
/** @psalm-readonly */
private FrontMatterDataParserInterface $frontMatterParser;

private const REGEX_FRONT_MATTER = '/^---\\n.*?\\n---\n/s';
private const REGEX_FRONT_MATTER = '/^---\\R.*?\\R---\\R/s';

public function __construct(FrontMatterDataParserInterface $frontMatterParser)
{
Expand All @@ -39,18 +39,21 @@ public function parse(string $markdownContent): MarkdownInputWithFrontMatter
return new MarkdownInputWithFrontMatter($markdownContent);
}

// Trim the last 4 characters (ending ---s and newline)
$frontMatter = \substr($frontMatter, 0, -4);
// Trim the last line (ending ---s and newline)
$frontMatter = \preg_replace('/---\R$/', '', $frontMatter);
if ($frontMatter === null) {
return new MarkdownInputWithFrontMatter($markdownContent);
}

// Parse the resulting YAML data
$data = $this->frontMatterParser->parse($frontMatter);

// Advance through any remaining newlines which separated the front matter from the Markdown text
$trailingNewlines = $cursor->match('/^\n+/');
$trailingNewlines = $cursor->match('/^\R+/');

// Calculate how many lines the Markdown is offset from the front matter by counting the number of newlines
// Don't forget to add 1 because we stripped one out when trimming the trailing delims
$lineOffset = \preg_match_all('/\n/', $frontMatter . $trailingNewlines) + 1;
$lineOffset = \preg_match_all('/\R/', $frontMatter . $trailingNewlines) + 1;

return new MarkdownInputWithFrontMatter($cursor->getRemainder(), $lineOffset, $data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,34 @@ public function testWithMultipleYamlDocuments(FrontMatterDataParserInterface $pa
$this->assertSame(9, $result->getDocument()->firstChild()->getStartLine());
}

/**
* @dataProvider parserProvider
*/
public function testWithWindowsLineEndings(FrontMatterDataParserInterface $parser, bool $shouldTest): void
{
$this->skipIfParserNotAvailable($parser, $shouldTest);

$markdown = "---\r\nfoo: bar\r\n---\r\n\r\n# Test";

$expectedHtml = "<h1>Test</h1>\n";
$expectedFrontMatter = ['foo' => 'bar'];

$converter = new MarkdownConverter($this->getEnvironment($parser));
$result = $converter->convertToHtml($markdown);

$this->assertInstanceOf(RenderedContentWithFrontMatter::class, $result);
$this->assertInstanceOf(\Stringable::class, $result);

\assert($result instanceof RenderedContentWithFrontMatter);
$this->assertSame($expectedFrontMatter, $result->getFrontMatter());

$this->assertSame($expectedHtml, (string) $result->getContent());
$this->assertSame($expectedHtml, (string) $result);

$this->assertSame(1, $result->getDocument()->getStartLine());
$this->assertSame(5, $result->getDocument()->firstChild()->getStartLine());
}

/**
* @dataProvider parserProvider
*/
Expand Down

0 comments on commit 3b0fb78

Please sign in to comment.