A world-class math input for the web
1export type VNode =
2 | {
3 type: string;
4 namespaceURI?: string;
5 attributes: { [key: string]: string };
6 children: VNode[];
7 }
8 | { text: string };
9
10export function h(
11 type: string,
12 attributes: { [key: string]: string },
13 ...children: (VNode | null | undefined | false)[]
14): VNode {
15 let namespaceURI: string | undefined;
16 if (mathmlTagNames.includes(type)) {
17 namespaceURI = "http://www.w3.org/1998/Math/MathML";
18 } else if (svgTagNames.includes(type)) {
19 namespaceURI = "http://www.w3.org/2000/svg";
20 }
21
22 return {
23 type,
24 namespaceURI,
25 attributes,
26 children: children.filter((child) => !!child),
27 };
28}
29
30export function t(text: string): VNode {
31 return { text };
32}
33
34const svgTagNames = [
35 "a",
36 "altGlyph",
37 "altGlyphDef",
38 "altGlyphItem",
39 "animate",
40 "animateColor",
41 "animateMotion",
42 "animateTransform",
43 "animation",
44 "audio",
45 "canvas",
46 "circle",
47 "clipPath",
48 "color-profile",
49 "cursor",
50 "defs",
51 "desc",
52 "discard",
53 "ellipse",
54 "feBlend",
55 "feColorMatrix",
56 "feComponentTransfer",
57 "feComposite",
58 "feConvolveMatrix",
59 "feDiffuseLighting",
60 "feDisplacementMap",
61 "feDistantLight",
62 "feDropShadow",
63 "feFlood",
64 "feFuncA",
65 "feFuncB",
66 "feFuncG",
67 "feFuncR",
68 "feGaussianBlur",
69 "feImage",
70 "feMerge",
71 "feMergeNode",
72 "feMorphology",
73 "feOffset",
74 "fePointLight",
75 "feSpecularLighting",
76 "feSpotLight",
77 "feTile",
78 "feTurbulence",
79 "filter",
80 "font",
81 "font-face",
82 "font-face-format",
83 "font-face-name",
84 "font-face-src",
85 "font-face-uri",
86 "foreignObject",
87 "g",
88 "glyph",
89 "glyphRef",
90 "handler",
91 "hkern",
92 "iframe",
93 "image",
94 "line",
95 "linearGradient",
96 "listener",
97 "marker",
98 "mask",
99 "metadata",
100 "missing-glyph",
101 "mpath",
102 "path",
103 "pattern",
104 "polygon",
105 "polyline",
106 "prefetch",
107 "radialGradient",
108 "rect",
109 "script",
110 "set",
111 "solidColor",
112 "stop",
113 "style",
114 "svg",
115 "switch",
116 "symbol",
117 "tbreak",
118 "text",
119 "textArea",
120 "textPath",
121 "title",
122 "tref",
123 "tspan",
124 "unknown",
125 "use",
126 "video",
127 "view",
128 "vkern",
129];
130
131const mathmlTagNames = [
132 "abs",
133 "and",
134 "annotation",
135 "annotation-xml",
136 "apply",
137 "approx",
138 "arccos",
139 "arccosh",
140 "arccot",
141 "arccoth",
142 "arccsc",
143 "arccsch",
144 "arcsec",
145 "arcsech",
146 "arcsin",
147 "arcsinh",
148 "arctan",
149 "arctanh",
150 "arg",
151 "bind",
152 "bvar",
153 "card",
154 "cartesianproduct",
155 "cbytes",
156 "ceiling",
157 "cerror",
158 "ci",
159 "cn",
160 "codomain",
161 "complexes",
162 "compose",
163 "condition",
164 "conjugate",
165 "cos",
166 "cosh",
167 "cot",
168 "coth",
169 "cs",
170 "csc",
171 "csch",
172 "csymbol",
173 "curl",
174 "declare",
175 "degree",
176 "determinant",
177 "diff",
178 "divergence",
179 "divide",
180 "domain",
181 "domainofapplication",
182 "emptyset",
183 "encoding",
184 "eq",
185 "equivalent",
186 "eulergamma",
187 "exists",
188 "exp",
189 "exponentiale",
190 "factorial",
191 "factorof",
192 "false",
193 "floor",
194 "fn",
195 "forall",
196 "function",
197 "gcd",
198 "geq",
199 "grad",
200 "gt",
201 "ident",
202 "image",
203 "imaginary",
204 "imaginaryi",
205 "implies",
206 "in",
207 "infinity",
208 "int",
209 "integers",
210 "intersect",
211 "interval",
212 "inverse",
213 "lambda",
214 "laplacian",
215 "lcm",
216 "leq",
217 "limit",
218 "list",
219 "ln",
220 "log",
221 "logbase",
222 "lowlimit",
223 "lt",
224 "maction",
225 "malign",
226 "maligngroup",
227 "malignmark",
228 "malignscope",
229 "math",
230 "matrix",
231 "matrixrow",
232 "max",
233 "mean",
234 "median",
235 "menclose",
236 "merror",
237 "mfenced",
238 "mfrac",
239 "mfraction",
240 "mglyph",
241 "mi",
242 "min",
243 "minus",
244 "mlabeledtr",
245 "mlongdiv",
246 "mmultiscripts",
247 "mn",
248 "mo",
249 "mode",
250 "moment",
251 "momentabout",
252 "mover",
253 "mpadded",
254 "mphantom",
255 "mprescripts",
256 "mroot",
257 "mrow",
258 "ms",
259 "mscarries",
260 "mscarry",
261 "msgroup",
262 "msline",
263 "mspace",
264 "msqrt",
265 "msrow",
266 "mstack",
267 "mstyle",
268 "msub",
269 "msubsup",
270 "msup",
271 "mtable",
272 "mtd",
273 "mtext",
274 "mtr",
275 "munder",
276 "munderover",
277 "naturalnumbers",
278 "neq",
279 "none",
280 "not",
281 "notanumber",
282 "notin",
283 "notprsubset",
284 "notsubset",
285 "or",
286 "otherwise",
287 "outerproduct",
288 "partialdiff",
289 "pi",
290 "piece",
291 "piecewice",
292 "piecewise",
293 "plus",
294 "power",
295 "primes",
296 "product",
297 "prsubset",
298 "quotient",
299 "rationals",
300 "real",
301 "reals",
302 "reln",
303 "rem",
304 "root",
305 "scalarproduct",
306 "sdev",
307 "sec",
308 "sech",
309 "select",
310 "selector",
311 "semantics",
312 "sep",
313 "set",
314 "setdiff",
315 "share",
316 "sin",
317 "sinh",
318 "subset",
319 "sum",
320 "tan",
321 "tanh",
322 "tendsto",
323 "times",
324 "transpose",
325 "true",
326 "union",
327 "uplimit",
328 "var",
329 "variance",
330 "vector",
331 "vectorproduct",
332 "xor",
333];