|
1 | 1 | /**
|
2 |
| - * @typedef {import('mdast').Root|import('mdast').Content} Node |
| 2 | + * @typedef {import('mdast').Root} Root |
3 | 3 | * @typedef {import('mdast-util-to-markdown').Options} ToMarkdownOptions
|
| 4 | + * @typedef {import('unified').Compiler<Root, string>} Compiler |
| 5 | + * @typedef {import('unified').Processor<undefined, undefined, undefined, Root, string>} Processor |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
4 | 9 | * @typedef {Omit<ToMarkdownOptions, 'extensions'>} Options
|
5 | 10 | */
|
6 | 11 |
|
7 | 12 | import {toMarkdown} from 'mdast-util-to-markdown'
|
8 | 13 |
|
9 | 14 | /**
|
10 |
| - * @this {import('unified').Processor} |
11 |
| - * @type {import('unified').Plugin<[Options?]|void[], Node, string>} |
| 15 | + * Add support for serializing as markdown. |
| 16 | + * |
| 17 | + * @param {Readonly<Options> | null | undefined} [options] |
| 18 | + * Configuration (optional). |
| 19 | + * @returns {undefined} |
| 20 | + * Nothing. |
12 | 21 | */
|
13 | 22 | export default function remarkStringify(options) {
|
14 |
| - /** @type {import('unified').Compiler<Node, string>} */ |
15 |
| - const compiler = (tree) => { |
| 23 | + /** @type {Processor} */ |
| 24 | + // @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly. |
| 25 | + const self = this |
| 26 | + |
| 27 | + self.compiler = compiler |
| 28 | + |
| 29 | + /** |
| 30 | + * @type {Compiler} |
| 31 | + */ |
| 32 | + function compiler(tree) { |
| 33 | + // To do: remove cast when typed. |
16 | 34 | // Assume options.
|
17 |
| - const settings = /** @type {Options} */ (this.data('settings')) |
| 35 | + const settings = /** @type {Options} */ (self.data('settings')) |
18 | 36 |
|
19 |
| - return toMarkdown( |
20 |
| - tree, |
21 |
| - Object.assign({}, settings, options, { |
22 |
| - // Note: this option is not in the readme. |
23 |
| - // The goal is for it to be set by plugins on `data` instead of being |
24 |
| - // passed by users. |
25 |
| - extensions: |
26 |
| - // @ts-expect-error: to do: type. |
27 |
| - /** @type {ToMarkdownOptions['extensions']} */ ( |
28 |
| - // @ts-expect-error: to do: type. |
29 |
| - this.data('toMarkdownExtensions') |
30 |
| - ) || [] |
31 |
| - }) |
32 |
| - ) |
33 |
| - } |
| 37 | + /** @type {ToMarkdownOptions} */ |
| 38 | + const resolvedOptions = { |
| 39 | + ...settings, |
| 40 | + ...options, |
| 41 | + // Note: this option is not in the readme. |
| 42 | + // The goal is for it to be set by plugins on `data` instead of being |
| 43 | + // passed by users. |
| 44 | + // @ts-expect-error: to do: type. |
| 45 | + extensions: self.data('toMarkdownExtensions') || [] |
| 46 | + } |
34 | 47 |
|
35 |
| - Object.assign(this, {Compiler: compiler}) |
| 48 | + return toMarkdown(tree, resolvedOptions) |
| 49 | + } |
36 | 50 | }
|
0 commit comments