Skip to content

Commit e4e0725

Browse files
committed
Generate TOC
1 parent 9fd8553 commit e4e0725

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

_layouts/toc.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
---
22
layout: default
33
---
4-
<div id="toc">
5-
<ul><li>Item one</li></ul>
6-
</div>
4+
<div id="toc"></div>
75

86
<div id="primary">
97
{{ content }}

css/libgit2.css

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,16 +1990,34 @@ img {
19901990
/* Table of contents */
19911991

19921992
#primary {
1993-
width: 750px;
1993+
width: 700px;
19941994
}
19951995

19961996
#toc {
19971997
width: 190px;
19981998
float: right;
19991999
background-color: #fff;
20002000
-webkit-border-radius: 3px;
2001-
-moz-border-radius: 3px;
2002-
-ms-border-radius: 3px;
2003-
-o-border-radius: 3px;
2004-
border-radius: 3px;
2001+
-moz-border-radius: 3px;
2002+
-ms-border-radius: 3px;
2003+
-o-border-radius: 3px;
2004+
border-radius: 3px;
2005+
padding: 10px;
2006+
margin-top: 40px;
2007+
}
2008+
2009+
.toc-h1 {
2010+
margin: 10px 0;
2011+
font-size: 150%;
2012+
}
2013+
.toc-h2 {
2014+
margin-left: 10px;
2015+
margin-top: 10px;
2016+
font-size: 120%;
2017+
}
2018+
.toc-h3 {
2019+
margin-left: 20px;
2020+
}
2021+
.toc-h4 {
2022+
margin-left: 30px;
20052023
}

docs/guides/101-samples/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ layout: toc
77
## Diff
88

99
### One
10+
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
11+
12+
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
1013

1114
### Two
1215

js/toc.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1-
$(window).scroll(function(){
2-
$("#toc")
3-
.stop()
4-
.animate({"marginTop": ($(window).scrollTop()) + "px",
5-
"marginLeft":($(window).scrollLeft()) + "px"},
6-
"fast");
1+
$(function () {
2+
var toc = $('#toc');
3+
if (toc.length == 0) {
4+
return;
5+
}
6+
7+
// Keep the TOC in view
8+
$(window).scroll(function(){
9+
$("#toc")
10+
.stop()
11+
.animate({"marginTop": ($(window).scrollTop()) + "px",
12+
"marginLeft":($(window).scrollLeft()) + "px"},
13+
"fast");
14+
});
15+
16+
// Generate the TOC contents
17+
var current_level = 0;
18+
$('#primary :header[id]').each(function(i, el) {
19+
var del = $(el);
20+
console.log(el);
21+
22+
var div = $('<div></div>')
23+
.addClass('toc-' + el.nodeName.toLowerCase());
24+
div.append($('<a></a>')
25+
.attr('href', '#' + del.attr('id'))
26+
.text(del.text()));
27+
toc.append(div);
28+
});
729
});

0 commit comments

Comments
 (0)