|  | @@ -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);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |