1
1
import React from 'react' ;
2
2
import { PluginItem } from '@babel/core' ;
3
3
import { Options as RIOptions } from 'babel-plugin-transform-remove-imports' ;
4
- import { getProcessor , getCodeBlock , getHeadings , HeadingItem } from './utils' ;
4
+ import { getProcessor , getCodeBlock , getHeadings , HeadingItem , HeadingListType } from './utils' ;
5
5
import { LoaderDefinitionFunction } from 'webpack' ;
6
6
export * from './utils' ;
7
7
@@ -23,6 +23,7 @@ export type CodeBlockData = {
23
23
components : Record < CodeBlockItem [ 'name' ] , React . FC > ;
24
24
data : Record < CodeBlockItem [ 'name' ] , CodeBlockItem > ;
25
25
headings ?: HeadingItem [ ] ;
26
+ headingsList : HeadingListType [ ] ;
26
27
} ;
27
28
28
29
export const FUNNAME_PREFIX = '__BaseCode__' ;
@@ -47,26 +48,28 @@ export type Options = {
47
48
48
49
const codePreviewLoader : LoaderDefinitionFunction = function ( source ) {
49
50
const options : Options = this . getOptions ( ) ;
51
+ const { isHeading, ...rest } = options ;
50
52
51
53
let components = '' ;
52
54
let codeBlock = { } as CodeBlockData [ 'data' ] ;
53
55
const child = getProcessor ( source ) ;
54
56
try {
55
- codeBlock = getCodeBlock ( child , options , this . resourcePath ) ;
57
+ codeBlock = getCodeBlock ( child , rest , this . resourcePath ) ;
56
58
Object . keys ( codeBlock ) . forEach ( ( key ) => {
57
59
components += `${ key } : (function() { ${ codeBlock [ key ] . code } })(),` ;
58
60
} ) ;
59
61
} catch ( error ) {
60
62
this . emitError ( error ) ;
61
63
}
62
64
63
- const headings = options . isHeading ? getHeadings ( child ) : [ ] ;
65
+ const { headingsList , headings } = isHeading ? getHeadings ( child ) : { headingsList : [ ] , headings : [ ] } ;
64
66
65
67
return `\nexport default {
66
68
components: { ${ components } },
67
69
data: ${ JSON . stringify ( codeBlock , null , 2 ) } ,
68
70
source: ${ JSON . stringify ( source ) } ,
69
- headings:${ JSON . stringify ( headings ) }
71
+ headings:${ JSON . stringify ( headings ) } ,
72
+ headingsList:${ JSON . stringify ( headingsList ) } ,
70
73
}` ;
71
74
} ;
72
75
0 commit comments