Skip to content

Commit e8db600

Browse files
committed
Refactor code-style
1 parent 2d5f247 commit e8db600

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

Diff for: lib/index.js

+28-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @typedef {import('hast').Element} HastElement
66
* @typedef {import('hast').Text} HastText
77
* @typedef {import('hast').Comment} HastComment
8-
* @typedef {HastParent['children'][number]} HastChild
8+
* @typedef {import('hast').Content} HastChild
99
* @typedef {HastChild|HastRoot} HastNode
1010
*
1111
* @callback AfterTransform
@@ -18,9 +18,9 @@
1818
*
1919
* @typedef Options
2020
* @property {AfterTransform} [afterTransform]
21-
*
22-
* @typedef Context
23-
* @property {AfterTransform} [afterTransform]
21+
* Function called after a DOM node is transformed into a hast node.
22+
* Given the DOM node that was handled as the first parameter and the
23+
* corresponding hast node as the second parameter.
2424
*/
2525

2626
import {webNamespaces} from 'web-namespaces'
@@ -33,9 +33,25 @@ const DOCUMENT_NODE = 9
3333
const DOCUMENT_TYPE_NODE = 10
3434
const DOCUMENT_FRAGMENT_NODE = 11
3535

36+
/**
37+
* Transform a DOM tree to a hast tree.
38+
*
39+
* @param {Node} node
40+
* @param {Options} [options]
41+
* @returns {HastNode}
42+
*/
43+
export function fromDom(node, options = {}) {
44+
return (
45+
(node ? transform(node, options) : undefined) || {
46+
type: 'root',
47+
children: []
48+
}
49+
)
50+
}
51+
3652
/**
3753
* @param {Node} node
38-
* @param {Context} ctx
54+
* @param {Options} ctx
3955
* @returns {HastNode|undefined}
4056
*/
4157
function transform(node, ctx) {
@@ -46,7 +62,7 @@ function transform(node, ctx) {
4662

4763
/**
4864
* @param {Node} node
49-
* @param {Context} ctx
65+
* @param {Options} ctx
5066
* @returns {HastNode|undefined}
5167
*/
5268
function one(node, ctx) {
@@ -75,7 +91,7 @@ function one(node, ctx) {
7591
* Transform a document.
7692
*
7793
* @param {Document|DocumentFragment} node
78-
* @param {Context} ctx
94+
* @param {Options} ctx
7995
* @returns {HastRoot}
8096
*/
8197
function root(node, ctx) {
@@ -116,7 +132,7 @@ function comment(node) {
116132
* Transform an element.
117133
*
118134
* @param {Element} node
119-
* @param {Context} ctx
135+
* @param {Options} ctx
120136
* @returns {HastElement}
121137
*/
122138
function element(node, ctx) {
@@ -129,7 +145,7 @@ function element(node, ctx) {
129145
// @ts-expect-error Types are wrong.
130146
space === webNamespaces.html && tagName === 'template' ? node.content : node
131147
const attributes = node.getAttributeNames()
132-
/** @type {Object.<string, string>} */
148+
/** @type {Record<string, string>} */
133149
const props = {}
134150
let index = -1
135151

@@ -144,12 +160,12 @@ function element(node, ctx) {
144160
* Transform an element.
145161
*
146162
* @param {Document|DocumentFragment|Element} node
147-
* @param {Context} ctx
148-
* @returns {Array.<HastChild>}
163+
* @param {Options} ctx
164+
* @returns {Array<HastChild>}
149165
*/
150166
function all(node, ctx) {
151167
const nodes = node.childNodes
152-
/** @type {Array.<HastChild>} */
168+
/** @type {Array<HastChild>} */
153169
const children = []
154170
let index = -1
155171

@@ -164,12 +180,3 @@ function all(node, ctx) {
164180

165181
return children
166182
}
167-
168-
/**
169-
* @param {Node} node
170-
* @param {Options} [options]
171-
* @returns {HastNode}
172-
*/
173-
export function fromDom(node, options = {}) {
174-
return transform(node || {}, options) || {type: 'root', children: []}
175-
}

Diff for: readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Consequently, it does not maintain [positional info][positional-information].
7777

7878
###### `options.afterTransform`
7979

80-
Function called when a DOM node is transformed into a hast node (`Function?`).
80+
Function called after a DOM node is transformed into a hast node (`Function?`).
8181
Given the DOM node that was handled as the first parameter and the
8282
corresponding hast node as the second parameter.
8383

0 commit comments

Comments
 (0)