Skip to content

Commit c72eca4

Browse files
authored
Merge pull request #179 from code-hike/hide-tabs
Hide tabs
2 parents 16fc256 + 943ad1d commit c72eca4

File tree

10 files changed

+170
-57
lines changed

10 files changed

+170
-57
lines changed

examples/contentlayer/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Next.js + Contentlayer + Code Hike
22

3-
See [this guide](https://codehike.org/docs/installation/contentlayer).
3+
See [this guide](https://codehike.org/docs/installation/contentlayer) or try it on [Stackblitz](https://stackblitz.com/github/code-hike/codehike/tree/main/examples/contentlayer).

examples/contentlayer/posts/one.mdx

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Post one
3+
---
4+
5+
This is the first post.
6+
7+
```python hello.py
8+
# mark[16:24]
9+
print("This is Code Hike")
10+
```

examples/contentlayer/posts/post-one.mdx

-9
This file was deleted.

examples/contentlayer/posts/post-two.mdx

-5
This file was deleted.

examples/contentlayer/posts/two.mdx

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Post two
3+
---
4+
5+
This is the second post.
6+
7+
```js foo.js
8+
const x = 2
9+
```

packages/mdx/dev/content/scrollycoding.mdx

+37-12
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/dev/content/section.mdx

+25-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Lorem ipsum dolor sit amet.
44

55
<CH.Section>
66

7-
Consectetur adipiscing elit, sed do eiusmod tempor [incididunt](focus://4:7) ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget.
8-
_`orem`_, _`sum`_
7+
Consectetur adipiscing elit, sed do eiusmod tempor [incididunt](focus://4:7) ut labore et dolore magna aliqua. Praesent elementum facilisis leo vel fringilla est ullamcorper eget. _`rem(ipsum, dol`_ xxx
8+
_`orem`_, xxx _`sum`_
99

1010
```js
1111
function lorem(ipsum, dolor) {
@@ -49,3 +49,26 @@ function lorem(ipsum, dolor) {
4949
_`ackground-color: va`_
5050

5151
</CH.Section>
52+
53+
## Issue #117
54+
55+
<CH.Section>
56+
57+
{/* prettier-ignore */}
58+
```js
59+
const PostLayout = ({ post }) => {
60+
const MDXContent = useMDXComponent(
61+
post.body.code
62+
)
63+
return (
64+
<article style={{ width: 600 }}>
65+
<h1>{post.title}</h1>
66+
<MDXContent />
67+
</article>
68+
)
69+
}
70+
```
71+
72+
It transforms the _`post.body.code`_ into a React component.
73+
74+
</CH.Section>

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

+28-11
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

+22-1
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+
}

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

+38-16
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ function getExistingCode(
138138
return undefined
139139
}
140140

141+
/**
142+
* Slice a line of tokens from a given index to a given length
143+
* Turns ("[abc][de][fgh]", 2, 4) into "[c][de][f]")
144+
*/
141145
function sliceTokens(
142146
line: Code["lines"][0],
143147
start: number,
@@ -148,44 +152,62 @@ function sliceTokens(
148152

149153
let headTokens = [] as Code["lines"][0]["tokens"]
150154

151-
for (let i = 0; i < tokens.length; i++) {
155+
// this for loop remove the unwanted prefix tokens and put the rest in headTokens
156+
for (
157+
let tokenIndex = 0;
158+
tokenIndex < tokens.length;
159+
tokenIndex++
160+
) {
152161
if (currentLength === start) {
153-
headTokens = tokens.slice(i)
162+
headTokens = tokens.slice(tokenIndex)
154163
break
155164
}
156-
if (currentLength + tokens[i].content.length > start) {
165+
if (
166+
currentLength + tokens[tokenIndex].content.length >
167+
start
168+
) {
157169
const newToken = {
158-
...tokens[i],
159-
content: tokens[i].content.slice(
170+
...tokens[tokenIndex],
171+
content: tokens[tokenIndex].content.slice(
160172
start - currentLength
161173
),
162174
}
163-
headTokens = [newToken].concat(tokens.slice(i + 1))
175+
headTokens = [newToken].concat(
176+
tokens.slice(tokenIndex + 1)
177+
)
164178
break
165179
}
166-
currentLength += tokens[i].content.length
180+
currentLength += tokens[tokenIndex].content.length
167181
}
168-
182+
// headTokens is now "[c][de][fgh]" (from the example above)
169183
currentLength = 0
170-
for (let i = 0; i < headTokens.length; i++) {
171-
if (currentLength === length) {
172-
return headTokens.slice(0, i)
184+
// this for loop remove the unwanted suffix tokens from headTokens
185+
for (
186+
let headTokenIndex = 0;
187+
headTokenIndex <= headTokens.length;
188+
headTokenIndex++
189+
) {
190+
if (currentLength >= length) {
191+
return headTokens.slice(0, headTokenIndex)
173192
}
193+
const currentToken = headTokens[headTokenIndex]
174194
if (
175-
currentLength + headTokens[i].content.length >
195+
currentLength + currentToken.content.length >
176196
length
177197
) {
178198
const newToken = {
179-
...headTokens[i],
180-
content: headTokens[i].content.slice(
199+
...currentToken,
200+
content: currentToken.content.slice(
181201
0,
182202
length - currentLength
183203
),
184204
}
185205

186-
return headTokens.slice(0, i).concat([newToken])
206+
return headTokens
207+
.slice(0, headTokenIndex)
208+
.concat([newToken])
187209
}
188-
currentLength += headTokens[i].content.length
210+
currentLength += currentToken.content.length
189211
}
190212
return []
191213
}

0 commit comments

Comments
 (0)