From fc5e05278defe56018070debd2ede32a7e0c503e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dalibor=20Karlovi=C4=87?= Date: Mon, 18 Jul 2022 15:57:38 +0200 Subject: [PATCH] feat: Markdown (#125) * feat: Markdown * feat: Markdown, front matter reader * feat: Markdown, code block highlighting --- composer.json | 2 + config/services.yaml | 29 ++++++++++ src/Decoder/CachingFileDecoder.php | 5 +- src/Decoder/MarkdownFileDecoder.php | 55 +++++++++++++++++++ tests/functional/site/assets/app.ts | 1 + .../site/config/packages/yassg_databases.yaml | 8 +++ .../site/config/packages/yassg_routes.yaml | 4 ++ .../site/content/articles/hello-world.md | 18 ++++++ .../site/fixtures/assets/app.2e773b84.css | 1 - .../site/fixtures/assets/app.d4da4e7e.css | 1 + .../site/fixtures/assets/entrypoints.json | 4 +- .../site/fixtures/assets/manifest.json | 2 +- tests/functional/site/fixtures/de/index.html | 2 +- .../site/fixtures/en/a/hello-world/index.html | 35 ++++++++++++ .../site/fixtures/en/example1/index.html | 2 +- .../site/fixtures/en/example2/index.html | 2 +- tests/functional/site/fixtures/en/index.html | 2 +- .../fixtures/en/nested-example/index.html | 2 +- tests/functional/site/fixtures/hr/index.html | 2 +- tests/functional/site/fixtures/index.html | 5 +- tests/functional/site/src/Model/Article.php | 20 +++++++ .../site/templates/pages/article.html.twig | 16 ++++++ tests/functional/site/webpack.config.js | 1 + 23 files changed, 207 insertions(+), 12 deletions(-) create mode 100644 src/Decoder/MarkdownFileDecoder.php create mode 100644 tests/functional/site/content/articles/hello-world.md delete mode 100644 tests/functional/site/fixtures/assets/app.2e773b84.css create mode 100644 tests/functional/site/fixtures/assets/app.d4da4e7e.css create mode 100644 tests/functional/site/fixtures/en/a/hello-world/index.html create mode 100644 tests/functional/site/src/Model/Article.php create mode 100644 tests/functional/site/templates/pages/article.html.twig diff --git a/composer.json b/composer.json index 66668aa..ab9c451 100644 --- a/composer.json +++ b/composer.json @@ -5,9 +5,11 @@ "require": { "php": "^8.0", "bentools/cartesian-product": "^1.3", + "league/commonmark": "^2.3", "phpdocumentor/type-resolver": "^1.0", "phpstan/phpdoc-parser": "^1.0", "sigwin/infra": "^0.6", + "spatie/commonmark-highlighter": "^3.0", "symfony/console": "^5.4 || ^6.0", "symfony/expression-language": "^5.4 || ^6.0", "symfony/filesystem": "^5.4 || ^6.0", diff --git a/config/services.yaml b/config/services.yaml index d0fe65d..63a808a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -97,6 +97,35 @@ services: - name: sigwin_yassg.file_decoder + League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension: ~ + League\CommonMark\Extension\FrontMatter\FrontMatterExtension: ~ + + Spatie\CommonMarkHighlighter\FencedCodeRenderer: ~ + Spatie\CommonMarkHighlighter\IndentedCodeRenderer: ~ + + League\CommonMark\Environment\Environment: + calls: + - + addExtension: [ '@League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension' ] + - + addExtension: [ '@League\CommonMark\Extension\FrontMatter\FrontMatterExtension' ] + - + addRenderer: [ League\CommonMark\Extension\CommonMark\Node\Block\FencedCode, '@Spatie\CommonMarkHighlighter\FencedCodeRenderer', 10 ] + - + addRenderer: [ League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode, '@Spatie\CommonMarkHighlighter\IndentedCodeRenderer', 10 ] + League\CommonMark\Environment\EnvironmentInterface: '@League\CommonMark\Environment\Environment' + + League\CommonMark\MarkdownConverter: ~ + League\CommonMark\ConverterInterface: '@League\CommonMark\MarkdownConverter' + sigwin_yassg.file_decoder.markdown_file_decoder: + class: Sigwin\YASSG\Decoder\MarkdownFileDecoder + arguments: + - '@League\CommonMark\ConverterInterface' + tags: + - + name: sigwin_yassg.file_decoder + + Symfony\Component\Config\FileLocatorInterface: '@file_locator' when@prod: diff --git a/src/Decoder/CachingFileDecoder.php b/src/Decoder/CachingFileDecoder.php index 5dc555a..f219ae3 100644 --- a/src/Decoder/CachingFileDecoder.php +++ b/src/Decoder/CachingFileDecoder.php @@ -37,7 +37,10 @@ public function decode(\SplFileInfo $file): array /** @var string $path */ $path = $file->getRealPath(); - $key = md5($path); + /** @var string $content */ + $content = file_get_contents($path); + + $key = md5($content); $item = $this->cachePoolItem->getItem($key); if ($item->isHit()) { diff --git a/src/Decoder/MarkdownFileDecoder.php b/src/Decoder/MarkdownFileDecoder.php new file mode 100644 index 0000000..13093bd --- /dev/null +++ b/src/Decoder/MarkdownFileDecoder.php @@ -0,0 +1,55 @@ +converter = $converter; + } + + public function decode(\SplFileInfo $file): array + { + $path = $file->getRealPath(); + if ($path === false) { + throw new \RuntimeException('Invalid file path'); + } + + $content = file_get_contents($path); + if ($content === false) { + throw new \RuntimeException('Failed to read file'); + } + + $result = $this->converter->convert($content); + $metadata = []; + if ($result instanceof FrontMatterProviderInterface) { + /** @var array $metadata */ + $metadata = $result->getFrontMatter(); + } + $metadata['body'] = $result->getContent(); + + return $metadata; + } +} diff --git a/tests/functional/site/assets/app.ts b/tests/functional/site/assets/app.ts index e8011a8..5662981 100644 --- a/tests/functional/site/assets/app.ts +++ b/tests/functional/site/assets/app.ts @@ -1,4 +1,5 @@ import './styles/app.scss'; +import '../../../../vendor/scrivo/highlight.php/styles/a11y-dark.css'; document.addEventListener('DOMContentLoaded', () => { console.log('DOM ready!'); diff --git a/tests/functional/site/config/packages/yassg_databases.yaml b/tests/functional/site/config/packages/yassg_databases.yaml index 16e7e98..dfa7071 100644 --- a/tests/functional/site/config/packages/yassg_databases.yaml +++ b/tests/functional/site/config/packages/yassg_databases.yaml @@ -1,5 +1,13 @@ sigwin_yassg: databases: + articles: + class: Sigwin\YASSG\Test\Functional\Site\Model\Article + storage: filesystem + options: + root: + - "%sigwin_yassg.base_dir%/content/articles" + names: + - "*.md" products: class: Sigwin\YASSG\Test\Functional\Site\Model\Product storage: filesystem diff --git a/tests/functional/site/config/packages/yassg_routes.yaml b/tests/functional/site/config/packages/yassg_routes.yaml index 45050c6..69d7af3 100644 --- a/tests/functional/site/config/packages/yassg_routes.yaml +++ b/tests/functional/site/config/packages/yassg_routes.yaml @@ -13,6 +13,10 @@ sigwin_yassg: catalog: # map results as an array _locale: "yassg_find_all('locale')['isoCode']" + article: + path: /{_locale}/a/{slug} + catalog: + slug: "yassg_find_all('articles').slug" product: path: /{_locale}/{slug} catalog: diff --git a/tests/functional/site/content/articles/hello-world.md b/tests/functional/site/content/articles/hello-world.md new file mode 100644 index 0000000..5f0a73b --- /dev/null +++ b/tests/functional/site/content/articles/hello-world.md @@ -0,0 +1,18 @@ +--- +slug: hello-world +--- +# Hello World! + +here I am, writing some Markdown. + +Fine. + +```php{4} +class HelloWorld +{ + public function __construct() + { + echo 'Hello World!'; + } +} +``` diff --git a/tests/functional/site/fixtures/assets/app.2e773b84.css b/tests/functional/site/fixtures/assets/app.2e773b84.css deleted file mode 100644 index bd46c54..0000000 --- a/tests/functional/site/fixtures/assets/app.2e773b84.css +++ /dev/null @@ -1 +0,0 @@ -body{background-color:#eee} \ No newline at end of file diff --git a/tests/functional/site/fixtures/assets/app.d4da4e7e.css b/tests/functional/site/fixtures/assets/app.d4da4e7e.css new file mode 100644 index 0000000..1d893ff --- /dev/null +++ b/tests/functional/site/fixtures/assets/app.d4da4e7e.css @@ -0,0 +1 @@ +body{background-color:#eee}.hljs-comment,.hljs-quote{color:#d4d0ab}.hljs-deletion,.hljs-name,.hljs-regexp,.hljs-selector-class,.hljs-selector-id,.hljs-tag,.hljs-template-variable,.hljs-variable{color:#ffa07a}.hljs-built_in,.hljs-builtin-name,.hljs-link,.hljs-literal,.hljs-meta,.hljs-number,.hljs-params,.hljs-type{color:#f5ab35}.hljs-attribute{color:gold}.hljs-addition,.hljs-bullet,.hljs-string,.hljs-symbol{color:#abe338}.hljs-section,.hljs-title{color:#00e0e0}.hljs-keyword,.hljs-selector-tag{color:#dcc6e0}.hljs{background:#2b2b2b;color:#f8f8f2;display:block;overflow-x:auto;padding:.5em}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}@media screen and (-ms-high-contrast:active){.hljs-addition,.hljs-attribute,.hljs-built_in,.hljs-builtin-name,.hljs-bullet,.hljs-comment,.hljs-link,.hljs-literal,.hljs-meta,.hljs-number,.hljs-params,.hljs-quote,.hljs-string,.hljs-symbol,.hljs-type{color:highlight}.hljs-keyword,.hljs-selector-tag{font-weight:700}} \ No newline at end of file diff --git a/tests/functional/site/fixtures/assets/entrypoints.json b/tests/functional/site/fixtures/assets/entrypoints.json index 9a52a0f..50abd5f 100644 --- a/tests/functional/site/fixtures/assets/entrypoints.json +++ b/tests/functional/site/fixtures/assets/entrypoints.json @@ -6,13 +6,13 @@ "/sub/dir/another/assets/app.9267444a.js" ], "css": [ - "/sub/dir/another/assets/app.2e773b84.css" + "/sub/dir/another/assets/app.d4da4e7e.css" ] } }, "integrity": { "/sub/dir/another/assets/runtime.4d1205b9.js": "sha384-J2jFm3DPgrcqyvls0fZlS51nUYgiBB+JLwWiU5v7vxEbLwKolmaHrRuz4BjP6qaE", "/sub/dir/another/assets/app.9267444a.js": "sha384-WbAUi92ziCGFD1kGZRzpOP8Nxymnw5vXM3szOB1JmBrCU+MVGXBRReCttizXHDdI", - "/sub/dir/another/assets/app.2e773b84.css": "sha384-t56F6yDakn/jYRHL6uxVbMaf/JtnF0k7yEall1cerAGjo0kfE3W7foG5+n1Ynxd5" + "/sub/dir/another/assets/app.d4da4e7e.css": "sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ" } } \ No newline at end of file diff --git a/tests/functional/site/fixtures/assets/manifest.json b/tests/functional/site/fixtures/assets/manifest.json index 8ed9631..4c2a3e6 100644 --- a/tests/functional/site/fixtures/assets/manifest.json +++ b/tests/functional/site/fixtures/assets/manifest.json @@ -1,5 +1,5 @@ { - "assets/app.css": "/sub/dir/another/assets/app.2e773b84.css", + "assets/app.css": "/sub/dir/another/assets/app.d4da4e7e.css", "assets/app.js": "/sub/dir/another/assets/app.9267444a.js", "assets/runtime.js": "/sub/dir/another/assets/runtime.4d1205b9.js", "assets/images/sigwin.svg": "/sub/dir/another/assets/images/sigwin.6f9a3d5b.svg" diff --git a/tests/functional/site/fixtures/de/index.html b/tests/functional/site/fixtures/de/index.html index 091f89b..2a71b09 100644 --- a/tests/functional/site/fixtures/de/index.html +++ b/tests/functional/site/fixtures/de/index.html @@ -7,7 +7,7 @@ Homepage - + diff --git a/tests/functional/site/fixtures/en/a/hello-world/index.html b/tests/functional/site/fixtures/en/a/hello-world/index.html new file mode 100644 index 0000000..897a08d --- /dev/null +++ b/tests/functional/site/fixtures/en/a/hello-world/index.html @@ -0,0 +1,35 @@ + + + + + + + + hello-world + + + + + +
+
+
+

Hello World!

+

here I am, writing some Markdown.

+

Fine.

+
class HelloWorld
+{
+    public function __construct()
+    {
+        echo 'Hello World!';
+    }
+}
+
+ +
+
+
+ + + + diff --git a/tests/functional/site/fixtures/en/example1/index.html b/tests/functional/site/fixtures/en/example1/index.html index 6a5da6e..2069c23 100644 --- a/tests/functional/site/fixtures/en/example1/index.html +++ b/tests/functional/site/fixtures/en/example1/index.html @@ -7,7 +7,7 @@ Example 1 - + diff --git a/tests/functional/site/fixtures/en/example2/index.html b/tests/functional/site/fixtures/en/example2/index.html index cb5361f..957d6a8 100644 --- a/tests/functional/site/fixtures/en/example2/index.html +++ b/tests/functional/site/fixtures/en/example2/index.html @@ -7,7 +7,7 @@ Example 2 - + diff --git a/tests/functional/site/fixtures/en/index.html b/tests/functional/site/fixtures/en/index.html index 6576165..0d8f3bc 100644 --- a/tests/functional/site/fixtures/en/index.html +++ b/tests/functional/site/fixtures/en/index.html @@ -7,7 +7,7 @@ Homepage - + diff --git a/tests/functional/site/fixtures/en/nested-example/index.html b/tests/functional/site/fixtures/en/nested-example/index.html index c553c0b..fe9e8aa 100644 --- a/tests/functional/site/fixtures/en/nested-example/index.html +++ b/tests/functional/site/fixtures/en/nested-example/index.html @@ -7,7 +7,7 @@ Nested example - + diff --git a/tests/functional/site/fixtures/hr/index.html b/tests/functional/site/fixtures/hr/index.html index 052ed02..46203b6 100644 --- a/tests/functional/site/fixtures/hr/index.html +++ b/tests/functional/site/fixtures/hr/index.html @@ -7,7 +7,7 @@ Homepage - + diff --git a/tests/functional/site/fixtures/index.html b/tests/functional/site/fixtures/index.html index d045c1a..42dc523 100644 --- a/tests/functional/site/fixtures/index.html +++ b/tests/functional/site/fixtures/index.html @@ -7,7 +7,7 @@ Index - + @@ -32,6 +32,9 @@

Index

/sub/dir/another/hr/
/sub/dir/another/en/
+
article
+ +
/sub/dir/another/en/a/hello-world/
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 new file mode 100644 index 0000000..0524939 --- /dev/null +++ b/tests/functional/site/src/Model/Article.php @@ -0,0 +1,20 @@ + +
+
+ {{ article.body|raw }} +
+
+ +{% endblock %} diff --git a/tests/functional/site/webpack.config.js b/tests/functional/site/webpack.config.js index 890483b..e223338 100644 --- a/tests/functional/site/webpack.config.js +++ b/tests/functional/site/webpack.config.js @@ -19,6 +19,7 @@ Encore options.liveReload = true; options.hot = true; options.watchFiles = [ + './content/**/*', './templates/**/*', ] })