Quickstart CLI
Setup guide for the Content Collections CLI
Install required packages:
We have to install the following packages:
@content-collections/core
@content-collections/cli
concurrently
Adjust your tsconfig.json
:
We require a path alias for the generated files.
This is necessary because the CLI will generate the files in the .content-collections/generated
folder.
Add the content-collection cli to your package.json
scripts:
First, we modify the dev
script to simultaneously execute the content-collections watch
command along with our regular dev
command.
Next, we execute the content-collections build
command prior to our regular build
command.
Note: Make sure to replace build-scripts
with the appropriate command for your framework, such as next
or vite
.
Add the new .content-collections
directory to your project's .gitignore
file — the files in this directory are always regenerated and can be safely ignored by Git.
Create a content-collections.ts
file at the root of your project:
This file defines a collection named posts
in the src/posts
folder.
The collection will include all markdown files (**/*.md
) and the schema will validate the title
and summary
fields.
For more information about the configuration have a look at the documentation.
Create your content files (e.g.: src/posts/hello-world.md
):
You can create unlimited content files.
These files will be validated against the schema defined in the content-collections.ts
file.
If the files are valid, they will be automatically added to the posts
collection.
Usage in your code:
Now you can just import the allPosts
collection and use it in your code.
The allPosts
collection will contain all posts that are valid.
The post
object will contain the title
, summary
and content
fields as well as some meta information in the _meta
field.