Skip to content

Instantly share code, notes, and snippets.

@ABB00717
Last active January 22, 2026 07:21
Show Gist options
  • Select an option

  • Save ABB00717/42a74b6ab993f2a2fc9d0d7d3ff83fa8 to your computer and use it in GitHub Desktop.

Select an option

Save ABB00717/42a74b6ab993f2a2fc9d0d7d3ff83fa8 to your computer and use it in GitHub Desktop.
A Javascript script prints headers in current HTML file with tabs representing their depth.
class Tag {
constructor(level, text) {
this.level = level;
this.text = text;
};
};
function extractHeaders() {
const headers = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
console.log(headers);
const tagList = Array.from(headers).map(header => {
const level = parseInt(header.tagName.substring(1));
const text = header.innerText.trim();
return new Tag(level, text);
});
tagList.forEach(tag => {
const indentation = '\t'.repeat(tag.level);
console.log(`${indentation}${tag.text}`);
});
return tagList;
};
extractHeaders();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment