File tree Expand file tree Collapse file tree 3 files changed +49
-7
lines changed Expand file tree Collapse file tree 3 files changed +49
-7
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,14 @@ module.exports = toMDAST;
55var minify = require ( 'rehype-minify-whitespace' ) ( ) ;
66var xtend = require ( 'xtend' ) ;
77var one = require ( './one' ) ;
8+ var handlers = require ( './handlers' ) ;
89
910h . augment = augment ;
1011
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 ) ;
1316}
1417
1518function h ( node , type , props , children ) {
Original file line number Diff line number Diff line change @@ -4,15 +4,14 @@ module.exports = one;
44
55var has = require ( 'has' ) ;
66var all = require ( './all' ) ;
7- var handlers = require ( './handlers' ) ;
87
98function one ( h , node , parent ) {
109 var fn = null ;
1110
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 ] ;
1615 }
1716
1817 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) {
149149 } ) ;
150150 }
151151} ) ;
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