Skip to content

Commit e91717e

Browse files
committed
Remark refactor
1 parent 3506796 commit e91717e

File tree

12 files changed

+104
-91
lines changed

12 files changed

+104
-91
lines changed

packages/mdx/src/remark/code.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ import {
1616
} from "./annotations"
1717
import { mergeFocus } from "../utils"
1818
import { CodeNode, SuperNode } from "./nodes"
19+
import { CodeHikeConfig } from "./config"
1920

2021
export async function transformCodeNodes(
2122
tree: SuperNode,
22-
{ theme }: { theme: any }
23+
config: CodeHikeConfig
2324
) {
2425
await visitAsync(
2526
tree,
2627
"code",
2728
async (node: CodeNode, index, parent) => {
2829
await transformCode(
2930
{ node, index, parent: parent! },
30-
{ theme }
31+
config
3132
)
3233
}
3334
)
@@ -43,7 +44,7 @@ export function isEditorNode(node: SuperNode) {
4344

4445
async function transformCode(
4546
nodeInfo: NodeInfo<CodeNode>,
46-
config: { theme: any }
47+
config: CodeHikeConfig
4748
) {
4849
toJSX(nodeInfo.node, {
4950
name: "CH.Code",
@@ -52,7 +53,7 @@ async function transformCode(
5253
}
5354
export async function transformEditor(
5455
nodeInfo: NodeInfo,
55-
config: { theme: any }
56+
config: CodeHikeConfig
5657
) {
5758
toJSX(nodeInfo.node, {
5859
name: "CH.Code",
@@ -62,7 +63,7 @@ export async function transformEditor(
6263

6364
export async function mapAnyCodeNode(
6465
nodeInfo: NodeInfo,
65-
config: { theme: any }
66+
config: CodeHikeConfig
6667
) {
6768
const { node } = nodeInfo
6869
if (node.type === "code") {
@@ -74,7 +75,7 @@ export async function mapAnyCodeNode(
7475

7576
async function mapCode(
7677
nodeInfo: NodeInfo<CodeNode>,
77-
config: { theme: any }
78+
config: CodeHikeConfig
7879
): Promise<EditorProps> {
7980
const file = await mapFile(nodeInfo, config)
8081
const props: EditorProps = {
@@ -91,7 +92,7 @@ async function mapCode(
9192

9293
export async function mapEditor(
9394
{ node }: NodeInfo,
94-
config: { theme: any }
95+
config: CodeHikeConfig
9596
): Promise<EditorProps> {
9697
const [northNodes, southNodes = []] = splitChildren(
9798
node,
@@ -146,7 +147,7 @@ export async function mapEditor(
146147

147148
async function mapFile(
148149
{ node, index, parent }: NodeInfo<CodeNode>,
149-
config: { theme: any }
150+
config: CodeHikeConfig
150151
): Promise<CodeStep & FileOptions & { name: string }> {
151152
const { theme } = config
152153

packages/mdx/src/remark/config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export type CodeHikeConfig = {
2+
theme: any
3+
lineNumbers?: boolean
4+
autoImport?: boolean
5+
showCopyButton?: boolean
6+
}
7+
8+
/**
9+
* Add defaults and normalize config
10+
*/
11+
export function addConfigDefaults(
12+
config: Partial<CodeHikeConfig> | undefined
13+
): CodeHikeConfig {
14+
// TODO warn when config looks weird
15+
return {
16+
...config,
17+
theme: config?.theme || {},
18+
autoImport: config?.autoImport === false ? false : true,
19+
}
20+
}

packages/mdx/src/remark/editor.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { visitAsync } from "./unist-utils"
22
import { transformEditor } from "./code"
33
import { JsxNode, SuperNode } from "./nodes"
4+
import { CodeHikeConfig } from "./config"
45

56
export async function transformEditorNodes(
67
tree: SuperNode,
7-
{ theme }: { theme: any }
8+
config: CodeHikeConfig
89
) {
910
await visitAsync(
1011
tree,
@@ -13,7 +14,7 @@ export async function transformEditorNodes(
1314
if (node.name === "CH.Code") {
1415
await transformEditor(
1516
{ node, index, parent: parent! },
16-
{ theme }
17+
config
1718
)
1819
}
1920
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { EditorStep } from "../mini-editor"
88
import { Code } from "../utils"
99
import { SuperNode, visit } from "./nodes"
1010
import visitParents from "unist-util-visit-parents"
11+
import { CodeHikeConfig } from "./config"
1112

1213
export async function transformInlineCodes(
1314
tree: SuperNode,
14-
{ theme }: { theme: any }
15+
{ theme }: CodeHikeConfig
1516
) {
1617
// transform *`foo`* to <CH.InlineCode>foo</CH.InlineCode>
1718
visit(tree, "emphasis", (node: any) => {

packages/mdx/src/remark/scrollycoding.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import {
44
CH_CODE_CONFIG_PLACEHOLDER,
55
} from "./unist-utils"
66
import { extractStepsInfo } from "./steps"
7-
import { getPresetConfig } from "./preview"
7+
import { getPresetConfig } from "./transform.preview"
88
import { transformLinks } from "./section"
99
import { SuperNode } from "./nodes"
10+
import { CodeHikeConfig } from "./config"
1011

1112
export async function transformScrollycodings(
1213
tree: SuperNode,
13-
config: { theme: any }
14+
config: CodeHikeConfig
1415
) {
1516
await visitAsync(
1617
tree,
@@ -24,11 +25,11 @@ export async function transformScrollycodings(
2425
}
2526
async function transformScrollycoding(
2627
node: SuperNode,
27-
{ theme }: { theme: any }
28+
config: CodeHikeConfig
2829
) {
2930
const editorSteps = await extractStepsInfo(
3031
node,
31-
{ theme },
32+
config,
3233
"merge step with previous"
3334
)
3435

packages/mdx/src/remark/section.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { visitAsync, toJSX } from "./unist-utils"
22
import { isEditorNode, mapAnyCodeNode } from "./code"
33
import { SuperNode, visit } from "./nodes"
4+
import { CodeHikeConfig } from "./config"
45

56
export async function transformSections(
67
tree: SuperNode,
7-
config: { theme: any }
8+
config: CodeHikeConfig
89
) {
910
await visitAsync(
1011
tree,
@@ -19,7 +20,7 @@ export async function transformSections(
1920

2021
async function transformSection(
2122
node: SuperNode,
22-
config: { theme: any }
23+
config: CodeHikeConfig
2324
) {
2425
let props
2526
await visitAsync(

packages/mdx/src/remark/slideshow.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import {
44
CH_CODE_CONFIG_PLACEHOLDER,
55
} from "./unist-utils"
66
import { extractStepsInfo } from "./steps"
7-
import { getPresetConfig } from "./preview"
7+
import { getPresetConfig } from "./transform.preview"
88
import { JsxNode, SuperNode } from "./nodes"
9+
import { CodeHikeConfig } from "./config"
910

1011
export async function transformSlideshows(
1112
tree: SuperNode,
12-
config: { theme: any }
13+
config: CodeHikeConfig
1314
) {
1415
await visitAsync(
1516
tree,
@@ -23,11 +24,11 @@ export async function transformSlideshows(
2324
}
2425
async function transformSlideshow(
2526
node: SuperNode,
26-
{ theme }: { theme: any }
27+
config: CodeHikeConfig
2728
) {
2829
const editorSteps = await extractStepsInfo(
2930
node,
30-
{ theme },
31+
config,
3132
"merge step with previous"
3233
)
3334

packages/mdx/src/remark/spotlight.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import {
44
CH_CODE_CONFIG_PLACEHOLDER,
55
} from "./unist-utils"
66
import { extractStepsInfo } from "./steps"
7-
import { getPresetConfig } from "./preview"
7+
import { getPresetConfig } from "./transform.preview"
88
import { JsxNode, SuperNode } from "./nodes"
9+
import { CodeHikeConfig } from "./config"
910

1011
export async function transformSpotlights(
1112
tree: SuperNode,
12-
config: { theme: any }
13+
config: CodeHikeConfig
1314
) {
1415
await visitAsync(
1516
tree,
@@ -24,11 +25,11 @@ export async function transformSpotlights(
2425

2526
async function transformSpotlight(
2627
node: JsxNode,
27-
{ theme }: { theme: any }
28+
config: CodeHikeConfig
2829
) {
2930
const editorSteps = await extractStepsInfo(
3031
node,
31-
{ theme },
32+
config,
3233
"merge steps with header"
3334
)
3435

packages/mdx/src/remark/steps.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { EditorStep } from "../mini-editor"
22
import { isEditorNode, mapAnyCodeNode } from "./code"
33
import { reduceSteps } from "./code-files-reducer"
4+
import { CodeHikeConfig } from "./config"
45
import { SuperNode } from "./nodes"
56

67
// extract step info
78

89
export async function extractStepsInfo(
910
parent: SuperNode,
10-
config: { theme: any },
11+
config: CodeHikeConfig,
1112
merge:
1213
| "merge steps with header"
1314
| "merge step with previous"
File renamed without changes.

0 commit comments

Comments
 (0)