Skip to content

Contentlayer example #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/contentlayer/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["next/babel"]
}
39 changes: 39 additions & 0 deletions examples/contentlayer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo


# Contentlayer
.contentlayer
3 changes: 3 additions & 0 deletions examples/contentlayer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Next.js + Contentlayer + Code Hike

See [this guide](https://codehike.org/docs/installation/contentlayer).
31 changes: 31 additions & 0 deletions examples/contentlayer/contentlayer.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { defineDocumentType, makeSource } from "contentlayer/source-files"
import { remarkCodeHike } from "@code-hike/mdx"
import { createRequire } from "module"
const require = createRequire(import.meta.url)
const theme = require("shiki/themes/dark-plus.json")

const Post = defineDocumentType(() => ({
name: "Post",
filePathPattern: `**/*.mdx`,
contentType: "mdx",
fields: {
title: {
type: "string",
description: "The title of the post",
required: true,
},
},
computedFields: {
url: {
type: "string",
resolve: (doc) => `/posts/${doc._raw.flattenedPath}`,
},
},
}))

export default makeSource({
contentDirPath: "posts",
documentTypes: [Post],

mdx: { remarkPlugins: [[remarkCodeHike, { theme }]] },
})
5 changes: 5 additions & 0 deletions examples/contentlayer/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
5 changes: 5 additions & 0 deletions examples/contentlayer/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { withContentlayer } = require("next-contentlayer")

module.exports = withContentlayer(
{} // Next.js config here
)
23 changes: 23 additions & 0 deletions examples/contentlayer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "codehike-contentlayer",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "INIT_CWD=$PWD next build",
"start": "next start"
},
"dependencies": {
"@code-hike/mdx": "^0.4.0",
"@types/node": "^17.0.30",
"contentlayer": "latest",
"next": "^12.1.0",
"next-contentlayer": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@types/react": "^17.0.2",
"typescript": "4.6.3"
}
}
7 changes: 7 additions & 0 deletions examples/contentlayer/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import "@code-hike/mdx/dist/index.css"

function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}

export default MyApp
24 changes: 24 additions & 0 deletions examples/contentlayer/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Link from "next/link"
import { allPosts, Post } from "contentlayer/generated"

export async function getStaticProps() {
return { props: { posts: allPosts } }
}

export default function Home({ posts }: { posts: Post[] }) {
return (
<div>
<h1>A Blog</h1>
Posts:
<ul>
{posts.map((post, idx) => (
<li key={idx}>
<Link href={post.url}>
<a>{post.title}</a>
</Link>
</li>
))}
</ul>
</div>
)
}
35 changes: 35 additions & 0 deletions examples/contentlayer/pages/posts/[slug].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Head from "next/head"
import { allPosts, Post } from "contentlayer/generated"
import { useMDXComponent } from "next-contentlayer/hooks"

export async function getStaticPaths() {
const paths: string[] = allPosts.map((post) => post.url)
return {
paths,
fallback: false,
}
}

export async function getStaticProps({ params }) {
const post = allPosts.find((post) => post._raw.flattenedPath === params.slug)
return {
props: {
post,
},
}
}

const PostLayout = ({ post }: { post: Post }) => {
const MDXContent = useMDXComponent(post.body.code)
return (
<article style={{ width: 600, margin: "0 auto" }}>
<Head>
<title>{post.title}</title>
</Head>
<h1>{post.title}</h1>
<MDXContent />
</article>
)
}

export default PostLayout
9 changes: 9 additions & 0 deletions examples/contentlayer/posts/post-one.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Post one
---

Blog posts have their own pages. The content source is a markdown file, parsed to HTML by Contentlayer.

```js foo.js
const x = 2
```
5 changes: 5 additions & 0 deletions examples/contentlayer/posts/post-two.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Post two
---

**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.
29 changes: 29 additions & 0 deletions examples/contentlayer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"baseUrl": ".",
"paths": {
"contentlayer/generated": ["./.contentlayer/generated"]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".contentlayer/generated"
],
"exclude": ["node_modules"]
}
Loading