-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.js
50 lines (44 loc) · 1.13 KB
/
images.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
const splitCDN = (uri) => {
let imgArr = uri.split('/')
let filename = imgArr.pop()
let size = imgArr.pop()
let base = imgArr.join('/')
return { base, size, filename }
}
const getCDNBase = (uri) => {
let { base } = splitCDN(uri)
return base
}
const getCDNSize = (uri) => {
let { size } = splitCDN(uri)
return size
}
const getCDNfilename = (uri) => {
let { filename } = splitCDN(uri)
return filename
}
const smImg = (md, config = {}) => (tokens, idx, options, env, self) => {
let token = tokens[idx]
let srcIndex = token.attrIndex('src')
let url = token.attrs[srcIndex][1]
let alt = md.utils.escapeHtml(token.content)
let urlBase = getCDNBase(url)
let filename = getCDNfilename(url)
return `
<figure class="figure">
<picture>
<img
class="figure-image lazyload"
src="${urlBase}/180x180/${filename}"
data-optimumx="1.6"
data-src="${urlBase}/{width}x5000/${filename}"
data-sizes="auto"
alt="${alt}" />
</picture>
</figure>
`
}
const mdsm = (md, config) => {
md.renderer.rules.image = smImg(md, config)
}
module.exports = mdsm