@@ -11,6 +11,8 @@ import { CH_CODE_CONFIG_VAR_NAME } from "./unist-utils"
11
11
import { JsxNode , SuperNode , visit } from "./nodes"
12
12
import { addConfigDefaults , CodeHikeConfig } from "./config"
13
13
14
+ import type { Node } from "unist"
15
+
14
16
const transforms = [
15
17
transformPreviews ,
16
18
transformScrollycodings ,
@@ -27,43 +29,46 @@ type VFile = {
27
29
}
28
30
29
31
type Transformer = (
30
- node : SuperNode ,
32
+ node : Node ,
31
33
file : VFile
32
34
) => Promise < void >
33
35
34
- type Plugin = ( config : CodeHikeConfig ) => Transformer
35
-
36
- export const attacher : Plugin = unsafeConfig => {
37
- return async ( tree : SuperNode , file : VFile ) => {
38
- const config = addConfigDefaults (
39
- unsafeConfig ,
40
- file ?. cwd ,
41
- file ?. history
42
- ? file . history [ file . history . length - 1 ]
43
- : undefined
44
- )
45
-
46
- try {
47
- for ( const transform of transforms ) {
48
- await transform ( tree , config )
49
- }
36
+ type CodeHikeRemarkPlugin = (
37
+ config : CodeHikeConfig
38
+ ) => Transformer
39
+
40
+ export const attacher : CodeHikeRemarkPlugin =
41
+ unsafeConfig => {
42
+ return async ( tree : SuperNode , file : VFile ) => {
43
+ const config = addConfigDefaults (
44
+ unsafeConfig ,
45
+ file ?. cwd ,
46
+ file ?. history
47
+ ? file . history [ file . history . length - 1 ]
48
+ : undefined
49
+ )
50
+
51
+ try {
52
+ for ( const transform of transforms ) {
53
+ await transform ( tree , config )
54
+ }
50
55
51
- const usedCodeHikeComponents =
52
- getUsedCodeHikeComponentNames ( tree )
56
+ const usedCodeHikeComponents =
57
+ getUsedCodeHikeComponentNames ( tree )
53
58
54
- if ( usedCodeHikeComponents . length > 0 ) {
55
- addConfig ( tree , config )
59
+ if ( usedCodeHikeComponents . length > 0 ) {
60
+ addConfig ( tree , config )
56
61
57
- if ( config . autoImport ) {
58
- addSmartImport ( tree , usedCodeHikeComponents )
62
+ if ( config . autoImport ) {
63
+ addSmartImport ( tree , usedCodeHikeComponents )
64
+ }
59
65
}
66
+ } catch ( e ) {
67
+ console . error ( "error running remarkCodeHike" , e )
68
+ throw e
60
69
}
61
- } catch ( e ) {
62
- console . error ( "error running remarkCodeHike" , e )
63
- throw e
64
70
}
65
71
}
66
- }
67
72
68
73
/**
69
74
* Returns a the list of component names
0 commit comments