This repository has been archived by the owner on Aug 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
150 lines (126 loc) · 3.17 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// ESM port of https://github.com/tkhquang/gridsome-remark-figure-caption
import { visit } from "unist-util-visit";
import { whitespace } from "hast-util-whitespace";
import { remove } from "unist-util-remove";
import { fromMarkdown } from "mdast-util-from-markdown";
/** @type {import("unified").Plugin<[], import("mdast").Root>} */
export default function remarkFigureCaption(options = {}) {
return (tree) => {
// Unwrap the images inside Paragraphs
visit(tree, "paragraph", (node, index, parent) => {
if (!hasOnlyImages(node)) {
return;
}
remove(node, "text");
parent.children.splice(index, 1, ...node.children);
return index;
});
// Wrap image nodes in figure
visit(
tree,
(node) => isImageWithAlt(node),
(node, index, parent) => {
if (isImageWithCaption(parent) || isImageLink(parent)) {
return;
}
const figure = createNodes(node, options);
node.type = figure.type;
node.children = figure.children;
node.data = figure.data;
}
);
};
}
const createNodes = (imageNode, { figureClassName, imageClassName, captionClassName }) => {
let figchildren = null;
try {
figchildren = fromMarkdown(imageNode.alt).children.flatMap(node => node.children);
} catch (e) {
console.log(`figure-caption-plugin: Failed to parse image alt-text as markdown - using raw value as fallback: ${imageNode.alt}`);
figchildren = [
{
type: "text",
value: imageNode.alt,
},
];
}
const figcaption = {
type: "figcaption",
children: figchildren,
data: {
hName: "figcaption",
...getClassProp(captionClassName),
},
};
const figure = {
type: "figure",
children: [getImageNodeWithClasses(imageNode, imageClassName), figcaption],
data: {
hName: "figure",
...getClassProp(figureClassName),
},
};
return figure;
};
const hasOnlyImages = (node) => {
return node.children.every((child) => {
return child.type === "image" || whitespace(child);
});
};
const isImageNodeWithAlt = (node) => {
return node.type === "image" && Boolean(node.alt) && Boolean(node.url);
};
const isHTMLImageNode = (node) => {
return node.type === "html" && Boolean(node.alt) && /^<img\s/.test(node.value);
};
const isImageWithAlt = (node) => {
return isImageNodeWithAlt(node) || isHTMLImageNode(node);
};
const isImageWithCaption = (parent) => {
return (
parent.type === "figure" &&
parent.children.some((child) => child.type === "figcaption")
);
};
const isImageLink = (parent) => {
return (parent.type === "link");
};
const getClassProp = (className) => {
return {
...(className && {
hProperties: {
class: [className],
},
}),
};
};
const classRegex = /\sclass="(.*?)"\s/gi;
const getImageNodeWithClasses = (node, classes) => {
// Is Image type node
if (!isHTMLImageNode(node)) {
return {
...node,
data: {
...getClassProp(classes),
},
};
}
// is HTML Image node
if (!classes) {
return {
...node,
};
}
// Bruteforce adding classes for now
const hasClass = classRegex.exec(node.value);
if (!hasClass) {
return {
...node,
value: node.value.replace(/<img\s/, `<img class="${classes}" `),
};
}
return {
...node,
value: node.value.replace(classRegex, ` class="$1 ${classes}" `),
};
};