Skip to content

Instantly share code, notes, and snippets.

@makmac213
Created October 6, 2014 15:57
Show Gist options
  • Save makmac213/981aab7808603b8b3858 to your computer and use it in GitHub Desktop.
Save makmac213/981aab7808603b8b3858 to your computer and use it in GitHub Desktop.
TOC generator based from https://github.com/matthewkastor/html-table-of-contents. Only changed function to accept a string id of the element to be parsed.
<html>
<head>
<style type="text/css">
#toc .h1 {
font-size: 20px;
font-weight: bold;
text-indent: .25in;
}
#toc .h2 {
font-size: 20px;
text-indent: .5in;
}
#toc .h3 {
font-size: 20px;
text-indent: .75in;
}
#toc .h4 {
font-size: 20px;
text-indent: 1in;
}
#toc .h5 {
font-size: 20px;
text-indent: 1.25in;
}
#toc .h6 {
font-size: 20px;
text-indent: 1.5in;
}
</style>
</head>
<body>
<div id="toc"></div>
<div id="contents-container">
<h1>Chapter 1</h1>
<h2>Mabuhay Mundo</h2>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<h1>Chapter 2</h1>
<h3>Mabuhay Mundo</h3>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<h1>Chapter 3</h1>
<h4>Mabuhay Mundo</h4>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<script type="text/javascript">
htmlTableOfContents('contents-container');
function htmlTableOfContents (el) {
var toc = document.getElementById('toc');
var container = document.getElementById(el);
var headings = [].slice.call(container.querySelectorAll('h1, h2, h3, h4, h5, h6'));
headings.forEach(function (heading, index) {
var anchor = document.createElement('a');
anchor.setAttribute('name', 'toc' + index);
anchor.setAttribute('id', 'toc' + index);
var link = document.createElement('a');
link.setAttribute('href', '#toc' + index);
link.textContent = heading.textContent;
var div = document.createElement('div');
div.setAttribute('class', heading.tagName.toLowerCase());
div.appendChild(link);
toc.appendChild(div);
heading.parentNode.insertBefore(anchor, heading);
});
}
</script>
</body>
</html>
@makmac213
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment