Skip to content

Commit

Permalink
Replace one with state.one
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 11, 2023
1 parent 765021e commit 9bf96b9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 1 addition & 3 deletions lib/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* @typedef {Extract<Node, import('unist').Parent>} Parent
*/

import {one} from './one.js'

/**
* @param {State} state
* State.
Expand All @@ -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`.
Expand Down
21 changes: 18 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -41,6 +39,7 @@ export function toMdast(tree, options) {
const state = {
patch,
all: allBound,
one: oneBound,
options: options_,
nodeById: byId,
handlers: options_.handlers
Expand Down Expand Up @@ -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: []}
Expand Down Expand Up @@ -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<MdastNode> | void}
* mdast result.
*/
function oneBound(node, parent) {
return one(this, node, parent)
}
11 changes: 11 additions & 0 deletions lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Element>} nodeById
Expand Down Expand Up @@ -87,6 +89,15 @@
* Parent.
* @returns {Array<MdastContent>}
* 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<MdastNode> | void}
* mdast result.
*/

export {}
3 changes: 1 addition & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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()
})
Expand Down

0 comments on commit 9bf96b9

Please sign in to comment.