-
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
-
224
-
225
-
226
-
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
-
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
// 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 { css } from "../css.ts";
const enum C {
BlockContainer = "fm-code--bc",
InlineCode = "fm-code--i",
TokenKeyword = "fm-code--tk",
TokenBuiltin = "fm-code--tb",
TokenClassName = "fm-code--tc",
TokenFunction = "fm-code--tf",
TokenBoolean = "fm-code--tl",
TokenNumber = "fm-code--tn",
TokenString = "fm-code--ts",
TokenChar = "fm-code--th",
TokenSymbol = "fm-code--ty",
TokenRegex = "fm-code--tr",
TokenUrl = "fm-code--tu",
TokenOperator = "fm-code--to",
TokenVariable = "fm-code--tv",
TokenConstant = "fm-code--ta",
TokenProperty = "fm-code--tp",
TokenPunctuation = "fm-code--tpu",
TokenImportant = "fm-code--ti",
TokenComment = "fm-code--tcm",
TokenTag = "fm-code--tt",
TokenAttrName = "fm-code--tan",
TokenAttrValue = "fm-code--tav",
TokenNamespace = "fm-code--tns",
TokenProlog = "fm-code--tg",
TokenDoctype = "fm-code--td",
TokenCdata = "fm-code--tcd",
TokenEntity = "fm-code--te",
TokenBold = "fm-code--tbl",
TokenItalic = "fm-code--til",
TokenAtrule = "fm-code--tat",
TokenSelector = "fm-code--tsl",
TokenInserted = "fm-code--tin",
TokenDeleted = "fm-code--tdl",
}
// https://prismjs.com/tokens.html
const prismTokenToClassMap = new Map<string, string>([
["keyword", C.TokenKeyword],
["builtin", C.TokenBuiltin],
["class-name", C.TokenClassName],
["function", C.TokenFunction],
["boolean", C.TokenBoolean],
["number", C.TokenNumber],
["string", C.TokenString],
["char", C.TokenChar],
["symbol", C.TokenSymbol],
["regex", C.TokenRegex],
["url", C.TokenUrl],
["operator", C.TokenOperator],
["variable", C.TokenVariable],
["constant", C.TokenConstant],
["property", C.TokenProperty],
["punctuation", C.TokenPunctuation],
["important", C.TokenImportant],
["comment", C.TokenComment],
["tag", C.TokenTag],
["attr-name", C.TokenAttrName],
["attr-value", C.TokenAttrValue],
["namespace", C.TokenNamespace],
["prolog", C.TokenProlog],
["doctype", C.TokenDoctype],
["cdata", C.TokenCdata],
["entity", C.TokenEntity],
["bold", C.TokenBold],
["italic", C.TokenItalic],
["atrule", C.TokenAtrule],
["selector", C.TokenSelector],
["inserted", C.TokenInserted],
["deleted", C.TokenDeleted],
]);
// 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.TokenComment} {
opacity: 0.65;
}
.${C.TokenString},
.${C.TokenRegex},
.${C.TokenAttrValue},
.${C.TokenNumber} {
color: var(--_accent);
}
.${C.TokenOperator},
.${C.TokenVariable},
.${C.TokenConstant} {
color: var(--color-fg);
}
.${C.TokenFunction},
.${C.TokenPunctuation},
.${C.TokenClassName} {
color: var(--color-fg-sub);
opacity: 0.9;
}
.${C.TokenKeyword} {
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
: className.split(" ");
let replacedClassName: string | undefined;
if (classNames.includes("token")) {
replacedClassName = classNames.filter((c) => c !== "token").map(
(c) => {
return prismTokenToClassMap.get(c);
},
).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]);
},
};
}