Skip to content

Commit

Permalink
fix: frontmatter extraction before Twig
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarlovi committed Dec 8, 2023
1 parent db5fdd2 commit f81e0a9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/Decoder/MarkdownFileDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<string, string> $metadata */
$metadata = Yaml::parse($frontMatter);
} catch (ParseException $e) {
throw new \RuntimeException('Invalid frontmatter, failed to parse YAML: '.$e->getMessage(), 0, $e);
}
/** @var array<string, string> $metadata */
$metadata = Yaml::parse($parts[0]);
}

$content = $this->twig->createTemplate($content)->render([
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/site/content/articles/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)}}
Expand Down
28 changes: 27 additions & 1 deletion tests/functional/site/fixtures/en/article/images/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,33 @@
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Images!</h1>
<p>20.07.2022. 12:35</p>
<p>This is a database lookup example: Hello World!</p>
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
<tr>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 6</td>
</tr>
<tr>
<td>Data 7</td>
<td>Data 8</td>
<td>Data 9</td>
</tr>
</tbody>
</table>
<p>This is a database lookup example: Hello World!</p>
<p>This is an asset lookup: /sub/dir/another/assets/images/sigwin.6f9a3d5b.svg</p>
<p><img src="/sub/dir/another/assets/images/sigwin.6f9a3d5b.svg" alt="Logo" /></p>

Expand Down

0 comments on commit f81e0a9

Please sign in to comment.