UNPKG

830 BJavaScriptView Raw
1/**
2 * @typedef {import('hast').Root} Root
3 * @typedef {import('hast-util-to-html').Options} Options
4 * @typedef {import('unified').Compiler<Root, string>} Compiler
5 */
6
7import {toHtml} from 'hast-util-to-html'
8
9/**
10 * Plugin to add support for serializing as HTML.
11 *
12 * @param {Options | null | undefined} [options]
13 * Configuration (optional).
14 * @returns {undefined}
15 * Nothing.
16 */
17export default function rehypeStringify(options) {
18 /** @type {import('unified').Processor<undefined, undefined, undefined, Root, string>} */
19 // @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
20 const self = this
21 const settings = {...self.data('settings'), ...options}
22
23 self.compiler = compiler
24
25 /**
26 * @type {Compiler}
27 */
28 function compiler(tree) {
29 return toHtml(tree, settings)
30 }
31}