docs: add anchored links, closes #2756 (#2951)

This commit is contained in:
Lars Hvam 2019-10-07 22:48:28 -07:00 committed by GitHub
parent 28e052dbce
commit d7fd8af1d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -14,5 +14,38 @@
</div>
</div>
{% include footer.html %}
<script type="text/javascript">
(function(){
'use strict';
/*
Create intra-page links
Requires that your headings already have an `id` attribute set (because that's what jekyll does)
For every heading in your page, this adds a little anchor link `#` that you can click to get a permalink to the heading.
Ignores `h1`, because you should only have one per page.
The text content of the tag is used to generate the link, so it will fail "gracefully-ish" if you have duplicate heading text.
*/
var headingNodes = [], results, link,
tags = ['h2', 'h3', 'h4'];
tags.forEach(function(tag){
results = document.getElementsByTagName(tag);
Array.prototype.push.apply(headingNodes, results);
});
headingNodes.forEach(function(node){
link = document.createElement('a');
link.className = 'anchor-link';
link.textContent = '#';
link.href = '#' + node.getAttribute('id');
if (node.getAttribute('id')) {
node.appendChild(link);
}
});
})();
</script>
</body>
</html>

View File

@ -116,3 +116,8 @@ footer.nav-footer {
.nav-refs a {
color: #808080;
}
.anchor-link {
margin-left: 5px;
font-size: 80%;
}