Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 18, 2023
1 parent 3658edd commit 2cc0743
Show file tree
Hide file tree
Showing 14 changed files with 1,007 additions and 828 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@
"rehype-parse": "^9.0.0",
"rehype-stringify": "^10.0.0",
"remark-cli": "^11.0.0",
"remark-html": "^15.0.0",
"remark-parse": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"remark-rehype": "^10.0.0",
"remark-stringify": "^11.0.0",
"to-vfile": "^8.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"unified": "^11.0.0",
"unist-builder": "^4.0.0",
"unist-util-remove-position": "^5.0.0",
"xo": "^0.56.0"
},
Expand Down
86 changes: 51 additions & 35 deletions packages/rehype-katex/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
/**
* @typedef {import('hast').ElementContent} ElementContent
* @typedef {import('hast').Root} Root
*
* @typedef {import('katex').KatexOptions} Options
*
* @typedef {import('vfile').VFile} VFile
*/

import {fromHtmlIsomorphic} from 'hast-util-from-html-isomorphic'
import {toText} from 'hast-util-to-text'
import katex from 'katex'
import {visit} from 'unist-util-visit'
import {toText} from 'hast-util-to-text'
import {fromHtmlIsomorphic} from 'hast-util-from-html-isomorphic'

const assign = Object.assign

const source = 'rehype-katex'
/** @type {Readonly<Options>} */
const emptyOptions = {}
/** @type {ReadonlyArray<unknown>} */
const emptyClasses = []

/**
* Plugin to transform `<span class=math-inline>` and `<div class=math-display>`
* with KaTeX.
*
* @type {import('unified').Plugin<[Options?]|void[], Root>}
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns
* Transform.
*/
export default function rehypeKatex(options) {
const settings = options || {}
const settings = options || emptyOptions
const throwOnError = settings.throwOnError || false

return (tree, file) => {
visit(tree, 'element', (element) => {
const classes =
element.properties && Array.isArray(element.properties.className)
? element.properties.className
: []
/**
* Transform.
*
* @param {Root} tree
* Tree.
* @param {VFile} file
* File.
* @returns {undefined}
* Nothing.
*/
return function (tree, file) {
visit(tree, 'element', function (element) {
const classes = Array.isArray(element.properties.className)
? element.properties.className
: emptyClasses
const inline = classes.includes('math-inline')
const displayMode = classes.includes('math-display')

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

try {
result = katex.renderToString(
value,
assign({}, settings, {displayMode, throwOnError: true})
)
} catch (error_) {
const error = /** @type {Error} */ (error_)
result = katex.renderToString(value, {
...settings,
displayMode,
throwOnError: true
})
} catch (error) {
const exception = /** @type {Error} */ (error)
const fn = throwOnError ? 'fail' : 'message'
const origin = [source, error.name.toLowerCase()].join(':')
const origin = ['rehype-katex', exception.name.toLowerCase()].join(':')

file[fn](error.message, element.position, origin)
file[fn](exception.message, element.position, origin)

// KaTeX can handle `ParseError` itself, but not others.
// Generate similar markup if this is an other error.
// See: <https://github.com/KaTeX/KaTeX/blob/5dc7af0/docs/error.md>.
if (error.name !== 'ParseError') {
if (exception.name !== 'ParseError') {
element.children = [
{
type: 'element',
tagName: 'span',
properties: {
className: ['katex-error'],
title: String(error),
style: 'color:' + (settings.errorColor || '#cc0000')
style: 'color:' + (settings.errorColor || '#cc0000'),
title: String(error)
},
children: [{type: 'text', value}]
}
]
return
}

result = katex.renderToString(
value,
assign({}, settings, {
displayMode,
throwOnError: false,
strict: 'ignore'
})
)
result = katex.renderToString(value, {
...settings,
displayMode,
strict: 'ignore',
throwOnError: false
})
}

const root = fromHtmlIsomorphic(result, {fragment: true})
// To do: cast content.
// @ts-expect-error: assume no `doctypes` in KaTeX result.
element.children = root.children
// Cast because there will not be `doctypes` in KaTeX result.
const content = /** @type {Array<ElementContent>} */ (root.children)
element.children = content
})
}
}
3 changes: 2 additions & 1 deletion packages/rehype-katex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"hast-util-from-html-isomorphic": "^2.0.0",
"hast-util-to-text": "^4.0.0",
"katex": "^0.16.0",
"unist-util-visit": "^5.0.0"
"unist-util-visit": "^5.0.0",
"vfile": "^6.0.0"
},
"scripts": {
"test-api": "node --conditions development test.js",
Expand Down
Loading

0 comments on commit 2cc0743

Please sign in to comment.