-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoc-css.lua
221 lines (199 loc) · 5.18 KB
/
toc-css.lua
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
--[[
toc-css.lua: lua script to add CSS and Vanilla Javascript to native Pandoc HTML output
Formats HTML table of content as produced by Pandoc to the top left of the document
Copyright © 2021 Michael Cysouw <cysouw@mac.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
]]
css = [[
<!-- CSS added by filter 'toc-css.lua' for TOC hovering to the side -->
<style>
body {
padding-left: 1cm;
padding-right: 1cm;
transition: 0.5s;
}
nav {
width: 1em;
margin-left: -1cm;
font-size: smaller;
color: grey;
transition: 0.5s;
float: left;
position: fixed;
top: 0;
bottom: 0;
white-space: nowrap;
overflow: hidden;
overflow-y: scroll;
transition: 0.5s;
}
nav::-webkit-scrollbar {
display: none;
}
nav a, nav a:visited {
color: grey;
}
nav h2:before {
content: "≡ ";
font-size: 150%;
}
nav h2:after {
content: " ◂";
}
nav li {
margin-left: 1em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
nav li > a:not(:only-child):before {
content: "▸ ";
}
nav li > a:only-child {
margin-left: 0.75em;
}
nav li li {
margin-left: 1em;
}
nav li li li {
margin-left: 0.5em;
font-size: smaller;
}
nav ul li ul {
visibility: hidden;
display: none;
margin-top: 0.2em;
margin-bottom: 0.2em;
transition: 0.5s;
}
.paddingleft {
padding-left: 9cm;
transition: 0.5s;
}
.navside {
width: 7cm;
margin-left: -8.5cm;
padding-right: 1cm;
transition: 0.5s;
}
.navside h2:after {
content: " ▸";
}
.navshown {
width: 50%;
transition: 0.5s;
background-color: rgba(255, 255, 255, 0.95);
}
.subShow > ul {
visibility: visible;
display: block;
transition: 0.5s;
margin-left: -1em;
}
.subShow > a:not(:only-child):before {
content: "▾ ";
}
</style>
]]
-----------------------------
script = [[
<!-- Javascript added by toc-css.lua to make TOC expandable on click -->
<script>
const b = document.querySelector("body");
const n = document.querySelector("nav");
const buttonsize = 20;
// click on "toc-title" to show TOC to the side
document.querySelector("#toc-title").addEventListener("click", function(e) {
if (e.clientX < e.currentTarget.getBoundingClientRect().left + buttonsize) {
n.classList.toggle("navshown");
} else {
b.classList.toggle("paddingleft");
n.classList.toggle("navside");
n.classList.remove("navshown");
};
});
// by default show TOC in large window
window.onload = function() {
if (window.innerWidth > 1000) {
b.classList.add("paddingleft");
n.classList.add("navside");
};
};
// show/hide TOC on resize
window.onresize = function () {
if (window.innerWidth > 1000) {
b.classList.add("paddingleft");
n.classList.add("navside");
} else {
b.classList.remove("paddingleft");
n.classList.remove("navside");
};
};
// show/hide subsections
const allLis = document.querySelectorAll("nav li");
for (const li of allLis) {
li.addEventListener('click', function (e) {
if (e.clientX < e.currentTarget.getBoundingClientRect().left + buttonsize) {
li.classList.toggle('subShow');
e.preventDefault();
};
if (e.clientX > e.currentTarget.getBoundingClientRect().left + 3*buttonsize) {
n.classList.remove("navshown");
};
});
};
// show full nav on tab, hide full nav on escape
document.addEventListener("keydown", function (e) {
if (e.which === 27) {
n.classList.remove("navshown");
e.preventDefault();
};
if (e.which === 9) {
n.classList.add("navshown");
e.preventDefault();
};
});
// hide full nav when clicked outside
document.addEventListener("click", function(e) {
if (n.classList.contains("navshown")) {
if (!n.contains(e.target)) {
n.classList.remove("navshown");
};
};
});
</script>
]]
----------------------------
function addCSS (meta)
-- read current "header-includes" from metadata, or make a new one
-- and add css to the end of "header-includes"
local current = meta['header-includes'] or pandoc.MetaList{meta['header-includes']}
current[#current+1] = pandoc.MetaBlocks(pandoc.RawBlock("html", css))
meta['header-includes'] = current
-- add default toc-title if there is none
if meta['toc-title'] == nil then
meta['toc-title'] = "Contents"
end
-- return metadata
return(meta)
end
----------------------------
function addScript (doc)
-- add javascript to the end of the document
table.insert(doc.blocks, pandoc.RawBlock("html", script) )
return(doc)
end
----------------------------
return {
{ Meta = addCSS },
{ Pandoc = addScript }
}