Skip to content

Commit 2cc0743

Browse files
committed
Refactor code-style
1 parent 3658edd commit 2cc0743

File tree

14 files changed

+1007
-828
lines changed

14 files changed

+1007
-828
lines changed

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,13 @@
2828
"rehype-parse": "^9.0.0",
2929
"rehype-stringify": "^10.0.0",
3030
"remark-cli": "^11.0.0",
31-
"remark-html": "^15.0.0",
3231
"remark-parse": "^11.0.0",
3332
"remark-preset-wooorm": "^9.0.0",
3433
"remark-rehype": "^10.0.0",
3534
"remark-stringify": "^11.0.0",
36-
"to-vfile": "^8.0.0",
3735
"type-coverage": "^2.0.0",
3836
"typescript": "^5.0.0",
3937
"unified": "^11.0.0",
40-
"unist-builder": "^4.0.0",
4138
"unist-util-remove-position": "^5.0.0",
4239
"xo": "^0.56.0"
4340
},

packages/rehype-katex/index.js

+51-35
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
11
/**
2+
* @typedef {import('hast').ElementContent} ElementContent
23
* @typedef {import('hast').Root} Root
4+
*
35
* @typedef {import('katex').KatexOptions} Options
6+
*
7+
* @typedef {import('vfile').VFile} VFile
48
*/
59

10+
import {fromHtmlIsomorphic} from 'hast-util-from-html-isomorphic'
11+
import {toText} from 'hast-util-to-text'
612
import katex from 'katex'
713
import {visit} from 'unist-util-visit'
8-
import {toText} from 'hast-util-to-text'
9-
import {fromHtmlIsomorphic} from 'hast-util-from-html-isomorphic'
10-
11-
const assign = Object.assign
1214

13-
const source = 'rehype-katex'
15+
/** @type {Readonly<Options>} */
16+
const emptyOptions = {}
17+
/** @type {ReadonlyArray<unknown>} */
18+
const emptyClasses = []
1419

1520
/**
1621
* Plugin to transform `<span class=math-inline>` and `<div class=math-display>`
1722
* with KaTeX.
1823
*
19-
* @type {import('unified').Plugin<[Options?]|void[], Root>}
24+
* @param {Readonly<Options> | null | undefined} [options]
25+
* Configuration (optional).
26+
* @returns
27+
* Transform.
2028
*/
2129
export default function rehypeKatex(options) {
22-
const settings = options || {}
30+
const settings = options || emptyOptions
2331
const throwOnError = settings.throwOnError || false
2432

25-
return (tree, file) => {
26-
visit(tree, 'element', (element) => {
27-
const classes =
28-
element.properties && Array.isArray(element.properties.className)
29-
? element.properties.className
30-
: []
33+
/**
34+
* Transform.
35+
*
36+
* @param {Root} tree
37+
* Tree.
38+
* @param {VFile} file
39+
* File.
40+
* @returns {undefined}
41+
* Nothing.
42+
*/
43+
return function (tree, file) {
44+
visit(tree, 'element', function (element) {
45+
const classes = Array.isArray(element.properties.className)
46+
? element.properties.className
47+
: emptyClasses
3148
const inline = classes.includes('math-inline')
3249
const displayMode = classes.includes('math-display')
3350

@@ -41,50 +58,49 @@ export default function rehypeKatex(options) {
4158
let result
4259

4360
try {
44-
result = katex.renderToString(
45-
value,
46-
assign({}, settings, {displayMode, throwOnError: true})
47-
)
48-
} catch (error_) {
49-
const error = /** @type {Error} */ (error_)
61+
result = katex.renderToString(value, {
62+
...settings,
63+
displayMode,
64+
throwOnError: true
65+
})
66+
} catch (error) {
67+
const exception = /** @type {Error} */ (error)
5068
const fn = throwOnError ? 'fail' : 'message'
51-
const origin = [source, error.name.toLowerCase()].join(':')
69+
const origin = ['rehype-katex', exception.name.toLowerCase()].join(':')
5270

53-
file[fn](error.message, element.position, origin)
71+
file[fn](exception.message, element.position, origin)
5472

5573
// KaTeX can handle `ParseError` itself, but not others.
5674
// Generate similar markup if this is an other error.
5775
// See: <https://github.com/KaTeX/KaTeX/blob/5dc7af0/docs/error.md>.
58-
if (error.name !== 'ParseError') {
76+
if (exception.name !== 'ParseError') {
5977
element.children = [
6078
{
6179
type: 'element',
6280
tagName: 'span',
6381
properties: {
6482
className: ['katex-error'],
65-
title: String(error),
66-
style: 'color:' + (settings.errorColor || '#cc0000')
83+
style: 'color:' + (settings.errorColor || '#cc0000'),
84+
title: String(error)
6785
},
6886
children: [{type: 'text', value}]
6987
}
7088
]
7189
return
7290
}
7391

74-
result = katex.renderToString(
75-
value,
76-
assign({}, settings, {
77-
displayMode,
78-
throwOnError: false,
79-
strict: 'ignore'
80-
})
81-
)
92+
result = katex.renderToString(value, {
93+
...settings,
94+
displayMode,
95+
strict: 'ignore',
96+
throwOnError: false
97+
})
8298
}
8399

84100
const root = fromHtmlIsomorphic(result, {fragment: true})
85-
// To do: cast content.
86-
// @ts-expect-error: assume no `doctypes` in KaTeX result.
87-
element.children = root.children
101+
// Cast because there will not be `doctypes` in KaTeX result.
102+
const content = /** @type {Array<ElementContent>} */ (root.children)
103+
element.children = content
88104
})
89105
}
90106
}

packages/rehype-katex/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"hast-util-from-html-isomorphic": "^2.0.0",
4444
"hast-util-to-text": "^4.0.0",
4545
"katex": "^0.16.0",
46-
"unist-util-visit": "^5.0.0"
46+
"unist-util-visit": "^5.0.0",
47+
"vfile": "^6.0.0"
4748
},
4849
"scripts": {
4950
"test-api": "node --conditions development test.js",

0 commit comments

Comments
 (0)