From db5fdd2531ad24b0deb559a01e6301a65a66ddd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Thu, 7 Dec 2023 17:43:57 +0100 Subject: [PATCH] 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 --- psalm.baseline.xml | 1 + src/Decoder/MarkdownFileDecoder.php | 21 +++++++++- .../site/config/packages/yassg_routes.yaml | 2 +- .../site/content/articles/images.md | 13 +++++++ .../fixtures/en/article/images/index.html | 29 ++++++++++++++ .../site/fixtures/en/articles/1/index.html | 11 ++++-- .../site/fixtures/en/articles/2/index.html | 7 +++- .../site/fixtures/en/articles/3/index.html | 38 +++++++++++++++++++ tests/functional/site/fixtures/index.html | 4 ++ tests/functional/site/src/Model/Article.php | 1 + .../site/templates/pages/articles.html.twig | 4 +- 11 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 tests/functional/site/content/articles/images.md create mode 100644 tests/functional/site/fixtures/en/article/images/index.html create mode 100644 tests/functional/site/fixtures/en/articles/3/index.html diff --git a/psalm.baseline.xml b/psalm.baseline.xml index f95308a..318662d 100644 --- a/psalm.baseline.xml +++ b/psalm.baseline.xml @@ -646,6 +646,7 @@ $publishedAt $slug $title + $image Article diff --git a/src/Decoder/MarkdownFileDecoder.php b/src/Decoder/MarkdownFileDecoder.php index cb6f74a..e3aa595 100644 --- a/src/Decoder/MarkdownFileDecoder.php +++ b/src/Decoder/MarkdownFileDecoder.php @@ -16,6 +16,8 @@ use League\CommonMark\ConverterInterface; use League\CommonMark\Extension\FrontMatter\FrontMatterProviderInterface; use Sigwin\YASSG\FileDecoder; +use Symfony\Component\Yaml\Yaml; +use Twig\Environment; final readonly class MarkdownFileDecoder implements FileDecoder { @@ -23,7 +25,7 @@ private const EXTENSIONS = ['md', 'markdown']; - public function __construct(private ConverterInterface $converter) {} + public function __construct(private ConverterInterface $converter, private Environment $twig) {} public function decode(\SplFileInfo $file): array { @@ -37,8 +39,23 @@ public function decode(\SplFileInfo $file): array throw new \RuntimeException('Failed to read file'); } - $result = $this->converter->convert($content); $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'); + } + /** @var array $metadata */ + $metadata = Yaml::parse($parts[0]); + } + + $content = $this->twig->createTemplate($content)->render([ + 'item' => $metadata, + ]); + } + + $result = $this->converter->convert($content); if ($result instanceof FrontMatterProviderInterface) { /** @var array $metadata */ $metadata = $result->getFrontMatter(); diff --git a/tests/functional/site/config/packages/yassg_routes.yaml b/tests/functional/site/config/packages/yassg_routes.yaml index 5b0ee3a..3829c4f 100644 --- a/tests/functional/site/config/packages/yassg_routes.yaml +++ b/tests/functional/site/config/packages/yassg_routes.yaml @@ -22,7 +22,7 @@ sigwin_yassg: defaults: page: 1 catalog: - page: "yassg_pages('articles', 'item.publishedAt.getTimestamp() <= 1658238497')" + page: "yassg_pages('articles', 'item.publishedAt.getTimestamp() <= 1701776753')" product: path: /{_locale}/{slug} catalog: diff --git a/tests/functional/site/content/articles/images.md b/tests/functional/site/content/articles/images.md new file mode 100644 index 0000000..e4dddbc --- /dev/null +++ b/tests/functional/site/content/articles/images.md @@ -0,0 +1,13 @@ +--- +title: Images! +slug: images +publishedAt: "2022-07-20 12:35:00" +image: assets/images/sigwin.svg +--- + +This is a database lookup example: {{yassg_get('articles', '/hello-world.md').title}} + +This is an asset lookup: {{asset(item.image)}} + +![Logo]({{ 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 new file mode 100644 index 0000000..a765b0d --- /dev/null +++ b/tests/functional/site/fixtures/en/article/images/index.html @@ -0,0 +1,29 @@ + + + + + + + + Images! + + + + + +
+
+
+

Images!

+

20.07.2022. 12:35

+

This is a database lookup example: Hello World!

+

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

+

Logo

+ +
+
+
+ + + + diff --git a/tests/functional/site/fixtures/en/articles/1/index.html b/tests/functional/site/fixtures/en/articles/1/index.html index 82f336a..6e725e1 100644 --- a/tests/functional/site/fixtures/en/articles/1/index.html +++ b/tests/functional/site/fixtures/en/articles/1/index.html @@ -15,10 +15,10 @@

Articles

-

Hello World!

-

18.07.2022. 12:44

-

Paragraphs!

-

19.07.2022. 12:09

+

Images!

+

20.07.2022. 12:35

+

Lists!

+

19.07.2022. 12:13

diff --git a/tests/functional/site/fixtures/en/articles/2/index.html b/tests/functional/site/fixtures/en/articles/2/index.html index 1a68fcc..0e8dece 100644 --- a/tests/functional/site/fixtures/en/articles/2/index.html +++ b/tests/functional/site/fixtures/en/articles/2/index.html @@ -17,8 +17,8 @@

Articles

Titles!

19.07.2022. 12:11

-

Lists!

-

19.07.2022. 12:13

+

Paragraphs!

+

19.07.2022. 12:09

  • @@ -27,6 +27,9 @@

    Lists! 2

  • +
  • + 3 +
diff --git a/tests/functional/site/fixtures/en/articles/3/index.html b/tests/functional/site/fixtures/en/articles/3/index.html new file mode 100644 index 0000000..9816d3b --- /dev/null +++ b/tests/functional/site/fixtures/en/articles/3/index.html @@ -0,0 +1,38 @@ + + + + + + + + Articles, page #3 + + + + + +
+
+
+

Articles

+

Hello World!

+

18.07.2022. 12:44

+ +
    +
  • + 1 +
  • +
  • + 2 +
  • +
  • + 3 +
  • +
+
+
+
+ + + + diff --git a/tests/functional/site/fixtures/index.html b/tests/functional/site/fixtures/index.html index f2ef5bc..42eda00 100644 --- a/tests/functional/site/fixtures/index.html +++ b/tests/functional/site/fixtures/index.html @@ -36,6 +36,8 @@

Index

/sub/dir/another/en/article/hello-world/
+
/sub/dir/another/en/article/images/
+
/sub/dir/another/en/article/lists/
/sub/dir/another/en/article/paragraphs/
@@ -48,6 +50,8 @@

Index

/sub/dir/another/en/articles/1/
/sub/dir/another/en/articles/2/
+ +
/sub/dir/another/en/articles/3/
product
/sub/dir/another/en/nested-example/
diff --git a/tests/functional/site/src/Model/Article.php b/tests/functional/site/src/Model/Article.php index e03a1a5..7ef55a6 100644 --- a/tests/functional/site/src/Model/Article.php +++ b/tests/functional/site/src/Model/Article.php @@ -20,6 +20,7 @@ final class Article public string $title; public string $slug; public string $body; + public string $image; #[Context(['datetime_format' => 'Y-m-d H:i:s'])] public \DateTimeInterface $publishedAt; diff --git a/tests/functional/site/templates/pages/articles.html.twig b/tests/functional/site/templates/pages/articles.html.twig index a067a4f..8c19345 100644 --- a/tests/functional/site/templates/pages/articles.html.twig +++ b/tests/functional/site/templates/pages/articles.html.twig @@ -1,8 +1,8 @@ {% extends 'layout.html.twig' %} -{% set condition = 'item.publishedAt.getTimestamp() <= 1658238497' %} +{% set condition = 'item.publishedAt.getTimestamp() <= 1701776753' %} -{% set articles = yassg_paginate('articles', page, {condition: condition, sort: {'item.publishedAt': 'asc'}}) %} +{% set articles = yassg_paginate('articles', page, {condition: condition, sort: {'item.publishedAt': 'desc'}}) %} {% block title %}Articles, page #{{ page }}{% endblock %}