Skip to content

Commit

Permalink
feat: Markdown (#125)
Browse files Browse the repository at this point in the history
* feat: Markdown
* feat: Markdown, front matter reader
* feat: Markdown, code block highlighting
  • Loading branch information
dkarlovi authored Jul 18, 2022
1 parent 7d4a677 commit fc5e052
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 12 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion src/Decoder/CachingFileDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
55 changes: 55 additions & 0 deletions src/Decoder/MarkdownFileDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/*
* This file is part of the yassg project.
*
* (c) sigwin.hr
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sigwin\YASSG\Decoder;

use League\CommonMark\ConverterInterface;
use League\CommonMark\Extension\FrontMatter\FrontMatterProviderInterface;
use Sigwin\YASSG\FileDecoder;

final class MarkdownFileDecoder implements FileDecoder
{
use FileDecoderTrait;

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

private ConverterInterface $converter;

public function __construct(ConverterInterface $converter)
{
$this->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<string, string> $metadata */
$metadata = $result->getFrontMatter();
}
$metadata['body'] = $result->getContent();

return $metadata;
}
}
1 change: 1 addition & 0 deletions tests/functional/site/assets/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './styles/app.scss';
import '../../../../vendor/scrivo/highlight.php/styles/a11y-dark.css';

document.addEventListener('DOMContentLoaded', () => {
console.log('DOM ready!');
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/site/config/packages/yassg_databases.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/site/config/packages/yassg_routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/site/content/articles/hello-world.md
Original file line number Diff line number Diff line change
@@ -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!';
}
}
```
1 change: 0 additions & 1 deletion tests/functional/site/fixtures/assets/app.2e773b84.css

This file was deleted.

1 change: 1 addition & 0 deletions tests/functional/site/fixtures/assets/app.d4da4e7e.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/functional/site/fixtures/assets/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion tests/functional/site/fixtures/assets/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/site/fixtures/de/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<title>Homepage</title>

<link rel="stylesheet" href="/sub/dir/another/assets/app.2e773b84.css" integrity="sha384-t56F6yDakn/jYRHL6uxVbMaf/JtnF0k7yEall1cerAGjo0kfE3W7foG5+n1Ynxd5">
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
</head>
<body>

Expand Down
35 changes: 35 additions & 0 deletions tests/functional/site/fixtures/en/a/hello-world/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.tailwindcss.com?plugins=typography"></script>

<title>hello-world</title>

<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
</head>
<body>

<main class="min-h-full flex flex-col justify-center py-12 sm:px-6 lg:px-8">
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
<h1>Hello World!</h1>
<p>here I am, writing some Markdown.</p>
<p>Fine.</p>
<pre><code class="language-php hljs php" data-lang="php"><span class="loc"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HelloWorld</span></span></span>
<span class="loc"><span class="hljs-class"></span>{</span>
<span class="loc"> <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span><span class="hljs-params">()</span></span></span>
<span class="loc highlighted"><span class="hljs-function"> </span>{</span>
<span class="loc"> <span class="hljs-keyword">echo</span> <span class="hljs-string">'Hello World!'</span>;</span>
<span class="loc"> }</span>
<span class="loc">}</span>
<span class="loc"></span></code></pre>

</div>
</div>
</main>

<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>
</body>
</html>
2 changes: 1 addition & 1 deletion tests/functional/site/fixtures/en/example1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<title>Example 1</title>

<link rel="stylesheet" href="/sub/dir/another/assets/app.2e773b84.css" integrity="sha384-t56F6yDakn/jYRHL6uxVbMaf/JtnF0k7yEall1cerAGjo0kfE3W7foG5+n1Ynxd5">
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
</head>
<body>

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/site/fixtures/en/example2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<title>Example 2</title>

<link rel="stylesheet" href="/sub/dir/another/assets/app.2e773b84.css" integrity="sha384-t56F6yDakn/jYRHL6uxVbMaf/JtnF0k7yEall1cerAGjo0kfE3W7foG5+n1Ynxd5">
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
</head>
<body>

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/site/fixtures/en/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<title>Homepage</title>

<link rel="stylesheet" href="/sub/dir/another/assets/app.2e773b84.css" integrity="sha384-t56F6yDakn/jYRHL6uxVbMaf/JtnF0k7yEall1cerAGjo0kfE3W7foG5+n1Ynxd5">
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
</head>
<body>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<title>Nested example</title>

<link rel="stylesheet" href="/sub/dir/another/assets/app.2e773b84.css" integrity="sha384-t56F6yDakn/jYRHL6uxVbMaf/JtnF0k7yEall1cerAGjo0kfE3W7foG5+n1Ynxd5">
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
</head>
<body>

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/site/fixtures/hr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<title>Homepage</title>

<link rel="stylesheet" href="/sub/dir/another/assets/app.2e773b84.css" integrity="sha384-t56F6yDakn/jYRHL6uxVbMaf/JtnF0k7yEall1cerAGjo0kfE3W7foG5+n1Ynxd5">
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
</head>
<body>

Expand Down
5 changes: 4 additions & 1 deletion tests/functional/site/fixtures/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<title>Index</title>

<link rel="stylesheet" href="/sub/dir/another/assets/app.2e773b84.css" integrity="sha384-t56F6yDakn/jYRHL6uxVbMaf/JtnF0k7yEall1cerAGjo0kfE3W7foG5+n1Ynxd5">
<link rel="stylesheet" href="/sub/dir/another/assets/app.d4da4e7e.css" integrity="sha384-t9qUhz8OfBEKvYdnnNndPZAf+S5Q3p0dBnxLn+5SShoNhInHUF+Bu8WJTo+FQJYJ">
</head>
<body>

Expand All @@ -32,6 +32,9 @@ <h1>Index</h1>
<dd><a href="/sub/dir/another/hr/">/sub/dir/another/hr/</a></dd>

<dd><a href="/sub/dir/another/en/">/sub/dir/another/en/</a></dd>
<dt>article</dt>

<dd><a href="/sub/dir/another/en/a/hello-world/">/sub/dir/another/en/a/hello-world/</a></dd>
<dt>product</dt>

<dd><a href="/sub/dir/another/en/nested-example/">/sub/dir/another/en/nested-example/</a></dd>
Expand Down
20 changes: 20 additions & 0 deletions tests/functional/site/src/Model/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/*
* This file is part of the yassg project.
*
* (c) sigwin.hr
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sigwin\YASSG\Test\Functional\Site\Model;

final class Article
{
public string $slug;
public string $body;
}
16 changes: 16 additions & 0 deletions tests/functional/site/templates/pages/article.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'layout.html.twig' %}

{# query the database #}
{% set article = yassg_find_one_by('articles', {condition: {'item.slug': slug}}) %}

{% block title %}{{ article.slug }}{% endblock %}

{% block body %}
<main class="min-h-full flex flex-col justify-center py-12 sm:px-6 lg:px-8">
<div class="mt-8 sm:mx-auto sm:w-full sm:max-w-xl">
<div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10 prose">
{{ article.body|raw }}
</div>
</div>
</main>
{% endblock %}
1 change: 1 addition & 0 deletions tests/functional/site/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Encore
options.liveReload = true;
options.hot = true;
options.watchFiles = [
'./content/**/*',
'./templates/**/*',
]
})
Expand Down

0 comments on commit fc5e052

Please sign in to comment.