Skip to content

Instantly share code, notes, and snippets.

@AriPerkkio
Created August 2, 2023 06:30
Show Gist options
  • Save AriPerkkio/a3059f025aa1ce84bf436aa3f501936a to your computer and use it in GitHub Desktop.
Save AriPerkkio/a3059f025aa1ce84bf436aa3f501936a to your computer and use it in GitHub Desktop.
Query HTML headings and print them
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