This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki_hover.js
167 lines (159 loc) · 5.61 KB
/
wiki_hover.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
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
/**
* @file Wiki Hover
* @description Hover over a link to see the content of the link
* Doesn't work on mobile and header
* @see tippy https://atomiks.github.io/tippyjs/
*/
const blogURL = document.querySelector('meta[name="site_url"]')
? document.querySelector('meta[name="site_url"]').content
: location.origin;
const position = ["top", "right", "bottom", "left"];
/**
* @description Replace broken image with encoded image in first para
* @param {Element} firstPara
* @returns {Element} firstPara
*/
function brokenImage(firstPara) {
const brokenImage = firstPara?.querySelectorAll("img");
if (brokenImage) {
for (let i = 0; i < brokenImage.length; i++) {
const encodedImage = brokenImage[i];
encodedImage.src = decodeURI(decodeURI(encodedImage.src));
//replace broken image with encoded image in first para
encodedImage.src = encodedImage.src.replace(
location.origin,
blogURL
);
}
}
return firstPara
}
/**
* Strip text of first para of unwanted characters
* @param {Element} firstPara
* @returns {Element} firstPara
*/
function cleanText(firstPara) {
firstPara.innerText = firstPara.innerText
.replaceAll("↩", "")
.replaceAll("¶", "");
return firstPara
}
function calculateHeight(firstPara) {
const paragraph = firstPara ? firstPara.innerText ? firstPara.innerText : firstPara : "";
const height = Math.floor(
paragraph.split(" ").length / 100
);
if (height < 2) {
return `auto`;
} else if (height >= 5) {
return `20rem`;
}
return `${height}rem`;
}
try {
tippy(`.md-content a[href^="${blogURL}"], a.footnote-ref, a[href^="./"]`, {
content: "",
allowHTML: true,
animation: "scale-subtle",
theme: "translucent",
followCursor: true,
arrow: false,
touch: "hold",
inlinePositioning: true,
placement: position[Math.floor(Math.random() * position.length - 1)],
onShow(instance) {
fetch(instance.reference.href)
.then((response) => response.text())
.then((html) => {
const parser = new DOMParser();
return parser.parseFromString(html, "text/html");
})
.then((doc) => {
//create section for each content after header
const headers = doc.querySelectorAll("h1, h2, h3, h4, h5, h6");
headers.forEach(function (header) {
const headerName = header.id || header.innerText.split("\n")[0].toLowerCase().replaceAll(" ", "-");
if (headerName.length > 0) {
const div = doc.createElement("div");
div.classList.add(headerName);
let nextElement = header.nextElementSibling;
while (nextElement && !nextElement.matches("h1, h2, h3, h4, h5, h6")) {
div.appendChild(nextElement);
nextElement = nextElement.nextElementSibling;
}
header.parentNode.insertBefore(div, header.nextSibling);
}
});
return doc;
})
//inject head into doc
.then((doc) => {
if (location.href.replace(location.hash, "") === instance.reference.href) {
instance.hide();
instance.destroy();
return;
}
let firstPara = doc.querySelector("article");
const firstHeader = doc.querySelector("h1");
if (firstHeader && firstHeader.innerText === "Index") {
const realFileName = decodeURI(
doc.querySelector('link[rel="canonical"]').href
)
.split("/")
.filter((e) => e)
.pop();
firstHeader.innerText = realFileName;
}
//broken link in first para
firstPara = brokenImage(firstPara);
const element1 = document.querySelector(`[id^="tippy"]`);
if (element1) {
element1.classList.add("tippy");
}
const partOfText = instance.reference.href.replace(/.*#/, "#");
let toDisplay = firstPara;
let displayType;
if (partOfText.startsWith("#")) {
firstPara = doc.querySelector(
`[id="${partOfText.replace("#", "")}"]`
);
if (firstPara.tagName.includes("H")) {
const articleDOM = doc.createElement("article");
articleDOM.classList.add("md-content__inner", "md-typeset");
articleDOM.appendChild(doc.querySelector(`div.${partOfText.replace("#", "")}`));
toDisplay = articleDOM;
firstPara = toDisplay;
} else if (firstPara.innerText.replace(partOfText).length === 0) {
firstPara = doc.querySelector("div.citation");
toDisplay = firstPara;
} else {
toDisplay = cleanText(firstPara).innerText;
}
instance.popper.style.height = "auto";
} else {
instance.popper.style.height = calculateHeight(firstPara);
}
instance.popper.placement =
position[Math.floor(Math.random() * position.length)];
if (firstPara.innerText.length > 0) {
if (!displayType) {
instance.setContent(toDisplay)
instance.popper.style.height = calculateHeight(toDisplay);
}
} else {
firstPara = doc.querySelector("article");
instance.reference.href.replace(/.*#/, "#");
instance.popper.style.height = calculateHeight(firstPara);
}
})
.catch((error) => {
console.log(error);
instance.hide();
instance.destroy();
});
},
});
} catch {
console.log("tippy error, ignore it");
}