How to use Content Collections

The usage of Content Collections is very simple. Content Collections will generate an array of documents for each collection you define. Each document is a TypeScript object that represents a file in your project. The documents are generated from the files in the collection directory that match the glob pattern you provide.

To use those documents in your project, you can import the generated file and use the documents as you would with any other TypeScript object. We recommend using a TypeScript alias for the generated files to make the import paths more readable. The alias should point to the generated files in the .content-collections/generated folder. Have a look at the installation guide for more information on how to set up the alias for your framework.

After you have configured the alias, you can import the generated files in your project like this:

import { posts } from "content-collections";

The posts object contains an array of documents that you can use in your project. Each document has the shape defined in the collection configuration. You can access the properties of the document as you would with any other TypeScript object.

Here is an example of how you can use the posts object in your project:

import { posts } from "content-collections";

posts.forEach((post) => {
  console.log(post.title);
  console.log(post.summary);
});

Each document has a _meta property (unless it has been removed in the transform function) that contains metadata about the document, such as the file path and the file name.