A Pen by Matthew Bott on CodePen.
Created
August 18, 2017 22:44
-
-
Save manbearwolf/37bb7bab55c70be7abbc84a492da8f77 to your computer and use it in GitHub Desktop.
Automatic Table of Contents
This file contains hidden or 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
| <article> | |
| <h1>Documentation</h1> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <div class="all-questions"> | |
| <h3 id="one">How do you smurf a murf?</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <h3 id="two">How do many licks does a giraffe?</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <h3 id="three">Which Frank is?</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <h3 id="four">Is fourteen enough?</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <h3 id="five">Is a circle an oval y/n?</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <h3 id="six">Many mongerals manifest mountains</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <h3 id="seven">How would you like a soda drink?</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <h3 id="eight">I would very much like a soda drink.</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| <h3 id="nine">Thank you for accepting the soda drink.</h3> | |
| <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p> | |
| </div> | |
| </article> |
This file contains hidden or 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
| var ToC = | |
| "<nav role='navigation' class='table-of-contents'>" + | |
| "<h2>On this page:</h2>" + | |
| "<ul>"; | |
| var newLine, el, title, link; | |
| $("article h3").each(function() { | |
| el = $(this); | |
| title = el.text(); | |
| link = "#" + el.attr("id"); | |
| newLine = | |
| "<li>" + | |
| "<a href='" + link + "'>" + | |
| title + | |
| "</a>" + | |
| "</li>"; | |
| ToC += newLine; | |
| }); | |
| ToC += | |
| "</ul>" + | |
| "</nav>"; | |
| $(".all-questions").prepend(ToC); | |
This file contains hidden or 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
| <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
This file contains hidden or 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
| body { | |
| background: #eee; | |
| padding: 20px; | |
| } | |
| article { | |
| max-width: 50em; | |
| background: white; | |
| padding: 2em; | |
| margin: 1em auto; | |
| } | |
| .table-of-contents { | |
| float: right; | |
| width: 40%; | |
| background: #eee; | |
| font-size: 0.8em; | |
| padding: 1em 2em; | |
| margin: 0 0 0.5em 0.5em; | |
| } | |
| .table-of-contents ul { | |
| padding: 0; | |
| } | |
| .table-of-contents li { | |
| margin: 0 0 0.25em 0; | |
| } | |
| .table-of-contents a { | |
| text-decoration: none; | |
| } | |
| .table-of-contents a:hover, | |
| .table-of-contents a:active { | |
| text-decoration: underline; | |
| } | |
| h3:target { | |
| animation: highlight 1s ease; | |
| } | |
| @keyframes highlight { | |
| from { background: yellow; } | |
| to { background: white; } | |
| } |
Author
Author
tables in posts
Saves time about drawing it (the links on Table-Of-Contents), (is maybe only it!) Otherwise you can draw them using html. (and no js is even used, no article, just pure css and html).
Example:
HTML
<div class="table-of-contents">
<h2>On this Page</h2>
<ul>
<li><a href="#Test">Test</a></li>
</ul>
</div>
<h2 id="Test">Test</h2>
Jumps and is where CSS Put it the same.(the table on the right...)
CSS
table...
.table-of-contents {
float: right;
width: 40%;
background: #eee;
font-size: 0.8em;
padding: 0em 2em 1em 2em;
margin: 0 0 0.5em 0.5em;
}
.table-of-contents ul {
padding: 0;
}
.table-of-contents li {
margin: 0 0 0.25em 0;
}
.table-of-contents a {
text-decoration: none;
}
.table-of-contents a:hover,
.table-of-contents a:active {
text-decoration: underline;
}
/* Optionable */
h2:target {
animation: highlight 3s ease;
}
@keyframes highlight {
from { background: green; }
to { background: white; }
}
Author
if java seems
buggy
it doesn't work with bootstrap, a lot of different loads (scripts) makes it buggy.
Author
Media Queries
// custom class widths that I made up
.table-of-contents{
float: none;
margin: 2.0em 0 0 0;
padding: 0.5em 2em 1em 2em;
width: auto;
}
Author
I drew the tables in posts (no java) and used media queries...
(example: https://codepen.io/robott1982/pen/vJdaaL) w/java instead.
also, see...
## Table of Contents
## Table of Contents
{:.no_toc}
1. TOC
{:toc}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This just uses jquery to draw contents in a standard list and a var (named toc or all-questions). (No extras needed)! (Sorta simple)
just include a jquery file somewhere (body, head.html, header)
include article somehow for it not to catch whole blog (Header, Footer). Preferably article is in some kind of jekyll coding Not blog post. (ex: Paginator loop or default, home, post.html (best spot), or somewhere...).
articleis HTML5, just in case.