Skip to content

Commit ff73d54

Browse files
authored
Merge pull request #149 from code-hike/ch-test-site
Add test site
2 parents 4b940f2 + 42f5a9c commit ff73d54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2463
-7913
lines changed

.gitpod.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/contentlayer/.gitignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/contentlayer/contentlayer.config.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

examples/contentlayer/jsconfig.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/contentlayer/next.config.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/contentlayer/package.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/contentlayer/pages/[slug].js

Lines changed: 0 additions & 31 deletions
This file was deleted.

examples/contentlayer/pages/_app.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/contentlayer/pages/index.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/contentlayer/posts/my.mdx

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Comment annotations
2+
3+
Instead of defining the focus on the codeblock metastring, you can us comments inside the code to make the focus relative.
4+
5+
```js
6+
function lorem(ipsum, dolor = 1) {
7+
const sit = ipsum == null && 0
8+
dolor = sit - amet(dolor)
9+
// focus
10+
return sit ? consectetur(ipsum) : []
11+
}
12+
13+
// focus(1:4)
14+
function adipiscing(...elit) {
15+
console.log(elit)
16+
return elit.map(ipsum => ipsum.sit)
17+
}
18+
19+
// focus[13:17]
20+
console.log("hey")
21+
```
22+
23+
Same with other annotations like `bg` and `box`.
24+
25+
```js
26+
// bg(1:2)
27+
function foo() {
28+
// box[6:9]
29+
console.log("hover me")
30+
return 8
31+
}
32+
```
33+
34+
You can pass a string parameter to comment annotations. For `bg` and `box`, it will be used as a color.
35+
36+
```js index.js
37+
function lorem(ipsum, dolor = 1) {
38+
// bg(1:3) linear-gradient(90deg, #020024 0%, #090979 35%, #00d4ff 100%)
39+
const sit = ipsum == null && 0
40+
dolor = sit - amet(dolor)
41+
return sit ? consectetur(ipsum) : []
42+
}
43+
44+
// this comment isn't an annotation
45+
//hello
46+
// 你好
47+
// å
48+
function adipiscing(...elit) {
49+
console.log(elit)
50+
// box[19:36] aqua
51+
return elit.map(ipsum => ipsum.sit)
52+
}
53+
```
54+
55+
And now we introduce two more annotations: `link` and `label`
56+
57+
```js focus=4,8
58+
function lorem(ipsum, dolor = 1) {
59+
const sit = ipsum == null && 0
60+
dolor = sit - amet(dolor)
61+
// link[16:26] https://github.com/code-hike/codehike
62+
return sit ? consectetur(ipsum) : []
63+
}
64+
65+
function adipiscing(...elit) {
66+
// label something something
67+
console.log("hover me")
68+
return elit.map(ipsum => ipsum.sit)
69+
}
70+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Annotations
2+
3+
Besides `focus`, there are a few more annotations that can be used to highlight code.
4+
5+
<div foo={CH.annotations.Label} />
6+
7+
### `box` annotation
8+
9+
```js box=2[15:43],4[10:12]
10+
function lorem(ipsum, dolor = 1) {
11+
const sit = ipsum == null ? 0 : ipsum.sit
12+
dolor = sit - amet(dolor)
13+
return sit
14+
? consectetur(ipsum, 0, dolor < 0 ? 0 : dolor)
15+
: []
16+
}
17+
```
18+
19+
### `bg` annotation
20+
21+
```js bg=2,4:6
22+
function lorem(ipsum, dolor = 1) {
23+
const sit = ipsum == null ? 0 : ipsum.sit
24+
dolor = sit - amet(dolor)
25+
return sit
26+
? consectetur(ipsum, 0, dolor < 0 ? 0 : dolor)
27+
: []
28+
}
29+
```
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
_Based on [Next.js official docs](https://nextjs.org/docs/advanced-features/using-mdx)._
2+
3+
Start by installing next and react on an empty directory:
4+
5+
```bash
6+
npm install next react react-dom
7+
```
8+
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" }} />
20+
21+
<CH.Scrollycoding>
22+
23+
Create a `next.config.js` file at the root of your project.
24+
25+
Here we use the [_`require("@next/mdx")`_](focus://1,6,8) plugin to set up MDX imports.
26+
27+
Also, make sure you include `"md"` and `"mdx"` on the _`pageExtensions`_ setting.
28+
29+
After this step, you can use MDX files in your project, but you can't use Code Hike yet.
30+
31+
{/* prettier-ignore */}
32+
```js next.config.js
33+
const withMDX = require("@next/mdx")({
34+
extension: /\.mdx?$/,
35+
options: {
36+
remarkPlugins: [],
37+
},
38+
})
39+
40+
module.exports = withMDX({
41+
pageExtensions: [
42+
"ts", "tsx", "js",
43+
"jsx", "md", "mdx"
44+
],
45+
})
46+
```
47+
48+
---
49+
50+
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.
51+
52+
You will also want to import a theme. You can import one from shiki, or make a custom one.
53+
54+
Pass the theme into Code Hike's config object, there are a few more settings you can use, like lineNumbers for example.
55+
56+
{/* prettier-ignore */}
57+
```js next.config.js focus=1:2,7:9
58+
const { remarkCH } = require("@code-hike/mdx")
59+
const theme = require("shiki/themes/nord.json")
60+
61+
const withMDX = require("@next/mdx")({
62+
extension: /\.mdx?$/,
63+
options: {
64+
remarkPlugins: [
65+
[remarkCH, { theme }]
66+
],
67+
},
68+
})
69+
70+
module.exports = withMDX({
71+
pageExtensions: [
72+
"ts", "tsx", "js",
73+
"jsx", "md", "mdx"
74+
],
75+
})
76+
```
77+
78+
---
79+
80+
Then you need to create a `pages/_app.js` file if you don't have one.
81+
82+
You can find more information about the `_app.js` file in the [Next.js official docs](https://nextjs.org/docs/advanced-features/custom-app).
83+
84+
{/* prettier-ignore */}
85+
```js pages/_app.js
86+
function MyApp({ Component, pageProps }) {
87+
return <Component {...pageProps} />
88+
}
89+
90+
export default MyApp
91+
```
92+
93+
---
94+
95+
The pages/\_app.js file is where you add global stylesheets in Next.js.
96+
97+
Here we need to import Code Hike's CSS.
98+
99+
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.
100+
101+
{/* prettier-ignore */}
102+
```js pages/_app.js focus=1
103+
import "@code-hike/mdx/dist/index.css"
104+
105+
function MyApp({ Component, pageProps }) {
106+
return <Component {...pageProps} />
107+
}
108+
109+
export default MyApp
110+
```
111+
112+
---
113+
114+
Now you can create mdx files using codehike.
115+
116+
Markdown (.md) files should also work.
117+
118+
{/* prettier-ignore */}
119+
~~~md pages/my.mdx
120+
# Hello
121+
122+
Lorem ipsum dolor sit amet.
123+
124+
```python hello.py
125+
print("Rendered with Code Hike")
126+
```
127+
128+
Lorem ipsum dolor sit amet.
129+
~~~
130+
131+
</CH.Scrollycoding>
132+
133+
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.
134+
135+
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.

0 commit comments

Comments
 (0)