-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockFromDef.js
48 lines (44 loc) · 1.18 KB
/
blockFromDef.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
import yaml from 'yaml'
var blocks = [
'image',
'text',
'embed',
'youtube',
'svg',
'video',
'vegalite',
'chartjs',
'fitty',
'flowchartjs',
'modelviewer'
]
export default src => {
const parser = new DOMParser()
const dom = parser.parseFromString(src, 'text/html').body
const pre = dom.querySelectorAll('pre')
const precode = dom.querySelectorAll('pre > code')
const preArr = Array.from(pre)
const blocksRes = []
var nsrc = src
if (precode.length > 0) {
preArr.forEach(el => {
const code = el.querySelector('code')
const cls = code.getAttribute('class')
if (cls) {
const type = cls.replace('language-', '')
const isBlockLegacy = blocks.indexOf(type) >= 0
const isBlock = type.indexOf('_') === 0
if (isBlockLegacy || isBlock) {
const btype = isBlock ? type.substr(1) : type
const b = code.innerHTML ? yaml.parse(code.innerHTML) : {}
b.type = btype
blocksRes.push(b)
nsrc = nsrc.replace(/"/mig, '"')
nsrc = nsrc.replace(el.innerHTML, '')
}
}
})
return { blocks: blocksRes, src: nsrc }
}
return { blocks: [], src: nsrc }
}