Skip to content

Commit 95731ad

Browse files
committed
Allow empty code steps
1 parent 80062c9 commit 95731ad

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

packages/mdx/dev/content/scrollycoding-preview-jsx.mdx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,6 @@ You will also want to import a theme. You can import one from shiki, or make a c
9090

9191
Pass the theme into Code Hike's config object, there are a few more settings you can use, like lineNumbers for example.
9292

93-
{/* prettier-ignore */}
94-
```js next.config.js focus=1:2,7:9
95-
const { remarkCH } = require("@code-hike/mdx")
96-
const theme = require("shiki/themes/nord.json")
97-
98-
const withMDX = require("@next/mdx")({
99-
extension: /\.mdx?$/,
100-
options: {
101-
remarkPlugins: [
102-
[remarkCH, { theme }]
103-
],
104-
},
105-
})
106-
107-
module.exports = withMDX({
108-
pageExtensions: [
109-
"ts", "tsx", "js",
110-
"jsx", "md", "mdx"
111-
],
112-
})
113-
```
114-
11593
---
11694

11795
### Step 4

packages/mdx/src/remark/steps.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export async function extractStepsInfo(
5151
const baseStep =
5252
merge === "merge steps with header"
5353
? steps[0].editorStep
54-
: steps[stepIndex - 1].editorStep
54+
: previousEditorStep(steps, stepIndex)
5555

5656
step.editorStep = reduceStep(
5757
baseStep,
@@ -113,14 +113,41 @@ export async function extractStepsInfo(
113113
parent.children = parent.children.concat(previewSteps)
114114
}
115115

116+
// fill editor steps holes
117+
const editorSteps = steps.map(step => step.editorStep)
118+
editorSteps.forEach((editorStep, i) => {
119+
if (!editorStep) {
120+
editorSteps[i] =
121+
merge === "merge steps with header"
122+
? editorSteps[0]
123+
: editorSteps[i - 1]
124+
}
125+
})
126+
116127
return {
117-
editorSteps: steps.map(step => step.editorStep),
128+
editorSteps,
118129
hasPreviewSteps,
119130

120131
presetConfig,
121132
}
122133
}
123134

135+
function previousEditorStep(
136+
steps: {
137+
editorStep?: EditorStep
138+
}[],
139+
index: number
140+
) {
141+
if (index === 0) {
142+
throw new Error("The first step should have some code")
143+
}
144+
145+
return (
146+
steps[index - 1].editorStep ||
147+
previousEditorStep(steps, index - 1)
148+
)
149+
}
150+
124151
/**
125152
* Extracts the `show` prop from <CH.Code show={["foo.js", "bar.html"]} />
126153
*/

0 commit comments

Comments
 (0)