-
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
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
-
206
-
207
-
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
-
216
-
217
-
218
-
219
-
220
-
221
-
222
-
223
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
/** @jsx h */
import { visit } from "../../../../deps/esm.sh/unist-util-visit/mod.ts";
import type * as Mdast from "../../../../deps/esm.sh/mdast/types.ts";
import type * as Hast from "../../../../deps/esm.sh/hast/types.ts";
import { refractor } from "../../../../deps/esm.sh/refractor/mod.ts";
import { h } from "../../../../deps/esm.sh/hastscript/mod.ts";
import { type Handlers } from "../../../../deps/esm.sh/mdast-util-to-hast/mod.ts";
import { buildClasses, css } from "../css.ts";
const c = buildClasses("fm-cd", [
"blockContainer",
"inlineCode",
// https://prismjs.com/tokens.html
"keyword",
"builtin",
"class-name",
"function",
"boolean",
"number",
"string",
"char",
"symbol",
"regex",
"url",
"operator",
"variable",
"constant",
"property",
"punctuation",
"important",
"comment",
"tag",
"attr-name",
"attr-value",
"namespace",
"prolog",
"doctype",
"cdata",
"entity",
"bold",
"italic",
"atrule",
"selector",
"inserted",
"deleted",
]);
// This highlighting style is inspired by https://github.com/Alligator/accent.vim
// Code block intentionally breaks vertical rhythm: while code block contents is
// textual, they are normally interrupts main contents' flow by default and needs
// to stand out. In addition to that, reading code written in vertical rhythm is
// simply a pain...
// TODO: Make tabsize configurable (build config and runtime option if possible)
// TODO: Make font-family configurable
// TODO: Make accent color configurable
export const codeStyles = css`
.${c.blockContainer} {
--_accent: var(--color-primary);
tab-size: 4ch;
margin: 0;
margin-top: calc(var(--baseline) * 1rem);
padding: calc(var(--baseline) * 0.5rem) 1em;
line-height: 1.5;
max-width: 100%;
font-size: 1rem;
font-family: monospace;
font-size: 0.9rem;
background-color: var(--color-bg-light);
color: var(--color-fg);
border-radius: 4px;
overflow-x: auto;
}
.${c.inlineCode} {
margin: 0 0.2em;
padding: calc(1rem / 4);
font-family: monospace;
font-size: 0.8rem;
background-color: var(--color-bg-light);
color: var(--color-fg-sub);
border-radius: 4px;
}
.${c.comment} {
opacity: 0.65;
}
.${c.string},
.${c.regex},
.${c["attr-value"]},
.${c.number} {
color: var(--_accent);
}
.${c.operator},
.${c.variable},
.${c.constant} {
color: var(--color-fg);
}
.${c.function},
.${c.punctuation},
.${c["class-name"]} {
color: var(--color-fg-sub);
opacity: 0.9;
}
.${c.keyword} {
color: var(--color-fg-sub);
font-weight: bold;
}
`;
function isValidClassName(value: unknown): value is string | readonly string[] {
if (typeof value === "string") {
return true;
}
if (Array.isArray(value) && value.every((x) => typeof x === "string")) {
return true;
}
return false;
}
export interface CodeHandlersOptions {
/**
* Class name to add to the container element.
* `null` to not setting class.
* This is for user writing their own styles. Macana sets its own class
* to the generated `<pre>` element.
*
* @default null
*/
className?: string | null;
/**
* Attribute name to set space separated list of node type (e.g. "token", "string", "comment").
* `null` to not set node types to attribute.
*
* @default null
*/
nodeTypeAttribute?: string | null;
/**
* Attribute name to set language name (e.g. "css", "html")
* `null` to not set language name to attribute.
*
* @default null
*/
langNameAttribute?: string | null;
}
export function codeHandlers({
className = null,
nodeTypeAttribute = null,
langNameAttribute = null,
}: CodeHandlersOptions = {}): Handlers {
return {
code(_state, node: Mdast.Code) {
if (!node.lang || !refractor.registered(node.lang)) {
return h("pre", { class: c.blockContainer }, [
<code>{node.value}</code>,
]);
}
const code = refractor.highlight(node.value, node.lang);
visit(code, (node) => node.type === "element", (node) => {
if (node.type !== "element") {
return;
}
if (!node.properties || !isValidClassName(node.properties.className)) {
return;
}
const className = node.properties.className;
if (!className) {
return;
}
const classNames = Array.isArray(className)
? (className as string[])
: className.split(" ");
let replacedClassName: string | undefined;
if (classNames.includes("token")) {
replacedClassName = classNames.filter((c) => c !== "token").map(
(className) => {
return className in c ? c[className as keyof typeof c] : null;
},
).filter((c) => !!c).join(" ");
}
node.properties.className = replacedClassName;
if (typeof nodeTypeAttribute === "string") {
node.properties[nodeTypeAttribute] = className;
}
});
return h("pre", {
class: [c.blockContainer, className].filter((s): s is string => !!s)
.join(" "),
...(langNameAttribute ? { [langNameAttribute]: node.lang } : {}),
}, [
<code>{code.children as Hast.ElementContent[]}</code>,
]);
},
inlineCode(_state, node: Mdast.InlineCode) {
return h("code", { class: c.inlineCode }, [node.value]);
},
};
}