-
Notifications
You must be signed in to change notification settings - Fork 0
/
FancyAnkiCards.js
358 lines (325 loc) · 8.89 KB
/
FancyAnkiCards.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
function loadScript(url, id) {
if (id && document.getElementById(id + "_JS")) return;
return new Promise((resolve, reject) => {
let script = document.createElement("script");
if (id) {
script.id = id + "_JS";
}
script.src = url;
script.onload = () => {
console.info(`Succesfully loaded : ${url}`);
resolve();
};
script.onerror = (e) => {
console.error(`Error while loading : ${url}`);
script.remove();
reject(e);
};
document.head.appendChild(script);
});
}
function loadStyle(url, id) {
if (id && document.getElementById(id + "_CSS")) return;
return new Promise((resolve, reject) => {
let link = document.createElement("link");
if (id) {
link.id = id + "_CSS";
}
link.rel = "stylesheet";
link.type = "text/css";
link.href = url;
link.onload = () => {
console.info(`Succesfully loaded : ${url}}`);
resolve();
};
link.onerror = (e) => {
console.error(`Error while loading : ${url}}`);
link.remove();
reject(e);
};
document.head.appendChild(link);
});
}
function hashCode(s) {
return s.split("").reduce((a, b) => {
a = (a << 5) - a + b.charCodeAt(0);
return a & a;
}, 0);
}
function hashColor(text) {
return "#" + Math.abs(hashCode(text)).toString(16).slice(0, 6).padEnd(6, "0");
}
function pastel(text) {
return text
.replace(/0/g, "C")
.replace(/1/g, "D")
.replace(/2/g, "E")
.replace(/3/g, "F")
.replace(/4/g, "C")
.replace(/5/g, "D")
.replace(/6/g, "E")
.replace(/7/g, "F")
.replace(/8/g, "C")
.replace(/9/g, "D")
.replace(/A/g, "E")
.replace(/B/g, "F");
}
function light(text) {
return text
.replace(/0/g, "8")
.replace(/1/g, "9")
.replace(/2/g, "A")
.replace(/3/g, "B")
.replace(/4/g, "C")
.replace(/5/g, "D")
.replace(/6/g, "E")
.replace(/7/g, "F");
}
function medium(text) {
return text
.replace(/C/g, "4")
.replace(/D/g, "5")
.replace(/E/g, "6")
.replace(/F/g, "7")
.replace(/0/g, "8")
.replace(/1/g, "9")
.replace(/2/g, "A")
.replace(/3/g, "B");
}
function dark(text) {
return text
.replace(/0/gi, "8")
.replace(/1/gi, "9")
.replace(/2/gi, "A")
.replace(/B/gi, "3")
.replace(/C/gi, "4")
.replace(/D/gi, "5")
.replace(/E/gi, "6")
.replace(/F/gi, "7");
}
function breadcrumb() {
let card = document.querySelector("#qa");
let breadcrumb = document.querySelector(".breadcrumb");
if (!breadcrumb) return;
breadcrumb.innerHTML = breadcrumb.innerHTML.split("::").join(" :: ");
card.style = `
--deckcolor:${medium(hashColor(breadcrumb.innerHTML))};
`;
background();
}
function background() {
try {
let page = document.querySelector(".card");
let breadcrumb = document.querySelector(".breadcrumb");
var pattern = GeoPattern.generate(breadcrumb.innerHTML);
page.style = `--image: ${pattern.toDataUrl()}`;
} catch (e) {
if (e instanceof ReferenceError) window.requestAnimationFrame(background);
}
}
function tags() {
let tags = document.querySelector(".tags");
if (tags.classList.contains("tagged")) return;
if (!tags) return;
tags.innerHTML = tags.innerHTML
.split(" ")
.filter((e) => e !== "")
.map(
(text) => `<li style="--tagcolor:${medium(hashColor(text))}">${text}</li>`
)
.join("");
tags.classList.add("tagged");
}
function markdownize(node) {
if (!node) return;
const content = node.innerHTML
.replace(/style="zoom: 1;"/gi, "")
.replace(/<[\/]?div\s*>/gi, "\n")
.replace(/<br\s*[\/]?>/gi, "\n");
var decoded = new DOMParser().parseFromString(
"<!doctype html><body>" + content,
"text/html"
).body.textContent;
node.innerHTML = window.FAC.md.render(decoded);
}
/////
/////
/////
let module = { exports: {} };
module.exports.tokenize = function emphasis(state, silent) {
var i,
scanned,
token,
start = state.pos,
marker = state.src.charCodeAt(start);
if (silent) {
return false;
}
if (marker !== 0x5f /* _ */ && marker !== 0x2a /* * */) {
return false;
}
scanned = state.scanDelims(state.pos, marker === 0x2a);
for (i = 0; i < scanned.length; i++) {
token = state.push("text", "", 0);
token.content = String.fromCharCode(marker);
state.delimiters.push({
// Char code of the starting marker (number).
//
marker: marker,
// Total length of these series of delimiters.
//
length: scanned.length,
// A position of the token this delimiter corresponds to.
//
token: state.tokens.length - 1,
// If this delimiter is matched as a valid opener, `end` will be
// equal to its position, otherwise it's `-1`.
//
end: -1,
// Boolean flags that determine if this delimiter could open or close
// an emphasis.
//
open: scanned.can_open,
close: scanned.can_close,
});
}
state.pos += scanned.length;
return true;
};
function postProcess(state, delimiters) {
var i,
startDelim,
endDelim,
token,
ch,
isStrong,
max = delimiters.length;
for (i = max - 1; i >= 0; i--) {
startDelim = delimiters[i];
if (
startDelim.marker !== 0x5f /* _ */ &&
startDelim.marker !== 0x2a /* * */
) {
continue;
}
// Process only opening markers
if (startDelim.end === -1) {
continue;
}
endDelim = delimiters[startDelim.end];
// If the previous delimiter has the same marker and is adjacent to this one,
// merge those into one strong delimiter.
//
// `<em><em>whatever</em></em>` -> `<strong>whatever</strong>`
//
isStrong =
i > 0 &&
delimiters[i - 1].end === startDelim.end + 1 &&
// check that first two markers match and adjacent
delimiters[i - 1].marker === startDelim.marker &&
delimiters[i - 1].token === startDelim.token - 1 &&
// check that last two markers are adjacent (we can safely assume they match)
delimiters[startDelim.end + 1].token === endDelim.token + 1;
ch = String.fromCharCode(startDelim.marker);
console.log(ch);
let isUnder = ch === "_";
token = state.tokens[startDelim.token];
token.type = isStrong
? isUnder
? "under_open"
: "strong_open"
: isUnder
? "em_open"
: "mark_open";
token.tag = isStrong ? (isUnder ? "u" : "strong") : isUnder ? "em" : "mark";
token.nesting = 1;
token.markup = isStrong ? ch + ch : ch;
token.content = "";
token = state.tokens[endDelim.token];
token.type = isStrong
? isUnder
? "under_close"
: "strong_close"
: isUnder
? "em_close"
: "mark_close";
token.tag = isStrong ? (isUnder ? "u" : "strong") : isUnder ? "em" : "mark";
token.nesting = -1;
token.markup = isStrong ? ch + ch : ch;
token.content = "";
if (isStrong) {
state.tokens[delimiters[i - 1].token].content = "";
state.tokens[delimiters[startDelim.end + 1].token].content = "";
i--;
}
}
}
module.exports.postProcess = function emphasis(state) {
var curr;
var tokens_meta = state.tokens_meta;
var max = state.tokens_meta.length;
postProcess(state, state.delimiters);
for (curr = 0; curr < max; curr++) {
if (tokens_meta[curr] && tokens_meta[curr].delimiters) {
postProcess(state, tokens_meta[curr].delimiters);
}
}
};
function discordEmphasis(md) {
md.inline.ruler2.at("emphasis", module.exports.postProcess);
}
/////
/////
/////
async function load() {
await Promise.all([
loadScript(
"https://cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"
),
loadScript(
"https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"
),
//loadStyle("https://cdn.jsdelivr.net/npm/[email protected]/styles/default.css"),
loadStyle(
"https://cdn.jsdelivr.net/npm/[email protected]/styles/a11y-dark.css"
),
loadScript(
"https://cdnjs.cloudflare.com/ajax/libs/markdown-it/12.3.2/markdown-it.min.js"
),
]);
window.FAC.md = window
.markdownit({
html: true,
xhtmlOut: true,
breaks: false,
linkify: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang))
try {
return hljs.highlight(str, { language: lang }).value;
} catch (__) {}
return ""; // use external default escaping
},
})
.use(discordEmphasis);
}
const qa = document.getElementById("qa");
const config = { attributes: false, childList: true, subtree: true };
const observer = new MutationObserver((mutationsList, observer) => redraw());
function redraw() {
observer.disconnect();
markdownize(document.querySelector("header"));
markdownize(document.querySelector("footer"));
breadcrumb();
tags();
observer.observe(qa, config);
}
// Later, you can stop observing
async function main() {
if (!window.FAC) {
window.FAC = {};
await load();
}
redraw();
}
main();