Skip to content

Commit db5fdd2

Browse files
authored
feat: expressions in markdown (#181)
* feat: assets in Markdown * feat: expressions in Markdown * feat: expressions in Markdown * feat: expressions in Markdown * feat: expressions in Markdown * feat: Twig (!) in Markdown * feat: Twig (!) in Markdown * feat: Twig (!) in Markdown
1 parent 86a5048 commit db5fdd2

File tree

11 files changed

+120
-11
lines changed

11 files changed

+120
-11
lines changed

psalm.baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,7 @@
646646
<code>$publishedAt</code>
647647
<code>$slug</code>
648648
<code>$title</code>
649+
<code>$image</code>
649650
</MissingConstructor>
650651
<UnusedClass>
651652
<code>Article</code>

src/Decoder/MarkdownFileDecoder.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
use League\CommonMark\ConverterInterface;
1717
use League\CommonMark\Extension\FrontMatter\FrontMatterProviderInterface;
1818
use Sigwin\YASSG\FileDecoder;
19+
use Symfony\Component\Yaml\Yaml;
20+
use Twig\Environment;
1921

2022
final readonly class MarkdownFileDecoder implements FileDecoder
2123
{
2224
use FileDecoderTrait;
2325

2426
private const EXTENSIONS = ['md', 'markdown'];
2527

26-
public function __construct(private ConverterInterface $converter) {}
28+
public function __construct(private ConverterInterface $converter, private Environment $twig) {}
2729

2830
public function decode(\SplFileInfo $file): array
2931
{
@@ -37,8 +39,23 @@ public function decode(\SplFileInfo $file): array
3739
throw new \RuntimeException('Failed to read file');
3840
}
3941

40-
$result = $this->converter->convert($content);
4142
$metadata = [];
43+
if (str_contains($content, '{{') || str_contains($content, '{%')) {
44+
if (str_starts_with($content, '---')) {
45+
$parts = explode('---', ltrim($content, '-'), 3);
46+
if (\count($parts) !== 2) {
47+
throw new \RuntimeException('Failed to extract frontmatter');
48+
}
49+
/** @var array<string, string> $metadata */
50+
$metadata = Yaml::parse($parts[0]);
51+
}
52+
53+
$content = $this->twig->createTemplate($content)->render([
54+
'item' => $metadata,
55+
]);
56+
}
57+
58+
$result = $this->converter->convert($content);
4259
if ($result instanceof FrontMatterProviderInterface) {
4360
/** @var array<string, string> $metadata */
4461
$metadata = $result->getFrontMatter();

tests/functional/site/config/packages/yassg_routes.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sigwin_yassg:
2222
defaults:
2323
page: 1
2424
catalog:
25-
page: "yassg_pages('articles', 'item.publishedAt.getTimestamp() <= 1658238497')"
25+
page: "yassg_pages('articles', 'item.publishedAt.getTimestamp() <= 1701776753')"
2626
product:
2727
path: /{_locale}/{slug}
2828
catalog:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Images!
3+
slug: images
4+
publishedAt: "2022-07-20 12:35:00"
5+
image: assets/images/sigwin.svg
6+
---
7+
8+
This is a database lookup example: {{yassg_get('articles', '/hello-world.md').title}}
9+
10+
This is an asset lookup: {{asset(item.image)}}
11+
12+
![Logo]({{ asset(item.image) }})
13+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
7+
8+
<title>Images!</title>
9+
10+
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
11+
</head>
12+
<body>
13+
14+
<main class="min-h-full flex flex-col justify-center py-12 sm:px-6 lg:px-8">
15+
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
16+
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
17+
<h1>Images!</h1>
18+
<p>20.07.2022. 12:35</p>
19+
<p>This is a database lookup example: Hello World!</p>
20+
<p>This is an asset lookup: /sub/dir/another/assets/images/sigwin.6f9a3d5b.svg</p>
21+
<p><img src="/sub/dir/another/assets/images/sigwin.6f9a3d5b.svg" alt="Logo" /></p>
22+
23+
</div>
24+
</div>
25+
</main>
26+
27+
<script src="/sub/dir/another/assets/runtime.4d1205b9.js" integrity="sha384-J2jFm3DPgrcqyvls0fZlS51nUYgiBB+JLwWiU5v7vxEbLwKolmaHrRuz4BjP6qaE"></script><script src="/sub/dir/another/assets/app.9267444a.js" integrity="sha384-WbAUi92ziCGFD1kGZRzpOP8Nxymnw5vXM3szOB1JmBrCU+MVGXBRReCttizXHDdI"></script>
28+
</body>
29+
</html>

tests/functional/site/fixtures/en/articles/1/index.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
1616
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
1717
<h1>Articles</h1>
18-
<h2><a href="https://example.com/sub/dir/another/en/article/hello-world/">Hello World!</a></h2>
19-
<p>18.07.2022. 12:44</p>
20-
<h2><a href="https://example.com/sub/dir/another/en/article/paragraphs/">Paragraphs!</a></h2>
21-
<p>19.07.2022. 12:09</p>
18+
<h2><a href="https://example.com/sub/dir/another/en/article/images/">Images!</a></h2>
19+
<p>20.07.2022. 12:35</p>
20+
<h2><a href="https://example.com/sub/dir/another/en/article/lists/">Lists!</a></h2>
21+
<p>19.07.2022. 12:13</p>
2222

2323
<ul>
2424
<li>
@@ -27,6 +27,9 @@ <h2><a href="https://example.com/sub/dir/another/en/article/paragraphs/">Paragra
2727
<li>
2828
<a href="https://example.com/sub/dir/another/en/articles/2/">2</a>
2929
</li>
30+
<li>
31+
<a href="https://example.com/sub/dir/another/en/articles/3/">3</a>
32+
</li>
3033
</ul>
3134
</div>
3235
</div>

tests/functional/site/fixtures/en/articles/2/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
<h1>Articles</h1>
1818
<h2><a href="https://example.com/sub/dir/another/en/article/titles/">Titles!</a></h2>
1919
<p>19.07.2022. 12:11</p>
20-
<h2><a href="https://example.com/sub/dir/another/en/article/lists/">Lists!</a></h2>
21-
<p>19.07.2022. 12:13</p>
20+
<h2><a href="https://example.com/sub/dir/another/en/article/paragraphs/">Paragraphs!</a></h2>
21+
<p>19.07.2022. 12:09</p>
2222

2323
<ul>
2424
<li>
@@ -27,6 +27,9 @@ <h2><a href="https://example.com/sub/dir/another/en/article/lists/">Lists!</a></
2727
<li>
2828
2
2929
</li>
30+
<li>
31+
<a href="https://example.com/sub/dir/another/en/articles/3/">3</a>
32+
</li>
3033
</ul>
3134
</div>
3235
</div>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
7+
8+
<title>Articles, page #3</title>
9+
10+
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
11+
</head>
12+
<body>
13+
14+
<main class="min-h-full flex flex-col justify-center py-12 sm:px-6 lg:px-8">
15+
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
16+
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
17+
<h1>Articles</h1>
18+
<h2><a href="https://example.com/sub/dir/another/en/article/hello-world/">Hello World!</a></h2>
19+
<p>18.07.2022. 12:44</p>
20+
21+
<ul>
22+
<li>
23+
<a href="https://example.com/sub/dir/another/en/articles/1/">1</a>
24+
</li>
25+
<li>
26+
<a href="https://example.com/sub/dir/another/en/articles/2/">2</a>
27+
</li>
28+
<li>
29+
3
30+
</li>
31+
</ul>
32+
</div>
33+
</div>
34+
</main>
35+
36+
<script src="/sub/dir/another/assets/runtime.4d1205b9.js" integrity="sha384-J2jFm3DPgrcqyvls0fZlS51nUYgiBB+JLwWiU5v7vxEbLwKolmaHrRuz4BjP6qaE"></script><script src="/sub/dir/another/assets/app.9267444a.js" integrity="sha384-WbAUi92ziCGFD1kGZRzpOP8Nxymnw5vXM3szOB1JmBrCU+MVGXBRReCttizXHDdI"></script>
37+
</body>
38+
</html>

tests/functional/site/fixtures/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ <h1>Index</h1>
3636

3737
<dd><a href="/sub/dir/another/en/article/hello-world/">/sub/dir/another/en/article/hello-world/</a></dd>
3838

39+
<dd><a href="/sub/dir/another/en/article/images/">/sub/dir/another/en/article/images/</a></dd>
40+
3941
<dd><a href="/sub/dir/another/en/article/lists/">/sub/dir/another/en/article/lists/</a></dd>
4042

4143
<dd><a href="/sub/dir/another/en/article/paragraphs/">/sub/dir/another/en/article/paragraphs/</a></dd>
@@ -48,6 +50,8 @@ <h1>Index</h1>
4850
<dd><a href="/sub/dir/another/en/articles/1/">/sub/dir/another/en/articles/1/</a></dd>
4951

5052
<dd><a href="/sub/dir/another/en/articles/2/">/sub/dir/another/en/articles/2/</a></dd>
53+
54+
<dd><a href="/sub/dir/another/en/articles/3/">/sub/dir/another/en/articles/3/</a></dd>
5155
<dt>product</dt>
5256

5357
<dd><a href="/sub/dir/another/en/nested-example/">/sub/dir/another/en/nested-example/</a></dd>

tests/functional/site/src/Model/Article.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ final class Article
2020
public string $title;
2121
public string $slug;
2222
public string $body;
23+
public string $image;
2324

2425
#[Context(['datetime_format' => 'Y-m-d H:i:s'])]
2526
public \DateTimeInterface $publishedAt;

tests/functional/site/templates/pages/articles.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{% extends 'layout.html.twig' %}
22

3-
{% set condition = 'item.publishedAt.getTimestamp() <= 1658238497' %}
3+
{% set condition = 'item.publishedAt.getTimestamp() <= 1701776753' %}
44

5-
{% set articles = yassg_paginate('articles', page, {condition: condition, sort: {'item.publishedAt': 'asc'}}) %}
5+
{% set articles = yassg_paginate('articles', page, {condition: condition, sort: {'item.publishedAt': 'desc'}}) %}
66

77
{% block title %}Articles, page #{{ page }}{% endblock %}
88

0 commit comments

Comments
 (0)