Skip to content

Commit feba575

Browse files
committed
Add examples
1 parent 94a6ec8 commit feba575

Some content is hidden

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

51 files changed

+4494
-523
lines changed

examples/.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"semi": false
3+
}

examples/contentlayer/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.next
3+
.contentlayer
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { defineDocumentType, makeSource } from "contentlayer/source-files"
2+
import { remarkCodeHike } from "@code-hike/mdx"
3+
import { createRequire } from "module"
4+
const require = createRequire(import.meta.url)
5+
const theme = require("shiki/themes/nord.json")
6+
7+
export const Post = defineDocumentType(() => ({
8+
name: "Post",
9+
contentType: "mdx",
10+
filePathPattern: "**/*.mdx",
11+
fields: {
12+
title: {
13+
type: "string",
14+
description: "The title of the post",
15+
required: true,
16+
},
17+
},
18+
}))
19+
20+
export default makeSource({
21+
contentDirPath: "posts",
22+
documentTypes: [Post],
23+
mdx: { remarkPlugins: [[remarkCodeHike, { theme }]] },
24+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"contentlayer/generated": ["./.contentlayer/generated"]
6+
}
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { withContentlayer } = require('next-contentlayer')
2+
3+
module.exports = withContentlayer()({})

examples/contentlayer/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"dependencies": {
3+
"@code-hike/mdx": "^0.3.0-next.26",
4+
"next": "^12.0.10",
5+
"react": "17.0.2",
6+
"react-dom": "17.0.2"
7+
},
8+
"devDependencies": {
9+
"contentlayer": "^0.1.1",
10+
"next-contentlayer": "^0.1.1"
11+
}
12+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { allPosts } from "contentlayer/generated"
2+
import { useMDXComponent } from "next-contentlayer/hooks"
3+
4+
export async function getStaticPaths() {
5+
const paths = allPosts.map((_) => "/" + _._raw.flattenedPath)
6+
return {
7+
paths,
8+
fallback: false,
9+
}
10+
}
11+
12+
export async function getStaticProps({ params }) {
13+
const post = allPosts.find((_) => _._raw.flattenedPath === params.slug)
14+
return {
15+
props: {
16+
post,
17+
},
18+
}
19+
}
20+
21+
const Page = ({ post }) => {
22+
const Component = useMDXComponent(post.body.code)
23+
return (
24+
<article style={{ maxWidth: 800 }}>
25+
<h1>{post.title}</h1>
26+
<Component />
27+
</article>
28+
)
29+
}
30+
31+
export default Page
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "@code-hike/mdx/dist/index.css"
2+
3+
function MyApp({ Component, pageProps }) {
4+
return <Component {...pageProps} />
5+
}
6+
7+
export default MyApp
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { allPosts } from "contentlayer/generated"
2+
import Link from "next/link"
3+
4+
export const getStaticProps = async ({}) => {
5+
return {
6+
props: {
7+
posts: allPosts.map((post) => ({
8+
slug: post._raw.flattenedPath,
9+
title: post.title,
10+
})),
11+
},
12+
}
13+
}
14+
15+
const Home = ({ posts }) => {
16+
return (
17+
<ul>
18+
{posts.map(({ slug, title }) => (
19+
<li key={slug}>
20+
<Link href={`/${slug}`}>
21+
<a>{title}</a>
22+
</Link>
23+
</li>
24+
))}
25+
</ul>
26+
)
27+
}
28+
29+
export default Home

examples/contentlayer/posts/my.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
type: "Post"
3+
title: "Hello World"
4+
---
5+
6+
Lorem ipsum dolor sit amet.
7+
8+
```python hello.py
9+
print("Rendered with Code Hike")
10+
```
11+
12+
Lorem ipsum dolor sit amet.

0 commit comments

Comments
 (0)