fuwari icon indicating copy to clipboard operation
fuwari copied to clipboard

Adoption of Loader for Content Layer API

Open L4Ph opened this issue 1 year ago • 7 comments

Astro 5.0(and Astro 4.14(experimental)) introduced Content Layer API. One of these is added as Loader. What this does is that by executing glob(), you can receive an array of content from it and display it. The great part about this is that you can change the entry point, remote or local, and it doesn't matter where you are as long as you get the content.

This solves the problem that fuwari has of having to forcibly place content under src/content.

I would like to adopt this. What do you think? @saicaca

https://astro.build/blog/astro-4140/ https://astro.build/blog/astro-5/

L4Ph avatar Dec 03 '24 14:12 L4Ph

// src/content/config.ts to src/content.config.ts

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

const postsCollection = defineCollection({
  loader: glob({ pattern: '**/*.md', base: 'path/to/content' }),
  schema: z.object({
    title: z.string(),
    published: z.date(),
    draft: z.boolean().optional().default(false),
    description: z.string().optional().default(''),
    image: z.string().optional().default(''),
    tags: z.array(z.string()).optional().default([]),
    category: z.string().optional().default(''),
    lang: z.string().optional().default(''),

    /* For internal use */
    prevTitle: z.string().default(''),
    prevSlug: z.string().default(''),
    nextTitle: z.string().default(''),
    nextSlug: z.string().default(''),
  }),
})
export const collections = {
  posts: postsCollection,
}

L4Ph avatar Dec 03 '24 14:12 L4Ph

Yes, I think it's great to adopt the new API.

saicaca avatar Dec 05 '24 12:12 saicaca

They said they would adopt it, so I'm creating a PoC.

Thinking about where to place content.

L4Ph avatar Dec 06 '24 03:12 L4Ph

I think it's okay to keep them in /src/content since it matches existing practices. Users can easily modify the location if they want.

saicaca avatar Dec 06 '24 04:12 saicaca

I made a PoC. There is no problem until the article is displayed, but it seems to be causing a rather troublesome problem around images...

Personally, I feel it is more reliable to take this opportunity to place all content...

L4Ph avatar Dec 06 '24 08:12 L4Ph

https://github.com/L4Ph/fuwari/tree/content-layer-api

L4Ph avatar Dec 06 '24 08:12 L4Ph

I've tried a few things since then, but I can't think of a good way to do it, so I'd appreciate some advice. ImageWrapper is the only problem

@saicaca

L4Ph avatar Dec 16 '24 01:12 L4Ph