5
5
* @typedef {import('hast').Element } HastElement
6
6
* @typedef {import('hast').Text } HastText
7
7
* @typedef {import('hast').Comment } HastComment
8
- * @typedef {HastParent['children'][number] } HastChild
8
+ * @typedef {import('hast').Content } HastChild
9
9
* @typedef {HastChild|HastRoot } HastNode
10
10
*
11
11
* @callback AfterTransform
18
18
*
19
19
* @typedef Options
20
20
* @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.
24
24
*/
25
25
26
26
import { webNamespaces } from 'web-namespaces'
@@ -33,9 +33,25 @@ const DOCUMENT_NODE = 9
33
33
const DOCUMENT_TYPE_NODE = 10
34
34
const DOCUMENT_FRAGMENT_NODE = 11
35
35
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
+
36
52
/**
37
53
* @param {Node } node
38
- * @param {Context } ctx
54
+ * @param {Options } ctx
39
55
* @returns {HastNode|undefined }
40
56
*/
41
57
function transform ( node , ctx ) {
@@ -46,7 +62,7 @@ function transform(node, ctx) {
46
62
47
63
/**
48
64
* @param {Node } node
49
- * @param {Context } ctx
65
+ * @param {Options } ctx
50
66
* @returns {HastNode|undefined }
51
67
*/
52
68
function one ( node , ctx ) {
@@ -75,7 +91,7 @@ function one(node, ctx) {
75
91
* Transform a document.
76
92
*
77
93
* @param {Document|DocumentFragment } node
78
- * @param {Context } ctx
94
+ * @param {Options } ctx
79
95
* @returns {HastRoot }
80
96
*/
81
97
function root ( node , ctx ) {
@@ -116,7 +132,7 @@ function comment(node) {
116
132
* Transform an element.
117
133
*
118
134
* @param {Element } node
119
- * @param {Context } ctx
135
+ * @param {Options } ctx
120
136
* @returns {HastElement }
121
137
*/
122
138
function element ( node , ctx ) {
@@ -129,7 +145,7 @@ function element(node, ctx) {
129
145
// @ts -expect-error Types are wrong.
130
146
space === webNamespaces . html && tagName === 'template' ? node . content : node
131
147
const attributes = node . getAttributeNames ( )
132
- /** @type {Object. <string, string> } */
148
+ /** @type {Record <string, string> } */
133
149
const props = { }
134
150
let index = - 1
135
151
@@ -144,12 +160,12 @@ function element(node, ctx) {
144
160
* Transform an element.
145
161
*
146
162
* @param {Document|DocumentFragment|Element } node
147
- * @param {Context } ctx
148
- * @returns {Array. <HastChild> }
163
+ * @param {Options } ctx
164
+ * @returns {Array<HastChild> }
149
165
*/
150
166
function all ( node , ctx ) {
151
167
const nodes = node . childNodes
152
- /** @type {Array. <HastChild> } */
168
+ /** @type {Array<HastChild> } */
153
169
const children = [ ]
154
170
let index = - 1
155
171
@@ -164,12 +180,3 @@ function all(node, ctx) {
164
180
165
181
return children
166
182
}
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
- }
0 commit comments