File tree Expand file tree Collapse file tree 4 files changed +75
-5
lines changed Expand file tree Collapse file tree 4 files changed +75
-5
lines changed Original file line number Diff line number Diff line change @@ -1309,9 +1309,15 @@ function extension(combined, extension) {
1309
1309
1310
1310
case 'enter' :
1311
1311
case 'exit' : {
1312
+ const left = combined [ key ]
1312
1313
const right = extension [ key ]
1313
- if ( right ) {
1314
- Object . assign ( combined [ key ] , right )
1314
+ for ( const tokenType in right ) {
1315
+ if ( right [ tokenType ] ) {
1316
+ left [ tokenType ] = combineHandles (
1317
+ left [ tokenType ] ,
1318
+ right [ tokenType ]
1319
+ )
1320
+ }
1315
1321
}
1316
1322
1317
1323
break
@@ -1322,6 +1328,22 @@ function extension(combined, extension) {
1322
1328
}
1323
1329
}
1324
1330
1331
+ /**
1332
+ * Creates a new handle that calls `right` first, then falls through to `left`
1333
+ * only if `right` explicitly returns `false`.
1334
+ * @param {Handle? } left
1335
+ * @param {Handle } right
1336
+ * @returns {Handle }
1337
+ */
1338
+ function combineHandles ( left , right ) {
1339
+ if ( ! left ) return right
1340
+
1341
+ return function ( ...params ) {
1342
+ const rightResult = right . apply ( this , params )
1343
+ return rightResult === false ? left . apply ( this , params ) : rightResult
1344
+ }
1345
+ }
1346
+
1325
1347
/** @type {OnEnterError } */
1326
1348
function defaultOnError ( left , right ) {
1327
1349
if ( left ) {
Original file line number Diff line number Diff line change @@ -226,9 +226,10 @@ export type Handles = Record<string, Handle>
226
226
* @param token
227
227
* Current token.
228
228
* @returns
229
- * Nothing.
229
+ * Nothing, if the token was fully handled by this extension and should be ignored by previous extensions / default logic.
230
+ * Or `false`, if the token has not been handled by this extension and handling should fall back to preceding logic.
230
231
*/
231
- export type Handle = ( this : CompileContext , token : Token ) => undefined | void
232
+ export type Handle = ( this : CompileContext , token : Token ) => undefined | void | false
232
233
233
234
/**
234
235
* Handle the case where the `right` token is open, but it is closed (by the
Original file line number Diff line number Diff line change @@ -248,7 +248,9 @@ Handle a token (TypeScript type).
248
248
249
249
###### Returns
250
250
251
- Nothing ( ` undefined ` ).
251
+ Nothing, if the token was fully handled and should be ignored by previous
252
+ extensions / default logic. Or ` false ` , if the token has not been handled here
253
+ and the parser should fall back to preceding logic.
252
254
253
255
### ` OnEnterError `
254
256
Original file line number Diff line number Diff line change @@ -146,6 +146,51 @@ test('fromMarkdown', async function (t) {
146
146
)
147
147
} )
148
148
149
+ await t . test (
150
+ 'should allow extension handlers to fall through' ,
151
+ async function ( ) {
152
+ assert . deepEqual (
153
+ fromMarkdown ( 'a\nb' , {
154
+ mdastExtensions : [
155
+ {
156
+ enter : {
157
+ paragraph ( ) {
158
+ return false
159
+ }
160
+ }
161
+ }
162
+ ]
163
+ } ) ,
164
+ {
165
+ type : 'root' ,
166
+ children : [
167
+ {
168
+ type : 'paragraph' ,
169
+ children : [
170
+ {
171
+ type : 'text' ,
172
+ value : 'a\nb' ,
173
+ position : {
174
+ start : { line : 1 , column : 1 , offset : 0 } ,
175
+ end : { line : 2 , column : 2 , offset : 3 }
176
+ }
177
+ }
178
+ ] ,
179
+ position : {
180
+ start : { line : 1 , column : 1 , offset : 0 } ,
181
+ end : { line : 2 , column : 2 , offset : 3 }
182
+ }
183
+ }
184
+ ] ,
185
+ position : {
186
+ start : { line : 1 , column : 1 , offset : 0 } ,
187
+ end : { line : 2 , column : 2 , offset : 3 }
188
+ }
189
+ }
190
+ )
191
+ }
192
+ )
193
+
149
194
await t . test ( 'should support multiple extensions' , async function ( ) {
150
195
assert . deepEqual (
151
196
fromMarkdown ( 'a\nb' , {
You can’t perform that action at this time.
0 commit comments