/
index.js
29 lines (25 loc) · 882 Bytes
/
index.js
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
/**
* @typedef {import('hast').Root} Root
* @typedef {import('hast-util-to-jsx-runtime').Options} Options
* @typedef {import('unified').Compiler<Root, JSX.Element>} Compiler
* @typedef {import('unified').Processor<undefined, undefined, undefined, Root, JSX.Element>} Processor
*/
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'
/**
* Turn HTML into preact, react, solid, svelte, vue, etc.
*
* @param {Options} options
* Configuration (required).
* @returns {undefined}
* Nothing.
*/
export default function rehypeReact(options) {
// @ts-expect-error: TypeScript doesn’t handle `this` well.
// eslint-disable-next-line unicorn/no-this-assignment
const self = /** @type {Processor} */ (this)
self.compiler = compiler
/** @type {Compiler} */
function compiler(tree, file) {
return toJsxRuntime(tree, {filePath: file.path, ...options})
}
}