Skip to content

Commit 71ab3ca

Browse files
committed
Remove config placeholder
1 parent b01b1d0 commit 71ab3ca

10 files changed

+47
-73
lines changed

packages/mdx/src/remark/code.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { highlight } from "../highlighter"
22
import { extractLinks } from "./links"
3-
import {
4-
visitAsync,
5-
toJSX,
6-
NodeInfo,
7-
splitChildren,
8-
CH_CODE_CONFIG_PLACEHOLDER,
9-
} from "./unist-utils"
3+
import { NodeInfo, splitChildren } from "./unist-utils"
104
import { CodeStep } from "../smooth-code"
115
import { EditorProps } from "../mini-editor"
126
import {
@@ -38,27 +32,28 @@ export async function mapAnyCodeNode(
3832
}
3933
}
4034

35+
type Props = Omit<EditorProps, "codeConfig">
36+
4137
async function mapCode(
4238
nodeInfo: NodeInfo<CodeNode>,
4339
config: CodeHikeConfig
44-
): Promise<EditorProps> {
40+
): Promise<Props> {
4541
const file = await mapFile(nodeInfo, config)
46-
const props: EditorProps = {
42+
const props: Props = {
4743
northPanel: {
4844
tabs: [file.name],
4945
active: file.name,
5046
heightRatio: 1,
5147
},
5248
files: [file],
53-
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
5449
}
5550
return props
5651
}
5752

5853
export async function mapEditor(
5954
{ node }: NodeInfo,
6055
config: CodeHikeConfig
61-
): Promise<EditorProps> {
56+
): Promise<Props> {
6257
const [northNodes, southNodes = []] = splitChildren(
6358
node,
6459
"thematicBreak"
@@ -105,7 +100,6 @@ export async function mapEditor(
105100
}
106101
: undefined,
107102
files: allFiles as any,
108-
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
109103
}
110104
return props
111105
}

packages/mdx/src/remark/steps.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ export async function extractStepsInfo(
3030
steps[stepIndex] = steps[stepIndex] || { children: [] }
3131
const step = steps[stepIndex]
3232
if (!step.editorStep && isEditorNode(child)) {
33-
const { codeConfig, ...editorStep } =
34-
await mapAnyCodeNode(
35-
{ node: child, parent, index: i },
36-
config
37-
)
33+
const editorStep = await mapAnyCodeNode(
34+
{ node: child, parent, index: i },
35+
config
36+
)
3837

3938
if (stepIndex === 0) {
4039
// for the header props, keep it as it is

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

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

@@ -31,6 +31,7 @@ async function transformCode(
3131
) {
3232
toJSX(nodeInfo.node, {
3333
name: "CH.Code",
34-
props: await mapEditor(nodeInfo, config),
34+
props: await mapAnyCodeNode(nodeInfo, config),
35+
addConfigProp: true,
3536
})
3637
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
visitAsync,
3-
toJSX,
4-
CH_CODE_CONFIG_PLACEHOLDER,
5-
} from "./unist-utils"
1+
import { visitAsync, toJSX } from "./unist-utils"
62
import { highlight } from "../highlighter"
73
import { EditorStep } from "../mini-editor"
84
import { Code } from "../utils"
@@ -46,9 +42,9 @@ export async function transformInlineCodes(
4642
lang,
4743
theme
4844
),
49-
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
5045
},
5146
appendProps: true,
47+
addConfigProp: true,
5248
})
5349
}
5450
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import fetch from "node-fetch"
2-
import {
3-
visitAsync,
4-
toJSX,
5-
CH_CODE_CONFIG_PLACEHOLDER,
6-
} from "./unist-utils"
2+
import { visitAsync, toJSX } from "./unist-utils"
73
import { JsxNode, SuperNode } from "./nodes"
84

95
export async function getPresetConfig(
@@ -36,9 +32,8 @@ export async function transformPreviews(tree: SuperNode) {
3632

3733
async function transformPreview(node: Node) {
3834
toJSX(node, {
39-
props: {
40-
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
41-
},
35+
props: {},
4236
appendProps: true,
37+
addConfigProp: true,
4338
})
4439
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
visitAsync,
3-
toJSX,
4-
CH_CODE_CONFIG_PLACEHOLDER,
5-
} from "./unist-utils"
1+
import { visitAsync, toJSX } from "./unist-utils"
62
import { extractStepsInfo } from "./steps"
73
import { getPresetConfig } from "./transform.preview"
84
import { transformLinks } from "./transform.section"
@@ -41,10 +37,10 @@ async function transformScrollycoding(
4137

4238
toJSX(node, {
4339
props: {
44-
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
4540
editorSteps: editorSteps,
4641
presetConfig,
4742
},
4843
appendProps: true,
44+
addConfigProp: true,
4945
})
5046
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function transformSection(
2929
async (editorNode, index, parent) => {
3030
if (isEditorNode(editorNode)) {
3131
props = await mapAnyCodeNode(
32-
{ node: editorNode, index, parent: parent! },
32+
{ node: editorNode, index, parent },
3333
config
3434
)
3535
toJSX(editorNode, {
@@ -43,7 +43,11 @@ async function transformSection(
4343
transformLinks(node)
4444

4545
if (props) {
46-
toJSX(node, { name: "CH.Section", props: props as any })
46+
toJSX(node, {
47+
name: "CH.Section",
48+
props: props as any,
49+
addConfigProp: true,
50+
})
4751
} else {
4852
toJSX(node, { name: "div", props: {} })
4953
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
visitAsync,
3-
toJSX,
4-
CH_CODE_CONFIG_PLACEHOLDER,
5-
} from "./unist-utils"
1+
import { visitAsync, toJSX } from "./unist-utils"
62
import { extractStepsInfo } from "./steps"
73
import { getPresetConfig } from "./transform.preview"
84
import { JsxNode, SuperNode } from "./nodes"
@@ -38,10 +34,10 @@ async function transformSlideshow(
3834

3935
toJSX(node, {
4036
props: {
41-
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
4237
editorSteps: editorSteps,
4338
presetConfig,
4439
},
4540
appendProps: true,
41+
addConfigProp: true,
4642
})
4743
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
visitAsync,
3-
toJSX,
4-
CH_CODE_CONFIG_PLACEHOLDER,
5-
} from "./unist-utils"
1+
import { visitAsync, toJSX } from "./unist-utils"
62
import { extractStepsInfo } from "./steps"
73
import { getPresetConfig } from "./transform.preview"
84
import { JsxNode, SuperNode } from "./nodes"
@@ -39,10 +35,10 @@ async function transformSpotlight(
3935

4036
toJSX(node, {
4137
props: {
42-
codeConfig: CH_CODE_CONFIG_PLACEHOLDER,
4338
editorSteps: editorSteps,
4439
presetConfig,
4540
},
4641
appendProps: true,
42+
addConfigProp: true,
4743
})
4844
}

packages/mdx/src/remark/unist-utils.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ export async function visitAsync(
4646
await Promise.all(promises)
4747
}
4848

49-
// if one of the props in a toJSX call has the CH_CODE_CONFIG_PLACEHOLDER value
50-
// it will be replaced with a reference to the CH_CODE_CONFIG_VAR_NAME var
51-
export const CH_CODE_CONFIG_PLACEHOLDER =
52-
"CH_CodeConfig" as any
5349
export const CH_CODE_CONFIG_VAR_NAME = "chCodeConfig"
5450

5551
/**
@@ -64,14 +60,15 @@ export function toJSX(
6460
props,
6561
name,
6662
appendProps = false,
63+
addConfigProp = false,
6764
}: {
6865
type?: string
6966
props: Record<string, any>
7067
name?: string
7168
appendProps?: boolean
69+
addConfigProp?: boolean
7270
}
7371
) {
74-
// console.log(`transforming ${node.name} to ${name}`)
7572
node.type = type
7673
if (name) {
7774
node.name = name
@@ -82,26 +79,26 @@ export function toJSX(
8279
node.attributes = node.attributes || []
8380
}
8481

82+
if (addConfigProp) {
83+
node.attributes.push(
84+
toAttribute("codeConfig", CH_CODE_CONFIG_VAR_NAME, {
85+
type: "Identifier",
86+
name: CH_CODE_CONFIG_VAR_NAME,
87+
})
88+
)
89+
}
90+
8591
Object.keys(props).forEach(key => {
8692
if (props[key] === undefined) {
8793
return
8894
}
89-
if (props[key] === CH_CODE_CONFIG_PLACEHOLDER) {
90-
node.attributes.push(
91-
toAttribute(key, CH_CODE_CONFIG_VAR_NAME, {
92-
type: "Identifier",
93-
name: CH_CODE_CONFIG_VAR_NAME,
94-
})
95+
node.attributes.push(
96+
toAttribute(
97+
key,
98+
JSON.stringify(props[key]),
99+
valueToEstree(props[key])
95100
)
96-
} else {
97-
node.attributes.push(
98-
toAttribute(
99-
key,
100-
JSON.stringify(props[key]),
101-
valueToEstree(props[key])
102-
)
103-
)
104-
}
101+
)
105102
})
106103
}
107104

0 commit comments

Comments
 (0)