-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (67 loc) · 1.98 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
77
78
79
80
81
82
83
84
85
86
const path = require("path");
const fs = require("fs").promises;
const visit = require("unist-util-visit");
const renderToSvg = require("lottie-to-svg");
const getOptions = require("./opts");
const isLottieAnimation = node =>
node.type === "image" && node.url.endsWith(".json");
module.exports = async ({ markdownAST, cache }, pluginOptions) => {
const nodes = [];
visit(markdownAST, "image", (node, idx, parent) => {
if (isLottieAnimation(node)) {
nodes.push({ node, parent });
}
});
const {
renderer,
loop,
autoplay,
generatePlaceholders,
rendererSettings
} = getOptions(pluginOptions);
for (let item of nodes) {
let { node, parent } = item;
const svgPreviewUrl = await loadSvgPreview({
generatePlaceholders,
url: node.url,
rendererSettings,
cache
});
const { url, alt, title } = node;
if (parent.type === "paragraph" && parent.children.length === 1) {
// it's a paragraph with just the image in it, replace it with our lottie div
node = parent;
}
node.type = "html";
node.value = `<div class="lottie" data-animation-path="${url}" data-anim-type="${renderer}" data-anim-loop="${loop}" data-anim-autoplay="${autoplay}">
${
svgPreviewUrl
? `<img src="${svgPreviewUrl}" alt="${alt}" title="${title}" />`
: ""
}
</div>`;
node.children = null;
}
return markdownAST;
};
async function loadSvgPreview({
generatePlaceholders,
url,
rendererSettings,
cache
}) {
if (!generatePlaceholders) return "";
const publicPath = path.join(process.cwd(), "public");
const lottiePath = path.join(publicPath, url);
const svgUrl = url.replace(".json", ".svg");
const svgPath = path.join(publicPath, svgUrl);
let result = await cache.get(svgUrl);
if (!result) {
const jsonContent = await fs.readFile(lottiePath, "utf8");
const animationData = JSON.parse(jsonContent);
result = await renderToSvg(animationData, rendererSettings);
cache.set(svgUrl, result);
}
await fs.writeFile(svgPath, result, "utf8");
return svgUrl;
}