28
28
*
29
29
* @typedef Options
30
30
* Configuration.
31
- * @property {Array<string> } [passThrough]
31
+ * @property {Array<string> | null | undefined } [passThrough]
32
32
* List of custom hast node types to pass through (keep) in hast.
33
33
*
34
34
* If the passed through nodes have children, those children are expected to
35
- * be hast and will be handled.
35
+ * be hast again and will be handled.
36
+ * @property {VFile | null | undefined } [file]
37
+ * Corresponding virtual file.
36
38
*
37
39
* @typedef State
38
40
* Info passed around about the current state.
42
44
* Add a hast node to the parser.
43
45
* @property {boolean } stitches
44
46
* Whether there are stitches.
45
- * @property {VFile | undefined } file
46
- * Virtual file.
47
47
* @property {Options } options
48
48
* User configuration.
49
49
*/
@@ -61,88 +61,67 @@ import {zwitch} from 'zwitch'
61
61
const parseOptions = { sourceCodeLocationInfo : true , scriptingEnabled : false }
62
62
63
63
/**
64
- * Given a hast tree and an optional vfile (for positional info), return a new
65
- * parsed-again hast tree .
64
+ * Pass a hast tree through an HTML parser, which will fix nesting, and
65
+ * turn raw nodes into actual nodes .
66
66
*
67
- * @param tree
68
- * Original hast tree.
69
- * @param file
70
- * Virtual file for positional info, optional.
71
- * @param options
67
+ * @param {Node } tree
68
+ * Original hast tree to transform.
69
+ * @param {Options | null | undefined } [options]
72
70
* Configuration.
71
+ * @returns {Node }
72
+ * Parsed again tree.
73
73
*/
74
- // To do: remove support for overload.
75
- export const raw =
76
- /**
77
- * @type {(
78
- * ((tree: Node, file: VFile | null | undefined, options?: Options) => Node) &
79
- * ((tree: Node, options?: Options) => Node)
80
- * )}
81
- */
82
- (
83
- /**
84
- * @param {Node } tree
85
- * @param {VFile | Options | null | undefined } [file]
86
- * @param {Options | null | undefined } [options]
87
- */
88
- function ( tree , file , options ) {
89
- if ( isOptions ( file ) ) {
90
- options = file
91
- file = undefined
92
- }
93
-
94
- const document = documentMode ( tree )
95
- /** @type {(node: Node, state: State) => void } */
96
- const one = zwitch ( 'type' , {
97
- handlers : { root, element, text, comment, doctype, raw : handleRaw } ,
98
- unknown
99
- } )
100
-
101
- /** @type {State } */
102
- const state = {
103
- parser : document
104
- ? new Parser ( parseOptions )
105
- : Parser . getFragmentParser ( null , parseOptions ) ,
106
- handle ( node ) {
107
- one ( node , state )
108
- } ,
109
- stitches : false ,
110
- file : file || undefined ,
111
- options : options || { }
112
- }
74
+ export function raw ( tree , options ) {
75
+ const document = documentMode ( tree )
76
+ /** @type {(node: Node, state: State) => void } */
77
+ const one = zwitch ( 'type' , {
78
+ handlers : { root, element, text, comment, doctype, raw : handleRaw } ,
79
+ unknown
80
+ } )
81
+
82
+ /** @type {State } */
83
+ const state = {
84
+ parser : document
85
+ ? new Parser ( parseOptions )
86
+ : Parser . getFragmentParser ( null , parseOptions ) ,
87
+ handle ( node ) {
88
+ one ( node , state )
89
+ } ,
90
+ stitches : false ,
91
+ options : options || { }
92
+ }
113
93
114
- one ( tree , state )
115
- resetTokenizer ( state , pointStart ( ) )
116
-
117
- const p5 = document ? state . parser . document : state . parser . getFragment ( )
118
- const result = fromParse5 ( p5 , {
119
- // To do: support `space`?
120
- file
121
- } )
122
-
123
- if ( state . stitches ) {
124
- visit ( result , 'comment' , ( node , index , parent ) => {
125
- const stitch = /** @type {Stitch } */ ( /** @type {unknown } */ ( node ) )
126
- if ( stitch . value . stitch && parent !== null && index !== null ) {
127
- // @ts -expect-error: assume the stitch is allowed.
128
- parent . children [ index ] = stitch . value . stitch
129
- return index
130
- }
131
- } )
94
+ one ( tree , state )
95
+ resetTokenizer ( state , pointStart ( ) )
96
+
97
+ const p5 = document ? state . parser . document : state . parser . getFragment ( )
98
+ const result = fromParse5 ( p5 , {
99
+ // To do: support `space`?
100
+ file : state . options . file
101
+ } )
102
+
103
+ if ( state . stitches ) {
104
+ visit ( result , 'comment' , ( node , index , parent ) => {
105
+ const stitch = /** @type {Stitch } */ ( /** @type {unknown } */ ( node ) )
106
+ if ( stitch . value . stitch && parent !== null && index !== null ) {
107
+ // @ts -expect-error: assume the stitch is allowed.
108
+ parent . children [ index ] = stitch . value . stitch
109
+ return index
132
110
}
111
+ } )
112
+ }
133
113
134
- // Unpack if possible and when not given a `root`.
135
- if (
136
- tree . type !== 'root' &&
137
- result . type === 'root' &&
138
- result . children . length === 1
139
- ) {
140
- return result . children [ 0 ]
141
- }
114
+ // Unpack if possible and when not given a `root`.
115
+ if (
116
+ tree . type !== 'root' &&
117
+ result . type === 'root' &&
118
+ result . children . length === 1
119
+ ) {
120
+ return result . children [ 0 ]
121
+ }
142
122
143
- return result
144
- }
145
- )
123
+ return result
124
+ }
146
125
147
126
/**
148
127
* Transform all nodes
@@ -288,11 +267,7 @@ function stitch(node, state) {
288
267
// Recurse, because to somewhat handle `[<x>]</x>` (where `[]` denotes the
289
268
// passed through node).
290
269
if ( 'children' in node && 'children' in clone ) {
291
- const fakeRoot = raw (
292
- { type : 'root' , children : node . children } ,
293
- state . file ,
294
- state . options
295
- )
270
+ const fakeRoot = raw ( { type : 'root' , children : node . children } , state . options )
296
271
// @ts -expect-error Assume a given parent yields a parent.
297
272
clone . children = fakeRoot . children
298
273
}
@@ -626,18 +601,6 @@ function createParse5Location(node) {
626
601
}
627
602
}
628
603
629
- /**
630
- * Check if `value` is an options object.
631
- *
632
- * @param {VFile | Options | null | undefined } value
633
- * Thing.
634
- * @return {value is Options }
635
- * Whether `value` is an options object.
636
- */
637
- function isOptions ( value ) {
638
- return Boolean ( value && ! ( 'message' in value && 'messages' in value ) )
639
- }
640
-
641
604
/**
642
605
* @template {Node} NodeType
643
606
* Node type.
0 commit comments