From f81e0a9e9c66f276458981d3fdfbbdab7f138adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Fri, 8 Dec 2023 10:56:21 +0100 Subject: [PATCH] fix: frontmatter extraction before Twig --- src/Decoder/MarkdownFileDecoder.php | 17 +++++++---- .../site/content/articles/images.md | 6 ++++ .../fixtures/en/article/images/index.html | 28 ++++++++++++++++++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/Decoder/MarkdownFileDecoder.php b/src/Decoder/MarkdownFileDecoder.php index e3aa595..c999b21 100644 --- a/src/Decoder/MarkdownFileDecoder.php +++ b/src/Decoder/MarkdownFileDecoder.php @@ -16,6 +16,7 @@ use League\CommonMark\ConverterInterface; use League\CommonMark\Extension\FrontMatter\FrontMatterProviderInterface; use Sigwin\YASSG\FileDecoder; +use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Yaml; use Twig\Environment; @@ -42,12 +43,18 @@ public function decode(\SplFileInfo $file): array $metadata = []; if (str_contains($content, '{{') || str_contains($content, '{%')) { if (str_starts_with($content, '---')) { - $parts = explode('---', ltrim($content, '-'), 3); - if (\count($parts) !== 2) { - throw new \RuntimeException('Failed to extract frontmatter'); + $end = mb_strpos($content, '---', 3); + if ($end === false) { + throw new \RuntimeException('Invalid frontmatter, missing closing ---'); + } + $frontMatter = mb_substr($content, 3, $end - 3); + + try { + /** @var array $metadata */ + $metadata = Yaml::parse($frontMatter); + } catch (ParseException $e) { + throw new \RuntimeException('Invalid frontmatter, failed to parse YAML: '.$e->getMessage(), 0, $e); } - /** @var array $metadata */ - $metadata = Yaml::parse($parts[0]); } $content = $this->twig->createTemplate($content)->render([ diff --git a/tests/functional/site/content/articles/images.md b/tests/functional/site/content/articles/images.md index e4dddbc..bfe9f69 100644 --- a/tests/functional/site/content/articles/images.md +++ b/tests/functional/site/content/articles/images.md @@ -5,6 +5,12 @@ publishedAt: "2022-07-20 12:35:00" image: assets/images/sigwin.svg --- +| Column 1 | Column 2 | Column 3 | +| -------- | -------- | -------- | +| Data 1 | Data 2 | Data 3 | +| Data 4 | Data 5 | Data 6 | +| Data 7 | Data 8 | Data 9 | + This is a database lookup example: {{yassg_get('articles', '/hello-world.md').title}} This is an asset lookup: {{asset(item.image)}} diff --git a/tests/functional/site/fixtures/en/article/images/index.html b/tests/functional/site/fixtures/en/article/images/index.html index a765b0d..df7e3d6 100644 --- a/tests/functional/site/fixtures/en/article/images/index.html +++ b/tests/functional/site/fixtures/en/article/images/index.html @@ -16,7 +16,33 @@

Images!

20.07.2022. 12:35

-

This is a database lookup example: Hello World!

+ + + + + + + + + + + + + + + + + + + + + + + + + +
Column 1Column 2Column 3
Data 1Data 2Data 3
Data 4Data 5Data 6
Data 7Data 8Data 9
+

This is a database lookup example: Hello World!

This is an asset lookup: /sub/dir/another/assets/images/sigwin.6f9a3d5b.svg

Logo