Skip to content

Commit

Permalink
add anchors to toc and style it a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
clux committed Oct 12, 2015
1 parent baa732b commit 161f20a
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 15 deletions.
45 changes: 31 additions & 14 deletions assets/main.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
var generateToc = function (children) {
var toc = [];
var findHeadings = function (children) {
return Array.prototype.filter.call(children, function (node) {
return ['H1', 'H2', 'H3', 'H4', 'H5', 'H6'].indexOf(node.nodeName) >= 0;
});
};

var anchorize = function (text, linktext) {
var safelink = text.replace(/\W/g, '_').replace(/_{2,}/g, '_');
var idstr = linktext === '#' ? ' id="'+ safelink + '" ': '';
return '<a ' + idstr + 'href="#' + safelink + '">' + (linktext || text) + '</a>';
};

var insertAnchors = function (headings) {
headings.forEach(function (hx) {
hx.innerHTML = hx.innerText + '<span>' + anchorize(hx.innerText, '#') + '</span>';
});
};

var generateToc = function (headings) {
var toc = ['<h2>Table of Contents</h2>'];
var activeLevel = 0;

Array.prototype.forEach.call(children, function (node) {
if (['H1', 'H2', 'H3', 'H4', 'H5'].indexOf(node.nodeName) >= 0) {
var level = node.nodeName.slice(1) | 0;
var goingUp = level > activeLevel;
while (activeLevel !== level) {
activeLevel += goingUp ? 1 : -1;
toc.push(goingUp ? '<ul>' : '</ul>');
}
// TODO: sensible anchor link
toc.push('<li><a href=#>' + node.innerText + '</a></li>');
headings.forEach(function (hx) {
var level = hx.nodeName.slice(1) | 0;
var goingUp = level > activeLevel;
while (activeLevel !== level) {
activeLevel += goingUp ? 1 : -1;
toc.push(goingUp ? '<ul>' : '</ul>');
}
toc.push('<li>' + anchorize(hx.innerText) + '</li>');
});

while (activeLevel > 0) {
Expand All @@ -24,6 +39,8 @@ var generateToc = function (children) {

document.addEventListener("DOMContentLoaded", function () {
var toc = document.querySelector('#toc');
var body = document.querySelector('body');
toc.innerHTML = generateToc(body.children);
var contents = document.querySelector('#apicontent');
var headings = findHeadings(contents.children);
toc.innerHTML = generateToc(headings);
insertAnchors(headings);
});
189 changes: 189 additions & 0 deletions assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,192 @@ body {
color: #3a3a3a;
background: #fcfefa;
}

a {
color: #FE5210;
text-decoration: none;
}

a:visited {
color: #FE7110;
}

a:hover,
a:focus {
color: #FFA158;
}

ol, ul, dl {
margin: 0 0 0.6em 0;
padding: 0;
}

ol ul, ol ol, ol dl,
ul ul, ul ol, ul dl,
dl ul, dl ol, dl dl {
margin-bottom: 0;
}

ul, ol {
margin-left: 2em;
}

dl dt {
position: relative;
margin: 1.5em 0 0;
}

dl dd {
position: relative;
margin: 0 1em 0;
}

dd + dt.pre {
margin-top: 1.6em;
}

h1, h2, h3, h4, h5, h6 {
color: #301004;
text-rendering: optimizeLegibility;
font-weight: 700;
position: relative;
margin-bottom: 0.5em;
}

header h1 {
line-height: 2.0em;
margin: 0;
}

#apicontent {
padding-top: 1.0em;
}

#toc + h1 {
margin-top: 1em;
padding-top: 0;
}

h2 {
font-size: 1.5em;
margin: 1.0em 0 0.5em;
}

h2 + h2 {
margin: 0 0 0.5em;
}

h3 {
font-size: 1.0em;
margin: 1.5em 0 0.5em;
}

h3 + h3 {
margin: 0 0 0.5em;
}

h2, h3, h4 {
position: relative;
padding-right: 40px;
}

h1 span, h2 span, h3 span, h4 span {
position: absolute;
display: block;
top: 0;
right: 0;
}

h1 span:hover, h2 span:hover, h3 span:hover, h4 span:hover {
opacity: 1;
}

h1 span a, h2 span a, h3 span a, h4 span a {
font-size: 0.8em;
color: #000;
text-decoration: none;
font-weight: bold;
}

h5 {
font-size: 1.125em;
line-height: 1.4em;
}

h6 {
font-size: 1em;
line-height: 1.4667em;
}

pre, tt, code {
line-height: 1.5em;
font-family: Monaco, Consolas, "Lucida Console", monospace;
margin: 0; padding: 0;
}

.pre {
font-family: Monaco, Consolas, "Lucida Console", monospace;
line-height: 1.5em;
font-size: 1.2em;
}

pre {
padding: 1.0em 1.5em;
vertical-align: top;
background: #f2f5f0;
margin: 0.166666em -4.0em 1.0em 0em;
overflow-x: auto;
}

pre > code {
font-size: 0.8em;
}

pre + h3 {
margin-top: 2.225em;
}

code.pre {
white-space: pre;
}

hr {
background: none;
border: medium none;
border-bottom: 1px solid #7a7a7a;
margin: 0em 0em 1em 0;
}

#toc {
}

#toc h2 {
color: #C73E09;
margin-top: 0;
font-size: 1.0em;
line-height: 0;
margin: 1.5em 0;
}

#toc ul {
font-size: 0.8125em;
}

#toc ul ul { font-size: 1.0em; }

#toc ul a {
text-decoration:none;
}

#toc ul li {
margin-bottom: 0.6666em;
list-style: square outside;
}

#toc li > ul {
margin-top: 0.6666em;
}

#toc ul a:hover,
#toc ul a:focus {
}
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ module.exports = function *(md, opts) {
<script>${behaviour}</script>
</head>
<body>
<h1>${namePretty} v${opts.version} Documentation</h1>
<div id="toc"></div>
${content}
<div id="apicontent">${content}</div>
</body>
</html>
`;
Expand Down

0 comments on commit 161f20a

Please sign in to comment.