parse markdown

This commit is contained in:
2025-04-10 08:49:24 +04:00
parent 61d81db7bf
commit c970291049
3 changed files with 121 additions and 4 deletions
+73 -1
View File
@@ -236,7 +236,79 @@
line-height: 1.6;
margin-bottom: 1.5rem;
color: #333;
white-space: pre-line;
}
/* Styles pour les éléments Markdown dans la description */
.video-description p {
margin-bottom: 1rem;
}
.video-description ol,
.video-description ul {
margin-bottom: 1rem;
padding-left: 1.5rem;
}
.video-description li {
margin-bottom: 0.5rem;
}
.video-description a {
color: var(--primary-red);
text-decoration: none;
transition: color 0.2s;
}
.video-description a:hover {
text-decoration: underline;
}
.video-description blockquote {
border-left: 4px solid #e0e0e0;
padding-left: 1rem;
margin-left: 0;
margin-right: 0;
margin-bottom: 1rem;
color: #555;
font-style: italic;
}
.video-description code {
background-color: #f5f5f5;
padding: 0.2rem 0.4rem;
border-radius: 3px;
font-family: monospace;
font-size: 0.875rem;
}
.video-description pre {
background-color: #f5f5f5;
padding: 1rem;
border-radius: 4px;
overflow-x: auto;
margin-bottom: 1rem;
}
.video-description pre code {
background-color: transparent;
padding: 0;
border-radius: 0;
display: block;
white-space: pre;
}
.video-description hr {
border: 0;
height: 1px;
background-color: #e0e0e0;
margin: 2rem 0;
}
.video-description .markdown-image {
max-width: 100%;
height: auto;
border-radius: 4px;
margin: 1rem 0;
}
.truncated-description, .full-description {
+43
View File
@@ -0,0 +1,43 @@
<?php
/**
* Fonction simple pour convertir du texte Markdown en HTML
* Adaptée pour PeerTube qui ne prend en charge qu'un sous-ensemble limité d'éléments:
* - Emphase (italique/gras)
* - Liens
* - Retours à la ligne
* - Listes
*
* @param string $markdown Le texte au format Markdown à convertir
* @return string Le texte converti en HTML
*/
function markdown_to_html($markdown) {
// Nettoyage du texte et sécurité
$markdown = htmlspecialchars($markdown, ENT_QUOTES, 'UTF-8');
// 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);
// Conversion des listes à puces
$markdown = preg_replace('/^- (.*?)$/m', '<li>$1</li>', $markdown);
$markdown = preg_replace('/(<li>.*?<\/li>\n?)+/s', '<ul>$0</ul>', $markdown);
// Conversion des listes numérotées
$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);
return $markdown;
}
+5 -3
View File
@@ -1,6 +1,8 @@
<?php
// Inclure la configuration
require_once 'includes/config.php';
// Inclure le convertisseur Markdown
require_once 'includes/lib/markdown.php';
// Récupérer l'ID de la vidéo depuis l'URL
$videoId = isset($_GET['id']) ? $_GET['id'] : '';
@@ -266,21 +268,21 @@ if (empty($videoData) || isset($videoData['error'])) {
if ($hasTruncatedDesc):
?>
<div class="truncated-description">
<?php echo nl2br(htmlspecialchars($video['truncatedDescription'])); ?>
<?php echo markdown_to_html($video['truncatedDescription']); ?>
<button class="show-more-btn">
<span>Voir plus</span>
<i class="fas fa-chevron-down"></i>
</button>
</div>
<div class="full-description" style="display: none;">
<?php echo nl2br(htmlspecialchars($video['description'])); ?>
<?php echo markdown_to_html($video['description']); ?>
<button class="show-less-btn">
<span>Voir moins</span>
<i class="fas fa-chevron-up"></i>
</button>
</div>
<?php else: ?>
<?php echo nl2br(htmlspecialchars($video['description'])); ?>
<?php echo markdown_to_html($video['description']); ?>
<?php endif; ?>
</div>