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