Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

0.3.0 #356

0.3.0 #356
Jan 18, 2023 · 1 comment
Discussion options

i️ [TLDR] New experimental source and required peer dependency update.

⚠️ Breaking Change: Updated esbuild Dependency

0.3.0 requires use of esbuild 0.17.0. You may need to update peer dependencies if experiencing installation issues.

✨ New Source: Remote Files [experimental]

While still focused on content coming from files, you can begin to explore loading content from files not located in your repository.

This works by syncing content from a remote location into your local workspace, and then behaves similarly to the files source. Contentlayer provides the hook (via a syncFiles property) for syncing the files, but you must write the code that pulls the files in.

Here is simple example with a remote Git repo and documentation.

import { makeSource } from 'contentlayer/source-remote-files'
export default makeSource({
 syncFiles: () => syncContentFromGit(),
 contentDirPath: 'remote-content',
 documentTypes: [Post],
 disableImportAliasWarning: true,
})
const syncContentFromGit = async () => {
 const syncRun = async () => {
 const repoAlreadyCloned = false
 if (repoAlreadyCloned) {
 // TODO `git clone` the repo
 } else {
 // TODO `git pull` the repo
 }
 }
 let wasCancelled = false
 let syncInterval
 const syncLoop = async () => {
 await syncRun()
 if (wasCancelled) return
 syncInterval = setTimeout(syncLoop, 1000 * 60)
 }
 syncLoop()
 return () => {
 wasCancelled = true
 clearTimeout(syncInterval)
 }
}

✨ New helper functions: defineComputedFields & defineFields

You can now use a defineComputedFields function to leverage the document type, including its static fields. Here's an example:

import { defineDocumentType, defineComputedFields } from 'contentlayer/source-files'
const computedFields = defineComputedFields<'Post'>({
 upperTitle: {
 type: 'string',
 resolve: (doc) => doc.title.toUpperCase(),
 },
})
const Post = defineDocumentType(() => ({
 name: 'Post',
 filePathPattern: `**/*.md`,
 fields: {
 // ...
 },
 computedFields,
}))

Other Improvements


This discussion was created from the release 0.3.0.
You must be logged in to vote

Replies: 1 comment

Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /