-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_block.js
421 lines (373 loc) · 9.86 KB
/
convert_block.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
const fs = require('fs')
const jsdom = require('jsdom')
var lo = require('lodash')
var flow = require('@subiz/flow')
const {JSDOM} = jsdom
async function html2block(html, docM) {
const dom = new JSDOM(html)
let out = []
await htmlMap(dom.window.document.body.childNodes, async (item, i) => {
let ret = await parse(item, undefined, docM || {})
if (ret) out.push(ret)
})
out = out.filter((ret) => ret)
// remove file ending new line
if (out.length > 1) {
let last = out[out.length - 1]
if (last.type == 'text' && !last.text.trim()) out.pop()
}
if (out.length == 1 && out[0].type == 'paragraph') return out[0]
return {type: 'div', content: out}
}
async function parse(item, format, docM) {
if (!item) return
if (checkTitle(item)) return parseTitle(item, docM)
if (checkH1(item)) return parseHeading(1, item, docM)
if (checkH2(item)) return parseHeading(2, item, docM)
if (checkH3(item)) return parseHeading(3, item, docM)
if (checkH4(item)) return parseHeading(4, item, docM)
if (checkCodeblock(item)) return parseCodeblock(item, docM)
if (checkTable(item)) return parseTable(item, docM)
return parsePara(item, format, docM)
}
// code must not contain ```
function parseCodeblock(item) {
let code = null
item.childNodes.forEach((tbody) => {
if (!tbody || !tbody.tagName) return
if (tbody.tagName.toLowerCase() !== 'tbody') return
tbody.childNodes.forEach((tr) => {
if (!tr || !tr.tagName) return
if (tr.tagName.toLowerCase() !== 'tr') return
tr.childNodes.forEach((td) => {
if (!td || !td.tagName) return
if (td.tagName.toLowerCase() !== 'td') return
code = td
return
})
})
})
return {
type: 'pre',
content: [codeContent(code)],
}
}
async function parseTable(item, docM) {
let rows = []
await htmlMap(item.childNodes, async (tbody) => {
if (!tbody || !tbody.tagName) return
if (tbody.tagName.toLowerCase() !== 'tbody') return
await htmlMap(tbody.childNodes, async (tr) => {
if (!tr || !tr.tagName) return
if (tr.tagName.toLowerCase() !== 'tr') return
let cells = []
let row = {
type: 'table_row',
content: cells,
}
await htmlMap(tr.childNodes, async (td) => {
if (!td || !td.tagName) return
if (td.tagName.toLowerCase() !== 'td') return
let parsed = await parse(td, {singleline: true}, docM)
cells.push({
type: 'table_cell',
colspan: 1,
rowspan: 1,
content: [parsed],
})
return
})
rows.push(row)
})
})
return {type: 'table', content: rows}
}
function codeContent(item) {
if (!item) return
if (item.childNodes.length == 0) {
let text = item.textContent
if (text) return {type: 'text', text: text}
return null
}
let out = ''
let par = {
type: 'paragraph',
content: [],
}
item.childNodes.forEach((child) => {
let content = codeContent(child)
if (content) par.content.push(content)
})
return par
}
async function parsePara(item, org_format, docM) {
if (!item || !item.tagName) {
let text = normalize(item.textContent, org_format)
if (text) return {type: 'text', text: text}
return null
}
let tagname = item.tagName.toLowerCase()
if (tagname == 'br') return null // {type: 'paragraph'}
if (item.childNodes.length == 0) {
let text = normalize(item.textContent, org_format)
if (text) return {type: 'text', text: text}
return null
}
if (tagname == 'a') {
let url = item.href || ''
if (url.startsWith('https://www.google.com/url?')) {
let qs = new URL(url).searchParams
url = qs.get('q') || ''
}
if (url.startsWith('https://docs.google.com/document/d/')) {
let doc_id = url.substr('https://docs.google.com/document/d/'.length)
doc_id = doc_id.split('/')[0]
doc_id = doc_id.split('#')[0]
if (docM[doc_id]) {
let fileName = sluggy(docM[doc_id].path_lower) + '.md'
return {
type: 'link',
href: fileName,
title: normalize(item.textContent, org_format) || url,
text: normalize(item.textContent, org_format) || url,
target: '_blank',
}
}
}
return {
type: 'link',
href: url,
title: normalize(item.textContent, org_format) || url,
text: normalize(item.textContent, org_format) || url,
target: '_blank',
}
}
if (tagname == 'li') {
let childs = []
await htmlMap(item.childNodes, async (child, i) => {
// if (!child) return
if (!child.tagName) return
let parsed = await parse(child, org_format, docM)
if (parsed) childs.push(parsed)
})
if (childs.length == 1 && childs[0].type == 'paragraph')
return {
type: 'list_item',
content: childs,
}
return {
type: 'list_item',
content: [{type: 'paragraph', content: childs}],
}
}
if (tagname == 'ol' || tagname == 'ul') {
let format = Object.assign({}, org_format)
let childs = []
await htmlMap(item.childNodes, async (child, i) => {
// if (!child) return
if (!child.tagName) return
let parsed = await parse(child, org_format, docM)
if (parsed) childs.push(parsed)
})
return {
type: tagname == 'ol' ? 'ordered_list' : 'bullet_list',
content: childs,
}
}
let out = []
let childs = out
if (tagname == 'p') {
out = [
{
type: 'paragraph',
content: childs,
},
]
}
await htmlMap(item.childNodes, async (child) => {
let childTagName = ''
if (child.tagName) childTagName = child.tagName.toLowerCase()
if (childTagName == 'img') {
let newsrc = await uploadImageToSubiz(child.src)
childs.push({
type: 'image',
alt_text: normalize(child.alt),
image: {url: newsrc},
})
return
}
// quote
let format = Object.assign({}, org_format)
let doitalic = false
let dobold = false
if (!format.italic && item.style.fontStyle == 'italic') {
format.italic = true
doitalic = true
}
if (!format.bold && item.style.fontWeight >= 500) {
format.bold = true
dobold = true
}
if (item.style.backgroundColor == 'rgb(0, 0, 0)' && item.style.color == 'rgb(255, 255, 255)') {
format.code = true
}
if (format.code) {
let text = normalize(item.textContent)
if (text)
childs.push({
type: 'text',
text: text,
code: true,
})
return
}
let ret = await parse(child, format, docM)
if (!ret) return
if (doitalic) ret.italic = true
if (dobold) ret.bold = true
childs.push(ret)
})
if (childs.length > 1) {
return {
type: 'paragraph',
content: childs,
}
}
return out[0]
}
function parseTitle(item, docM) {
return {
type: 'heading',
level: 1,
content: [{type: 'text', text: normalize(item.textContent) || ' '}],
}
}
function parseHeading(level, item, docM) {
return {
type: 'heading',
level: level,
content: [
{
type: 'text',
text: normalize(item.textContent) || ' ',
},
],
}
}
function checkTitle(item, docM) {
if (!item.classList) return false
if (item.classList.contains('title')) return true
}
function checkH1(item) {
if (!item.tagName) return false
return item.tagName.toLowerCase() == 'h1'
}
function checkH2(item) {
if (!item.tagName) return false
return item.tagName.toLowerCase() == 'h2'
}
function checkH3(item) {
if (!item.tagName) return false
return item.tagName.toLowerCase() == 'h3'
}
function checkH4(item) {
if (!item.tagName) return false
return item.tagName.toLowerCase() == 'h4'
}
function checkCodeblock(item) {
if (!item.tagName) return false
if (item.tagName.toLowerCase() !== 'table') return false
let ncell = 0
item.childNodes.forEach((tbody) => {
if (!tbody || !tbody.tagName) return
if (tbody.tagName.toLowerCase() !== 'tbody') return
tbody.childNodes.forEach((tr) => {
if (!tr || !tr.tagName) return
if (tr.tagName.toLowerCase() !== 'tr') return
tr.childNodes.forEach((td) => {
if (!td || !td.tagName) return
if (td.tagName.toLowerCase() !== 'td') return
ncell++
})
})
})
return ncell == 1
}
function checkTable(item) {
if (!item.tagName) return false
if (item.tagName.toLowerCase() !== 'table') return false
let ncell = 0
item.childNodes.forEach((tbody) => {
if (!tbody || !tbody.tagName) return
if (tbody.tagName.toLowerCase() !== 'tbody') return
tbody.childNodes.forEach((tr) => {
if (!tr || !tr.tagName) return
if (tr.tagName.toLowerCase() !== 'tr') return
tr.childNodes.forEach((td) => {
if (!td || !td.tagName) return
if (td.tagName.toLowerCase() !== 'td') return
ncell++
})
})
})
return ncell > 1
}
function normalize(str, format) {
// receive text
str = str || ''
str = str.replace(/[\t\r\n]/gm, ' ')
str = str.replace(/\s\s+/g, ' ')
str = str.replace(/\*/g, '\\*')
str = str.replace(/\_/g, '\\_')
str = str.replace(/\[/g, '\\[')
str = str.replace(/\]/g, '\\]')
str = str.replace(/\|/g, '\\|')
str = str.replace(/`/g, '\\`')
if (format && format.singleline) {
str = str.replace(/\r\n|\r|\n/g, '<br />')
}
return lo.trim(str)
}
function trimBr(str) {
while (true) {
newstr = str.replace(/^\<br \/\>/, '')
newstr = newstr.replace(/^\<br\/\>/, '')
newstr = newstr.replace(/^\<br\>/, '')
newstr = newstr.replace(/\<br \/\>$/, '')
newstr = newstr.replace(/\<br\/\>$/, '')
newstr = newstr.replace(/\<br\>$/, '')
if (newstr == str) break
str = newstr
}
return str
}
// check ./driveapi.js/sluggy
function sluggy(name) {
name = name || ''
return unicodeToAscii(name.toLowerCase().trim().replaceAll('?', '-').replaceAll(' ', '-').replaceAll('!', '-'))
}
function unicodeToAscii(str) {
str = str + ''
// work around Đ not work
str = str.replace('đ', 'd').replace('Đ', 'D')
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
}
async function uploadImageToSubiz(url) {
try {
let resp = await fetch('https://api.subiz.com.vn/4.0/accounts/acpxkgumifuoofoosble/files/url/download', {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({account_id: 'acpxkgumifuoofoosble', url: url}),
})
let out = await resp.json()
return out.url || url
} catch (e) {
return url
}
}
function htmlMap(dom, f, n) {
let items = []
dom.forEach((item) => items.push(item))
return flow.map(items, f, n)
}
module.exports = html2block