Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 26, 2023
1 parent 29e486f commit b1e7f8e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
25 changes: 18 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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<RawOptions, 'file'>} 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<void>, 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
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
60 changes: 34 additions & 26 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<div class="note">
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(`<div class="note">
A mix of *markdown* and <em>HTML</em>.
</div>`
</div>`)

const html = `<div class="note">
assert.equal(
String(file),
`<div class="note">
<p>A mix of <em>markdown</em> and <em>HTML</em>.</p>
</div>`

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<Array<void>, 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')
})
})

0 comments on commit b1e7f8e

Please sign in to comment.