handle link without prefix

This commit is contained in:
2025-04-10 09:11:45 +04:00
parent d0afd88031
commit f85fe59381
+21 -2
View File
@@ -37,8 +37,9 @@ function markdown_to_html($markdown) {
}, $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) {
// 1. URLs avec protocole http/https
$protocolUrlPattern = '/(https?:\/\/[^\s<]+[^\s<\.)])/i';
$markdown = preg_replace_callback($protocolUrlPattern, function($matches) use (&$links, &$link_count) {
$url = $matches[1];
$placeholder = "___LINK_{$link_count}___";
@@ -48,6 +49,24 @@ function markdown_to_html($markdown) {
return $placeholder;
}, $markdown);
// 2. Domaines sans protocole (comme "o-k-i.net", "gong.gp", "NUVEL.NU")
// Détecte les domaines avec TLD communs qui ne font pas partie d'autre chose
$domainPattern = '/\b([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+([a-zA-Z]{2,63})\b/';
$markdown = preg_replace_callback($domainPattern, function($matches) use (&$links, &$link_count) {
$domain = $matches[0];
// Éviter de convertir des éléments qui ressemblent à des versions/numéros ou qui sont déjà dans des liens
if (preg_match('/^v?\d+\.\d+/', $domain) || strpos($domain, '___LINK_') !== false) {
return $domain;
}
$placeholder = "___LINK_{$link_count}___";
$links[$placeholder] = '<a href="http://' . htmlspecialchars($domain, ENT_QUOTES, 'UTF-8') . '" target="_blank" rel="noopener noreferrer">' . $domain . '</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);