-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_entry.js
37 lines (36 loc) · 1.29 KB
/
open_entry.js
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
// returns prefixes; anchor(s) that nest another
// (e.g. 1862-01-09), '1862' and '01' are prefixes to 09 entry.
function getPrefixes(anchor) {
let prefixes = [];
// if anchor has dashes, split and store the first two prefixes
// (e.g.) '1862-01-09' -> ['1862', '01']
if (anchor.includes('-')) {
prefixes = anchor.split('-').slice(0, 2);
} else {
prefix.push(anchor);
}
return prefixes;
}
// wait for anchors to generate
window.onload = function() {
// if anchor exists in url
if (window.location.hash) {
// save anchor from url
let anchor = window.location.hash.substring(1);
let prefixes = getPrefixes(anchor);
// if year or month in prefix, open them
for (const prefix of prefixes) {
if (document.getElementById(prefix)) {
document.getElementById(prefix).open = true;
}
}
// If entry date in anchor, open it
if (document.getElementById(anchor)) {
document.getElementById(anchor).open = true;
} else {
// otherwise, reassign hash in url to prefixes to represent a valid anchor
// e.g. hash in url '#1862-01-203209320943----232-' -> '#1862-01'
window.location.hash = prefixes.join("-");
}
}
}