This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
96 lines (80 loc) · 2.33 KB
/
main.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
"use strict";
const body = document.querySelector("body");
const codeBefore = document.getElementById("code-before");
const codeAfter = document.getElementById("code-after");
const cleanBtn = document.getElementById("clean");
const copyBtn = document.getElementById("copy");
const resetBtn = document.getElementById("reset");
const toggleThemeBtn = document.getElementById("toggle-theme");
const THEME_STORAGE_KEY = "notion2anki-card-cleaner.theme";
const THEME = {
LIGHT: "light",
DARK: "dark",
};
function cleanPatterns(code) {
const patterns = [
/\n/g,
/<[/]?pre.*?>/g,
/<[/]?summary.*?>/g,
/<[/]?figure.*?>/g,
/\sid=".*?"/g,
/\sclass=""/g,
/<style>.*?<\/style>/g,
];
let cleanCode = code;
for (const pattern of patterns) {
cleanCode = cleanCode.replace(pattern, "");
}
return cleanCode;
}
function cleanWrappedImages(code) {
return code.replace(/(?:<br>|<p>|<a.*?>)*(<img.*?>)(?:<br>|<\/p>|<\/a.*?>)*/g, "<p>$1</p>");
}
function cleanLastBreaks(code) {
return code.replace(/(?:<br>)+((?:<\/\w+>)*)$/g, "$1");
}
function clean() {
let code = codeBefore.value.trim();
if (code === "") {
alert("Paste your Anki's card HTML code to continue.");
return;
}
code = cleanPatterns(code);
code = cleanWrappedImages(code);
code = cleanLastBreaks(code);
code = html_beautify(code);
codeAfter.value = code;
}
function copy() {
codeAfter.select();
document.execCommand("copy");
}
function reset() {
codeBefore.value = "";
codeAfter.value = "";
codeBefore.focus();
}
function toggleTheme() {
body.classList.toggle(THEME.LIGHT);
const currentTheme = body.classList.contains(THEME.LIGHT) ? THEME.LIGHT : THEME.DARK;
localStorage.setItem(THEME_STORAGE_KEY, currentTheme);
toggleThemeBtn.checked = currentTheme === THEME.LIGHT;
}
function loadSavedTheme() {
const savedTheme = localStorage.getItem(THEME_STORAGE_KEY);
if (savedTheme === THEME.LIGHT) {
body.classList.add(THEME.LIGHT);
toggleThemeBtn.checked = true;
} else {
body.classList.remove(THEME.LIGHT);
toggleThemeBtn.checked = false;
}
}
codeBefore.addEventListener("click", function () {
this.select();
});
cleanBtn.addEventListener("click", clean);
copyBtn.addEventListener("click", copy);
resetBtn.addEventListener("click", reset);
toggleThemeBtn.addEventListener("click", toggleTheme);
loadSavedTheme();