-
Notifications
You must be signed in to change notification settings - Fork 0
/
yokoishioka-table-of-contents
46 lines (36 loc) · 1.71 KB
/
yokoishioka-table-of-contents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* TABLE OF CONTENTS */
/* By Yoko Ishioka */
/* https://yokoishioka.com */
/* Web Engineer */
/* create dynamic table of contents */
var last;
/* add a table of contents header if there's a class called "table_of_contents" */
$('.table_of_contents').append("<h2>Table of Contents</h2> \n ");
/* give each topic/subtopic an id attribute */
$('.topic, .subtopic').each(function() {
var value = $(this).html();
var thisClass = $(this).attr("class");
/* 1) make string all lowercase and replace spaces with underscores so the terms don't break */
var valueDictionary = value.toLowerCase();
valueDictionary = valueDictionary.replace(/\s+/g, '_');
valueDictionary = valueDictionary.replace(/[^a-zA-Z0-9_]/g, '');
/* add Back-to-Top link */
$(this).append(" | <a href='#masthead'>Back to Top</a>");
/* give each topic and subtopic an id of its value */
$(this).attr("id", valueDictionary);
/* if the element has the "topic" class, hyperlink it with its value and note that the last element is a "topic" */
if (thisClass == 'topic') {
$('.table_of_contents').append("<p><a href='#" + valueDictionary + "'>" + value + "</a></p>");
last = "topic";
}
/* if the element has the "subtopic" class, make it into an unordered list, start a new section only if the last element was "topic", otherwise add it as a bullet */
else if (thisClass == 'subtopic'){
if (last == "topic") {
$('.table_of_contents').append("<div class='section'><li><a href='#" + valueDictionary + "'>" + value + "</a></li>");
}
else {
$('.table_of_contents').append("<li><a href='#" + valueDictionary + "'>" + value + "</a></li>");
}
last = "subtopic";
}
});