Skip to content

Commit c600c3e

Browse files
committed
Merge branch '2.1' into 2.2
2 parents 5028d6d + 7cdfc40 commit c600c3e

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
66

77
## [Unreleased][unreleased]
88

9+
## [2.1.3] - 2022-02-26
10+
11+
### Fixed
12+
13+
- Fixed front matter parsing with Windows line endings (#821)
14+
15+
## [2.0.4] - 2022-02-26
16+
17+
### Fixed
18+
19+
- Fixed front matter parsing with Windows line endings (#821)
20+
921
## [2.2.2] - 2022-02-13
1022

1123
### Fixed
@@ -420,9 +432,11 @@ No changes were introduced since the previous release.
420432
[2.2.2]: https://github.com/thephpleague/commonmark/compare/2.2.1...2.2.2
421433
[2.2.1]: https://github.com/thephpleague/commonmark/compare/2.2.0...2.2.1
422434
[2.2.0]: https://github.com/thephpleague/commonmark/compare/2.1.1...2.2.0
435+
[2.1.3]: https://github.com/thephpleague/commonmark/compare/2.1.2...2.1.3
423436
[2.1.2]: https://github.com/thephpleague/commonmark/compare/2.1.1...2.1.2
424437
[2.1.1]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.1.1
425438
[2.1.0]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.1.0
439+
[2.0.4]: https://github.com/thephpleague/commonmark/compare/2.0.3...2.0.4
426440
[2.0.3]: https://github.com/thephpleague/commonmark/compare/2.0.2...2.0.3
427441
[2.0.2]: https://github.com/thephpleague/commonmark/compare/2.0.1...2.0.2
428442
[2.0.1]: https://github.com/thephpleague/commonmark/compare/2.0.0...2.0.1

psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0"?>
22
<psalm
3-
totallyTyped="false"
43
errorLevel="3"
54
resolveFromConfigFile="true"
65
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

src/Extension/FrontMatter/FrontMatterParser.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ final class FrontMatterParser implements FrontMatterParserInterface
2222
/** @psalm-readonly */
2323
private FrontMatterDataParserInterface $frontMatterParser;
2424

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

2727
public function __construct(FrontMatterDataParserInterface $frontMatterParser)
2828
{
@@ -39,18 +39,21 @@ public function parse(string $markdownContent): MarkdownInputWithFrontMatter
3939
return new MarkdownInputWithFrontMatter($markdownContent);
4040
}
4141

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

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

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

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

5558
return new MarkdownInputWithFrontMatter($cursor->getRemainder(), $lineOffset, $data);
5659
}

tests/functional/Extension/FrontMatterExtension/FrontMatterExtensionTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,34 @@ public function testWithMultipleYamlDocuments(FrontMatterDataParserInterface $pa
154154
$this->assertSame(9, $result->getDocument()->firstChild()->getStartLine());
155155
}
156156

157+
/**
158+
* @dataProvider parserProvider
159+
*/
160+
public function testWithWindowsLineEndings(FrontMatterDataParserInterface $parser, bool $shouldTest): void
161+
{
162+
$this->skipIfParserNotAvailable($parser, $shouldTest);
163+
164+
$markdown = "---\r\nfoo: bar\r\n---\r\n\r\n# Test";
165+
166+
$expectedHtml = "<h1>Test</h1>\n";
167+
$expectedFrontMatter = ['foo' => 'bar'];
168+
169+
$converter = new MarkdownConverter($this->getEnvironment($parser));
170+
$result = $converter->convertToHtml($markdown);
171+
172+
$this->assertInstanceOf(RenderedContentWithFrontMatter::class, $result);
173+
$this->assertInstanceOf(\Stringable::class, $result);
174+
175+
\assert($result instanceof RenderedContentWithFrontMatter);
176+
$this->assertSame($expectedFrontMatter, $result->getFrontMatter());
177+
178+
$this->assertSame($expectedHtml, (string) $result->getContent());
179+
$this->assertSame($expectedHtml, (string) $result);
180+
181+
$this->assertSame(1, $result->getDocument()->getStartLine());
182+
$this->assertSame(5, $result->getDocument()->firstChild()->getStartLine());
183+
}
184+
157185
/**
158186
* @dataProvider parserProvider
159187
*/

0 commit comments

Comments
 (0)