Skip to content

Commit ad20260

Browse files
authored
Merge pull request #213 from code-hike/skip-languages
Add skipLanguages config
2 parents a6979be + 47834b0 commit ad20260

File tree

6 files changed

+25
-8
lines changed

6 files changed

+25
-8
lines changed

packages/mdx/dev/files.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export async function getCode(file: string, config = {}) {
3636
remarkCodeHike,
3737
{
3838
autoImport: false,
39+
skipLanguages: ["", "mermaid"],
3940
showCopyButton: true,
4041
theme,
4142
...config,

packages/mdx/src/remark/code.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ import { mergeFocus } from "../utils"
1212
import { CodeNode, SuperNode } from "./nodes"
1313
import { CodeHikeConfig } from "./config"
1414

15-
export function isEditorNode(node: SuperNode) {
15+
export function isEditorNode(
16+
node: SuperNode,
17+
config: CodeHikeConfig
18+
) {
19+
if (node.type === "code") {
20+
const lang = (node.lang as string) || ""
21+
const shouldSkip = config.skipLanguages.includes(lang)
22+
return !shouldSkip
23+
}
1624
return (
17-
node.type === "code" ||
18-
(node.type === "mdxJsxFlowElement" &&
19-
node.name === "CH.Code")
25+
node.type === "mdxJsxFlowElement" &&
26+
node.name === "CH.Code"
2027
)
2128
}
2229

packages/mdx/src/remark/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type CodeHikeConfig = {
22
theme: any
33
lineNumbers?: boolean
44
autoImport?: boolean
5+
skipLanguages: string[]
56
showExpandButton?: boolean
67
showCopyButton?: boolean
78
}
@@ -17,5 +18,6 @@ export function addConfigDefaults(
1718
...config,
1819
theme: config?.theme || {},
1920
autoImport: config?.autoImport === false ? false : true,
21+
skipLanguages: config?.skipLanguages || [],
2022
}
2123
}

packages/mdx/src/remark/steps.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function extractStepsInfo(
2929

3030
steps[stepIndex] = steps[stepIndex] || { children: [] }
3131
const step = steps[stepIndex]
32-
if (!step.editorStep && isEditorNode(child)) {
32+
if (!step.editorStep && isEditorNode(child, config)) {
3333
const editorStep = await mapAnyCodeNode(
3434
{ node: child, parent, index: i },
3535
config

packages/mdx/src/remark/transform.code.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { NodeInfo, toJSX, visitAsync } from "./unist-utils"
2-
import { mapAnyCodeNode, mapEditor } from "./code"
2+
import {
3+
isEditorNode,
4+
mapAnyCodeNode,
5+
mapEditor,
6+
} from "./code"
37
import { CodeNode, JsxNode, SuperNode } from "./nodes"
48
import { CodeHikeConfig } from "./config"
59

@@ -20,7 +24,10 @@ export async function transformCodes(
2024
tree,
2125
"code",
2226
async (node: CodeNode, index, parent) => {
23-
await transformCode({ node, index, parent }, config)
27+
// here we check if we should skip it because of the language:
28+
if (isEditorNode(node, config)) {
29+
await transformCode({ node, index, parent }, config)
30+
}
2431
}
2532
)
2633
}

packages/mdx/src/remark/transform.section.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function transformSection(
2727
node,
2828
["mdxJsxFlowElement", "code"],
2929
async (editorNode, index, parent) => {
30-
if (isEditorNode(editorNode)) {
30+
if (isEditorNode(editorNode, config)) {
3131
props = await mapAnyCodeNode(
3232
{ node: editorNode, index, parent },
3333
config

0 commit comments

Comments
 (0)