#Content Collection issue

4 messages · Page 1 of 1 (latest)

tiny sonnet
#

Hey Kapa is there anything wrong with this content.config file?
import { defineCollection, z } from 'astro:content';
import { glob, file } from 'astro/loaders';

const welcome = defineCollection({
loader: file("src/content/welcome/welcome.md"),
schema: z.object({
mainHeading: z.string(),
subHeading: z.string(),
}),
});
const about = defineCollection({
loader: file("src/content/about/about.md"),
schema: z.object({
title: z.string(),
}),
});

const tabBlock = defineCollection({
loader: glob({pattern: "**/*.md", base: "src/content/tabBlock"}),
schema: z.object({
tabLabel: z.string(),
tabHeading: z.string(),
}),
});

export const collections = {welcome, about, tabBlock};

ornate tokenBOT
#

Hi @tiny sonnet. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Your content.config.ts file is mostly correct, but there are a couple of important things to check and clarify based on the official Astro documentation:

  1. Use of the file() loader with a Markdown file:
    The file() loader is designed to load data from a single file that contains an array of objects (with unique id fields) or an object with IDs as keys and entries as values. It is typically used for JSON or YAML files, not for individual Markdown files. If you use file() with a Markdown file, it may not work as expected, since Markdown files are usually handled as individual entries with the glob() loader. The documentation examples for file() use JSON files, not Markdown files Astro Content Loader API.

  2. Correct loader for single Markdown files:
    If you have a single Markdown file and want to treat it as a collection entry, you should use the glob() loader with a pattern that matches just that file, or consider if you need a collection at all for a single file. Collections are most useful for groups of related content When to create a collection.

  3. Glob patterns in the file() loader are not supported:
    You are not using a glob pattern in your file() loader, so this is not an issue in your code, but it's worth noting that the file() loader does not support glob patterns Glob patterns are not supported in the file loader.

#

Summary of issues:

  • Using file() with a Markdown file is not the intended use and may not work. Use glob() for Markdown files, even if there is only one.
  • If you only have a single Markdown file, you may not need a collection at all.

How to fix:
If you want to keep these as collections, use glob() for your Markdown files:

import { defineCollection, z } from 'astro:content';
import { glob } from 'astro/loaders';

const welcome = defineCollection({
  loader: glob({ pattern: "welcome.md", base: "src/content/welcome" }),
  schema: z.object({
    mainHeading: z.string(),
    subHeading: z.string(),
  }),
});

const about = defineCollection({
  loader: glob({ pattern: "about.md", base: "src/content/about" }),
  schema: z.object({
    title: z.string(),
  }),
});

const tabBlock = defineCollection({
  loader: glob({ pattern: "**/*.md", base: "src/content/tabBlock" }),
  schema: z.object({
    tabLabel: z.string(),
    tabHeading: z.string(),
  }),
});

export const collections = { welcome, about, tabBlock };

This approach aligns with the official documentation and should work as expected Defining Collections.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: