Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 42 additions & 27 deletions packages/mdx/src/remark/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { valueToEstree } from "./to-estree"
import { CH_CODE_CONFIG_VAR_NAME } from "./unist-utils"
import { JsxNode, SuperNode, visit } from "./nodes"
import { addConfigDefaults, CodeHikeConfig } from "./config"
import { Attacher } from "unified"

import type { Node } from "unist"

const transforms = [
transformPreviews,
Expand All @@ -21,39 +22,53 @@ const transforms = [
transformInlineCodes,
transformCodes,
]
export const attacher: Attacher<
[CodeHikeConfig?]
> = unsafeConfig => {
return async (tree: SuperNode, file: any) => {
const config = addConfigDefaults(
unsafeConfig,
file?.cwd,
file?.history
? file.history[file.history.length - 1]
: undefined
)

try {
for (const transform of transforms) {
await transform(tree, config)
}

const usedCodeHikeComponents =
getUsedCodeHikeComponentNames(tree)
type VFile = {
history: string[]
cwd: string
}

type Transformer = (
node: Node,
file: VFile
) => Promise<void>

type CodeHikeRemarkPlugin = (
config: CodeHikeConfig
) => Transformer

if (usedCodeHikeComponents.length > 0) {
addConfig(tree, config)
export const attacher: CodeHikeRemarkPlugin =
unsafeConfig => {
return async (tree: SuperNode, file: VFile) => {
const config = addConfigDefaults(
unsafeConfig,
file?.cwd,
file?.history
? file.history[file.history.length - 1]
: undefined
)

if (config.autoImport) {
addSmartImport(tree, usedCodeHikeComponents)
try {
for (const transform of transforms) {
await transform(tree, config)
}

const usedCodeHikeComponents =
getUsedCodeHikeComponentNames(tree)

if (usedCodeHikeComponents.length > 0) {
addConfig(tree, config)

if (config.autoImport) {
addSmartImport(tree, usedCodeHikeComponents)
}
}
} catch (e) {
console.error("error running remarkCodeHike", e)
throw e
}
} catch (e) {
console.error("error running remarkCodeHike", e)
throw e
}
}
}

/**
* Returns a the list of component names
Expand Down