-
Notifications
You must be signed in to change notification settings - Fork 0
/
modifyQuoteblocks.js
59 lines (47 loc) · 1.87 KB
/
modifyQuoteblocks.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
const blockQuoteIcons = {
NOTE: "fa-solid fa-circle-plus",
CAUTION: "fa-solid fa-circle-exclamation",
WARNING: "fa-solid fa-circle-exclamation",
IMPORTANT: "fa-solid fa-circle-info",
};
const blockQuotePrefixes = Object.keys(blockQuoteIcons);
/** @type {import("unified").Plugin} */
const modifyQuoteblocks = () => {
return async (root) => {
return { ...root, children: [...modifyQuoteNodes(root.children)] };
};
};
// Changes code to HTML
function* modifyQuoteNodes(nodes) {
for (const node of nodes) {
let quoteType;
// Ignore it
if (node.type !== "blockquote" || !(quoteType = hasPrefix(node))) {
yield node;
continue;
}
const { position } = node;
const quoteTypeName = quoteType.toLowerCase();
yield {
type: "html",
value: `<div class="quote-block type-${quoteTypeName} container"><header class="quote-block header"><i class="quote-block header-icon ${blockQuoteIcons[quoteType]}"></i><span class="quote-block header-text">${quoteTypeName}</span></header><article class="quote-block text">`,
position,
};
// Remove the prefix (`NOTE: ...` to `...`)
// Really hacky and bad way of doing it, but can't be bothered to add additional stuff to for loop
node.children[0].children[0].value =
node.children[0].children[0].value.substring(quoteType.length + 2);
for (const subnode of node.children) yield subnode;
yield { type: "html", value: `</article></div>`, position };
}
}
function hasPrefix(node) {
return (
node.children[0]?.type === "paragraph" &&
node.children[0].children[0]?.type === "text" &&
blockQuotePrefixes.find((prefix) =>
node.children[0].children[0].value.startsWith(`${prefix}: `)
)
);
}
export default modifyQuoteblocks;