Skip to content

Commit 2d20cfd

Browse files
authored
Merge pull request #170 from code-hike/contentlayer-example
Contentlayer example
2 parents bdc878e + 2199907 commit 2d20cfd

14 files changed

+901
-11
lines changed

examples/contentlayer/.babelrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["next/babel"]
3+
}

examples/contentlayer/.gitignore

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
.pnpm-debug.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
37+
38+
# Contentlayer
39+
.contentlayer

examples/contentlayer/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Next.js + Contentlayer + Code Hike
2+
3+
See [this guide](https://codehike.org/docs/installation/contentlayer).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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/dark-plus.json")
6+
7+
const Post = defineDocumentType(() => ({
8+
name: "Post",
9+
filePathPattern: `**/*.mdx`,
10+
contentType: "mdx",
11+
fields: {
12+
title: {
13+
type: "string",
14+
description: "The title of the post",
15+
required: true,
16+
},
17+
},
18+
computedFields: {
19+
url: {
20+
type: "string",
21+
resolve: (doc) => `/posts/${doc._raw.flattenedPath}`,
22+
},
23+
},
24+
}))
25+
26+
export default makeSource({
27+
contentDirPath: "posts",
28+
documentTypes: [Post],
29+
30+
mdx: { remarkPlugins: [[remarkCodeHike, { theme }]] },
31+
})

examples/contentlayer/next-env.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/basic-features/typescript for more information.

examples/contentlayer/next.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const { withContentlayer } = require("next-contentlayer")
2+
3+
module.exports = withContentlayer(
4+
{} // Next.js config here
5+
)

examples/contentlayer/package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "codehike-contentlayer",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "INIT_CWD=$PWD next build",
8+
"start": "next start"
9+
},
10+
"dependencies": {
11+
"@code-hike/mdx": "^0.4.0",
12+
"@types/node": "^17.0.30",
13+
"contentlayer": "latest",
14+
"next": "^12.1.0",
15+
"next-contentlayer": "latest",
16+
"react": "^17.0.2",
17+
"react-dom": "^17.0.2"
18+
},
19+
"devDependencies": {
20+
"@types/react": "^17.0.2",
21+
"typescript": "4.6.3"
22+
}
23+
}

examples/contentlayer/pages/_app.tsx

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

examples/contentlayer/pages/index.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Link from "next/link"
2+
import { allPosts, Post } from "contentlayer/generated"
3+
4+
export async function getStaticProps() {
5+
return { props: { posts: allPosts } }
6+
}
7+
8+
export default function Home({ posts }: { posts: Post[] }) {
9+
return (
10+
<div>
11+
<h1>A Blog</h1>
12+
Posts:
13+
<ul>
14+
{posts.map((post, idx) => (
15+
<li key={idx}>
16+
<Link href={post.url}>
17+
<a>{post.title}</a>
18+
</Link>
19+
</li>
20+
))}
21+
</ul>
22+
</div>
23+
)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Head from "next/head"
2+
import { allPosts, Post } from "contentlayer/generated"
3+
import { useMDXComponent } from "next-contentlayer/hooks"
4+
5+
export async function getStaticPaths() {
6+
const paths: string[] = allPosts.map((post) => post.url)
7+
return {
8+
paths,
9+
fallback: false,
10+
}
11+
}
12+
13+
export async function getStaticProps({ params }) {
14+
const post = allPosts.find((post) => post._raw.flattenedPath === params.slug)
15+
return {
16+
props: {
17+
post,
18+
},
19+
}
20+
}
21+
22+
const PostLayout = ({ post }: { post: Post }) => {
23+
const MDXContent = useMDXComponent(post.body.code)
24+
return (
25+
<article style={{ width: 600, margin: "0 auto" }}>
26+
<Head>
27+
<title>{post.title}</title>
28+
</Head>
29+
<h1>{post.title}</h1>
30+
<MDXContent />
31+
</article>
32+
)
33+
}
34+
35+
export default PostLayout
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Post one
3+
---
4+
5+
Blog posts have their own pages. The content source is a markdown file, parsed to HTML by Contentlayer.
6+
7+
```js foo.js
8+
const x = 2
9+
```
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Post two
3+
---
4+
5+
**Contentlayer makes working with content easy.** It is a content preprocessor that validates and transforms your content into type-safe JSON you can easily import into your application.

examples/contentlayer/tsconfig.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": false,
8+
"forceConsistentCasingInFileNames": true,
9+
"noEmit": true,
10+
"incremental": true,
11+
"esModuleInterop": true,
12+
"module": "esnext",
13+
"moduleResolution": "node",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"jsx": "preserve",
17+
"baseUrl": ".",
18+
"paths": {
19+
"contentlayer/generated": ["./.contentlayer/generated"]
20+
}
21+
},
22+
"include": [
23+
"next-env.d.ts",
24+
"**/*.ts",
25+
"**/*.tsx",
26+
".contentlayer/generated"
27+
],
28+
"exclude": ["node_modules"]
29+
}

0 commit comments

Comments
 (0)