Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adoption of Loader for Content Layer API #240

Open
Tracked by #232
L4Ph opened this issue Dec 3, 2024 · 7 comments
Open
Tracked by #232

Adoption of Loader for Content Layer API #240

L4Ph opened this issue Dec 3, 2024 · 7 comments

Comments

@L4Ph
Copy link
Contributor

L4Ph commented Dec 3, 2024

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
Copy link
Contributor Author

L4Ph commented Dec 3, 2024

// 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 L4Ph mentioned this issue Dec 4, 2024
2 tasks
@saicaca
Copy link
Owner

saicaca commented Dec 5, 2024

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

@L4Ph
Copy link
Contributor Author

L4Ph commented Dec 6, 2024

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

Thinking about where to place content.

@saicaca
Copy link
Owner

saicaca commented Dec 6, 2024

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.

@L4Ph
Copy link
Contributor Author

L4Ph commented Dec 6, 2024

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
Copy link
Contributor Author

L4Ph commented Dec 6, 2024

@L4Ph
Copy link
Contributor Author

L4Ph commented Dec 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants