Skip to content

Commit 46662f3

Browse files
committed
Rename configs
1 parent fecddba commit 46662f3

File tree

12 files changed

+106
-81
lines changed

12 files changed

+106
-81
lines changed

packages/mdx/src/core/types.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { Theme } from "@code-hike/lighter"
2+
3+
type TriggerPosition = `${number}px` | `${number}%`
4+
5+
export type RemarkConfig = {
6+
// remark only
7+
theme: Theme
8+
autoImport?: boolean
9+
skipLanguages: string[]
10+
autoLink?: boolean
11+
// path to the current file, internal use only
12+
filepath?: string
13+
14+
// client config
15+
lineNumbers?: boolean
16+
showCopyButton?: boolean
17+
staticMediaQuery?: string
18+
triggerPosition?: TriggerPosition
19+
}
20+
21+
// the config that is passed from remark to the client components
22+
export type GlobalConfig = {
23+
themeName: string
24+
lineNumbers?: boolean
25+
showCopyButton?: boolean
26+
staticMediaQuery?: string
27+
triggerPosition?: TriggerPosition
28+
}
29+
30+
export function toGlobalConfig({
31+
theme,
32+
staticMediaQuery,
33+
lineNumbers,
34+
showCopyButton,
35+
triggerPosition,
36+
}: RemarkConfig): GlobalConfig {
37+
return {
38+
themeName:
39+
typeof theme === "string" ? theme : theme.name,
40+
staticMediaQuery,
41+
lineNumbers,
42+
showCopyButton,
43+
triggerPosition,
44+
}
45+
}
46+
47+
export type CodeSettings = {
48+
// from global config
49+
lineNumbers?: boolean
50+
showCopyButton?: boolean
51+
staticMediaQuery?: string
52+
themeName?: string
53+
// scrollycoding only
54+
triggerPosition?: TriggerPosition
55+
56+
showExpandButton?: boolean
57+
/* not really the height, when this changes we measure everything again */
58+
parentHeight?: any
59+
minColumns?: number
60+
minZoom?: number
61+
maxZoom?: number
62+
horizontalCenter?: boolean
63+
rows?: number | "focus" | (number | "focus")[]
64+
debug?: boolean
65+
}

packages/mdx/src/mdx-client/code.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import React from "react"
2-
import { CodeConfig, CodeSpring } from "../smooth-code"
2+
import { CodeSpring } from "../smooth-code"
33
import {
44
EditorSpring,
55
EditorProps,
66
EditorStep,
77
} from "../mini-editor"
8-
import { CodeHikeConfig } from "../remark/config"
8+
import { CodeSettings, GlobalConfig } from "../core/types"
99

10-
export function Code(
11-
props: EditorProps & Partial<CodeHikeConfig>
12-
) {
10+
export function Code(props: EditorProps & GlobalConfig) {
1311
const [step, setStep] = React.useState(props)
1412

1513
function onTabClick(filename: string) {
@@ -22,8 +20,8 @@ export function Code(
2220

2321
// build the CodeConfig from props and props.codeConfig
2422
export function mergeCodeConfig<T>(
25-
props: Partial<CodeConfig> & {
26-
codeConfig: Partial<CodeConfig>
23+
props: Partial<CodeSettings> & {
24+
codeConfig: Partial<CodeSettings>
2725
} & T
2826
) {
2927
const {
@@ -66,7 +64,7 @@ export function InnerCode({
6664
...props
6765
}: EditorProps & {
6866
onTabClick?: (filename: string) => void
69-
} & Partial<CodeHikeConfig>) {
67+
} & GlobalConfig) {
7068
const { className, style, codeConfig, ...editorProps } =
7169
mergeCodeConfig(props)
7270

packages/mdx/src/mdx-client/preview.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
SandboxInfo,
77
} from "@codesandbox/sandpack-client"
88
import { EditorStep } from "../mini-editor"
9-
import { CodeConfig } from "../smooth-code"
9+
import { CodeSettings } from "../core/types"
1010

1111
export type PresetConfig = SandboxInfo
1212
export function Preview({
@@ -27,7 +27,7 @@ export function Preview({
2727
show?: string
2828
style?: React.CSSProperties
2929
children?: React.ReactNode
30-
codeConfig?: CodeConfig
30+
codeConfig?: CodeSettings
3131
}) {
3232
const kids = presetConfig ? (
3333
<SandpackPreview

packages/mdx/src/mdx-client/scrollycoding.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import React from "react"
2-
import { EditorProps, EditorStep } from "../mini-editor"
2+
import { EditorStep } from "../mini-editor"
33
import { InnerCode, updateEditorStep } from "./code"
44
import { Scroller, Step as ScrollerStep } from "../scroller"
55
import { Preview, PresetConfig } from "./preview"
66
import { LinkableSection } from "./section"
77
import { extractPreviewSteps } from "./steps"
88
import { Swap } from "./ssmq"
99
import { StaticStepContext } from "./slots"
10+
import { GlobalConfig } from "../core/types"
1011

1112
type ScrollycodingProps = {
1213
children: React.ReactNode
1314
editorSteps: EditorStep[]
14-
codeConfig: EditorProps["codeConfig"]
15+
codeConfig: GlobalConfig
1516
start?: number
1617
presetConfig?: PresetConfig
1718
className?: string

packages/mdx/src/mini-editor/editor-shift.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import {
2-
CodeTween,
3-
CodeConfig,
4-
CodeStep,
5-
} from "../smooth-code"
1+
import { CodeTween, CodeStep } from "../smooth-code"
62
import React from "react"
73
import { codeToText } from "utils"
4+
import { CodeSettings } from "../core/types"
85

96
export type CodeFile = CodeStep & {
107
name: string
@@ -56,7 +53,7 @@ type Snapshot = {
5653
southTabs: TabsSnapshot | null
5754
}
5855

59-
function northConfig(codeConfig: CodeConfig) {
56+
function northConfig(codeConfig: CodeSettings) {
6057
if (Array.isArray(codeConfig.rows)) {
6158
return {
6259
...codeConfig,
@@ -66,7 +63,7 @@ function northConfig(codeConfig: CodeConfig) {
6663
return codeConfig
6764
}
6865

69-
function southConfig(codeConfig: CodeConfig) {
66+
function southConfig(codeConfig: CodeSettings) {
7067
if (Array.isArray(codeConfig.rows)) {
7168
return {
7269
...codeConfig,
@@ -82,7 +79,7 @@ export function useTransition(
8279
next: EditorStep,
8380
t: number,
8481
backward: boolean,
85-
codeConfig: CodeConfig
82+
codeConfig: CodeSettings
8683
): Transition {
8784
// prevSnapshot has the dimensions of the editor for t=0
8885
// nextSnapshot has the dimensions of the editor for t=1
@@ -174,7 +171,7 @@ export function useTransition(
174171
function startingPosition(
175172
prev: EditorStep,
176173
next: EditorStep,
177-
codeConfig: CodeConfig
174+
codeConfig: CodeSettings
178175
): Transition {
179176
const inputNorthPanel = prev.northPanel
180177
const inputSouthPanel = prev.southPanel
@@ -235,7 +232,7 @@ function startingPosition(
235232
function endingPosition(
236233
prev: EditorStep,
237234
next: EditorStep,
238-
codeConfig: CodeConfig
235+
codeConfig: CodeSettings
239236
): Transition {
240237
const inputNorthPanel = next.northPanel
241238
const inputSouthPanel = next.southPanel
@@ -312,7 +309,7 @@ function CodeTransition({
312309
nextFile: CodeFile
313310
t: number
314311
parentHeight: string
315-
codeConfig: CodeConfig & { htmlProps?: any }
312+
codeConfig: CodeSettings & { htmlProps?: any }
316313
}) {
317314
const htmlProps = {
318315
...codeConfig?.htmlProps,

packages/mdx/src/mini-editor/editor-spring.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from "react"
22
import { useSpring } from "use-spring"
33
import { EditorTween } from "./editor-tween"
4-
import { CodeConfig } from "../smooth-code"
54
import { EditorFrameProps } from "./editor-frame"
65
import { EditorStep, CodeFile } from "./editor-shift"
6+
import { CodeSettings } from "../core/types"
77

88
export { EditorSpring }
99
export type { EditorProps, EditorStep, CodeFile }
@@ -22,7 +22,7 @@ const defaultSpring = {
2222
}
2323
type EditorProps = EditorStep & {
2424
frameProps?: Partial<EditorFrameProps>
25-
codeConfig: CodeConfig
25+
codeConfig: CodeSettings
2626
springConfig?: SpringConfig
2727
} & DivProps
2828

packages/mdx/src/mini-editor/editor-tween.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
} from "./editor-frame"
66
import { TerminalPanel } from "./terminal-panel"
77
import { useTransition, EditorStep } from "./editor-shift"
8-
import { CodeConfig } from "../smooth-code"
98
import { useLayoutEffect } from "../utils"
109
import { CopyButton } from "smooth-code/copy-button"
1110
import { EditorExpandButton } from "mini-editor/expand-button"
11+
import { CodeSettings } from "../core/types"
1212

1313
export { EditorTransition, EditorTween }
1414
export type { EditorTransitionProps, EditorTweenProps }
@@ -18,7 +18,7 @@ type EditorTransitionProps = {
1818
next?: EditorStep
1919
t: number
2020
backward: boolean
21-
codeConfig: CodeConfig
21+
codeConfig: CodeSettings
2222
} & Omit<EditorFrameProps, "northPanel" | "southPanel">
2323

2424
type DivProps = React.PropsWithoutRef<
@@ -30,7 +30,7 @@ type EditorTweenProps = {
3030
next?: EditorStep
3131
t: number
3232
backward: boolean
33-
codeConfig: CodeConfig
33+
codeConfig: CodeSettings
3434
frameProps?: Partial<EditorFrameProps>
3535
} & DivProps
3636

packages/mdx/src/remark/config.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
import { Theme } from "@code-hike/lighter"
1+
import { RemarkConfig } from "../core/types"
22

3-
export type CodeHikeConfig = {
4-
theme: Theme
5-
lineNumbers?: boolean
6-
autoImport?: boolean
7-
skipLanguages: string[]
8-
showExpandButton?: boolean
9-
showCopyButton?: boolean
10-
autoLink?: boolean
11-
staticMediaQuery?: string
12-
// path to the current file, internal use only
13-
filepath?: string
14-
}
3+
export type CodeHikeConfig = RemarkConfig
154

165
/**
176
* Add defaults and normalize config

packages/mdx/src/remark/transform.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { addConfigDefaults, CodeHikeConfig } from "./config"
1313

1414
import type { Node } from "unist"
1515
import { getThemeColors } from "@code-hike/lighter"
16+
import { toGlobalConfig } from "core/types"
1617

1718
const transforms = [
1819
transformPreviews,
@@ -155,16 +156,12 @@ async function addConfig(
155156
config: CodeHikeConfig
156157
) {
157158
const { theme } = config
158-
const themeName =
159-
typeof theme === "string" ? theme : theme.name
160-
const style = await getStyle(theme, themeName)
161159

162-
const codeConfig = {
163-
staticMediaQuery: config.staticMediaQuery,
164-
lineNumbers: config.lineNumbers,
165-
showCopyButton: config.showCopyButton,
166-
themeName,
167-
}
160+
const globalConfig = toGlobalConfig(config)
161+
const style = await getStyle(
162+
theme,
163+
globalConfig.themeName
164+
)
168165

169166
tree.children.unshift({
170167
type: "mdxJsxFlowElement",
@@ -236,7 +233,7 @@ async function addConfig(
236233
type: "Identifier",
237234
name: CH_CODE_CONFIG_VAR_NAME,
238235
},
239-
init: valueToEstree(codeConfig),
236+
init: valueToEstree(globalConfig),
240237
},
241238
],
242239
kind: "const",

packages/mdx/src/smooth-code/code-spring.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import React from "react"
22
import { useSpring } from "use-spring"
33
import { FullTween } from "../utils"
4-
import {
5-
CodeTween,
6-
CodeConfig,
7-
CodeStep,
8-
} from "./code-tween"
4+
import { CodeTween, CodeStep } from "./code-tween"
5+
import { CodeSettings } from "../core/types"
96

107
type SpringConfig = Parameters<typeof useSpring>[1]
118

@@ -27,7 +24,7 @@ export function CodeSpring({
2724
...htmlProps
2825
}: {
2926
step: CodeStep
30-
config: CodeConfig & { spring?: SpringConfig }
27+
config: CodeSettings & { spring?: SpringConfig }
3128
} & HTMLProps) {
3229
const { tween, t } = useStepSpring(step, config.spring)
3330
return (

0 commit comments

Comments
 (0)