Sangfroid 2 месяцев назад
Родитель
Сommit
b37830cd67

+ 1 - 1
assets/controllers/simplemde_controller.js

@@ -2,6 +2,6 @@ import { Controller } from '@hotwired/stimulus';
 
 export default class extends Controller {
     connect() {
-        const simplemde = new SimpleMDE({ element: document.querySelector('textarea') });
+        const simplemde = new SimpleMDE({ element: document.querySelector('textarea'), spellChecker: false });
     }
 }

+ 47 - 0
src/Service/MarkdownParser.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace App\Service;
+
+use League\CommonMark\CommonMarkConverter;
+
+class MarkdownParser
+{
+    protected CommonMarkConverter $converter;
+
+    public function __construct()
+    {
+        $this->converter = new CommonMarkConverter();
+    }
+
+    public function convertToHtml(string $markdown): string
+    {
+        // Convertit le Markdown en HTML
+        $html = $this->converter->convert($markdown);
+
+        // Remplace les liens YouTube par des miniatures
+        $html = $this->replaceYoutubeLinks($html);
+
+        return $html;
+    }
+
+    private function replaceYoutubeLinks(string $html): string
+    {
+        // Regex pour détecter les balises <a> avec des liens YouTube
+        $pattern = '/<a[^>]+href="https?:\/\/(?:www\.)?(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]+)"[^>]*>.*?<\/a>/';
+
+        // Remplacement par un embed vidéo
+        return preg_replace_callback($pattern, function ($matches) {
+            $videoId = $matches[1];
+            return <<<HTML
+            <div style="position:relative; padding-bottom:56.25%; height:0; overflow:hidden; max-width:100%; background:#000;">
+                <iframe src="https://www.youtube-nocookie.com/embed/{$videoId}" 
+                        frameborder="0" 
+                        allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
+                        allowfullscreen 
+                        style="position:absolute; top:0; left:0; width:100%; height:100%;">
+                </iframe>
+            </div>
+            HTML;
+        }, $html);
+    }
+}

+ 29 - 0
src/Twig/MarkdownExtension.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Twig;
+
+use App\Service\MarkdownParser;
+use Twig\Extension\AbstractExtension;
+use Twig\TwigFilter;
+
+class MarkdownExtension extends AbstractExtension
+{
+    public function __construct(
+        protected readonly MarkdownParser $markdownParser
+    )
+
+    {
+        
+    }
+    public function getFilters()
+    {
+        return [
+            new TwigFilter('markdown', [$this, 'parse'], ['is_safe' => ['html']])
+        ];
+    }
+
+    public function parse(string $content): string
+    {
+        return $this->markdownParser->convertToHtml($content);
+    }
+}

+ 1 - 3
templates/article/show.html.twig

@@ -27,9 +27,7 @@
                 <th>Contenu</th>
                 
                 <td>
-                    {% apply markdown_to_html %}
-                        {{ article.content }}
-                    {% endapply %}
+                    {{ article.content | markdown }}
                 </td>
             </tr>
         </tbody>

+ 1 - 3
templates/index/index.html.twig

@@ -10,9 +10,7 @@
             <article class="article-preview">
                 <h2><a href="{{ path('app_view', {'id': article.id}) }}">{{ article.title}}</a><span class="article-by">{{ article.publicationDate | date('d-m-Y')}} {{ article.author }}</span></h2>
                 <div class="contenu">
-                    {% apply markdown_to_html %}
-                    {{ article.content }}
-                    {% endapply %}
+                    {{ article.content | markdown }}
                 </div>
             </article>
             <a href="{{ path('app_view', {'id': article.id}) }}">Voir l'article</a>

+ 1 - 3
templates/page/index.html.twig

@@ -9,9 +9,7 @@
 {% endif %}
 <h1>{{ page.title }}</h1>
 <div class="contenu">
-    {% apply markdown_to_html %}
-        {{ page.content }}
-    {% endapply %}
+        {{ page.content | markdown }}
 </div>
 
 

+ 1 - 3
templates/view/index.html.twig

@@ -10,9 +10,7 @@
             <h2>{{ article.title}}<span class="article-by">{{ article.publicationDate | date('d-m-Y')}} {{ article.author }}</span></h2>
         </header>
         <section class="contenu">
-            {% apply markdown_to_html %}
-                {{ article.content }}
-            {% endapply %}
+                {{ article.content | markdown }}
         </section>
         <footer></footer>
     </article>