From 4299ab303b0743349fbd01f85340bea61a1c16a8 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Mon, 28 Mar 2022 17:16:06 -0700 Subject: [PATCH] New Markdown API (#2862) * Implement new markdown plugin with deferred markdown rendering * feat: switch from `getContent()` fn to `` API * update types * Update packages/astro/src/@types/astro.ts Co-authored-by: Nate Moore * update types * Create forty-coins-attend.md Co-authored-by: Nate Moore Co-authored-by: Nate Moore --- .changeset/forty-coins-attend.md | 16 +++ .../src/components/PostPreview.astro | 11 ++- .../src/pages/authors/[author].astro | 20 ++-- .../src/pages/index.astro | 15 +-- .../src/pages/posts/[...page].astro | 16 +-- .../blog/src/components/BlogPostPreview.astro | 6 +- examples/blog/src/pages/index.astro | 8 +- .../src/components/PortfolioPreview/index.jsx | 9 +- examples/portfolio/src/pages/index.astro | 2 +- examples/portfolio/src/pages/projects.astro | 10 +- packages/astro/env.d.ts | 2 +- packages/astro/package.json | 2 + packages/astro/src/@types/astro.ts | 29 +++--- packages/astro/src/core/render/dev/css.ts | 7 +- packages/astro/src/runtime/server/index.ts | 49 ++++------ .../vite-plugin-astro-postprocess/index.ts | 97 ++++++++++--------- .../astro/src/vite-plugin-markdown/index.ts | 94 ++++++++++++++++-- packages/astro/test/astro-global.test.js | 2 +- .../astro-global/src/pages/posts/[page].astro | 6 +- .../src/pages/posts/[slug]/[page].astro | 6 +- .../pages/posts/named-root-page/[page].astro | 2 +- .../posts/optional-root-page/[...page].astro | 2 +- .../src/pages/episodes/[...page].astro | 30 +++--- .../src/pages/posts/[slug].astro | 8 +- .../static build/src/pages/index.astro | 2 +- packages/astro/test/static-build.test.js | 2 +- pnpm-lock.yaml | 21 ++++ 27 files changed, 279 insertions(+), 195 deletions(-) create mode 100644 .changeset/forty-coins-attend.md diff --git a/.changeset/forty-coins-attend.md b/.changeset/forty-coins-attend.md new file mode 100644 index 000000000..467e520fd --- /dev/null +++ b/.changeset/forty-coins-attend.md @@ -0,0 +1,16 @@ +--- +"astro": minor +--- + +Implement RFC [#0017](https://github.com/withastro/rfcs/blob/main/proposals/0017-markdown-content-redesign.md) + +- New Markdown API +- New `Astro.glob()` API +- **BREAKING CHANGE:** Removed `Astro.fetchContent()` (replaced by `Astro.glob()`) + +```diff +// v0.25 +- let allPosts = Astro.fetchContent('./posts/*.md'); +// v0.26+ ++ let allPosts = await Astro.glob('./posts/*.md'); +``` diff --git a/examples/blog-multiple-authors/src/components/PostPreview.astro b/examples/blog-multiple-authors/src/components/PostPreview.astro index 81e80ba6c..5a9808348 100644 --- a/examples/blog-multiple-authors/src/components/PostPreview.astro +++ b/examples/blog-multiple-authors/src/components/PostPreview.astro @@ -4,6 +4,7 @@ export interface Props { author: string; } const { post, author } = Astro.props; +const { frontmatter } = post; function formatDate(date) { return new Date(date).toUTCString().replace(/(\d\d\d\d) .*/, '$1'); // remove everything after YYYY @@ -12,12 +13,12 @@ function formatDate(date) {
-

{post.title}

- {author.name} - +

{frontmatter.title}

+ {author.name} +

- {post.description} - Read + {frontmatter.description} + Read

diff --git a/examples/blog-multiple-authors/src/pages/authors/[author].astro b/examples/blog-multiple-authors/src/pages/authors/[author].astro index 21aab27a5..c2ba49d39 100644 --- a/examples/blog-multiple-authors/src/pages/authors/[author].astro +++ b/examples/blog-multiple-authors/src/pages/authors/[author].astro @@ -2,36 +2,28 @@ import MainHead from '../../components/MainHead.astro'; import Nav from '../../components/Nav.astro'; import PostPreview from '../../components/PostPreview.astro'; -import Pagination from '../../components/Pagination.astro'; import authorData from '../../data/authors.json'; -export function getStaticPaths() { - const allPosts = Astro.fetchContent('../post/*.md'); - let allAuthorsUnique = [...new Set(allPosts.map((p) => p.author))]; +export async function getStaticPaths() { + const allPosts = await Astro.glob('../post/*.md'); + let allAuthorsUnique = [...new Set(allPosts.map((p) => p.frontmatter.author))]; return allAuthorsUnique.map((author) => ({ params: { author }, props: { allPosts } })); } -interface MarkdownFrontmatter { - date: number; - description: string; - title: string; - author: string; -} - const { allPosts } = Astro.props; const { params, canonicalURL } = Astro.request; const title = 'Don’s Blog'; const description = 'An example blog on Astro'; /** filter posts by author, sort by date */ -const posts = allPosts.filter((post) => post.author === params.author).sort((a, b) => new Date(b.date).valueOf() - new Date(a.date).valueOf()); -const author = authorData[posts[0].author]; +const posts = allPosts.filter((post) => post.frontmatter.author === params.author).sort((a, b) => new Date(b.frontmatter.date).valueOf() - new Date(a.frontmatter.date).valueOf()); +const author = authorData[posts[0].frontmatter.author]; --- {title} - +