Skip to content

Commit cbfac22

Browse files
authored
Merge pull request #256 from code-hike/plugin-type
Change plugin type
2 parents 1161ab2 + a2aa5ff commit cbfac22

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

Diff for: packages/mdx/src/remark/transform.ts

+42-27
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { valueToEstree } from "./to-estree"
1010
import { CH_CODE_CONFIG_VAR_NAME } from "./unist-utils"
1111
import { JsxNode, SuperNode, visit } from "./nodes"
1212
import { addConfigDefaults, CodeHikeConfig } from "./config"
13-
import { Attacher } from "unified"
13+
14+
import type { Node } from "unist"
1415

1516
const transforms = [
1617
transformPreviews,
@@ -21,39 +22,53 @@ const transforms = [
2122
transformInlineCodes,
2223
transformCodes,
2324
]
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-
}
4025

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
4339

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+
)
4650

47-
if (config.autoImport) {
48-
addSmartImport(tree, usedCodeHikeComponents)
51+
try {
52+
for (const transform of transforms) {
53+
await transform(tree, config)
4954
}
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
5069
}
51-
} catch (e) {
52-
console.error("error running remarkCodeHike", e)
53-
throw e
5470
}
5571
}
56-
}
5772

5873
/**
5974
* Returns a the list of component names

0 commit comments

Comments
 (0)