forked from nimbleways/code-to-notion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
221 lines (196 loc) · 6.07 KB
/
script.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
const notionJsonTextInput = document.getElementById("notionMimeType");
function printOriginal(clipboardData) {
const clipboardContentElement = document.getElementById("original");
clipboardContentElement.innerHTML = "";
for (const type of clipboardData.types) {
const clipData = clipboardData.getData(type);
let h3 = document.createElement("h3");
h3.textContent = `Content type: ${type}`;
clipboardContentElement.appendChild(h3);
const resultElement = document.createElement("pre");
resultElement.style = "white-space: pre-wrap; word-wrap: break-word;";
resultElement.textContent = clipData;
clipboardContentElement.appendChild(resultElement);
}
}
function parseGithubHtml(htmlString) {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlString, "text/html");
const rows = doc.querySelectorAll(".blob-code");
const codeLines = [];
let lastFlag = "none";
rows.forEach((row) => {
if (row.classList.contains("blob-code")) {
// Check if this line represents addition, deletion, or none
if (row.classList.contains("blob-code-addition")) {
lastFlag = "added";
} else if (row.classList.contains("blob-code-deletion")) {
lastFlag = "removed";
} else {
lastFlag = "none";
}
// Extract code content and create object
const line = row.innerText;
codeLines.push({
line,
flag: lastFlag,
});
}
});
return codeLines;
}
function extractFileNameFromHtml(htmlString) {
const filenamePattern = /<button data-path="([^"]+)"/;
const filenameMatch = htmlString.match(filenamePattern);
if (filenameMatch) {
return filenameMatch[1];
}
return null;
}
function mapFileNameToLanguage(filename) {
if (!filename) {
return "Plain Text";
}
const languageToFilenameRegex = {
Bash: /\.(sh)$/,
CSS: /\.(css)$/,
CSharp: /\.(cs)$/,
Cpp: /\.(cpp)$/,
Docker: /(?:dockerfile)$/,
Go: /\.(go)$/,
Groovy: /\.(groovy)$/,
HTML: /\.(html?)$/,
JSON: /\.(json)$/,
Java: /\.(java)$/,
JavaScript: /\.(jsx?)$/,
Kotlin: /\.(kt)$/,
Lua: /\.(lua)$/,
Makefile: /(?:Makefile)$/,
Markdown: /\.(md)$/,
PHP: /\.(php)$/,
Perl: /\.(pl)$/,
Python: /\.(py)$/,
Ruby: /\.(rb)$/,
Rust: /\.(rs)$/,
SCSS: /\.(scss)$/,
SQL: /\.(sql)$/,
Sass: /\.(sass)$/,
Swift: /\.(swift)$/,
TOML: /\.(toml)$/,
TypeScript: /\.(tsx?)$/,
XML: /\.(xml|csproj)$/,
YAML: /\.(yaml|yml)$/,
};
for (const [language, regex] of Object.entries(languageToFilenameRegex)) {
if (regex.test(filename.toLowerCase())) {
return language;
}
}
}
function createNotionTitleProperty(htmlString) {
const linesWithDiffFlag = parseGithubHtml(htmlString);
const notionTitle = [];
for (const lineWithDiffFlag of linesWithDiffFlag) {
const notionLine = [];
let prefix;
if (lineWithDiffFlag.flag === "added") prefix = "+\t";
else if (lineWithDiffFlag.flag === "removed") prefix = "-\t";
else prefix = "\t";
notionLine.push(prefix + lineWithDiffFlag.line + "\n");
if (lineWithDiffFlag.flag === "added")
notionLine.push([["h", "teal_background"]]);
else if (lineWithDiffFlag.flag === "removed")
notionLine.push([["h", "red_background"]]);
notionTitle.push(notionLine);
}
return notionTitle;
}
function getNotionJson(clipboardData) {
const htmlString = clipboardData.getData("text/html");
if (htmlString === undefined) {
return null;
}
const title = createNotionTitleProperty(htmlString);
if (title.length == 0) {
return null;
}
const uuid = self.crypto.randomUUID();
const filename = extractFileNameFromHtml(htmlString);
const language = mapFileNameToLanguage(filename);
const notionJsonTemplate = {
blocks: [
{
blockId: uuid,
blockSubtree: {
__version__: 3,
block: {
[uuid]: {
value: {
id: uuid,
version: 55,
type: "code",
properties: {
title: title,
language: [[language]],
},
},
},
},
},
},
],
};
return JSON.stringify(notionJsonTemplate);
}
function fillNotionJsonTextInput(clipboardData) {
const notionJson = getNotionJson(clipboardData);
if (notionJson == null) {
notionJsonTextInput.value = "";
return false;
}
notionJsonTextInput.value = getNotionJson(clipboardData);
selectText(notionJsonTextInput);
return true;
}
function selectText(htmlElement) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(htmlElement);
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(htmlElement);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
}
function showToast(message, duration = 8000) {
const toast = document.getElementById('toast');
toast.innerText = message;
toast.classList.remove('hidden');
setTimeout(() => {
toast.classList.add('hidden');
}, duration);
}
document.addEventListener("paste", function (e) {
if (!e.clipboardData || !e.clipboardData.getData || !e.clipboardData.types) {
return;
}
printOriginal(e.clipboardData);
if (!fillNotionJsonTextInput(e.clipboardData)) {
return;
}
document.execCommand("copy");
showToast("Notion block is copied! Just paste it in a notion page");
e.preventDefault();
});
document.addEventListener("copy", function (event) {
if (event.target !== notionJsonTextInput) {
return;
}
event.clipboardData.setData(
"text/_notion-blocks-v3-production",
notionJsonTextInput.value,
);
event.preventDefault();
});