From 9bf96b93c7ed4c478c7a1110cdc594b9c2f8fcc9 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 11 Jan 2023 13:39:33 +0100 Subject: [PATCH] Replace `one` with `state.one` --- index.js | 2 +- lib/all.js | 4 +--- lib/index.js | 21 ++++++++++++++++++--- lib/types.js | 11 +++++++++++ test/index.js | 3 +-- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 64523d5..303fae1 100644 --- a/index.js +++ b/index.js @@ -4,4 +4,4 @@ * @typedef {import('./lib/types.js').Options} Options */ -export {one, defaultHandlers, toMdast} from './lib/index.js' +export {defaultHandlers, toMdast} from './lib/index.js' diff --git a/lib/all.js b/lib/all.js index 7397a7d..01bb69c 100644 --- a/lib/all.js +++ b/lib/all.js @@ -10,8 +10,6 @@ * @typedef {Extract} Parent */ -import {one} from './one.js' - /** * @param {State} state * State. @@ -28,7 +26,7 @@ export function all(state, parent) { while (++index < children.length) { const child = children[index] - const result = one(state, child, parent) + const result = state.one(child, parent) if (Array.isArray(result)) { // @ts-expect-error: assume no `root`. diff --git a/lib/index.js b/lib/index.js index fb89f2b..260b652 100644 --- a/lib/index.js +++ b/lib/index.js @@ -21,8 +21,6 @@ import {handlers} from './handlers/index.js' import {one} from './one.js' import {all} from './all.js' -export {one} from './one.js' - /** * Transform hast to mdast. * @@ -41,6 +39,7 @@ export function toMdast(tree, options) { const state = { patch, all: allBound, + one: oneBound, options: options_, nodeById: byId, handlers: options_.handlers @@ -69,7 +68,7 @@ export function toMdast(tree, options) { // @ts-expect-error: does return a transformer, that does accept any node. rehypeMinifyWhitespace({newlines: options_.newlines === true})(tree) - const result = one(state, tree, undefined) + const result = state.one(tree, undefined) if (!result) { mdast = {type: 'root', children: []} @@ -168,3 +167,19 @@ function patch(origin, node) { function allBound(parent) { return all(this, parent) } + +/** + * Transform a hast node to mdast. + * + * @this {State} + * Info passed around about the current state. + * @param {Node} node + * Expected hast node. + * @param {Parent | undefined} parent + * Parent of `node`. + * @returns {MdastNode | Array | void} + * mdast result. + */ +function oneBound(node, parent) { + return one(this, node, parent) +} diff --git a/lib/types.js b/lib/types.js index 7dc3371..f60382c 100644 --- a/lib/types.js +++ b/lib/types.js @@ -57,6 +57,8 @@ * Copy a node’s positional info. * @property {All} all * Transform the children of a hast parent to mdast. + * @property {One} one + * Transform a hast node to mdast. * @property {Options} options * User configuration. * @property {Record} nodeById @@ -87,6 +89,15 @@ * Parent. * @returns {Array} * mdast children. + * + * @callback One + * Transform a hast node to mdast. + * @param {Node} node + * Expected hast node. + * @param {Parent | undefined} parent + * Parent of `node`. + * @returns {MdastNode | Array | void} + * mdast result. */ export {} diff --git a/test/index.js b/test/index.js index a8d2856..6309fb3 100644 --- a/test/index.js +++ b/test/index.js @@ -15,7 +15,7 @@ import {toMarkdown} from 'mdast-util-to-markdown' import {gfm} from 'micromark-extension-gfm' import {u} from 'unist-builder' import {removePosition} from 'unist-util-remove-position' -import {one, defaultHandlers, toMdast} from '../index.js' +import {defaultHandlers, toMdast} from '../index.js' import {wrapNeeded} from '../lib/util/wrap.js' test('custom nodes', (t) => { @@ -45,7 +45,6 @@ test('custom nodes', (t) => { }) test('exports', (t) => { - t.ok(one, 'should export `one`') t.ok(defaultHandlers, 'should export `defaultHandlers`') t.end() })