Skip to content

Commit 9848407

Browse files
authored
Merge pull request #311 from openscript/astro5-contentlayer
Astro5 contentlayer
2 parents f8e8274 + f70560c commit 9848407

File tree

8 files changed

+28
-22
lines changed

8 files changed

+28
-22
lines changed

src/components/NoteListEntry.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { getContentEntryPath } from "../utils/i18n";
44
import type { GitInfoFrontmatter } from "../remark/remark-git-info";
55
import DateTime from "./DateTime.astro";
66
import { getEntry } from "astro:content";
7+
import { render } from "astro:content";
78
89
type Props = {
910
note: CollectionEntry<"notes">;
1011
};
1112
1213
const { note } = Astro.props;
13-
const { remarkPluginFrontmatter } = await note.render();
14+
const { remarkPluginFrontmatter } = await render(note);
1415
const gitInfo: GitInfoFrontmatter["git"] = remarkPluginFrontmatter.git;
1516
---
1617

@@ -23,7 +24,7 @@ const gitInfo: GitInfoFrontmatter["git"] = remarkPluginFrontmatter.git;
2324
}
2425
</style>
2526

26-
<a href={getContentEntryPath(await getEntry("notes", note.slug))}>
27+
<a href={getContentEntryPath(await getEntry("notes", note.id))}>
2728
<span class="note-title">{note.data.title}</span>
2829
{
2930
gitInfo.lastCommit && (

src/components/PostCard.astro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import { getContentEntryPath } from "../utils/i18n";
44
import { Image } from "astro:assets";
55
import DateTime from "./DateTime.astro";
66
import DerivedCover from "./DerivedCover.astro";
7-
import { getEntry } from "astro:content";
7+
import { getEntry, render } from "astro:content";
88
99
type Props = {
1010
post: CollectionEntry<"blog">;
1111
};
1212
1313
const { post } = Astro.props;
14-
const { remarkPluginFrontmatter } = await post.render();
14+
const { remarkPluginFrontmatter } = await render(post);
1515
---
1616

1717
<style>
@@ -36,7 +36,7 @@ const { remarkPluginFrontmatter } = await post.render();
3636
</style>
3737

3838
<div>
39-
<a href={getContentEntryPath(await getEntry("blog", post.slug))}>
39+
<a href={getContentEntryPath(await getEntry("blog", post.id))}>
4040
{
4141
post.data.cover ? (
4242
<Image

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineCollection, z } from "astro:content";
2-
import { localeSlugs, type Locale } from "../configuration";
2+
import { localeSlugs, type Locale } from "./configuration";
3+
import { glob } from "astro/loaders";
34

45
const localized = <T extends z.ZodTypeAny>(schema: T) =>
56
z.object(
@@ -13,7 +14,7 @@ const localized = <T extends z.ZodTypeAny>(schema: T) =>
1314
);
1415

1516
const blogCollection = defineCollection({
16-
type: "content",
17+
loader: glob({ pattern: "**/[^_]*.{md,mdx}", base: "./src/content/blog", generateId: ({entry}) => entry }),
1718
schema: ({ image }) =>
1819
z.object({
1920
title: z.string(),
@@ -29,13 +30,13 @@ const blogCollection = defineCollection({
2930
}),
3031
});
3132
const notesCollection = defineCollection({
32-
type: "content",
33+
loader: glob({ pattern: "**/[^_]*.{md,mdx}", base: "./src/content/notes", generateId: ({entry}) => entry }),
3334
schema: z.object({
3435
title: z.string(),
3536
}),
3637
});
3738
const galleryCollection = defineCollection({
38-
type: "data",
39+
loader: glob({ pattern: "**/[^_]*.yml", base: "./src/content/gallery"}),
3940
schema: ({ image }) =>
4041
z.object({
4142
title: localized(z.string()),
@@ -49,8 +50,8 @@ const galleryCollection = defineCollection({
4950
),
5051
}),
5152
});
52-
const projectCollection = defineCollection({
53-
type: "content",
53+
const projectsCollection = defineCollection({
54+
loader: glob({ pattern: "**/[^_]*.{md,mdx}", base: "./src/content/projects"}),
5455
schema: ({ image }) =>
5556
z.object({
5657
title: z.string(),
@@ -68,14 +69,14 @@ const projectCollection = defineCollection({
6869
}),
6970
});
7071
const pagesCollection = defineCollection({
71-
type: "content",
72+
loader: glob({ pattern: "**/[^_]*.{md,mdx}", base: "./src/content/pages"}),
7273
schema: z.object({
7374
path: z.string(),
7475
title: z.string(),
7576
}),
7677
});
7778
const navigationCollection = defineCollection({
78-
type: "data",
79+
loader: glob({ pattern: "**/[^_]*.yml", base: "./src/content/navigation"}),
7980
schema: ({ image }) =>
8081
localized(
8182
z.array(
@@ -88,15 +89,15 @@ const navigationCollection = defineCollection({
8889
),
8990
});
9091
const sectionsCollection = defineCollection({
91-
type: "content",
92+
loader: glob({ pattern: "**/[^_]*.{md,mdx}", base: "./src/content/sections"}),
9293
schema: z.object({}),
9394
})
9495

9596
export const collections = {
9697
blog: blogCollection,
9798
notes: notesCollection,
9899
gallery: galleryCollection,
99-
projects: projectCollection,
100+
projects: projectsCollection,
100101
pages: pagesCollection,
101102
navigation: navigationCollection,
102103
sections: sectionsCollection,

src/pages/[...locale]/[...pages].astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import type { InferGetStaticPropsType } from "astro";
33
import { entryPaths } from "../_paths";
44
import DefaultLayout from "../../layouts/DefaultLayout.astro";
55
import Meta from "../../layouts/groups/Meta.astro";
6+
import { render } from "astro:content";
67
78
export const getStaticPaths = entryPaths("pages");
89
910
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
1011
const page = Astro.props as Props;
1112
12-
const { Content } = await page.render();
13+
const { Content } = await render(page);
1314
---
1415

1516
<DefaultLayout translations={page.translations}>

src/pages/[...locale]/[blog]/[...slug].astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import slug from "limax";
1212
import Comments from "../../../components/Comments.astro";
1313
import Meta from "../../../layouts/groups/Meta.astro";
1414
import { getCollectionSlug, getLocaleSlug } from "../../../utils/slugs";
15+
import { render } from "astro:content";
1516
1617
export const getStaticPaths = entryPaths("blog", "slug");
1718
1819
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
1920
20-
const { translations, render, data } = Astro.props as Props;
21-
const { Content, headings, remarkPluginFrontmatter } = await render();
21+
const { translations, data } = Astro.props as Props;
22+
const { Content, headings, remarkPluginFrontmatter } = await render(Astro.props);
2223
const git: GitInfoFrontmatter["git"] = remarkPluginFrontmatter.git;
2324
const locale = getLocaleFromUrl(Astro.url);
2425
const t = useTranslations(locale);

src/pages/[...locale]/[notes]/[...note].astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import DefaultLayout from "../../../layouts/DefaultLayout.astro";
44
import { entryPaths } from "../../_paths";
55
import PageWithAside from "../../../layouts/pages/PageWithAside.astro";
66
import Meta from "../../../layouts/groups/Meta.astro";
7+
import { render } from "astro:content";
78
89
export const getStaticPaths = entryPaths("notes", "note");
910
1011
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
1112
const page = Astro.props as Props;
1213
13-
const { Content, headings } = await page.render();
14+
const { Content, headings } = await render(page);
1415
---
1516

1617
<DefaultLayout translations={page.translations}>

src/pages/[...locale]/index.astro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import DefaultLayout from "../../layouts/DefaultLayout.astro";
44
import { indexPaths } from "../_paths";
55
import { getEntry } from "astro:content";
66
import LatestBlogsSection from "../../layouts/sections/LatestBlogsSection.astro";
7+
import { render } from "astro:content";
78
89
export const getStaticPaths = indexPaths();
910
1011
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
1112
1213
const { translations, locale } = Astro.props as Props;
13-
const { Content: IntroSection } = await (
14+
const { Content: IntroSection } = await render(
1415
await getEntry("sections", `${locale}/intro`)!
15-
).render();
16+
);
1617
---
1718

1819
<style>

src/pages/rss[...locale].xml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const GET: APIRoute<Props, Params> = async (context) => {
3434
blogs.map(async (blog) => ({
3535
title: blog.data.title,
3636
pubDate: blog.data.publishedAt,
37-
link: getContentEntryPath(await getEntry("blog", blog.slug)),
37+
link: getContentEntryPath(await getEntry("blog", blog.id)),
3838
})),
3939
),
4040
customData: `<language>${C.LOCALES[locale]}</language>`,

0 commit comments

Comments
 (0)