-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
76 lines (66 loc) · 1.69 KB
/
index.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
const visit = require("unist-util-visit")
const unified = require("unified")
const markdown = require("remark-parse")
module.exports = ({markdownAST}, pluginOptions) => {
visit(markdownAST, "code", (node) => {
if (!node.lang || node.lang.indexOf("grid") === -1) {
return
}
const className = pluginOptions.className
? pluginOptions.className
: "gatsbyRemarkImagesGrid"
const meta = node.lang + (node.meta ? " " + node.meta : "")
let [, columnsCount, figcaption] = meta.split("|")
if (!columnsCount) {
columnsCount = 1
}
const contentAST = unified().use(markdown).parse(node.value)
let imagesNodes = []
visit(contentAST, "image", (node) => {
node.data = {
hProperties: {
width: "100%;",
},
}
imagesNodes.push(node)
})
const figcaptionNode = figcaption && {
type: "paragraph",
data: {
hName: "figcaption",
hProperties: {
className: `${className}-figcaption`,
},
},
children: [
{
type: "text",
value: figcaption,
},
],
}
const gridNode = {
type: "parent",
data: {
hName: "div",
hProperties: {
className: `${className}-grid`,
style: `
grid-template-columns: repeat(auto-fill, minmax(${
Math.floor(100 / columnsCount) - 2
}%, 1fr));
`,
},
},
children: imagesNodes,
}
node.type = "paragraph"
node.children = [gridNode, ...(figcaptionNode ? [figcaptionNode] : [])]
node.data = {
hName: "figure",
hProperties: {
className: className,
},
}
})
}