From 984ab48da9bbf057f045284efc18383175ea187d Mon Sep 17 00:00:00 2001 From: Stalgia Grigg Date: Thu, 29 Feb 2024 13:00:54 -0800 Subject: [PATCH] Correctly rendering examples --- src/pages/examples/[...slug].astro | 17 ----------------- src/pages/examples/[slug].astro | 29 +++++++++++++++++++++++++++++ src/pages/examples/index.astro | 21 +++++++++++++++++++-- src/pages/examples/utils.ts | 10 ++++++++++ 4 files changed, 58 insertions(+), 19 deletions(-) delete mode 100644 src/pages/examples/[...slug].astro create mode 100644 src/pages/examples/[slug].astro create mode 100644 src/pages/examples/utils.ts diff --git a/src/pages/examples/[...slug].astro b/src/pages/examples/[...slug].astro deleted file mode 100644 index 53a2229094..0000000000 --- a/src/pages/examples/[...slug].astro +++ /dev/null @@ -1,17 +0,0 @@ ---- -import { getCollection } from "astro:content"; - -export async function getStaticPaths() { - const examples = await getCollection("examples"); - return examples.map((entry) => ({ - params: { slug: entry.slug }, - props: { entry }, - })); -} - -const { entry } = Astro.props; -const { Content } = await entry.render(); ---- - -

{entry.data.title}

-{JSON.stringify(entry)} diff --git a/src/pages/examples/[slug].astro b/src/pages/examples/[slug].astro new file mode 100644 index 0000000000..e70cd57bd2 --- /dev/null +++ b/src/pages/examples/[slug].astro @@ -0,0 +1,29 @@ +--- +import { getCollection } from "astro:content"; +import { exampleContentSlugToLegacyWebsiteSlug } from "./utils"; +import { readFile } from "fs/promises"; + +export async function getStaticPaths() { + const examples = await getCollection("examples"); + const paths = await Promise.all( + examples.map(async (example) => { + const codePath = `src/content/examples/${example.id.replace("description.mdx", "code.js")}`; + const code = await readFile(codePath, "utf-8"); + return { + params: { slug: exampleContentSlugToLegacyWebsiteSlug(example.slug) }, + props: { example, code }, + }; + }), + ); + + return paths; +} + +const { example, code } = Astro.props; +const { Content } = await example.render(); +--- + +

{example.data.title}

+

{example.data.arialabel}

+ +
{code}
diff --git a/src/pages/examples/index.astro b/src/pages/examples/index.astro index a22671529e..c80f319e61 100644 --- a/src/pages/examples/index.astro +++ b/src/pages/examples/index.astro @@ -1,13 +1,30 @@ --- import { getCollection } from "astro:content"; -const exampleEntries = await getCollection("examples"); +import { exampleContentSlugToLegacyWebsiteSlug } from "./utils"; + +/* We have to modify the Astro.js slug to match existing routing */ +/* This is done dynamically here instead of relying on example authors */ +/* to update their slugs in the MDX Content Entry */ +async function transformExampleSlugs() { + const exampleEntries = await getCollection("examples"); + + const transformedEntries = exampleEntries.map((entry) => ({ + ...entry, + slug: exampleContentSlugToLegacyWebsiteSlug(entry.slug), + })); + + return transformedEntries; +} + +// Using the transformed collection +const exampleEntries = await transformExampleSlugs(); ---