forked from streetwriters/notesnook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrations.js
167 lines (145 loc) · 4.9 KB
/
migrations.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
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2022 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { parseHTML } from "./utils/html-parser";
import { decodeHTML5 } from "entities";
export const migrations = {
5.0: {},
5.1: {},
5.2: {
note: replaceDateEditedWithDateModified(),
notebook: replaceDateEditedWithDateModified(),
tag: replaceDateEditedWithDateModified(true),
attachment: replaceDateEditedWithDateModified(true),
trash: replaceDateEditedWithDateModified(),
tiny: (item) => {
item = replaceDateEditedWithDateModified()(item);
if (!item.data || item.data.iv) return migrations["5.3"].tiny(item);
item.data = removeToxClassFromChecklist(wrapTablesWithDiv(item.data));
return migrations["5.3"].tiny(item);
},
settings: replaceDateEditedWithDateModified(true)
},
5.3: {
tiny: (item) => {
if (!item.data || item.data.iv) return migrations["5.4"].tiny(item);
item.data = decodeWrappedTableHtml(item.data);
return migrations["5.4"].tiny(item);
}
},
5.4: {
tiny: (item) => {
if (!item.data || item.data.iv) return item;
item.type = "tiptap";
item.data = tinyToTiptap(item.data);
return item;
}
},
5.5: {},
5.6: {
note: false,
notebook: false,
tag: false,
attachment: false,
trash: false,
tiny: false,
tiptap: false,
settings: false
}
};
function replaceDateEditedWithDateModified(removeDateEditedProperty = false) {
return function (item) {
item.dateModified = item.dateEdited;
if (removeDateEditedProperty) delete item.dateEdited;
delete item.persistDateEdited;
return item;
};
}
function wrapTablesWithDiv(html) {
const document = parseHTML(html);
const tables = document.getElementsByTagName("table");
for (let table of tables) {
table.setAttribute("contenteditable", "true");
const div = document.createElement("div");
div.setAttribute("contenteditable", "false");
div.innerHTML = table.outerHTML;
div.classList.add("table-container");
table.replaceWith(div);
}
return document.outerHTML || document.body.innerHTML;
}
function removeToxClassFromChecklist(html) {
const document = parseHTML(html);
const checklists = document.querySelectorAll(
".tox-checklist,.tox-checklist--checked"
);
for (let item of checklists) {
if (item.classList.contains("tox-checklist--checked"))
item.classList.replace("tox-checklist--checked", "checked");
else if (item.classList.contains("tox-checklist"))
item.classList.replace("tox-checklist", "checklist");
}
return document.outerHTML || document.body.innerHTML;
}
const regex = /<div class="table-container".*<\/table><\/div>/gm;
function decodeWrappedTableHtml(html) {
return html.replaceAll(regex, (match) => {
const html = decodeHTML5(match);
return html;
});
}
const NEWLINE_REPLACEMENT_REGEX = /\n|<br>|<br\/>/gm;
const PREBLOCK_REGEX = /(<pre.*?>)(.*?)(<\/pre>)/gm;
const SPAN_REGEX = /<span class=.*?>(.*?)<\/span>/gm;
export function tinyToTiptap(html) {
if (typeof html !== "string") return html;
// Preserve newlines in pre blocks
html = html
.replace(/\n/gm, "<br/>")
.replace(PREBLOCK_REGEX, (_pre, start, inner, end) => {
let codeblock = start;
codeblock += inner
.replace(NEWLINE_REPLACEMENT_REGEX, "<br/>")
.replace(SPAN_REGEX, (_span, inner) => inner);
codeblock += end;
return codeblock;
});
const document = parseHTML(html);
const tables = document.querySelectorAll("table");
for (const table of tables) {
table.removeAttribute("contenteditable");
if (
table.parentElement &&
table.parentElement.nodeName.toLowerCase() === "div"
) {
table.parentElement.replaceWith(table);
}
}
const images = document.querySelectorAll("p > img");
for (const image of images) {
image.parentElement.replaceWith(image.cloneNode());
}
const bogus = document.querySelectorAll("[data-mce-bogus]");
for (const element of bogus) {
element.remove();
}
const attributes = document.querySelectorAll(
"[data-mce-href], [data-mce-flag]"
);
for (const element of attributes) {
element.removeAttribute("data-mce-href");
element.removeAttribute("data-mce-flag");
}
return document.body.innerHTML;
}