parse links
This commit is contained in:
@@ -11,9 +11,43 @@
|
||||
* @return string Le texte converti en HTML
|
||||
*/
|
||||
function markdown_to_html($markdown) {
|
||||
// Nettoyage du texte et sécurité
|
||||
// Échapper tout le contenu pour éviter les injections XSS
|
||||
$markdown = htmlspecialchars($markdown, ENT_QUOTES, 'UTF-8');
|
||||
|
||||
// Tableau pour stocker les liens convertis
|
||||
$links = [];
|
||||
$link_count = 0;
|
||||
|
||||
// Conversion des liens Markdown [texte](url)
|
||||
$markdown = preg_replace_callback('/\[([^\]]+)\]\(([^)]+)\)/s', function($matches) use (&$links, &$link_count) {
|
||||
$text = $matches[1];
|
||||
$url = $matches[2];
|
||||
|
||||
// Assurer que l'URL est correctement formée
|
||||
if (!preg_match('/^https?:\/\//i', $url)) {
|
||||
// Ajouter http:// si l'URL ne commence pas par http:// ou https://
|
||||
$url = 'http://' . $url;
|
||||
}
|
||||
|
||||
$placeholder = "___LINK_{$link_count}___";
|
||||
$links[$placeholder] = '<a href="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" target="_blank" rel="noopener noreferrer">' . $text . '</a>';
|
||||
$link_count++;
|
||||
|
||||
return $placeholder;
|
||||
}, $markdown);
|
||||
|
||||
// Détection et conversion des URLs brutes en liens cliquables
|
||||
$urlPattern = '/(https?:\/\/[^\s<]+[^\s<\.)])/i';
|
||||
$markdown = preg_replace_callback($urlPattern, function($matches) use (&$links, &$link_count) {
|
||||
$url = $matches[1];
|
||||
|
||||
$placeholder = "___LINK_{$link_count}___";
|
||||
$links[$placeholder] = '<a href="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" target="_blank" rel="noopener noreferrer">' . $url . '</a>';
|
||||
$link_count++;
|
||||
|
||||
return $placeholder;
|
||||
}, $markdown);
|
||||
|
||||
// Conversion du texte en gras et italique (**texte** et *texte*)
|
||||
$markdown = preg_replace('/\*\*(.*?)\*\*/s', '<strong>$1</strong>', $markdown);
|
||||
$markdown = preg_replace('/\*(.*?)\*/s', '<em>$1</em>', $markdown);
|
||||
@@ -26,18 +60,16 @@ function markdown_to_html($markdown) {
|
||||
$markdown = preg_replace('/^\d+\. (.*?)$/m', '<li>$1</li>', $markdown);
|
||||
$markdown = preg_replace('/(<li>.*?<\/li>\n?)+/s', '<ol>$0</ol>', $markdown);
|
||||
|
||||
// Conversion des liens [texte](url)
|
||||
$markdown = preg_replace('/\[([^\]]+)\]\(([^)]+)\)/', '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>', $markdown);
|
||||
|
||||
// Détection et conversion des URLs brutes en liens cliquables
|
||||
$urlPattern = '/(https?:\/\/[^\s<]+[^\s<\.)])/i';
|
||||
$markdown = preg_replace($urlPattern, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>', $markdown);
|
||||
|
||||
// Gestion des retours à la ligne
|
||||
$markdown = nl2br($markdown);
|
||||
|
||||
// Nettoyage des balises br dans les listes
|
||||
$markdown = preg_replace('/<\/li><br \/>/', '</li>', $markdown);
|
||||
|
||||
|
||||
// Restaurer les liens
|
||||
foreach ($links as $placeholder => $link) {
|
||||
$markdown = str_replace($placeholder, $link, $markdown);
|
||||
}
|
||||
|
||||
return $markdown;
|
||||
}
|
||||
Reference in New Issue
Block a user