From be8d2d0774fa7e5244aae2da053419d1a6184d51 Mon Sep 17 00:00:00 2001 From: bidi47 Date: Wed, 7 Oct 2026 15:04:14 +0300 Subject: [PATCH] updated markdown to html - tables, faq Signed-off-by: bidi47 --- src/App/assets/scss/components/_blog.scss | 51 +++++++++++++++++++ src/App/src/Service/FaqExtractor.php | 22 ++++++-- src/App/templates/layout/blog-post.html.twig | 2 +- .../templates/page/markdown-article.html.twig | 4 +- test/Unit/App/Service/FaqExtractorTest.php | 26 +++++++++- vite.config.js | 2 +- 6 files changed, 97 insertions(+), 10 deletions(-) diff --git a/src/App/assets/scss/components/_blog.scss b/src/App/assets/scss/components/_blog.scss index 183ca6a0..ebaa6e87 100644 --- a/src/App/assets/scss/components/_blog.scss +++ b/src/App/assets/scss/components/_blog.scss @@ -240,6 +240,57 @@ $bp-tablet: 900px; font-size: inherit; } } + + // Markdown tables render as bare elements, so they are styled by tag. + // display:block lets a wide table scroll sideways instead of overflowing the column. + table { + display: block; + width: 100%; + max-width: 100%; + margin: 0 0 24px; + overflow-x: auto; + border: 1px solid var(--border); + border-radius: 8px; + border-collapse: collapse; + font-size: 14.5px; + background: var(--surface); + + th, + td { + padding: 12px 16px; + text-align: left; + vertical-align: top; + border-bottom: 1px solid var(--border); + color: var(--text); + } + + thead th { + font-family: var(--mono); + font-size: 12px; + letter-spacing: 0.06em; + text-transform: uppercase; + white-space: nowrap; + color: var(--text-dim); + background: var(--surface-2); + } + + tbody tr:last-child td, + tbody tr:last-child th { + border-bottom: 0; + } + + tbody th, + td:first-child { + font-weight: 500; + color: var(--text-muted); + } + + p { + margin-bottom: 0; + font-size: inherit; + line-height: 1.6; + } + } } // ---------- Obsolete post warning ---------- diff --git a/src/App/src/Service/FaqExtractor.php b/src/App/src/Service/FaqExtractor.php index 3bae9438..4976cad7 100644 --- a/src/App/src/Service/FaqExtractor.php +++ b/src/App/src/Service/FaqExtractor.php @@ -14,12 +14,16 @@ /** * Pulls the trailing `## FAQ` section (a `**Q: ...**` / `A: ...` pair per question) out of a * Markdown article body, so it can be rendered as an accordion and fed into JSON-LD separately - * from the rest of the body. + * from the rest of the body. `question` and `answer` keep their Markdown (for rendering); + * `questionText` and `answerText` are plain-text copies (for JSON-LD). */ final class FaqExtractor { /** - * @return array{faq: list, body: string} + * @return array{ + * faq: list, + * body: string + * } */ public static function extract(string $body): array { @@ -35,9 +39,14 @@ public static function extract(string $body): array PREG_SET_ORDER ); foreach ($matches as $match) { + $question = trim($match[1]); + $answer = trim(preg_replace('/\s+/', ' ', $match[2]) ?? ''); + $faq[] = [ - 'question' => trim($match[1]), - 'answer' => trim(preg_replace('/\s+/', ' ', $match[2]) ?? ''), + 'question' => $question, + 'answer' => $answer, + 'questionText' => self::toPlainText($question), + 'answerText' => self::toPlainText($answer), ]; } @@ -45,4 +54,9 @@ public static function extract(string $body): array return ['faq' => $faq, 'body' => $body]; } + + private static function toPlainText(string $markdown): string + { + return preg_replace('/(`+)(.+?)\1/', '$2', $markdown) ?? $markdown; + } } diff --git a/src/App/templates/layout/blog-post.html.twig b/src/App/templates/layout/blog-post.html.twig index 7013570b..3d675b56 100644 --- a/src/App/templates/layout/blog-post.html.twig +++ b/src/App/templates/layout/blog-post.html.twig @@ -45,7 +45,7 @@ "@id": "{{ app.url ~ path('page::blog-resource', {categorySlug: article.category.slug, slug: article.slug}) }}#faq", "mainEntity": [ {% for item in faq %} - { "@type": "Question", "name": {{ item.question|json_encode|raw }}, "acceptedAnswer": { "@type": "Answer", "text": {{ item.answer|json_encode|raw }} } }{% if not loop.last %},{% endif %} + { "@type": "Question", "name": {{ item.questionText|json_encode|raw }}, "acceptedAnswer": { "@type": "Answer", "text": {{ item.answerText|json_encode|raw }} } }{% if not loop.last %},{% endif %} {% endfor %} ] }{% endif %} diff --git a/src/Blog/templates/page/markdown-article.html.twig b/src/Blog/templates/page/markdown-article.html.twig index 53ce183f..e6eea987 100644 --- a/src/Blog/templates/page/markdown-article.html.twig +++ b/src/Blog/templates/page/markdown-article.html.twig @@ -9,9 +9,9 @@
{% for item in faq %}
- {{ item.question }} + + {{ item.question|markdown_to_html|striptags('')|raw }} +
{% endfor %} diff --git a/test/Unit/App/Service/FaqExtractorTest.php b/test/Unit/App/Service/FaqExtractorTest.php index fbe57ebb..9a860a65 100644 --- a/test/Unit/App/Service/FaqExtractorTest.php +++ b/test/Unit/App/Service/FaqExtractorTest.php @@ -20,13 +20,35 @@ public function testExtractPullsTheQuestionAnswerPairsOutOfTheFaqSection(): void $this->assertSame('Some intro text.', $result['body']); $this->assertSame( [ - ['question' => 'What is it?', 'answer' => "It's an example."], - ['question' => 'Why does it matter?', 'answer' => 'Because tests need coverage.'], + [ + 'question' => 'What is it?', + 'answer' => "It's an example.", + 'questionText' => 'What is it?', + 'answerText' => "It's an example.", + ], + [ + 'question' => 'Why does it matter?', + 'answer' => 'Because tests need coverage.', + 'questionText' => 'Why does it matter?', + 'answerText' => 'Because tests need coverage.', + ], ], $result['faq'] ); } + public function testExtractKeepsMarkdownForRenderingAndStripsBackticksForPlainText(): void + { + $body = "## FAQ\n\n**Q: Is `dot-cache` needed?**\nA: Use `dot-cache` and ``dot-cli``."; + + $result = FaqExtractor::extract($body); + + $this->assertSame('Is `dot-cache` needed?', $result['faq'][0]['question']); + $this->assertSame('Use `dot-cache` and ``dot-cli``.', $result['faq'][0]['answer']); + $this->assertSame('Is dot-cache needed?', $result['faq'][0]['questionText']); + $this->assertSame('Use dot-cache and dot-cli.', $result['faq'][0]['answerText']); + } + public function testExtractReturnsAnEmptyFaqWhenThereIsNoFaqSection(): void { $body = "Some intro text.\n\nMore content."; diff --git a/vite.config.js b/vite.config.js index 6e2bd774..cf7e5c69 100644 --- a/vite.config.js +++ b/vite.config.js @@ -20,10 +20,10 @@ export default defineConfig({ }), ViteMinifyPlugin({}), ], - emptyOutDir: true, root: path.resolve(__dirname, 'src'), // Set the root directory for Vite build: { outDir: '../public', // Output directory for compiled assets + emptyOutDir: false, rollupOptions: { input: { main: '/App/assets/js/index.js', // Main JavaScript entry point