FEATURE Introducing index builder footnotes utilities
This commit is contained in:
parent
d1b3ffd81c
commit
49ab88505b
|
|
@ -46,3 +46,82 @@ function count_user_articles($userID, $postType)
|
||||||
$query = new WP_Query($args);
|
$query = new WP_Query($args);
|
||||||
return $query->found_posts;
|
return $query->found_posts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function build_sommaire_from_content($postID)
|
||||||
|
{
|
||||||
|
$blocks = parse_blocks(get_the_content($postID));
|
||||||
|
$titleBlocks = array_filter(
|
||||||
|
$blocks,
|
||||||
|
function ($block) {
|
||||||
|
return $block['blockName'] === 'core/heading' && isset($block['attrs']['level']) && in_array($block['attrs']['level'], array(2, 3), true);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
$outputIndex = [];
|
||||||
|
|
||||||
|
foreach ($titleBlocks as $block) {
|
||||||
|
$title = strip_tags($block['innerHTML']);
|
||||||
|
$anchor = $block['attrs']['idName'] ?? sanitize_title($title);
|
||||||
|
$level = $block['attrs']['level'];
|
||||||
|
|
||||||
|
$outputIndex[] = [
|
||||||
|
'title' => $title,
|
||||||
|
'anchor' => $anchor,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return $outputIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function build_footnotes_index_from_content($content)
|
||||||
|
{
|
||||||
|
if (empty($content)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$footnotes = [];
|
||||||
|
$dom = new DOMDocument();
|
||||||
|
// On supprime les erreurs de parsing pour le HTML5
|
||||||
|
@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||||
|
|
||||||
|
$links = $dom->getElementsByTagName('a');
|
||||||
|
foreach ($links as $key => $link) {
|
||||||
|
if ($link->hasAttribute('class') && strpos($link->getAttribute('class'), 'footnote-reference') !== false) {
|
||||||
|
$footnote_content = $link->getAttribute('footnote-content');
|
||||||
|
if (!empty($footnote_content)) {
|
||||||
|
$footnotes[] = array(
|
||||||
|
'key' => $key + 1,
|
||||||
|
'anchorID' => $key + 1,
|
||||||
|
'content' => $footnote_content
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $footnotes;
|
||||||
|
}
|
||||||
|
|
||||||
|
add_filter('the_content', 'apply_footnotes_urls_to_content', 10);
|
||||||
|
|
||||||
|
function apply_footnotes_urls_to_content($content)
|
||||||
|
{
|
||||||
|
$post_type = get_post_type();
|
||||||
|
if ($post_type !== 'articles' && !is_admin()) return $content;
|
||||||
|
|
||||||
|
$footnotes = build_footnotes_index_from_content($content);
|
||||||
|
$dom = new DOMDocument();
|
||||||
|
@$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||||
|
|
||||||
|
$links = $dom->getElementsByTagName('a');
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user