-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (48 loc) · 2.14 KB
/
index.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
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
const clues = [
"1a ","2a ","3a ","4a ","5a ","6a ","7a ","8a ","9a ",
"10a","11a","12a","13a","14a","15a","16a","17a","18a","19a",
"20a","21a","22a","23a","24a","25a","26a","27a","28a","29a",
"30a","31a","32a","33a","34a","35a","36a","37a","38a","39a",
"1d ","2d ","3d ","4d ","5d ","6d ","7d ","8d ","9d ",
"10d","11d","12d","13d","14d","15d","16d","17d","18d","19d",
"20d","21d","22d","23d","24d","25d","26d","27d","28d","29d",
"30d","31d","32d","33d","34d","35d","36d","37d","38d","39d"
];
const arrAnchorIds = clues.map( (clue) => 'jsanchor-' + clue.trim() );
const arrReplaceRegexs = clues.map( (clue) => new RegExp( '\\b' + clue.trim(), "gi" ) );
const createAnchors = () => {
const getClueNo = (text) => text.substring(0,3).replace(/\s/g, " ");
const insertAnchor = (tag, clueNo) => {
const anchor = document.createElement("a");
const anchorName = arrAnchorIds[clues.indexOf(clueNo)];
anchor.name = anchorName;
anchor.id = anchorName;
tag.insertBefore( anchor, tag.firstChild );
};
const insertAnchorsToClues = (tag, i) => {
const html = tag.innerText.trim();
const clueNo = getClueNo( html );
if ( clueNo && clues.includes( clueNo ) ) {
insertAnchor(tag, clueNo);
}
};
const entries = document.getElementsByClassName("entry-content");
const pTags = entries[0].getElementsByTagName("p");
Array.from(pTags).map(insertAnchorsToClues);
};
const createLinks = () => {
const anchorExists = (idx) => document.getElementById( arrAnchorIds[idx] );
const replaceCommentedCluesWithLinks = (tag, i) => {
let html = tag.innerHTML;
for ( i = 0; i < clues.length; i++ ) {
if ( anchorExists(i) ) {
html = html.replace( arrReplaceRegexs[i], ' <a href="#' + arrAnchorIds[i] + '" title="Goto to clue definition">' + clues[i].trim() + '</a>');
}
}
tag.innerHTML = html;
};
const comments = document.querySelectorAll(".comment-text, .comment-content");
Array.from(comments).map(replaceCommentedCluesWithLinks);
};
createAnchors();
createLinks();