Replies: 4 comments
-
I can confirm cwchentw comments on this wrong behaviour, when parsing Markdown files, which in turn contain three dashes. I corrected the code, here is my version: public static function parse(string $content): Document
{
$n3dash = 0; // count number of triple dashes
$pos1 = 0;
$pos2 = 0;
$len = strlen($content);
for ($pos=0;; $pos+=3) {
$pos = strpos($content,"---",$pos);
if ($pos === false) return new Document([],$content); // no pair of triple dashes at all
// Are we at end or is next character white space?
if ( $pos + 3 == $len || ctype_space(substr($content,$pos+3,1)) ) {
if ($n3dash == 0 && ($pos == 0 || $pos > 0 && substr($content,$pos-1,1)=="\n")) {
$n3dash = 1; // found first triple dash
$pos1 = $pos + 3;
} else if ($n3dash == 1 && substr($content,$pos-1,1) == "\n") {
// found 2nd properly enclosed triple dash
$n3dash = 2; $pos2 = $pos + 3; break;
}
}
}
$matter = substr($content,$pos1,$pos2-3-$pos1);
$body = substr($content,$pos2);
$matter = Yaml::parse($matter);
return new Document($matter, $body);
} |
Beta Was this translation helpful? Give feedback.
-
Can confirm that this is still an issue, but that @eklausme's patch works. Can we get a PR for this? |
Beta Was this translation helpful? Give feedback.
-
I make a fork of the package, and implemented the method @eklausme wrote. It's fully compatible as I created it as a new method: public static function markdownCompatibleParse(string $content): Document You can use it by adding this to your composer.json "repositories": [
{
"type": "vcs",
"url": "https://github.com/caendesilva/yaml-front-matter"
}
] and running |
Beta Was this translation helpful? Give feedback.
-
Submitted a PR to fix. #38 |
Beta Was this translation helpful? Give feedback.
-
Here is a Markdown post to repeat the bug:
Here is a simple test program:
Here is the wrongly parsed result:
In summary, the package wrongly parse a front matter nested in a Markdown post.
Beta Was this translation helpful? Give feedback.
All reactions