File tree 3 files changed +49
-7
lines changed
3 files changed +49
-7
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,14 @@ module.exports = toMDAST;
5
5
var minify = require ( 'rehype-minify-whitespace' ) ( ) ;
6
6
var xtend = require ( 'xtend' ) ;
7
7
var one = require ( './one' ) ;
8
+ var handlers = require ( './handlers' ) ;
8
9
9
10
h . augment = augment ;
10
11
11
- function toMDAST ( tree ) {
12
- return one ( h , minify ( tree ) ) ;
12
+ function toMDAST ( tree , options ) {
13
+ options = options || { } ;
14
+ h . handlers = xtend ( handlers , options . handlers || { } ) ;
15
+ return one ( h , minify ( tree ) , null ) ;
13
16
}
14
17
15
18
function h ( node , type , props , children ) {
Original file line number Diff line number Diff line change @@ -4,15 +4,14 @@ module.exports = one;
4
4
5
5
var has = require ( 'has' ) ;
6
6
var all = require ( './all' ) ;
7
- var handlers = require ( './handlers' ) ;
8
7
9
8
function one ( h , node , parent ) {
10
9
var fn = null ;
11
10
12
- if ( node . type === 'element' && has ( handlers , node . tagName ) ) {
13
- fn = handlers [ node . tagName ] ;
14
- } else if ( has ( handlers , node . type ) ) {
15
- fn = handlers [ node . type ] ;
11
+ if ( node . type === 'element' && has ( h . handlers , node . tagName ) ) {
12
+ fn = h . handlers [ node . tagName ] ;
13
+ } else if ( has ( h . handlers , node . type ) ) {
14
+ fn = h . handlers [ node . type ] ;
16
15
}
17
16
18
17
return ( typeof fn === 'function' ? fn : unknown ) ( h , node , parent ) ;
Original file line number Diff line number Diff line change @@ -149,3 +149,43 @@ test('fixtures', function (t) {
149
149
} ) ;
150
150
}
151
151
} ) ;
152
+
153
+ test ( 'handlers option' , function ( t ) {
154
+ var options = {
155
+ handlers : {
156
+ div : function ( h , node ) {
157
+ node . children [ 0 ] . value = 'Beta' ;
158
+ node . type = 'paragraph' ;
159
+ return h ( node , 'paragraph' , node . children ) ;
160
+ }
161
+ }
162
+ } ;
163
+
164
+ t . deepEqual (
165
+ toMDAST ( {
166
+ type : 'root' ,
167
+ children : [ {
168
+ type : 'element' ,
169
+ tagName : 'div' ,
170
+ properties : { } ,
171
+ children : [ {
172
+ type : 'text' ,
173
+ value : 'Alpha'
174
+ } ]
175
+ } ]
176
+ } , options ) ,
177
+ {
178
+ type : 'root' ,
179
+ children : [ {
180
+ type : 'paragraph' ,
181
+ children : [ {
182
+ type : 'text' ,
183
+ value : 'Beta'
184
+ } ]
185
+ } ]
186
+ } ,
187
+ 'use handlers passed as option'
188
+ ) ;
189
+
190
+ t . end ( ) ;
191
+ } ) ;
You can’t perform that action at this time.
0 commit comments