Skip to content

Instantly share code, notes, and snippets.

@acdvorak
Last active December 30, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdvorak/7894160 to your computer and use it in GitHub Desktop.
Save acdvorak/7894160 to your computer and use it in GitHub Desktop.
Generate document outline from heading tags (h1, h2, etc.)
/** Full version */
(function() {
var tab = ' ';
var lines = [];
$('h1, h2, h3, h4, h5, h6').each(function() {
var level = (this.tagName.replace(/^H/, '') - 1);
var indent = '';
while (level--) {
indent += tab;
}
lines.push(indent + this.innerText.trim().replace(/\s+/g, ' '));
});
console.log(lines.join('\n'));
}());
/** Bookmarklet */
javascript:(function()%7B(function()%20%7Bvar%20tab%20%3D%20'%20%20%20%20'%3Bvar%20lines%20%3D%20%5B%5D%3B%24('h1%2C%20h2%2C%20h3%2C%20h4%2C%20h5%2C%20h6').each(function()%20%7Bvar%20level%20%3D%20(this.tagName.replace(%2F%5EH%2F%2C%20'')%20-%201)%3Bvar%20indent%20%3D%20''%3Bwhile%20(level--)%20%7Bindent%20%2B%3D%20tab%3B%7Dlines.push(indent%20%2B%20this.innerText.trim().replace(%2F%5Cs%2B%2Fg%2C%20'%20'))%3B%7D)%3Bconsole.log(lines.join('%5Cn'))%3B%7D())%7D)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment