Skip to content

Commit

Permalink
Refactor to use @imports
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 24, 2024
1 parent a5546fb commit 8ed43df
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
10 changes: 5 additions & 5 deletions packages/rehype-parse/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @typedef {import('hast').Root} Root
* @typedef {import('hast-util-from-html').Options} FromHtmlOptions
* @typedef {import('unified').Parser<Root>} Parser
* @import {Root} from 'hast'
* @import {Options as FromHtmlOptions} from 'hast-util-from-html'
* @import {Parser, Processor} from 'unified'
*/

/**
Expand Down Expand Up @@ -35,15 +35,15 @@ import {fromHtml} from 'hast-util-from-html'
* Nothing.
*/
export default function rehypeParse(options) {
/** @type {import('unified').Processor<Root>} */
/** @type {Processor<Root>} */
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
const self = this
const {emitParseErrors, ...settings} = {...self.data('settings'), ...options}

self.parser = parser

/**
* @type {Parser}
* @type {Parser<Root>}
*/
function parser(document, file) {
return fromHtml(document, {
Expand Down
10 changes: 5 additions & 5 deletions packages/rehype-stringify/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @typedef {import('hast').Root} Root
* @typedef {import('hast-util-to-html').Options} Options
* @typedef {import('unified').Compiler<Root, string>} Compiler
* @import {Root} from 'hast'
* @import {Options} from 'hast-util-to-html'
* @import {Compiler, Processor} from 'unified'
*/

import {toHtml} from 'hast-util-to-html'
Expand All @@ -15,15 +15,15 @@ import {toHtml} from 'hast-util-to-html'
* Nothing.
*/
export default function rehypeStringify(options) {
/** @type {import('unified').Processor<undefined, undefined, undefined, Root, string>} */
/** @type {Processor<undefined, undefined, undefined, Root, string>} */
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
const self = this
const settings = {...self.data('settings'), ...options}

self.compiler = compiler

/**
* @type {Compiler}
* @type {Compiler<Root, string>}
*/
function compiler(tree) {
return toHtml(tree, settings)
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ With another plugin, you can turn this HTML:
<details><summary>Show example code</summary>

```js
/**
* @import {Root} from 'hast'
*/

import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
Expand All @@ -121,7 +125,7 @@ console.log(String(file))

function myRehypePluginToIncreaseHeadings() {
/**
* @param {import('hast').Root} tree
* @param {Root} tree
*/
return function (tree) {
visit(tree, 'element', function (node) {
Expand Down
4 changes: 2 additions & 2 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @typedef {import('unified').Settings} Settings
* @typedef {import('hast').Root} Root
* @import {Settings} from 'unified'
* @import {Root} from 'hast'
*/

import assert from 'node:assert/strict'
Expand Down

0 comments on commit 8ed43df

Please sign in to comment.