11import React from 'react' ;
22import { PluginItem } from '@babel/core' ;
33import { 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' ;
55import { LoaderDefinitionFunction } from 'webpack' ;
66export * from './utils' ;
77
@@ -23,6 +23,7 @@ export type CodeBlockData = {
2323 components : Record < CodeBlockItem [ 'name' ] , React . FC > ;
2424 data : Record < CodeBlockItem [ 'name' ] , CodeBlockItem > ;
2525 headings ?: HeadingItem [ ] ;
26+ headingsList : HeadingListType [ ] ;
2627} ;
2728
2829export const FUNNAME_PREFIX = '__BaseCode__' ;
@@ -47,26 +48,28 @@ export type Options = {
4748
4849const codePreviewLoader : LoaderDefinitionFunction = function ( source ) {
4950 const options : Options = this . getOptions ( ) ;
51+ const { isHeading, ...rest } = options ;
5052
5153 let components = '' ;
5254 let codeBlock = { } as CodeBlockData [ 'data' ] ;
5355 const child = getProcessor ( source ) ;
5456 try {
55- codeBlock = getCodeBlock ( child , options , this . resourcePath ) ;
57+ codeBlock = getCodeBlock ( child , rest , this . resourcePath ) ;
5658 Object . keys ( codeBlock ) . forEach ( ( key ) => {
5759 components += `${ key } : (function() { ${ codeBlock [ key ] . code } })(),` ;
5860 } ) ;
5961 } catch ( error ) {
6062 this . emitError ( error ) ;
6163 }
6264
63- const headings = options . isHeading ? getHeadings ( child ) : [ ] ;
65+ const { headingsList , headings } = isHeading ? getHeadings ( child ) : { headingsList : [ ] , headings : [ ] } ;
6466
6567 return `\nexport default {
6668 components: { ${ components } },
6769 data: ${ JSON . stringify ( codeBlock , null , 2 ) } ,
6870 source: ${ JSON . stringify ( source ) } ,
69- headings:${ JSON . stringify ( headings ) }
71+ headings:${ JSON . stringify ( headings ) } ,
72+ headingsList:${ JSON . stringify ( headingsList ) } ,
7073 }` ;
7174} ;
7275
0 commit comments