-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (84 loc) · 2.49 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
import { visit } from "unist-util-visit";
import { u } from "unist-builder";
import octicons from "@primer/octicons";
// Extend the String prototype to include a toTitleCase method
String.prototype.toTitleCase = function () {
return this.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(" ");
};
class Callout {
static NOTE = "NOTE";
static TIP = "TIP";
static IMPORTANT = "IMPORTANT";
static WARNING = "WARNING";
static CAUTION = "CAUTION";
static toIcon(type) {
switch (type) {
case Callout.NOTE:
return "info";
case Callout.TIP:
return "light-bulb";
case Callout.IMPORTANT:
return "report";
case Callout.WARNING:
return "alert";
case Callout.CAUTION:
return "stop";
default:
return null;
}
}
}
// Remark plugin to transform blockquotes with callout directives
// (e.g., [!NOTE]) into styled HTML elements.
export default function remarkCallouts() {
return (tree) => {
// Traverse the Markdown AST and find blockquote nodes
visit(tree, "blockquote", (node, index, parent) => {
// Check for the callout pattern in the first line of the blockquote
const cell = node.children[0]?.children?.[0];
const regx = cell?.value?.trim().match(/^\[!(\w+)\]/s);
if (!regx) return;
// Extract callout type (e.g., note, tip, warning)
cell.value = cell.value.slice(regx[0].length);
const type = regx[1];
// Replace blockquote with a div representing the callout
parent.children.splice(
index,
1,
u(
"containerDirective",
{
data: {
hName: "div",
hProperties: {
className: `callout callout-${type.toLowerCase()}`,
},
},
},
[
// Add the title as a normal paragraph with a specific class
u(
"paragraph",
{
data: {
hProperties: { className: `callout-title` },
},
},
[
u("html", octicons[Callout.toIcon(type)].toSVG()),
{
type: "text",
value: type.toTitleCase(),
},
],
),
// Add each line of content as a separate paragraph
...node.children.filter((node) => node.children[0]?.value),
],
),
);
});
};
}