Skip to content

Add skipLanguages config #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mdx/dev/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function getCode(file: string, config = {}) {
remarkCodeHike,
{
autoImport: false,
skipLanguages: ["", "mermaid"],
showCopyButton: true,
theme,
...config,
Expand Down
15 changes: 11 additions & 4 deletions packages/mdx/src/remark/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ import { mergeFocus } from "../utils"
import { CodeNode, SuperNode } from "./nodes"
import { CodeHikeConfig } from "./config"

export function isEditorNode(node: SuperNode) {
export function isEditorNode(
node: SuperNode,
config: CodeHikeConfig
) {
if (node.type === "code") {
const lang = (node.lang as string) || ""
const shouldSkip = config.skipLanguages.includes(lang)
return !shouldSkip
}
return (
node.type === "code" ||
(node.type === "mdxJsxFlowElement" &&
node.name === "CH.Code")
node.type === "mdxJsxFlowElement" &&
node.name === "CH.Code"
)
}

Expand Down
2 changes: 2 additions & 0 deletions packages/mdx/src/remark/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type CodeHikeConfig = {
theme: any
lineNumbers?: boolean
autoImport?: boolean
skipLanguages: string[]
showExpandButton?: boolean
showCopyButton?: boolean
}
Expand All @@ -17,5 +18,6 @@ export function addConfigDefaults(
...config,
theme: config?.theme || {},
autoImport: config?.autoImport === false ? false : true,
skipLanguages: config?.skipLanguages || [],
}
}
2 changes: 1 addition & 1 deletion packages/mdx/src/remark/steps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function extractStepsInfo(

steps[stepIndex] = steps[stepIndex] || { children: [] }
const step = steps[stepIndex]
if (!step.editorStep && isEditorNode(child)) {
if (!step.editorStep && isEditorNode(child, config)) {
const editorStep = await mapAnyCodeNode(
{ node: child, parent, index: i },
config
Expand Down
11 changes: 9 additions & 2 deletions packages/mdx/src/remark/transform.code.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { NodeInfo, toJSX, visitAsync } from "./unist-utils"
import { mapAnyCodeNode, mapEditor } from "./code"
import {
isEditorNode,
mapAnyCodeNode,
mapEditor,
} from "./code"
import { CodeNode, JsxNode, SuperNode } from "./nodes"
import { CodeHikeConfig } from "./config"

Expand All @@ -20,7 +24,10 @@ export async function transformCodes(
tree,
"code",
async (node: CodeNode, index, parent) => {
await transformCode({ node, index, parent }, config)
// here we check if we should skip it because of the language:
if (isEditorNode(node, config)) {
await transformCode({ node, index, parent }, config)
}
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/src/remark/transform.section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function transformSection(
node,
["mdxJsxFlowElement", "code"],
async (editorNode, index, parent) => {
if (isEditorNode(editorNode)) {
if (isEditorNode(editorNode, config)) {
props = await mapAnyCodeNode(
{ node: editorNode, index, parent },
config
Expand Down