From 814c99101f69df4a656a09f4d19e9135a09d5f47 Mon Sep 17 00:00:00 2001 From: Hans5958 Date: Sat, 27 Dec 2025 22:17:30 +0700 Subject: [PATCH] Redirect rule pages by ID instead of rewrite Done this way to maintain the meta tags for social media sharing --- src/layouts/DocRedirect.astro | 21 ++++++ src/layouts/Redirect.astro | 69 +++++++++++++++++++ src/pages/rules/[id].astro | 46 +++++++++++++ .../rules/{[...slug].astro => [slug].astro} | 29 +------- 4 files changed, 138 insertions(+), 27 deletions(-) create mode 100644 src/layouts/DocRedirect.astro create mode 100644 src/layouts/Redirect.astro create mode 100644 src/pages/rules/[id].astro rename src/pages/rules/{[...slug].astro => [slug].astro} (77%) diff --git a/src/layouts/DocRedirect.astro b/src/layouts/DocRedirect.astro new file mode 100644 index 00000000..12dfffa3 --- /dev/null +++ b/src/layouts/DocRedirect.astro @@ -0,0 +1,21 @@ +--- +import Redirect from "./Redirect.astro"; + +interface FrontMatter { + title?: string; + description?: string; + tags?: string[]; +} + +export interface Props { + frontmatter?: FrontMatter; + canonical: string; +} + +const { frontmatter = {}, canonical } = Astro.props; + +const title = frontmatter?.title; +const description = frontmatter?.description; +--- + + diff --git a/src/layouts/Redirect.astro b/src/layouts/Redirect.astro new file mode 100644 index 00000000..0340438f --- /dev/null +++ b/src/layouts/Redirect.astro @@ -0,0 +1,69 @@ +--- +import { + SITE_TITLE, + SITE_DESCRIPTION, + SITE_DEFAULT_OG_IMAGE, +} from "../site_config"; +import "@/styles/globals.css"; + +export interface Props { + title?: string; + description?: string; + thumbnail?: { + src: string; + alt: string; + }; + from?: string; + canonical?: string; +} + +const { + title = SITE_TITLE, + description = SITE_DESCRIPTION, + thumbnail = { + src: SITE_DEFAULT_OG_IMAGE, + alt: SITE_TITLE, + }, + from = Astro.request.url, + canonical, +} = Astro.props; + +const siteURL = Astro.site; +const absoluteLocation = siteURL + ? new URL(canonical || "", siteURL) + : canonical; +--- + + + + + + {title} + + + + + + + + + + + + + + + + Redirecting { + from ? ( + <> + from ${from}{" "} + + ) : ( + "" + ) + }to {canonical} + + diff --git a/src/pages/rules/[id].astro b/src/pages/rules/[id].astro new file mode 100644 index 00000000..17fed528 --- /dev/null +++ b/src/pages/rules/[id].astro @@ -0,0 +1,46 @@ +--- +import { getCollection, type CollectionEntry } from "astro:content"; +import { unified } from "unified"; +import remarkParse from "remark-parse"; +import stripMarkdown from "strip-markdown"; +import remarkStringify from "remark-stringify"; +import DocRedirect from "@/layouts/DocRedirect.astro"; + +export async function getStaticPaths() { + const ruleEntries: CollectionEntry<"rules">[] = await getCollection("rules"); + const paths = []; + + for (const entry of ruleEntries) { + paths.push({ + params: { + id: entry.data.id, + }, + props: { entry }, + }); + } + + return paths; +} + +const { entry } = Astro.props; + +async function stripText(markdownInput: string) { + const file = await unified() + .use(remarkParse) + .use(stripMarkdown) + .use(remarkStringify) + .process(markdownInput); + + return String(file); +} + +const description = await stripText(entry.data.excerpt || entry.body); +--- + + diff --git a/src/pages/rules/[...slug].astro b/src/pages/rules/[slug].astro similarity index 77% rename from src/pages/rules/[...slug].astro rename to src/pages/rules/[slug].astro index bf16816b..595b3e7f 100644 --- a/src/pages/rules/[...slug].astro +++ b/src/pages/rules/[slug].astro @@ -18,23 +18,12 @@ export async function getStaticPaths() { const paths = []; for (const entry of ruleEntries) { - const pathConfig = { + paths.push({ params: { slug: entry.slug, }, props: { entry }, - }; - - paths.push(pathConfig); - - if (typeof entry.data.id === "number") { - paths.push({ - params: { - slug: entry.data.id.toString(), - }, - props: { entry }, - }); - } + }); } return paths; @@ -45,8 +34,6 @@ const { headings } = await entry.render(); headings.push({ depth: 2, slug: "metadata", text: "Metadata" }); -const isNumericRoute = !isNaN(Number(Astro.params.slug)); - async function stripText(markdownInput: string) { const file = await unified() .use(remarkParse) @@ -73,19 +60,7 @@ const description = await stripText(entry.data.excerpt || entry.body); headings={headings} filePath={"src/content/rules/" + entry.id} pagePath={"rules/" + entry.slug} - pagefindIgnore={isNumericRoute} > - - {content && }