From 7d72aeeae8c867af2ae6c018611e39c184f50619 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Wed, 18 Jan 2023 08:43:58 -0500 Subject: [PATCH] Migrate blog and docs starters to content collections (#5858) * docs: update README for `content/` * chore: update env * feat: update blog to use content collections * chore: remove with-content starter * fix: single quotes -> double * feat: update docs starter to content collections * refactor: config -> consts * chore: import type * edit: Astro will -> Use Co-authored-by: Sarah Rainsberger * chore: remove unused null check * nit: spacing * nit: `as Props` 1 Co-authored-by: Nate Moore * nit: `as Props` 2 Co-authored-by: Nate Moore * chore: consistent CONSTS usage * chore: `type` imports at top * chore: consistent quote usage on YAML * chore: remove `as Props` from docs Co-authored-by: Sarah Rainsberger Co-authored-by: Nate Moore --- examples/blog/README.md | 3 + examples/blog/src/components/Header.astro | 2 +- examples/blog/src/config.ts | 5 - examples/{with-content => blog}/src/consts.ts | 0 .../src/{pages => content}/blog/first-post.md | 3 +- .../src/content/blog/markdown-style-guide.md | 8 +- .../{pages => content}/blog/second-post.md | 3 +- .../src/{pages => content}/blog/third-post.md | 3 +- .../src/content/blog/using-mdx.mdx | 0 .../src/content/config.ts | 5 +- examples/blog/src/env.d.ts | 1 + examples/blog/src/layouts/BlogPost.astro | 15 +-- examples/blog/src/pages/about.md | 2 +- examples/blog/src/pages/blog.astro | 59 ---------- .../src/pages/blog/[...slug].astro | 0 .../src/pages/blog/index.astro | 2 +- .../src/pages/blog/markdown-style-guide.md | 103 ------------------ examples/blog/src/pages/blog/using-mdx.mdx | 32 ------ examples/blog/src/pages/index.astro | 2 +- examples/blog/src/pages/rss.xml.js | 16 ++- .../src/components/Footer/AvatarList.astro | 2 +- .../docs/src/components/Footer/Footer.astro | 2 +- examples/docs/src/components/HeadSEO.astro | 26 ++--- .../src/components/Header/AstroLogo.astro | 2 +- .../docs/src/components/Header/Header.astro | 6 +- .../docs/src/components/Header/Search.tsx | 2 +- .../components/LeftSidebar/LeftSidebar.astro | 4 +- .../components/PageContent/PageContent.astro | 8 +- .../components/RightSidebar/MoreMenu.astro | 10 +- .../RightSidebar/RightSidebar.astro | 4 +- .../RightSidebar/TableOfContents.tsx | 2 +- examples/docs/src/{config.ts => consts.ts} | 15 +-- examples/docs/src/content/config.ts | 20 ++++ .../docs}/en/introduction.md | 5 +- .../src/{pages => content/docs}/en/page-2.md | 5 +- .../src/{pages => content/docs}/en/page-3.md | 5 +- .../src/{pages => content/docs}/en/page-4.md | 5 +- examples/docs/src/env.d.ts | 1 + examples/docs/src/languages.ts | 2 +- examples/docs/src/layouts/MainLayout.astro | 20 ++-- examples/docs/src/pages/[...slug].astro | 22 ++++ examples/docs/tsconfig.json | 3 +- examples/with-content/.gitignore | 21 ---- examples/with-content/.vscode/extensions.json | 4 - examples/with-content/.vscode/launch.json | 11 -- examples/with-content/README.md | 52 --------- examples/with-content/astro.config.mjs | 10 -- examples/with-content/package.json | 18 --- examples/with-content/public/favicon.svg | 13 --- .../with-content/public/placeholder-about.jpg | Bin 72964 -> 0 bytes .../with-content/public/placeholder-hero.jpg | Bin 8674 -> 0 bytes .../public/placeholder-social.jpg | Bin 18576 -> 0 bytes examples/with-content/sandbox.config.json | 11 -- .../src/components/BaseHead.astro | 38 ------- .../with-content/src/components/Footer.astro | 13 --- .../with-content/src/components/Header.astro | 25 ----- .../src/components/HeaderLink.astro | 22 ---- .../src/content/blog/first-post.md | 16 --- .../src/content/blog/second-post.md | 16 --- .../src/content/blog/third-post.md | 16 --- examples/with-content/src/env.d.ts | 2 - .../with-content/src/layouts/BlogPost.astro | 47 -------- examples/with-content/src/pages/about.md | 17 --- examples/with-content/src/pages/index.astro | 50 --------- examples/with-content/src/styles/global.css | 67 ------------ examples/with-content/tsconfig.json | 3 - 66 files changed, 129 insertions(+), 778 deletions(-) delete mode 100644 examples/blog/src/config.ts rename examples/{with-content => blog}/src/consts.ts (100%) rename examples/blog/src/{pages => content}/blog/first-post.md (98%) rename examples/{with-content => blog}/src/content/blog/markdown-style-guide.md (93%) rename examples/blog/src/{pages => content}/blog/second-post.md (98%) rename examples/blog/src/{pages => content}/blog/third-post.md (98%) rename examples/{with-content => blog}/src/content/blog/using-mdx.mdx (100%) rename examples/{with-content => blog}/src/content/config.ts (83%) delete mode 100644 examples/blog/src/pages/blog.astro rename examples/{with-content => blog}/src/pages/blog/[...slug].astro (100%) rename examples/{with-content => blog}/src/pages/blog/index.astro (94%) delete mode 100644 examples/blog/src/pages/blog/markdown-style-guide.md delete mode 100644 examples/blog/src/pages/blog/using-mdx.mdx rename examples/docs/src/{config.ts => consts.ts} (81%) create mode 100644 examples/docs/src/content/config.ts rename examples/docs/src/{pages => content/docs}/en/introduction.md (92%) rename examples/docs/src/{pages => content/docs}/en/page-2.md (96%) rename examples/docs/src/{pages => content/docs}/en/page-3.md (96%) rename examples/docs/src/{pages => content/docs}/en/page-4.md (96%) create mode 100644 examples/docs/src/pages/[...slug].astro delete mode 100644 examples/with-content/.gitignore delete mode 100644 examples/with-content/.vscode/extensions.json delete mode 100644 examples/with-content/.vscode/launch.json delete mode 100644 examples/with-content/README.md delete mode 100644 examples/with-content/astro.config.mjs delete mode 100644 examples/with-content/package.json delete mode 100644 examples/with-content/public/favicon.svg delete mode 100644 examples/with-content/public/placeholder-about.jpg delete mode 100644 examples/with-content/public/placeholder-hero.jpg delete mode 100644 examples/with-content/public/placeholder-social.jpg delete mode 100644 examples/with-content/sandbox.config.json delete mode 100644 examples/with-content/src/components/BaseHead.astro delete mode 100644 examples/with-content/src/components/Footer.astro delete mode 100644 examples/with-content/src/components/Header.astro delete mode 100644 examples/with-content/src/components/HeaderLink.astro delete mode 100644 examples/with-content/src/content/blog/first-post.md delete mode 100644 examples/with-content/src/content/blog/second-post.md delete mode 100644 examples/with-content/src/content/blog/third-post.md delete mode 100644 examples/with-content/src/env.d.ts delete mode 100644 examples/with-content/src/layouts/BlogPost.astro delete mode 100644 examples/with-content/src/pages/about.md delete mode 100644 examples/with-content/src/pages/index.astro delete mode 100644 examples/with-content/src/styles/global.css delete mode 100644 examples/with-content/tsconfig.json diff --git a/examples/blog/README.md b/examples/blog/README.md index 3fa19109c..c788fa653 100644 --- a/examples/blog/README.md +++ b/examples/blog/README.md @@ -29,6 +29,7 @@ Inside of your Astro project, you'll see the following folders and files: ├── public/ ├── src/ │   ├── components/ +│   ├── content/ │   ├── layouts/ │   └── pages/ ├── astro.config.mjs @@ -41,6 +42,8 @@ Astro looks for `.astro` or `.md` files in the `src/pages/` directory. Each page There's nothing special about `src/components/`, but that's where we like to put any Astro/React/Vue/Svelte/Preact components. +The `src/content/` directory contains "collections" of related Markdown and MDX documents. Use `getCollection()` to retrieve posts from `src/content/blog/`, and type-check your frontmatter using an optional schema. See [Astro's Content Collections docs](https://docs.astro.build/en/guides/content-collections/) to learn more. + Any static assets, like images, can be placed in the `public/` directory. ## 🧞 Commands diff --git a/examples/blog/src/components/Header.astro b/examples/blog/src/components/Header.astro index 75577e6bc..4d3da1fcf 100644 --- a/examples/blog/src/components/Header.astro +++ b/examples/blog/src/components/Header.astro @@ -1,6 +1,6 @@ --- import HeaderLink from './HeaderLink.astro'; -import { SITE_TITLE } from '../config'; +import { SITE_TITLE } from '../consts'; ---
diff --git a/examples/blog/src/config.ts b/examples/blog/src/config.ts deleted file mode 100644 index 1d5dbf632..000000000 --- a/examples/blog/src/config.ts +++ /dev/null @@ -1,5 +0,0 @@ -// Place any global data in this file. -// You can import this data from anywhere in your site by using the `import` keyword. - -export const SITE_TITLE = 'My personal website.'; -export const SITE_DESCRIPTION = 'Welcome to my website!'; diff --git a/examples/with-content/src/consts.ts b/examples/blog/src/consts.ts similarity index 100% rename from examples/with-content/src/consts.ts rename to examples/blog/src/consts.ts diff --git a/examples/blog/src/pages/blog/first-post.md b/examples/blog/src/content/blog/first-post.md similarity index 98% rename from examples/blog/src/pages/blog/first-post.md rename to examples/blog/src/content/blog/first-post.md index f98986a08..33b844032 100644 --- a/examples/blog/src/pages/blog/first-post.md +++ b/examples/blog/src/content/blog/first-post.md @@ -1,5 +1,4 @@ --- -layout: "../../layouts/BlogPost.astro" title: "First post" description: "Lorem ipsum dolor sit amet" pubDate: "Jul 08 2022" @@ -14,4 +13,4 @@ Mollis nunc sed id semper risus in. Convallis a cras semper auctor neque. Diam s Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Tortor posuere ac ut consequat semper viverra. Tellus mauris a diam maecenas sed enim ut sem viverra. Venenatis urna cursus eget nunc scelerisque viverra mauris in. Arcu ac tortor dignissim convallis aenean et tortor at. Curabitur gravida arcu ac tortor dignissim convallis aenean et tortor. Egestas tellus rutrum tellus pellentesque eu. Fusce ut placerat orci nulla pellentesque dignissim enim sit amet. Ut enim blandit volutpat maecenas volutpat blandit aliquam etiam. Id donec ultrices tincidunt arcu. Id cursus metus aliquam eleifend mi. -Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. \ No newline at end of file +Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. diff --git a/examples/with-content/src/content/blog/markdown-style-guide.md b/examples/blog/src/content/blog/markdown-style-guide.md similarity index 93% rename from examples/with-content/src/content/blog/markdown-style-guide.md rename to examples/blog/src/content/blog/markdown-style-guide.md index bb7bb2daa..242e86278 100644 --- a/examples/with-content/src/content/blog/markdown-style-guide.md +++ b/examples/blog/src/content/blog/markdown-style-guide.md @@ -1,8 +1,8 @@ --- -title: 'Markdown Style Guide' -description: 'Here is a sample of some basic Markdown syntax that can be used when writing Markdown content in Astro.' -pubDate: 'Jul 01 2022' -heroImage: '/placeholder-hero.jpg' +title: "Markdown Style Guide" +description: "Here is a sample of some basic Markdown syntax that can be used when writing Markdown content in Astro." +pubDate: "Jul 01 2022" +heroImage: "/placeholder-hero.jpg" --- Here is a sample of some basic Markdown syntax that can be used when writing Markdown content in Astro. diff --git a/examples/blog/src/pages/blog/second-post.md b/examples/blog/src/content/blog/second-post.md similarity index 98% rename from examples/blog/src/pages/blog/second-post.md rename to examples/blog/src/content/blog/second-post.md index 6b4f2d242..1bd5ee465 100644 --- a/examples/blog/src/pages/blog/second-post.md +++ b/examples/blog/src/content/blog/second-post.md @@ -1,5 +1,4 @@ --- -layout: "../../layouts/BlogPost.astro" title: "Second post" description: "Lorem ipsum dolor sit amet" pubDate: "Jul 22 2022" @@ -14,4 +13,4 @@ Mollis nunc sed id semper risus in. Convallis a cras semper auctor neque. Diam s Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Tortor posuere ac ut consequat semper viverra. Tellus mauris a diam maecenas sed enim ut sem viverra. Venenatis urna cursus eget nunc scelerisque viverra mauris in. Arcu ac tortor dignissim convallis aenean et tortor at. Curabitur gravida arcu ac tortor dignissim convallis aenean et tortor. Egestas tellus rutrum tellus pellentesque eu. Fusce ut placerat orci nulla pellentesque dignissim enim sit amet. Ut enim blandit volutpat maecenas volutpat blandit aliquam etiam. Id donec ultrices tincidunt arcu. Id cursus metus aliquam eleifend mi. -Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. \ No newline at end of file +Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. diff --git a/examples/blog/src/pages/blog/third-post.md b/examples/blog/src/content/blog/third-post.md similarity index 98% rename from examples/blog/src/pages/blog/third-post.md rename to examples/blog/src/content/blog/third-post.md index cba1991c5..d7f1f24b0 100644 --- a/examples/blog/src/pages/blog/third-post.md +++ b/examples/blog/src/content/blog/third-post.md @@ -1,5 +1,4 @@ --- -layout: "../../layouts/BlogPost.astro" title: "Third post" description: "Lorem ipsum dolor sit amet" pubDate: "Jul 15 2022" @@ -14,4 +13,4 @@ Mollis nunc sed id semper risus in. Convallis a cras semper auctor neque. Diam s Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Tortor posuere ac ut consequat semper viverra. Tellus mauris a diam maecenas sed enim ut sem viverra. Venenatis urna cursus eget nunc scelerisque viverra mauris in. Arcu ac tortor dignissim convallis aenean et tortor at. Curabitur gravida arcu ac tortor dignissim convallis aenean et tortor. Egestas tellus rutrum tellus pellentesque eu. Fusce ut placerat orci nulla pellentesque dignissim enim sit amet. Ut enim blandit volutpat maecenas volutpat blandit aliquam etiam. Id donec ultrices tincidunt arcu. Id cursus metus aliquam eleifend mi. -Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. \ No newline at end of file +Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. diff --git a/examples/with-content/src/content/blog/using-mdx.mdx b/examples/blog/src/content/blog/using-mdx.mdx similarity index 100% rename from examples/with-content/src/content/blog/using-mdx.mdx rename to examples/blog/src/content/blog/using-mdx.mdx diff --git a/examples/with-content/src/content/config.ts b/examples/blog/src/content/config.ts similarity index 83% rename from examples/with-content/src/content/config.ts rename to examples/blog/src/content/config.ts index 30cbbf293..b4e86c9f7 100644 --- a/examples/with-content/src/content/config.ts +++ b/examples/blog/src/content/config.ts @@ -6,7 +6,10 @@ const blog = defineCollection({ title: z.string(), description: z.string(), // Transform string to Date object - pubDate: z.string().transform((str) => new Date(str)), + pubDate: z + .string() + .or(z.date()) + .transform((val) => new Date(val)), updatedDate: z .string() .optional() diff --git a/examples/blog/src/env.d.ts b/examples/blog/src/env.d.ts index f964fe0cf..acef35f17 100644 --- a/examples/blog/src/env.d.ts +++ b/examples/blog/src/env.d.ts @@ -1 +1,2 @@ +/// /// diff --git a/examples/blog/src/layouts/BlogPost.astro b/examples/blog/src/layouts/BlogPost.astro index 4baf8e972..04a94ac48 100644 --- a/examples/blog/src/layouts/BlogPost.astro +++ b/examples/blog/src/layouts/BlogPost.astro @@ -1,21 +1,12 @@ --- +import type { CollectionEntry } from 'astro:content'; import BaseHead from '../components/BaseHead.astro'; import Header from '../components/Header.astro'; import Footer from '../components/Footer.astro'; -export interface Props { - content: { - title: string; - description: string; - pubDate?: string; - updatedDate?: string; - heroImage?: string; - }; -} +type Props = CollectionEntry<'blog'>['data']; -const { - content: { title, description, pubDate, updatedDate, heroImage }, -} = Astro.props; +const { title, description, pubDate, updatedDate, heroImage } = Astro.props; --- diff --git a/examples/blog/src/pages/about.md b/examples/blog/src/pages/about.md index 2aeb6562e..6a6dd0167 100644 --- a/examples/blog/src/pages/about.md +++ b/examples/blog/src/pages/about.md @@ -14,4 +14,4 @@ Mollis nunc sed id semper risus in. Convallis a cras semper auctor neque. Diam s Mattis ullamcorper velit sed ullamcorper morbi tincidunt. Tortor posuere ac ut consequat semper viverra. Tellus mauris a diam maecenas sed enim ut sem viverra. Venenatis urna cursus eget nunc scelerisque viverra mauris in. Arcu ac tortor dignissim convallis aenean et tortor at. Curabitur gravida arcu ac tortor dignissim convallis aenean et tortor. Egestas tellus rutrum tellus pellentesque eu. Fusce ut placerat orci nulla pellentesque dignissim enim sit amet. Ut enim blandit volutpat maecenas volutpat blandit aliquam etiam. Id donec ultrices tincidunt arcu. Id cursus metus aliquam eleifend mi. -Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. \ No newline at end of file +Tempus quam pellentesque nec nam aliquam sem. Risus at ultrices mi tempus imperdiet. Id porta nibh venenatis cras sed felis eget velit. Ipsum a arcu cursus vitae. Facilisis magna etiam tempor orci eu lobortis elementum. Tincidunt dui ut ornare lectus sit. Quisque non tellus orci ac. Blandit libero volutpat sed cras. Nec tincidunt praesent semper feugiat nibh sed pulvinar proin gravida. Egestas integer eget aliquet nibh praesent tristique magna. diff --git a/examples/blog/src/pages/blog.astro b/examples/blog/src/pages/blog.astro deleted file mode 100644 index cedf505b0..000000000 --- a/examples/blog/src/pages/blog.astro +++ /dev/null @@ -1,59 +0,0 @@ ---- -import BaseHead from '../components/BaseHead.astro'; -import Header from '../components/Header.astro'; -import Footer from '../components/Footer.astro'; -import { SITE_TITLE, SITE_DESCRIPTION } from '../config'; - -// Use Astro.glob() to fetch all posts, and then sort them by date. -const posts = (await Astro.glob('./blog/*.{md,mdx}')).sort( - (a, b) => new Date(b.frontmatter.pubDate).valueOf() - new Date(a.frontmatter.pubDate).valueOf() -); ---- - - - - - - - - -
-
-
-
    - { - posts.map((post) => ( -
  • - - {post.frontmatter.title} -
  • - )) - } -
-
-
-