@@ -6,8 +6,15 @@ import { transformSpotlights } from "./plugin/spotlight"
66import { transformScrollycodings } from "./plugin/scrollycoding"
77import visit from "unist-util-visit"
88import { transformSlideshows } from "./plugin/slideshow"
9+ import { valueToEstree } from "./plugin/to-estree"
10+ import { CH_CODE_CONFIG_VAR_NAME } from "./plugin/unist-utils"
911
10- export function remarkCodeHike ( { theme } : { theme : any } ) {
12+ type CodeHikeConfig = {
13+ theme : any
14+ lineNumbers ?: boolean
15+ }
16+
17+ export function remarkCodeHike ( config : CodeHikeConfig ) {
1118 return async ( tree : Node ) => {
1219 // TODO add opt-in config
1320 let hasCodeHikeImport = false
@@ -21,24 +28,60 @@ export function remarkCodeHike({ theme }: { theme: any }) {
2128 }
2229 } )
2330
31+ addConfig ( tree as Parent , config )
32+
2433 if ( ! hasCodeHikeImport ) {
2534 addImportNode ( tree as Parent )
2635 }
2736
2837 try {
29- await transformScrollycodings ( tree , { theme } )
30- await transformSpotlights ( tree , { theme } )
31- await transformSlideshows ( tree , { theme } )
32- await transformSections ( tree , { theme } )
33- await transformEditorNodes ( tree , { theme } )
34- await transformCodeNodes ( tree , { theme } )
38+ await transformScrollycodings ( tree , config )
39+ await transformSpotlights ( tree , config )
40+ await transformSlideshows ( tree , config )
41+ await transformSections ( tree , config )
42+ await transformEditorNodes ( tree , config )
43+ await transformCodeNodes ( tree , config )
3544 } catch ( e ) {
3645 console . error ( "error running remarkCodeHike" , e )
3746 throw e
3847 }
3948 }
4049}
4150
51+ function addConfig ( tree : Parent , config : CodeHikeConfig ) {
52+ tree . children . unshift ( {
53+ type : "mdxjsEsm" ,
54+ value : "export const chCodeConfig = {}" ,
55+ data : {
56+ estree : {
57+ type : "Program" ,
58+ body : [
59+ {
60+ type : "ExportNamedDeclaration" ,
61+ declaration : {
62+ type : "VariableDeclaration" ,
63+ declarations : [
64+ {
65+ type : "VariableDeclarator" ,
66+ id : {
67+ type : "Identifier" ,
68+ name : CH_CODE_CONFIG_VAR_NAME ,
69+ } ,
70+ init : valueToEstree ( config ) ,
71+ } ,
72+ ] ,
73+ kind : "const" ,
74+ } ,
75+ specifiers : [ ] ,
76+ source : null ,
77+ } ,
78+ ] ,
79+ sourceType : "module" ,
80+ } ,
81+ } ,
82+ } )
83+ }
84+
4285function addImportNode ( tree : Parent ) {
4386 tree . children . unshift ( {
4487 type : "mdxjsEsm" ,
0 commit comments