|
1 | 1 | /**
|
2 |
| - * @typedef {import('hast').ElementContent} ElementContent |
3 |
| - * @typedef {import('hast').Root} Root |
4 |
| - * |
5 |
| - * @typedef {import('katex').KatexOptions} KatexOptions |
6 |
| - * |
7 |
| - * @typedef {import('vfile').VFile} VFile |
| 2 | + * @typedef {import('./lib/index.js').Options} Options |
8 | 3 | */
|
9 | 4 |
|
10 |
| -/** |
11 |
| - * @typedef {Omit<KatexOptions, 'throwOnError'>} Options |
12 |
| - */ |
13 |
| - |
14 |
| -import {fromHtmlIsomorphic} from 'hast-util-from-html-isomorphic' |
15 |
| -import {toText} from 'hast-util-to-text' |
16 |
| -import katex from 'katex' |
17 |
| -import {visit} from 'unist-util-visit' |
18 |
| - |
19 |
| -/** @type {Readonly<Options>} */ |
20 |
| -const emptyOptions = {} |
21 |
| -/** @type {ReadonlyArray<unknown>} */ |
22 |
| -const emptyClasses = [] |
23 |
| - |
24 |
| -/** |
25 |
| - * Plugin to transform `<span class=math-inline>` and `<div class=math-display>` |
26 |
| - * with KaTeX. |
27 |
| - * |
28 |
| - * @param {Readonly<Options> | null | undefined} [options] |
29 |
| - * Configuration (optional). |
30 |
| - * @returns |
31 |
| - * Transform. |
32 |
| - */ |
33 |
| -export default function rehypeKatex(options) { |
34 |
| - const settings = options || emptyOptions |
35 |
| - |
36 |
| - /** |
37 |
| - * Transform. |
38 |
| - * |
39 |
| - * @param {Root} tree |
40 |
| - * Tree. |
41 |
| - * @param {VFile} file |
42 |
| - * File. |
43 |
| - * @returns {undefined} |
44 |
| - * Nothing. |
45 |
| - */ |
46 |
| - return function (tree, file) { |
47 |
| - visit(tree, 'element', function (element, _, parent) { |
48 |
| - const classes = Array.isArray(element.properties.className) |
49 |
| - ? element.properties.className |
50 |
| - : emptyClasses |
51 |
| - const inline = classes.includes('math-inline') |
52 |
| - const displayMode = classes.includes('math-display') |
53 |
| - |
54 |
| - if (!inline && !displayMode) { |
55 |
| - return |
56 |
| - } |
57 |
| - |
58 |
| - const value = toText(element, {whitespace: 'pre'}) |
59 |
| - |
60 |
| - /** @type {string} */ |
61 |
| - let result |
62 |
| - |
63 |
| - try { |
64 |
| - result = katex.renderToString(value, { |
65 |
| - ...settings, |
66 |
| - displayMode, |
67 |
| - throwOnError: true |
68 |
| - }) |
69 |
| - } catch (error) { |
70 |
| - const cause = /** @type {Error} */ (error) |
71 |
| - const ruleId = cause.name.toLowerCase() |
72 |
| - |
73 |
| - file.message('Could not render math with KaTeX', { |
74 |
| - /* c8 ignore next -- verbose to test */ |
75 |
| - ancestors: parent ? [parent, element] : [element], |
76 |
| - cause, |
77 |
| - place: element.position, |
78 |
| - ruleId, |
79 |
| - source: 'rehype-katex' |
80 |
| - }) |
81 |
| - |
82 |
| - // KaTeX can handle `ParseError` itself, but not others. |
83 |
| - if (ruleId === 'parseerror') { |
84 |
| - result = katex.renderToString(value, { |
85 |
| - ...settings, |
86 |
| - displayMode, |
87 |
| - strict: 'ignore', |
88 |
| - throwOnError: false |
89 |
| - }) |
90 |
| - } |
91 |
| - // Generate similar markup if this is an other error. |
92 |
| - // See: <https://github.com/KaTeX/KaTeX/blob/5dc7af0/docs/error.md>. |
93 |
| - else { |
94 |
| - element.children = [ |
95 |
| - { |
96 |
| - type: 'element', |
97 |
| - tagName: 'span', |
98 |
| - properties: { |
99 |
| - className: ['katex-error'], |
100 |
| - style: 'color:' + (settings.errorColor || '#cc0000'), |
101 |
| - title: String(error) |
102 |
| - }, |
103 |
| - children: [{type: 'text', value}] |
104 |
| - } |
105 |
| - ] |
106 |
| - return |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - const root = fromHtmlIsomorphic(result, {fragment: true}) |
111 |
| - // Cast because there will not be `doctypes` in KaTeX result. |
112 |
| - const content = /** @type {Array<ElementContent>} */ (root.children) |
113 |
| - element.children = content |
114 |
| - }) |
115 |
| - } |
116 |
| -} |
| 5 | +export {default} from './lib/index.js' |
0 commit comments