Skip to content

Commit 943ad1d

Browse files
committed
Add show prop to CH.Code
1 parent 81f9767 commit 943ad1d

File tree

3 files changed

+87
-24
lines changed

3 files changed

+87
-24
lines changed

packages/mdx/dev/content/scrollycoding.mdx

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ Start by installing next and react on an empty directory:
66
npm install next react react-dom
77
```
88

9-
<CH.Section>
10-
11-
Then also install the [mdx plugin for next](focus://1[13:21]), the [mdx loader](focus://1[23:36]), and [Code Hike](focus://1[38:56]).
12-
13-
```bash
14-
npm install @next/mdx @mdx-js/loader @code-hike/mdx@next
15-
```
16-
17-
</CH.Section>
18-
19-
<div style={{ height: "0.5em" }} />
9+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. At imperdiet dui accumsan sit amet nulla facilities morbi tempus. Praesent elementum facilisis leo vel fringilla. Congue mauris rhoncus aenean vel. Egestas sed tempus urna et pharetra pharetra massa massa ultricies.
2010

2111
<CH.Scrollycoding>
2212

@@ -28,6 +18,8 @@ Also, make sure you include `"md"` and `"mdx"` on the _`pageExtensions`_ setting
2818

2919
After this step, you can use MDX files in your project, but you can't use Code Hike yet.
3020

21+
<CH.Code>
22+
3123
{/* prettier-ignore */}
3224
```js next.config.js
3325
const withMDX = require("@next/mdx")({
@@ -47,6 +39,17 @@ module.exports = withMDX({
4739

4840
---
4941

42+
```css index.css
43+
.scrollycoding {
44+
overflow: auto;
45+
max-height: calc(100vh - 100px);
46+
}
47+
```
48+
49+
</CH.Code>
50+
51+
---
52+
5053
To set up Code Hike you need to [import the <CH.InlineCode>@code-hike/mdx</CH.InlineCode> plugin](focus://1,6[1:6]), and add it to the remarkPlugins array in the next.config.js file.
5154

5255
You will also want to import a theme. You can import one from shiki, or make a custom one.
@@ -81,6 +84,8 @@ Then you need to create a `pages/_app.js` file if you don't have one.
8184

8285
You can find more information about the `_app.js` file in the [Next.js official docs](https://nextjs.org/docs/advanced-features/custom-app).
8386

87+
<CH.Code show={["pages/_app.js"]}>
88+
8489
{/* prettier-ignore */}
8590
```js pages/_app.js
8691
function MyApp({ Component, pageProps }) {
@@ -90,6 +95,8 @@ function MyApp({ Component, pageProps }) {
9095
export default MyApp
9196
```
9297

98+
</CH.Code>
99+
93100
---
94101

95102
The pages/\_app.js file is where you add global stylesheets in Next.js.
@@ -98,8 +105,14 @@ Here we need to import Code Hike's CSS.
98105

99106
If you want to customize Code Hike's styles with a global stylesheet make sure to import it after this import to avoid specificity issues.
100107

108+
<CH.Code show={["next.config.js", "pages/_app.js"]}>
109+
110+
```js next.config.js
111+
112+
```
113+
101114
{/* prettier-ignore */}
102-
```js pages/_app.js focus=1
115+
```js pages/_app.js focus=1 active
103116
import "@code-hike/mdx/dist/index.css"
104117

105118
function MyApp({ Component, pageProps }) {
@@ -109,12 +122,16 @@ function MyApp({ Component, pageProps }) {
109122
export default MyApp
110123
```
111124

125+
</CH.Code>
126+
112127
---
113128

114129
Now you can create mdx files using codehike.
115130

116131
Markdown (.md) files should also work.
117132

133+
<CH.Code show={["next.config.js","pages/my.mdx"]}>
134+
118135
{/* prettier-ignore */}
119136
~~~md pages/my.mdx
120137
# Hello
@@ -128,8 +145,16 @@ print("Rendered with Code Hike")
128145
Lorem ipsum dolor sit amet.
129146
~~~
130147

148+
</CH.Code>
149+
131150
</CH.Scrollycoding>
132151

133152
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. At imperdiet dui accumsan sit amet nulla facilities morbi tempus. Praesent elementum facilisis leo vel fringilla. Congue mauris rhoncus aenean vel. Egestas sed tempus urna et pharetra pharetra massa massa ultricies.
134153

154+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. At imperdiet dui accumsan sit amet nulla facilities morbi tempus. Praesent elementum facilisis leo vel fringilla. Congue mauris rhoncus aenean vel. Egestas sed tempus urna et pharetra pharetra massa massa ultricies.
155+
156+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. At imperdiet dui accumsan sit amet nulla facilities morbi tempus. Praesent elementum facilisis leo vel fringilla. Congue mauris rhoncus aenean vel. Egestas sed tempus urna et pharetra pharetra massa massa ultricies.
157+
135158
Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. At imperdiet dui accumsan sit amet nulla facilities morbi tempus. Praesent elementum facilisis leo vel fringilla. Congue mauris rhoncus aenean vel. Egestas sed tempus urna et pharetra pharetra massa massa ultricies.
159+
160+
<div style={{ height: "8em" }} />

packages/mdx/src/remark/code-files-reducer.tsx

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,41 @@ import { EditorStep, CodeFile } from "../mini-editor"
44

55
export function reduceSteps(
66
baseStep: EditorStep,
7-
extraStep: EditorStep
7+
extraStep: EditorStep,
8+
filter?: string[]
89
): EditorStep {
910
let files = reduceFiles(baseStep.files, extraStep.files)
1011

12+
const newNorthPanel = reducePanel(
13+
baseStep.northPanel,
14+
extraStep.northPanel,
15+
extraStep.southPanel
16+
)!
17+
18+
const newSouthPanel = reducePanel(
19+
baseStep.southPanel,
20+
extraStep.southPanel,
21+
extraStep.northPanel
22+
)
23+
24+
if (filter) {
25+
newNorthPanel.tabs = newNorthPanel.tabs.filter(
26+
filename => filter.includes(filename)
27+
)
28+
29+
if (newSouthPanel) {
30+
newNorthPanel.tabs = newNorthPanel.tabs.filter(
31+
filename => filter.includes(filename)
32+
)
33+
}
34+
}
35+
1136
return {
1237
...baseStep,
1338
...extraStep,
1439
files: files,
15-
northPanel: reducePanel(
16-
baseStep.northPanel,
17-
extraStep.northPanel,
18-
extraStep.southPanel
19-
)!,
20-
southPanel: reducePanel(
21-
baseStep.southPanel,
22-
extraStep.southPanel,
23-
extraStep.northPanel
24-
),
40+
northPanel: newNorthPanel,
41+
southPanel: newSouthPanel,
2542
}
2643
}
2744

packages/mdx/src/remark/steps.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export async function extractStepsInfo(
3535
config
3636
)
3737

38+
const filter = getFilterFromEditorNode(child)
39+
3840
if (stepIndex === 0) {
3941
// for the header props, keep it as it is
4042
step.editorStep = editorStep
@@ -45,7 +47,11 @@ export async function extractStepsInfo(
4547
? steps[0].editorStep
4648
: steps[stepIndex - 1].editorStep
4749

48-
step.editorStep = reduceSteps(baseStep, editorStep)
50+
step.editorStep = reduceSteps(
51+
baseStep,
52+
editorStep,
53+
filter
54+
)
4955
}
5056
} else {
5157
step.children.push(child)
@@ -62,3 +68,18 @@ export async function extractStepsInfo(
6268

6369
return steps.map(step => step.editorStep)
6470
}
71+
72+
/**
73+
* Extracts the `show` prop from <CH.Code show={["foo.js", "bar.html"]} />
74+
*/
75+
function getFilterFromEditorNode(node: any) {
76+
const value = node.attributes?.find(
77+
a => a.name === "show"
78+
)?.value?.value
79+
80+
if (value) {
81+
return JSON.parse(value)
82+
} else {
83+
return undefined
84+
}
85+
}

0 commit comments

Comments
 (0)