Created
August 2, 2023 06:30
-
-
Save AriPerkkio/a3059f025aa1ce84bf436aa3f501936a to your computer and use it in GitHub Desktop.
Query HTML headings and print them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
printHeadings(); | |
function printHeadings() { | |
const headings = document.querySelectorAll("h1,h2,h3,h4,h5,h6"); | |
const formatted = Array.from(headings).map((element) => { | |
const tag = element.tagName; | |
const padding = parseInt(tag.charAt(1)) - 1; | |
const text = element.textContent?.replace(/\s{2,}/g, " "); | |
return `${" ".repeat(padding)}${tag} ${text}`; | |
}).join('\n'); | |
console.log(formatted) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment