Skip to content

Commit 3a8ca11

Browse files
authored
Merge pull request #354 from contentlayerdev/feat/v0.3.0
0.3.0
2 parents 899684e + 237b1a9 commit 3a8ca11

File tree

55 files changed

+1792
-7314
lines changed

Some content is hidden

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

55 files changed

+1792
-7314
lines changed

.github/workflows/ci.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,27 @@ jobs:
5151
- run: yarn build
5252
- run: yarn start
5353
working-directory: examples/node-script
54+
55+
build-example-node-script-mdx:
56+
strategy:
57+
matrix:
58+
node-version: [14, 16, 17, 18]
59+
os: [ubuntu-latest, windows-latest]
60+
runs-on: ${{ matrix.os }}
61+
steps:
62+
- uses: schickling-actions/checkout-and-install@main
63+
- run: yarn build
64+
- run: yarn start
65+
working-directory: examples/node-script-mdx
66+
67+
build-example-node-script-remote-content:
68+
strategy:
69+
matrix:
70+
node-version: [14, 16, 17, 18]
71+
os: [ubuntu-latest, windows-latest]
72+
runs-on: ${{ matrix.os }}
73+
steps:
74+
- uses: schickling-actions/checkout-and-install@main
75+
- run: yarn build
76+
- run: yarn start
77+
working-directory: examples/node-script-remote-content

.vscode/operators.code-snippets

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"body": ["function* ($) {}"],
1010
"description": "Generator FUnction with $ input"
1111
},
12+
"Gen Yield * tmp": {
13+
"prefix": "yy",
14+
"body": ["yield* $($0)"],
15+
"description": "Yield generator calling $()"
16+
},
1217
"Gen Yield *": {
1318
"prefix": "!",
1419
"body": ["yield* $($0)"],

.vscode/tasks.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
// for the documentation about the tasks.json format
44
"version": "2.0.0",
55
"tasks": [
6+
{
7+
"label": "yarn-install",
8+
"type": "shell",
9+
"command": "yarn install",
10+
"presentation": {
11+
"focus": true,
12+
"panel": "shared",
13+
"group": "yarn",
14+
"showReuseMessage": true,
15+
"clear": false,
16+
"close": true
17+
}
18+
},
619
{
720
"label": "build:clean",
821
"type": "shell",
@@ -13,7 +26,7 @@
1326
"group": "dev",
1427
"showReuseMessage": true,
1528
"clear": false,
16-
"close": true,
29+
"close": true
1730
}
1831
},
1932
{
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { defineDocumentType, makeSource } from 'contentlayer/source-files'
2+
import { bundleMDX } from 'mdx-bundler'
3+
import * as ReactDOMServer from 'react-dom/server'
4+
import { getMDXComponent } from 'mdx-bundler/client/index.js'
5+
6+
const mdxToHtml = async (mdxSource: string) => {
7+
const { code } = await bundleMDX({ source: mdxSource })
8+
const MDXLayout = getMDXComponent(code)
9+
// TODO add your own components here
10+
const element = MDXLayout({ components: {} })!
11+
const html = ReactDOMServer.renderToString(element)
12+
return html
13+
}
14+
15+
const Post = defineDocumentType(() => ({
16+
name: 'Post',
17+
filePathPattern: `**/*.mdx`,
18+
contentType: 'mdx',
19+
fields: {
20+
title: {
21+
type: 'string',
22+
description: 'The title of the post',
23+
required: true,
24+
},
25+
date: {
26+
type: 'date',
27+
description: 'The date of the post',
28+
required: true,
29+
},
30+
},
31+
computedFields: {
32+
url: {
33+
type: 'string',
34+
resolve: (doc) => `/posts/${doc._raw.flattenedPath}`,
35+
},
36+
mdxHtml: {
37+
type: 'string',
38+
resolve: async (doc) => mdxToHtml(doc.body.raw),
39+
},
40+
},
41+
}))
42+
43+
export default makeSource({
44+
contentDirPath: 'posts',
45+
documentTypes: [Post],
46+
disableImportAliasWarning: true,
47+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { allPosts } from './.contentlayer/generated/index.mjs'
2+
3+
console.log(allPosts)

examples/node-script-mdx/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "node-script-mdx-example",
3+
"private": true,
4+
"scripts": {
5+
"start": "contentlayer build && node --experimental-json-modules my-script.mjs"
6+
},
7+
"dependencies": {
8+
"contentlayer": "latest"
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Change me!
3+
date: 2022-03-11
4+
---
5+
6+
When you change a source file, Contentlayer automatically updates the content cache, which prompts Next.js to reload the content on screen.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Click me!
3+
date: 2022-02-28
4+
---
5+
6+
Blog posts have their own pages. The content source is a markdown file, parsed to HTML by Contentlayer.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: What is Contentlayer?
3+
date: 2022-02-22
4+
---
5+
6+
**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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nextjs-repo
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { defineDocumentType } from 'contentlayer/source-files'
2+
import { spawn } from 'node:child_process'
3+
import { makeSource } from 'contentlayer/source-remote-files'
4+
5+
const Post = defineDocumentType(() => ({
6+
name: 'Post',
7+
filePathPattern: `docs/**/*.md`,
8+
fields: {
9+
title: {
10+
type: 'string',
11+
required: false,
12+
},
13+
description: {
14+
type: 'string',
15+
required: false,
16+
},
17+
},
18+
computedFields: {
19+
url: {
20+
type: 'string',
21+
resolve: (doc) => `/posts/${doc._raw.flattenedPath}`,
22+
},
23+
},
24+
}))
25+
26+
const syncContentFromGit = async (contentDir: string) => {
27+
const syncRun = async () => {
28+
const gitUrl = 'https://github.com/vercel/next.js.git'
29+
await runBashCommand(`
30+
if [ -d "${contentDir}" ];
31+
then
32+
cd "${contentDir}"; git pull;
33+
else
34+
git clone --depth 1 --single-branch ${gitUrl} ${contentDir};
35+
fi
36+
`)
37+
}
38+
39+
let wasCancelled = false
40+
let syncInterval
41+
42+
const syncLoop = async () => {
43+
console.log('Syncing content files from git')
44+
45+
await syncRun()
46+
47+
if (wasCancelled) return
48+
49+
syncInterval = setTimeout(syncLoop, 1000 * 60)
50+
}
51+
52+
// Block until the first sync is done
53+
await syncLoop()
54+
55+
return () => {
56+
wasCancelled = true
57+
clearTimeout(syncInterval)
58+
}
59+
}
60+
61+
const runBashCommand = (command: string) =>
62+
new Promise((resolve, reject) => {
63+
const child = spawn(command, [], { shell: true })
64+
65+
child.stdout.setEncoding('utf8')
66+
child.stdout.on('data', (data) => process.stdout.write(data))
67+
68+
child.stderr.setEncoding('utf8')
69+
child.stderr.on('data', (data) => process.stderr.write(data))
70+
71+
child.on('close', function (code) {
72+
if (code === 0) {
73+
resolve(void 0)
74+
} else {
75+
reject(new Error(`Command failed with exit code ${code}`))
76+
}
77+
})
78+
})
79+
80+
export default makeSource({
81+
syncFiles: syncContentFromGit,
82+
contentDirPath: 'nextjs-repo',
83+
contentDirInclude: ['docs'],
84+
documentTypes: [Post],
85+
disableImportAliasWarning: true,
86+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { allPosts } from './.contentlayer/generated/index.mjs'
2+
3+
const postUrls = allPosts.map(post => post.url)
4+
5+
console.log(`Found ${postUrls.length} posts:`);
6+
console.log(postUrls)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "node-script-remote-content-example",
3+
"private": true,
4+
"scripts": {
5+
"start": "contentlayer build && node --experimental-json-modules my-script.mjs"
6+
},
7+
"dependencies": {
8+
"contentlayer": "latest"
9+
}
10+
}

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@
2727
"devDependencies": {
2828
"@changesets/cli": "2.19.0-temp.0",
2929
"@effect-ts/tracing-plugin": "^0.20.0",
30-
"@types/prettier": "^2.7.1",
31-
"@typescript-eslint/eslint-plugin": "^5.42.0",
32-
"@typescript-eslint/parser": "^5.42.0",
33-
"eslint": "^8.27.0",
34-
"eslint-config-prettier": "^8.5.0",
35-
"eslint-plugin-import": "^2.26.0",
30+
"@types/prettier": "^2.7.2",
31+
"@typescript-eslint/eslint-plugin": "^5.48.1",
32+
"@typescript-eslint/parser": "^5.48.1",
33+
"eslint": "^8.32.0",
34+
"eslint-config-prettier": "^8.6.0",
35+
"eslint-plugin-import": "^2.27.4",
3636
"eslint-plugin-react-hooks": "^4.6.0",
3737
"eslint-plugin-simple-import-sort": "^8.0.0",
38-
"prettier": "^2.7.1",
39-
"ts-patch": "^2.0.2",
40-
"typescript": "^4.8.4"
38+
"prettier": "^2.8.3",
39+
"ts-patch": "^2.1.0",
40+
"typescript": "^4.9.4"
4141
},
4242
"resolutions": {
43-
"esbuild": "0.15.7",
43+
"esbuild": "0.17.0",
4444
"contentlayer": "workspace:*",
4545
"@contentlayer/*": "workspace:*",
4646
"contentlayer-stackbit-yaml-generator": "workspace:*",

packages/@contentlayer/cli/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/cli",
3-
"version": "0.2.9",
3+
"version": "0.3.0",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -15,10 +15,10 @@
1515
"dependencies": {
1616
"@contentlayer/core": "workspace:*",
1717
"@contentlayer/utils": "workspace:*",
18-
"clipanion": "^3.2.0-rc.13",
18+
"clipanion": "^3.2.0-rc.14",
1919
"typanion": "^3.12.1"
2020
},
2121
"devDependencies": {
22-
"@types/node": "^18.11.9"
22+
"@types/node": "^18.11.18"
2323
}
2424
}

packages/@contentlayer/cli/src/commands/DevCommand.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as core from '@contentlayer/core'
22
import { errorToString } from '@contentlayer/utils'
3-
import { E, pipe, S, T } from '@contentlayer/utils/effect'
3+
import { E, OT, pipe, S, T } from '@contentlayer/utils/effect'
44
import type { Usage } from 'clipanion'
55

66
import { BaseCommand } from './_BaseCommand.js'
@@ -31,6 +31,8 @@ export class DevCommand extends BaseCommand {
3131
core.generateDotpkgStream({ config, verbose: this.verbose, isDev: true }),
3232
),
3333
S.tap(E.fold((error) => T.log(errorToString(error)), core.logGenerateInfo)),
34+
OT.withStreamSpan('@contentlayer/cli/commands/DevCommand:stream'),
3435
S.runDrain,
36+
OT.withSpan('@contentlayer/cli/commands/DevCommand:executeSafe'),
3537
)
3638
}

packages/@contentlayer/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/client",
3-
"version": "0.2.9",
3+
"version": "0.3.0",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

packages/@contentlayer/core/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentlayer/core",
3-
"version": "0.2.9",
3+
"version": "0.3.0",
44
"type": "module",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",
@@ -13,7 +13,7 @@
1313
"test": "echo No tests yet"
1414
},
1515
"peerDependencies": {
16-
"esbuild": "^0.12.1 || 0.13.x || 0.14.x || 0.15.x",
16+
"esbuild": "0.17.x",
1717
"markdown-wasm": "1.x"
1818
},
1919
"peerDependenciesMeta": {
@@ -28,15 +28,15 @@
2828
"@contentlayer/utils": "workspace:*",
2929
"camel-case": "^4.1.2",
3030
"comment-json": "^4.2.3",
31-
"esbuild": "^0.12.1 || 0.13.x || 0.14.x || 0.15.x",
31+
"esbuild": "0.17.x",
3232
"gray-matter": "^4.0.3",
3333
"mdx-bundler": "^9.2.1",
3434
"rehype-stringify": "^9.0.3",
3535
"remark-frontmatter": "^4.0.1",
3636
"remark-parse": "^10.0.1",
3737
"remark-rehype": "^10.1.0",
3838
"source-map-support": "^0.5.21",
39-
"type-fest": "^3.2.0",
39+
"type-fest": "^3.5.2",
4040
"unified": "^10.1.2"
4141
},
4242
"devDependencies": {

0 commit comments

Comments
 (0)