Update template to Astro 5.0, use Content Collections

This commit is contained in:
Cassidy Williams
2024-12-14 09:46:58 -06:00
parent 90a26b0085
commit 444cbe1502
16 changed files with 11018 additions and 9215 deletions

View File

@@ -46,3 +46,7 @@ TINACLIENTID=<from tina.io>
TINATOKEN=<from tina.io> TINATOKEN=<from tina.io>
TINASEARCH=<from tina.io> TINASEARCH=<from tina.io>
``` ```
If you get a remote GraphQL schema error, chances are you need to update TinaCMS, [details here](https://tina.io/docs/introduction/faq#how-do-i-resolve-the-local-graphql-schema-doesnt-match-the-remote-graphql-schema-errors)!
## Have fun!

20136
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,14 +10,14 @@
"astro": "astro" "astro": "astro"
}, },
"devDependencies": { "devDependencies": {
"@astrojs/rss": "^4.0.1", "@astrojs/rss": "^4.0.10",
"@astrojs/sitemap": "^3.0.5", "@astrojs/sitemap": "^3.2.1",
"astro": "^4.1.1", "astro": "^5.0.5",
"prettier": "^3.1.0", "prettier": "^3.3.3",
"prettier-plugin-astro": "^0.12.2" "prettier-plugin-astro": "^0.14.1"
}, },
"dependencies": { "dependencies": {
"@tinacms/cli": "^1.5.43", "@tinacms/cli": "^1.7.0",
"tinacms": "^1.6.1" "tinacms": "^2.5.2"
} }
} }

View File

@@ -8,8 +8,8 @@ const { allPosts } = Astro.props;
let posts = allPosts let posts = allPosts
.sort( .sort(
(a, b) => (a, b) =>
new Date(b.frontmatter.updated || b.frontmatter.added).valueOf() - new Date(b.data.updated || b.data.added).valueOf() -
new Date(a.frontmatter.updated || a.frontmatter.added).valueOf() new Date(a.data.updated || a.data.added).valueOf(),
) )
.slice(0, 5); .slice(0, 5);
--- ---
@@ -20,7 +20,7 @@ let posts = allPosts
posts.map( posts.map(
({ ({
url, url,
frontmatter: { description, slug, title, tags, added: date }, data: { description, slug, title, tags, added: date },
}) => <Post {url} {description} {date} {slug} {title} {tags} /> }) => <Post {url} {description} {date} {slug} {title} {tags} />
) )
} }

17
src/content.config.js Normal file
View File

@@ -0,0 +1,17 @@
import { defineCollection, z } from "astro:content";
import { glob } from "astro/loaders";
const posts = defineCollection({
loader: glob({ pattern: "*.md", base: "./src/posts" }),
schema: z.object({
title: z.string(),
slug: z.string(),
description: z.string(),
added: z.union([z.string(), z.date()]),
updated: z.union([z.string(), z.date()]).optional(),
tags: z.array(z.string()),
}),
});
export const collections = { posts };

View File

@@ -1,4 +1,5 @@
--- ---
import { getCollection } from "astro:content";
import BaseHead from "../components/BaseHead.astro"; import BaseHead from "../components/BaseHead.astro";
import Header from "../components/Header.astro"; import Header from "../components/Header.astro";
import Footer from "../components/Footer.astro"; import Footer from "../components/Footer.astro";
@@ -6,7 +7,7 @@ import ColorScript from "../components/ColorScript.astro";
import Tags from "../components/Tags.astro"; import Tags from "../components/Tags.astro";
import getTags from "../scripts/getTags"; import getTags from "../scripts/getTags";
const posts = await Astro.glob("../posts/*.md"); const posts = await getCollection("posts");
const allTags = getTags(posts); const allTags = getTags(posts);
let { let {

View File

@@ -1,4 +1,5 @@
--- ---
import { getCollection } from "astro:content";
import BaseHead from "../components/BaseHead.astro"; import BaseHead from "../components/BaseHead.astro";
import Header from "../components/Header.astro"; import Header from "../components/Header.astro";
import Footer from "../components/Footer.astro"; import Footer from "../components/Footer.astro";
@@ -9,8 +10,7 @@ import { SITE_TITLE, SITE_DESCRIPTION } from "../config";
import { Content as About } from "./about.md"; import { Content as About } from "./about.md";
import getTags from "../scripts/getTags"; import getTags from "../scripts/getTags";
const posts = await Astro.glob("../posts/*.md"); const posts = await getCollection("posts");
const tags = getTags(posts); const tags = getTags(posts);
--- ---

View File

@@ -1,19 +1,22 @@
--- ---
// Credit to @rachsmithcodes for this function import { getCollection, render } from "astro:content";
import BlogPost from "../../layouts/BlogPost.astro";
export async function getStaticPaths() { export async function getStaticPaths() {
let posts = await Astro.glob(`../../posts/*.md`); let posts = await getCollection("posts");
return posts.map((post) => { return posts.map((post) => {
return { return {
params: { slug: post.frontmatter.slug }, params: { slug: post.data.slug },
props: { post: post }, props: { post },
}; };
}); });
} }
const { post } = Astro.props; const { post } = Astro.props;
const { Content } = post; const { Content } = await render(post);
--- ---
<Content /> <BlogPost content={post.data}>
<Content />
</BlogPost>

View File

@@ -1,4 +1,5 @@
--- ---
import { getCollection } from "astro:content";
import BaseHead from "../components/BaseHead.astro"; import BaseHead from "../components/BaseHead.astro";
import Header from "../components/Header.astro"; import Header from "../components/Header.astro";
import Footer from "../components/Footer.astro"; import Footer from "../components/Footer.astro";
@@ -7,11 +8,12 @@ import ColorScript from "../components/ColorScript.astro";
import { SITE_TITLE, SITE_DESCRIPTION } from "../config"; import { SITE_TITLE, SITE_DESCRIPTION } from "../config";
// Use Astro.glob() to fetch all posts, and then sort them by date. let posts = await getCollection("posts");
const posts = (await Astro.glob("../posts/*.md")).sort(
posts = posts.sort(
(a, b) => (a, b) =>
new Date(b.frontmatter.updated || b.frontmatter.added).valueOf() - new Date(b.data.updated || b.data.added).valueOf() -
new Date(a.frontmatter.updated || a.frontmatter.added).valueOf() new Date(a.data.updated || a.data.added).valueOf(),
); );
--- ---
@@ -27,7 +29,7 @@ const posts = (await Astro.glob("../posts/*.md")).sort(
posts.map( posts.map(
({ ({
url, url,
frontmatter: { description, slug, title, tags, added: date }, data: { description, slug, title, tags, added: date },
}) => <Post {description} {date} {slug} {title} {tags} /> }) => <Post {description} {date} {slug} {title} {tags} />
) )
} }

View File

@@ -1,29 +1,31 @@
import rss from "@astrojs/rss"; import rss from "@astrojs/rss";
import { getCollection } from "astro:content";
import { SITE_TITLE, SITE_DESCRIPTION } from "../config"; import { SITE_TITLE, SITE_DESCRIPTION } from "../config";
let posts = Object.values(import.meta.glob("../posts/*.md", { eager: true })); let posts = await getCollection("posts");
posts = posts.sort( posts = posts.sort(
(a, b) => (a, b) =>
new Date(b.frontmatter.updated || b.frontmatter.added).valueOf() - new Date(b.data.updated || b.data.added).valueOf() -
new Date(a.frontmatter.updated || a.frontmatter.added).valueOf() new Date(a.data.updated || a.data.added).valueOf()
); );
export const GET = () => export const GET = () =>
rss({ rss({
title: SITE_TITLE, title: SITE_TITLE || "",
description: SITE_DESCRIPTION, description: SITE_DESCRIPTION || "",
site: import.meta.env.SITE, site: import.meta.env.SITE,
items: posts.map((post) => { items: posts.map((post) => {
return { return {
link: `/post/${post.frontmatter.slug}`, link: `/post/${post.data.slug}`,
title: post.frontmatter.title, title: post.data.title,
pubDate: post.frontmatter.added, pubDate: post.data.added,
description: post.frontmatter.description, description: post.data.description,
content: post.compiledContent(), content: post.body,
customData: `<updated>${ customData: `<updated>${
post.frontmatter.updated ? post.frontmatter.updated : "" post.data.updated ? post.data.updated : ""
}</updated>`, }</updated>`,
}; };
}), }),
stylesheet: "/rss-styles.xsl",
}); });

View File

@@ -1,4 +1,5 @@
--- ---
import { getCollection } from "astro:content";
import BaseHead from "../../components/BaseHead.astro"; import BaseHead from "../../components/BaseHead.astro";
import Header from "../../components/Header.astro"; import Header from "../../components/Header.astro";
import Footer from "../../components/Footer.astro"; import Footer from "../../components/Footer.astro";
@@ -7,11 +8,10 @@ import ColorScript from "../../components/ColorScript.astro";
import { SITE_TITLE, SITE_DESCRIPTION } from "../../config"; import { SITE_TITLE, SITE_DESCRIPTION } from "../../config";
// Credit to @rachsmithcodes for helping out my lil ol brain
export async function getStaticPaths() { export async function getStaticPaths() {
let posts = await Astro.glob(`../../posts/*.md`); let posts = await getCollection("posts");
const tags = posts.reduce((allTags, post) => { const tags = posts.reduce((allTags, post) => {
const postTags = post.frontmatter.tags; const postTags = post.data.tags;
if (postTags) { if (postTags) {
postTags.forEach((tag) => { postTags.forEach((tag) => {
if (!allTags[tag]) { if (!allTags[tag]) {
@@ -35,9 +35,7 @@ const { tag } = Astro.props;
const { tag: currentTag } = Astro.params; const { tag: currentTag } = Astro.params;
let sortedPosts = tag.sort( let sortedPosts = tag.sort(
(a, b) => (a, b) => new Date(b.data.added).valueOf() - new Date(a.data.added).valueOf(),
new Date(b.frontmatter.added).valueOf() -
new Date(a.frontmatter.added).valueOf()
); );
--- ---
@@ -54,7 +52,7 @@ let sortedPosts = tag.sort(
sortedPosts.map( sortedPosts.map(
({ ({
url, url,
frontmatter: { description, slug, title, tags, added: date }, data: { description, slug, title, tags, added: date },
}) => <Post {url} {description} {date} {slug} {title} {tags} /> }) => <Post {url} {description} {date} {slug} {title} {tags} />
) )
} }

View File

@@ -1,5 +1,4 @@
--- ---
layout: ../layouts/BlogPost.astro
title: Hello, world! title: Hello, world!
slug: hello-world slug: hello-world
description: >- description: >-

View File

@@ -1,5 +1,4 @@
--- ---
layout: ../layouts/BlogPost.astro
title: Markdown Example title: Markdown Example
slug: markdown slug: markdown
description: >- description: >-

View File

@@ -2,7 +2,7 @@ export default function getTags(posts) {
// Get tags from all posts // Get tags from all posts
const allTags = posts const allTags = posts
.map((post) => { .map((post) => {
const postTags = post.frontmatter.tags; const postTags = post.data.tags;
let allTags = []; let allTags = [];
if (postTags?.length > 0) { if (postTags?.length > 0) {

View File

@@ -26,7 +26,6 @@ export default defineConfig({
path: "src/posts", path: "src/posts",
defaultItem: () => ({ defaultItem: () => ({
title: "New Post", title: "New Post",
layout: "../layouts/BlogPost.astro",
added: new Date(), added: new Date(),
tags: [], tags: [],
}), }),
@@ -40,13 +39,6 @@ export default defineConfig({
}, },
}, },
fields: [ fields: [
{
name: "layout",
label: "Layout",
type: "string",
required: true,
searchable: false,
},
{ {
name: "title", name: "title",
label: "Title", label: "Title",

File diff suppressed because one or more lines are too long