From 04897c8d9f869739a3a456fe922999ea39818806 Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Thu, 24 Feb 2022 15:22:27 +0000 Subject: [PATCH] Add autoImport setting --- packages/mdx/src/plugin.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/mdx/src/plugin.ts b/packages/mdx/src/plugin.ts index cf452852..5c2851d9 100644 --- a/packages/mdx/src/plugin.ts +++ b/packages/mdx/src/plugin.ts @@ -14,12 +14,14 @@ import { transformInlineCodes } from "./plugin/inline-code" type CodeHikeConfig = { theme: any lineNumbers?: boolean + autoImport?: boolean } export function remarkCodeHike( unsafeConfig: CodeHikeConfig ) { return async (tree: Node) => { + const config = addConfigDefaults(unsafeConfig) // TODO add opt-in config let hasCodeHikeImport = false visit(tree, "mdxjsEsm", (node: any) => { @@ -32,11 +34,10 @@ export function remarkCodeHike( } }) - const config = addConfigDefaults(unsafeConfig) addConfig(tree as Parent, config) - if (!hasCodeHikeImport) { + if (config.autoImport && !hasCodeHikeImport) { addImportNode(tree as Parent) } @@ -59,7 +60,7 @@ export function remarkCodeHike( function addConfigDefaults( config: Partial | undefined ): CodeHikeConfig { - return { ...config, theme: config?.theme || {} } + return { ...config, theme: config?.theme || {}, autoImport: config?.autoImport === false ? false : true } } function addConfig(tree: Parent, config: CodeHikeConfig) {