0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Astro v5】defineCollectionでmdx記事が取得できなくなる問題の解決

0
Posted at

はじめに

astroでブログサイトを作成している最中に、defineCollection内のloaderプロパティでmdファイルとmdxファイルの指定を行っているのに、mdxファイルだけ取得できない問題が発生し、解決できたため、備忘録を残す。

問題

Content Collection APIdefineCollectionを用いて、次のようなブログ記事を取得するコードを書いた。

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

const blogCollection = defineCollection({
  loader: glob({ pattern: "**/*.{md, mdx}", base: "./src/content/blog" }),
  schema: z.object({
    title: z.string(),
    description: z.string(),
    publishedDate: z.coerce.date(),
    updatedDate: z.coerce.date().optional(),
    headingImage: z.string().optional(),
    tags: z.array(z.string()).optional(),
    relatedPosts: z.array(reference("blog")).optional(),
  }),
});

export const collections = {
  blog: blogCollection,
};

loaderではAstro Content Loader APIを使用してsrc/content/blogディレクトリ内にあるmdファイルとmdxファイルを全て取得するようになっている。
この状態でビルドを行い、ブログ記事一覧ページを開くと、mdxファイルの記事だけ表示されなかったことから、読み込まれていないことが判明した。

原因

原因は、loaderプロパティのglob関数のpatternプロパティにて、mdファイルとmdxファイルを取得する**/*.{md, mdx}というところで、カンマの次にスペースが入っていたことから、拡張子にスペースが入っているもの(**/*. mdx)と解釈され、正しくmdxファイルが取得されていなかった。

解決

スペースも文字列と解釈されるため、間を詰めて書く。

  loader: glob({ pattern: "**/*.{md,mdx}", base: "./src/content/blog" }),
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?