Skip to content

Commit 9bf96b9

Browse files
committed
Replace one with state.one
1 parent 765021e commit 9bf96b9

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
* @typedef {import('./lib/types.js').Options} Options
55
*/
66

7-
export {one, defaultHandlers, toMdast} from './lib/index.js'
7+
export {defaultHandlers, toMdast} from './lib/index.js'

lib/all.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
* @typedef {Extract<Node, import('unist').Parent>} Parent
1111
*/
1212

13-
import {one} from './one.js'
14-
1513
/**
1614
* @param {State} state
1715
* State.
@@ -28,7 +26,7 @@ export function all(state, parent) {
2826

2927
while (++index < children.length) {
3028
const child = children[index]
31-
const result = one(state, child, parent)
29+
const result = state.one(child, parent)
3230

3331
if (Array.isArray(result)) {
3432
// @ts-expect-error: assume no `root`.

lib/index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import {handlers} from './handlers/index.js'
2121
import {one} from './one.js'
2222
import {all} from './all.js'
2323

24-
export {one} from './one.js'
25-
2624
/**
2725
* Transform hast to mdast.
2826
*
@@ -41,6 +39,7 @@ export function toMdast(tree, options) {
4139
const state = {
4240
patch,
4341
all: allBound,
42+
one: oneBound,
4443
options: options_,
4544
nodeById: byId,
4645
handlers: options_.handlers
@@ -69,7 +68,7 @@ export function toMdast(tree, options) {
6968
// @ts-expect-error: does return a transformer, that does accept any node.
7069
rehypeMinifyWhitespace({newlines: options_.newlines === true})(tree)
7170

72-
const result = one(state, tree, undefined)
71+
const result = state.one(tree, undefined)
7372

7473
if (!result) {
7574
mdast = {type: 'root', children: []}
@@ -168,3 +167,19 @@ function patch(origin, node) {
168167
function allBound(parent) {
169168
return all(this, parent)
170169
}
170+
171+
/**
172+
* Transform a hast node to mdast.
173+
*
174+
* @this {State}
175+
* Info passed around about the current state.
176+
* @param {Node} node
177+
* Expected hast node.
178+
* @param {Parent | undefined} parent
179+
* Parent of `node`.
180+
* @returns {MdastNode | Array<MdastNode> | void}
181+
* mdast result.
182+
*/
183+
function oneBound(node, parent) {
184+
return one(this, node, parent)
185+
}

lib/types.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
* Copy a node’s positional info.
5858
* @property {All} all
5959
* Transform the children of a hast parent to mdast.
60+
* @property {One} one
61+
* Transform a hast node to mdast.
6062
* @property {Options} options
6163
* User configuration.
6264
* @property {Record<string, Element>} nodeById
@@ -87,6 +89,15 @@
8789
* Parent.
8890
* @returns {Array<MdastContent>}
8991
* mdast children.
92+
*
93+
* @callback One
94+
* Transform a hast node to mdast.
95+
* @param {Node} node
96+
* Expected hast node.
97+
* @param {Parent | undefined} parent
98+
* Parent of `node`.
99+
* @returns {MdastNode | Array<MdastNode> | void}
100+
* mdast result.
90101
*/
91102

92103
export {}

test/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {toMarkdown} from 'mdast-util-to-markdown'
1515
import {gfm} from 'micromark-extension-gfm'
1616
import {u} from 'unist-builder'
1717
import {removePosition} from 'unist-util-remove-position'
18-
import {one, defaultHandlers, toMdast} from '../index.js'
18+
import {defaultHandlers, toMdast} from '../index.js'
1919
import {wrapNeeded} from '../lib/util/wrap.js'
2020

2121
test('custom nodes', (t) => {
@@ -45,7 +45,6 @@ test('custom nodes', (t) => {
4545
})
4646

4747
test('exports', (t) => {
48-
t.ok(one, 'should export `one`')
4948
t.ok(defaultHandlers, 'should export `defaultHandlers`')
5049
t.end()
5150
})

0 commit comments

Comments
 (0)