Skip to content

Commit a2aa5ff

Browse files
committed
Use unist Node type
1 parent aefe40e commit a2aa5ff

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

packages/mdx/src/remark/transform.ts

+32-27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { CH_CODE_CONFIG_VAR_NAME } from "./unist-utils"
1111
import { JsxNode, SuperNode, visit } from "./nodes"
1212
import { addConfigDefaults, CodeHikeConfig } from "./config"
1313

14+
import type { Node } from "unist"
15+
1416
const transforms = [
1517
transformPreviews,
1618
transformScrollycodings,
@@ -27,43 +29,46 @@ type VFile = {
2729
}
2830

2931
type Transformer = (
30-
node: SuperNode,
32+
node: Node,
3133
file: VFile
3234
) => Promise<void>
3335

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+
}
5055

51-
const usedCodeHikeComponents =
52-
getUsedCodeHikeComponentNames(tree)
56+
const usedCodeHikeComponents =
57+
getUsedCodeHikeComponentNames(tree)
5358

54-
if (usedCodeHikeComponents.length > 0) {
55-
addConfig(tree, config)
59+
if (usedCodeHikeComponents.length > 0) {
60+
addConfig(tree, config)
5661

57-
if (config.autoImport) {
58-
addSmartImport(tree, usedCodeHikeComponents)
62+
if (config.autoImport) {
63+
addSmartImport(tree, usedCodeHikeComponents)
64+
}
5965
}
66+
} catch (e) {
67+
console.error("error running remarkCodeHike", e)
68+
throw e
6069
}
61-
} catch (e) {
62-
console.error("error running remarkCodeHike", e)
63-
throw e
6470
}
6571
}
66-
}
6772

6873
/**
6974
* Returns a the list of component names

0 commit comments

Comments
 (0)