1// Utilisation d'une IIFE avec jQuery en paramètre 2(($ = jQuery) => { 3 // Constantes pour la configuration 4 const CONFIG = { 5 MIN_H2_COUNT: 2, 6 MIN_ENTRY_HEIGHT: 500, 7 SOMMAIRE_ID: 'sommaire', 8 ENTRY_SELECTOR: '.entry', 9 H2_SELECTOR: 'h2', 10 EMPTY_SELECTORS: 'p:empty, h2:empty', 11 }; 12 13 // Fonction principale d'initialisation 14 const initTableOfContents = () => { 15 $(document).ready(createContentIndex); 16 }; 17 18 // Fonction pour vérifier si un sommaire est nécessaire 19 const shouldCreateIndex = ($entry) => { 20 const $h2Elements = $entry.find(CONFIG.H2_SELECTOR); 21 return $h2Elements.length >= CONFIG.MIN_H2_COUNT && $entry.height() > CONFIG.MIN_ENTRY_HEIGHT; 22 }; 23 24 // Fonction pour nettoyer le contenu 25 const cleanContent = ($entry) => { 26 $entry.find(CONFIG.EMPTY_SELECTORS).remove(); 27 }; 28 29 // Fonction pour trouver le point d'insertion 30 const findInsertionPoint = ($entry) => { 31 const $firstH2 = $entry.find(CONFIG.H2_SELECTOR).first(); 32 const $parent = $firstH2.parent(); 33 34 return { 35 $element: $parent.is(CONFIG.ENTRY_SELECTOR) ? $firstH2 : $parent, 36 isNested: !$parent.is(CONFIG.ENTRY_SELECTOR), 37 }; 38 }; 39 40 // Fonction pour créer le HTML du sommaire 41 const createIndexHTML = ($h2Elements) => { 42 const items = $h2Elements 43 .map((index, element) => { 44 const id = `id${index + 1}`; 45 $(element).attr('id', id); 46 return `<li><a href="#${id}">${$(element).text()}</a></li>`; 47 }) 48 .get(); 49 50 return `<ul>${items.join('')}</ul>`; 51 }; 52 53 // Fonction principale de création du sommaire 54 const createContentIndex = () => { 55 const $entry = $(CONFIG.ENTRY_SELECTOR); 56 57 if (!shouldCreateIndex($entry)) { 58 return; 59 } 60 61 cleanContent($entry); 62 63 const { $element, isNested } = findInsertionPoint($entry); 64 const $sommaire = $('<div>', { 65 id: CONFIG.SOMMAIRE_ID, 66 class: isNested ? 'mb30' : '', 67 }); 68 69 $element.before($sommaire); 70 71 const $h2Elements = $entry.find(CONFIG.H2_SELECTOR); 72 const indexHTML = createIndexHTML($h2Elements); 73 $sommaire.html(indexHTML); 74 }; 75 76 // Initialisation 77 initTableOfContents(); 78})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.