-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
197 lines (181 loc) · 4.73 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { visit } from "unist-util-visit";
import {
FlameIcon,
InfoIcon,
PencilIcon,
ClipboardListIcon,
CheckCircle2Icon,
CheckIcon,
HelpCircleIcon,
ListIcon,
QuoteIcon,
AlertTriangleIcon,
XIcon,
ZapIcon,
BugIcon,
} from "lucide-react";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
const icons = new Map();
// notes
icons.set("note", PencilIcon);
// abstracts
icons.set("abstract", ClipboardListIcon);
icons.set("summary", ClipboardListIcon);
icons.set("tldr", ClipboardListIcon);
// info
icons.set("info", InfoIcon);
// to do
icons.set("todo", CheckCircle2Icon);
// tip
icons.set("tip", FlameIcon);
icons.set("hint", FlameIcon);
icons.set("important", FlameIcon);
// success
icons.set("success", CheckIcon);
icons.set("check", CheckIcon);
icons.set("done", CheckIcon);
// question
icons.set("question", HelpCircleIcon);
icons.set("help", HelpCircleIcon);
icons.set("faq", HelpCircleIcon);
// warning
icons.set("warning", AlertTriangleIcon);
icons.set("caution", AlertTriangleIcon);
icons.set("attention", AlertTriangleIcon);
// failure
icons.set("failure", XIcon);
icons.set("fail", XIcon);
icons.set("missing", XIcon);
// danger
icons.set("danger", ZapIcon);
icons.set("error", ZapIcon);
// bug
icons.set("bug", BugIcon);
// example
icons.set("example", ListIcon);
// quote
icons.set("quote", QuoteIcon);
icons.set("cite", QuoteIcon);
export default function remarkObsidian() {
function blockVisitor(node) {
/*
Callouts are blockquotes denoted by [!type].
Blockquotes have paragraph children, who have text children.
*/
let quote = node.children[0].children[0].value;
if (quote.startsWith("[!")) {
// we have a callout. check for titles
let [title, ...content] = quote.split("\n");
let callout = title.split(" ")[0].slice(2, -1);
// get the icon
let iconMarkup = renderToStaticMarkup(createElement(icons.get(callout)));
let iconNode = {
type: "html",
value: iconMarkup,
};
let iconDiv = {
type: "div",
children: [iconNode],
data: {
hProperties: {
className: "callout-icon",
},
},
};
// get the title
let titleNode = {
type: "text",
value: title.split(" ").slice(1).join(" "),
};
let titleText = {
type: "div",
children: [titleNode, ...node.children[0].children.slice(1)],
data: {
hProperties: {
className: "callout-title-inner",
},
},
};
// change the title to a div including icon and rest of text
let titleDiv = {
type: "div",
children: [iconDiv, titleText],
data: {
hProperties: {
className: "callout-title",
},
},
};
// get the content
let contentNode = {
type: "text",
value: content.join("\n"),
};
let contentDiv = {
type: "div",
children: [
...(content.length > 0 ? [contentNode] : []),
...node.children.slice(1),
],
data: {
hProperties: {
className: "callout-content",
},
},
};
// reformat block quote as div and add children
node.type = "div";
node.data = {
hProperties: {
className: "callout",
callout: callout,
},
};
if (contentDiv.children.length > 0)
node.children = [titleDiv, contentDiv];
else node.children = [titleDiv];
}
}
function commentVisitor(node) {
node.children = node.children.map((child) => {
if (child.type === "text") {
child.value = child.value.replace(/%%[^=]+%%/g, "");
}
return child;
});
}
function highlightVisitor(node) {
visit(node, "text", (child, index, parent) => {
const regex = /==(.*?)==/g;
let match;
let parts = [];
let lastIndex = 0;
while ((match = regex.exec(child.value)) !== null) {
if (match.index > lastIndex) {
parts.push({
type: "text",
value: child.value.slice(lastIndex, match.index),
});
}
parts.push({
type: "html",
value: `<span class="highlight">${match[1]}</span>`,
});
lastIndex = regex.lastIndex;
}
if (lastIndex < child.value.length) {
parts.push({ type: "text", value: child.value.slice(lastIndex) });
}
if (parts.length > 0) {
parent.children.splice(index, 1, ...parts);
}
});
}
function transform(tree) {
visit(tree, "blockquote", blockVisitor);
visit(tree, "paragraph", commentVisitor);
visit(tree, "paragraph", highlightVisitor);
}
return transform;
}