-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheleventy.config.figure.js
36 lines (30 loc) · 1.11 KB
/
eleventy.config.figure.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
const { Liquid, Hash } = require('liquidjs')
const image = require('./_includes/javascripts/image.js')
const markdownIt = require('markdown-it')
module.exports = function(eleventyConfig) {
eleventyConfig.addLiquidTag('figure', function(liquidEngine) {
return {
parse: function(tagToken) {
this.args = new Hash(tagToken.args)
},
render: function*(context) {
let { src, width, height, alt, className, href, caption } = yield this.args.render(context)
let images = ''
let srcs = src.replace(/ /g,'').split(',')
alt = alt ? alt : caption
srcs.forEach(src => {
images += image(src, width, height, alt, className, href)
})
if (caption) {
caption = yield liquidEngine.parseAndRender(caption, context)
caption = markdownIt({ html: true, breaks: true }).render(caption)
caption = caption.replaceAll('<p>', '').replaceAll('</p>', '')
caption = `<figcaption>${caption}</figcaption>`
} else {
caption = ''
}
return `<figure>${images}${caption}</figure>`
}
}
})
}