From b1e7f8ee793471901230632de46635d5467993b1 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 26 Aug 2023 11:54:05 +0200 Subject: [PATCH] Refactor code-style --- lib/index.js | 25 ++++++++++++++++------ package.json | 3 ++- test.js | 60 +++++++++++++++++++++++++++++----------------------- 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3731210..80b2434 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,24 +1,35 @@ /** * @typedef {import('hast').Root} Root * @typedef {import('hast-util-raw').Options} RawOptions - * @typedef {import('hast-util-raw')} DoNotTouchAsThisImportIncludesRawInTree + * @typedef {import('vfile').VFile} VFile */ /** * @typedef {Omit} Options + * Configuration. */ import {raw} from 'hast-util-raw' /** - * Plugin to parse the tree again (and raw nodes). - * Keeping positional info OK. ๐Ÿ™Œ + * Parse the tree (and raw nodes) again, keeping positional info okay. * - * @type {import('unified').Plugin<[Options?] | Array, Root>} + * @param {Options | null | undefined} [options] + * Configuration (optional). + * @returns + * Transform. */ -export default function rehypeRaw(options = {}) { - return (tree, file) => { - // Assume that when a root was given, itโ€™s also returned. +export default function rehypeRaw(options) { + /** + * @param {Root} tree + * Tree. + * @param {VFile} file + * File. + * @returns {Root} + * New tree. + */ + return function (tree, file) { + // Assume root in -> root out. const result = /** @type {Root} */ (raw(tree, {...options, file})) return result } diff --git a/package.json b/package.json index 029db3e..817b2fb 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "dependencies": { "@types/hast": "^3.0.0", "hast-util-raw": "^9.0.0", - "unified": "^11.0.0" + "vfile": "^6.0.0" }, "devDependencies": { "@types/node": "^20.0.0", @@ -46,6 +46,7 @@ "remark-rehype": "^10.0.0", "type-coverage": "^2.0.0", "typescript": "^5.0.0", + "unified": "^11.0.0", "unist-util-visit": "^5.0.0", "xo": "^0.56.0" }, diff --git a/test.js b/test.js index fcdadd0..081f6d4 100644 --- a/test.js +++ b/test.js @@ -2,42 +2,50 @@ * @typedef {import('hast').Root} Root */ -import test from 'node:test' import assert from 'node:assert/strict' -import {unified} from 'unified' -import {visit} from 'unist-util-visit' +import test from 'node:test' +import rehypeStringify from 'rehype-stringify' import remarkParse from 'remark-parse' import remarkRehype from 'remark-rehype' -import rehypeStringify from 'rehype-stringify' +import {unified} from 'unified' +import {visit} from 'unist-util-visit' import rehypeRaw from './index.js' -const markdown = `
+test('rehypeRaw', async function (t) { + await t.test('should expose the public api', async function () { + assert.deepEqual(Object.keys(await import('./index.js')).sort(), [ + 'default' + ]) + }) + + await t.test('should work', async function () { + const file = await unified() + // @ts-expect-error: to do: remove when remark is released. + .use(remarkParse) + // @ts-expect-error: to do: remove when remark-rehype is released. + .use(remarkRehype, {allowDangerousHtml: true}) + .use(rehypeRaw) + .use(function () { + /** + * @param {Root} root + */ + return function (root) { + visit(root, 'raw', function () { + assert.fail() + }) + } + }) + .use(rehypeStringify).process(`
A mix of *markdown* and HTML. -
` +
`) -const html = `
+ assert.equal( + String(file), + `

A mix of markdown and HTML.

` - -test('rehypeRaw', async () => { - const file = await unified() - // @ts-expect-error: to do: remove when remark is released. - .use(remarkParse) - // @ts-expect-error: to do: remove when remark-rehype is released. - .use(remarkRehype, {allowDangerousHtml: true}) - .use(rehypeRaw) - .use( - /** @type {import('unified').Plugin, Root>} */ - () => (root) => { - visit(root, 'raw', () => { - assert.fail('should not include `raw` in tree after `rehype-raw`') - }) - } ) - .use(rehypeStringify) - .process(markdown) - - assert.equal(String(file), html, 'should equal the fixture') + }) })