-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #354 from contentlayerdev/feat/v0.3.0
0.3.0
- Loading branch information
Showing
55 changed files
with
1,792 additions
and
7,314 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { defineDocumentType, makeSource } from 'contentlayer/source-files' | ||
import { bundleMDX } from 'mdx-bundler' | ||
import * as ReactDOMServer from 'react-dom/server' | ||
import { getMDXComponent } from 'mdx-bundler/client/index.js' | ||
|
||
const mdxToHtml = async (mdxSource: string) => { | ||
const { code } = await bundleMDX({ source: mdxSource }) | ||
const MDXLayout = getMDXComponent(code) | ||
// TODO add your own components here | ||
const element = MDXLayout({ components: {} })! | ||
const html = ReactDOMServer.renderToString(element) | ||
return html | ||
} | ||
|
||
const Post = defineDocumentType(() => ({ | ||
name: 'Post', | ||
filePathPattern: `**/*.mdx`, | ||
contentType: 'mdx', | ||
fields: { | ||
title: { | ||
type: 'string', | ||
description: 'The title of the post', | ||
required: true, | ||
}, | ||
date: { | ||
type: 'date', | ||
description: 'The date of the post', | ||
required: true, | ||
}, | ||
}, | ||
computedFields: { | ||
url: { | ||
type: 'string', | ||
resolve: (doc) => `/posts/${doc._raw.flattenedPath}`, | ||
}, | ||
mdxHtml: { | ||
type: 'string', | ||
resolve: async (doc) => mdxToHtml(doc.body.raw), | ||
}, | ||
}, | ||
})) | ||
|
||
export default makeSource({ | ||
contentDirPath: 'posts', | ||
documentTypes: [Post], | ||
disableImportAliasWarning: true, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { allPosts } from './.contentlayer/generated/index.mjs' | ||
|
||
console.log(allPosts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "node-script-mdx-example", | ||
"private": true, | ||
"scripts": { | ||
"start": "contentlayer build && node --experimental-json-modules my-script.mjs" | ||
}, | ||
"dependencies": { | ||
"contentlayer": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: Change me! | ||
date: 2022-03-11 | ||
--- | ||
|
||
When you change a source file, Contentlayer automatically updates the content cache, which prompts Next.js to reload the content on screen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: Click me! | ||
date: 2022-02-28 | ||
--- | ||
|
||
Blog posts have their own pages. The content source is a markdown file, parsed to HTML by Contentlayer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: What is Contentlayer? | ||
date: 2022-02-22 | ||
--- | ||
|
||
**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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nextjs-repo |
86 changes: 86 additions & 0 deletions
86
examples/node-script-remote-content/contentlayer.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { defineDocumentType } from 'contentlayer/source-files' | ||
import { spawn } from 'node:child_process' | ||
import { makeSource } from 'contentlayer/source-remote-files' | ||
|
||
const Post = defineDocumentType(() => ({ | ||
name: 'Post', | ||
filePathPattern: `docs/**/*.md`, | ||
fields: { | ||
title: { | ||
type: 'string', | ||
required: false, | ||
}, | ||
description: { | ||
type: 'string', | ||
required: false, | ||
}, | ||
}, | ||
computedFields: { | ||
url: { | ||
type: 'string', | ||
resolve: (doc) => `/posts/${doc._raw.flattenedPath}`, | ||
}, | ||
}, | ||
})) | ||
|
||
const syncContentFromGit = async (contentDir: string) => { | ||
const syncRun = async () => { | ||
const gitUrl = 'https://github.com/vercel/next.js.git' | ||
await runBashCommand(` | ||
if [ -d "${contentDir}" ]; | ||
then | ||
cd "${contentDir}"; git pull; | ||
else | ||
git clone --depth 1 --single-branch ${gitUrl} ${contentDir}; | ||
fi | ||
`) | ||
} | ||
|
||
let wasCancelled = false | ||
let syncInterval | ||
|
||
const syncLoop = async () => { | ||
console.log('Syncing content files from git') | ||
|
||
await syncRun() | ||
|
||
if (wasCancelled) return | ||
|
||
syncInterval = setTimeout(syncLoop, 1000 * 60) | ||
} | ||
|
||
// Block until the first sync is done | ||
await syncLoop() | ||
|
||
return () => { | ||
wasCancelled = true | ||
clearTimeout(syncInterval) | ||
} | ||
} | ||
|
||
const runBashCommand = (command: string) => | ||
new Promise((resolve, reject) => { | ||
const child = spawn(command, [], { shell: true }) | ||
|
||
child.stdout.setEncoding('utf8') | ||
child.stdout.on('data', (data) => process.stdout.write(data)) | ||
|
||
child.stderr.setEncoding('utf8') | ||
child.stderr.on('data', (data) => process.stderr.write(data)) | ||
|
||
child.on('close', function (code) { | ||
if (code === 0) { | ||
resolve(void 0) | ||
} else { | ||
reject(new Error(`Command failed with exit code ${code}`)) | ||
} | ||
}) | ||
}) | ||
|
||
export default makeSource({ | ||
syncFiles: syncContentFromGit, | ||
contentDirPath: 'nextjs-repo', | ||
contentDirInclude: ['docs'], | ||
documentTypes: [Post], | ||
disableImportAliasWarning: true, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { allPosts } from './.contentlayer/generated/index.mjs' | ||
|
||
const postUrls = allPosts.map(post => post.url) | ||
|
||
console.log(`Found ${postUrls.length} posts:`); | ||
console.log(postUrls) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "node-script-remote-content-example", | ||
"private": true, | ||
"scripts": { | ||
"start": "contentlayer build && node --experimental-json-modules my-script.mjs" | ||
}, | ||
"dependencies": { | ||
"contentlayer": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.