FIX Refactoring and enhancement
All checks were successful
continuous-integration/drone/push Build is passing

of the regex build_footnotes_index_from_content and apply_footnotes_urls_to_content functions
This commit is contained in:
Nonimart 2025-08-28 11:43:41 +02:00
parent 7e80835b27
commit 9fb13847d0

View File

@ -144,18 +144,19 @@ function build_footnotes_index_from_content($content)
} }
$footnotes = []; $footnotes = [];
$dom = new DOMDocument(); // Trouve les balises <a> avec classe footnote-reference ET attribut footnote-content (ordre flexible)
// On supprime les erreurs de parsing pour le HTML5 $pattern = '/<a[^>]*class="[^"]*footnote-reference[^"]*"[^>]*footnote-content="([^"]*)"[^>]*>|<a[^>]*footnote-content="([^"]*)"[^>]*class="[^"]*footnote-reference[^"]*"[^>]*>/i';
@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $has_footnotes = preg_match_all($pattern, $content, $matches, PREG_SET_ORDER);
$links = $dom->getElementsByTagName('a'); if ($has_footnotes) {
foreach ($links as $key => $link) { foreach ($matches as $index => $match) {
if ($link->hasAttribute('class') && strpos($link->getAttribute('class'), 'footnote-reference') !== false) { write_log($match);
$footnote_content = $link->getAttribute('footnote-content'); // Le contenu peut être dans match[1] ou match[2] selon l'ordre des attributs
$footnote_content = !empty($match[1]) ? $match[1] : $match[2];
if (!empty($footnote_content)) { if (!empty($footnote_content)) {
$footnotes[] = array( $footnotes[] = array(
'key' => $key + 1, 'key' => $index + 1,
'anchorID' => $key + 1, 'anchorID' => $index + 1,
'content' => $footnote_content 'content' => $footnote_content
); );
} }
@ -165,27 +166,25 @@ function build_footnotes_index_from_content($content)
return $footnotes; return $footnotes;
} }
add_filter('the_content', 'apply_footnotes_urls_to_content', 10);
function apply_footnotes_urls_to_content($content) function apply_footnotes_urls_to_content($content)
{ {
$post_type = get_post_type(); $post_type = get_post_type();
if ($post_type !== 'articles' && !is_admin()) return $content; if ($post_type !== 'articles' && !is_admin()) return $content;
// Compter les références de notes de bas de page
$footnote_count = 0;
$footnotes = build_footnotes_index_from_content($content); // Utiliser preg_replace_callback pour traiter chaque occurrence individuellement
$dom = new DOMDocument(); $content = preg_replace_callback(
@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); '/<a([^>]*class="[^"]*footnote-reference[^"]*"[^>]*)>/i',
function ($matches) use (&$footnote_count) {
$footnote_count++;
return '<a id="footnote-' . $footnote_count . '" ' . $matches[1] . '>';
},
$content
);
$links = $dom->getElementsByTagName('a'); return $content;
foreach ($links as $key => $link) {
if ($link->hasAttribute('class') && strpos($link->getAttribute('class'), 'footnote-reference') !== false) {
$link->setAttribute('id', 'footnote-' . ($key + 1));
}
}
return $dom->saveHTML();
} }
add_filter('the_content', 'apply_footnotes_urls_to_content', 10);