sdorra / content-collections

Transform your content into type-safe data collections
https://content-collections.dev
MIT License
265 stars 8 forks source link
collections content content-layer json markdown mdx nextjs nodejs react remix svektekit vite yaml

Content Collections

logo: A staple of books

Transform your content into type-safe data collections. Eliminate the need for manual data fetching and parsing. Simply import your content and begin. Built-in validation ensures the accuracy of your data. Preprocess your data before it enters your app.

Features

Installation

Content Collections offers a variety of adapters that seamlessly integrate with popular web frameworks. The installation process depends on the chosen adapter:

If your framework is not listed, you can still use Content Collections by using the CLI. Please open a ticket if you want to see your framework listed.

Usage

  1. Create a content-collections.ts file at the root of your project:

    import { defineCollection, defineConfig } from "@content-collections/core";
    
    const posts = defineCollection({
     name: "posts",
     directory: "src/posts",
     include: "**/*.md",
     schema: (z) => ({
       title: z.string(),
       summary: z.string(),
     }),
    });
    
    export default defineConfig({
     collections: [posts],
    });
  2. Start writing content in src/posts:

    ---
    title: Hello World
    summary: This is my first post
    ---
    
    # Hello World
    
    This is my first post.
  3. Use your content in your app:

    import { allPosts } from "content-collections";
    
    export function Posts() {
     return (
       <ul>
         {allPosts.map((post) => (
           <li key={post._meta.path}>
             <a href={`/posts/${post._meta.path}`}>
               <h3>{post.title}</h3>
               <p>{post.summary}</p>
             </a>
           </li>
         ))}
       </ul>
     );
    }

    Please note that the example above shows only the very basics of Content Collections and it does not cover content transformation. Content Collections does not transform content like markdown or mdx by default. But it has packages which can do that for you:

    If you want to see more examples and use cases, please refer to the documentation.

Sponsors

supastarter

  

Cloudogu GmbH

Become a sponsor

License

Content Collections is licensed under the MIT License.