MediaWiki:Common.js: Difference between revisions

From stencil.wiki
No edit summary
No edit summary
Line 3: Line 3:


document.addEventListener("DOMContentLoaded", function () {
document.addEventListener("DOMContentLoaded", function () {
     const stepsHeader = document.querySelector("h2 > #Steps");
    // Select the h2 > #steps element
     const stepsHeader = document.querySelector("h2 > #steps");
   
     if (stepsHeader) {
     if (stepsHeader) {
        // Find the adjacent ordered list
         const orderedList = stepsHeader.parentElement.nextElementSibling;
         const orderedList = stepsHeader.parentElement.nextElementSibling;
       
        // Ensure the next sibling is an <ol>
         if (orderedList && orderedList.tagName === "OL") {
         if (orderedList && orderedList.tagName === "OL") {
             const listItems = orderedList.querySelectorAll("li");
             // Loop through each <li> in the <ol>
            listItems.forEach((li, index) => {
            orderedList.querySelectorAll("li").forEach((listItem) => {
                 li.textContent = `Step ${index + 1}: ${li.textContent}`;
                 // Split the text content into the first sentence and the rest
                const text = listItem.textContent.trim();
                const firstSentenceEnd = text.indexOf(".") + 1; // Find the end of the first sentence
               
                if (firstSentenceEnd > 0) {
                    // Extract the first sentence and the remaining content
                    const firstSentence = text.slice(0, firstSentenceEnd);
                    const restOfText = text.slice(firstSentenceEnd).trim();
                   
                    // Replace the <li> content with the formatted version
                    listItem.innerHTML = `<strong>${firstSentence}</strong> ${restOfText}`;
                }
             });
             });
         }
         }
     }
     }
});
});

Revision as of 01:38, 14 January 2025

/* Any JavaScript here will be loaded for all users on every page load. */


document.addEventListener("DOMContentLoaded", function () {
    // Select the h2 > #steps element
    const stepsHeader = document.querySelector("h2 > #steps");
    
    if (stepsHeader) {
        // Find the adjacent ordered list
        const orderedList = stepsHeader.parentElement.nextElementSibling;
        
        // Ensure the next sibling is an <ol>
        if (orderedList && orderedList.tagName === "OL") {
            // Loop through each <li> in the <ol>
            orderedList.querySelectorAll("li").forEach((listItem) => {
                // Split the text content into the first sentence and the rest
                const text = listItem.textContent.trim();
                const firstSentenceEnd = text.indexOf(".") + 1; // Find the end of the first sentence
                
                if (firstSentenceEnd > 0) {
                    // Extract the first sentence and the remaining content
                    const firstSentence = text.slice(0, firstSentenceEnd);
                    const restOfText = text.slice(firstSentenceEnd).trim();
                    
                    // Replace the <li> content with the formatted version
                    listItem.innerHTML = `<strong>${firstSentence}</strong> ${restOfText}`;
                }
            });
        }
    }
});