From dc8de971cdd014c7369653d6cc724162f922352a Mon Sep 17 00:00:00 2001 From: ykiko Date: Sun, 15 Nov 2026 22:02:29 +0800 Subject: [PATCH] fix(site): project homes go straight to their What-is pages ykiko finds the synced project home pages (a hero and feature cards) not worth showing: /clice/, /catter/, /xclang/ and their zh twins now redirect to guide/what-is-. The built page refreshes there before showing anything (GitHub Pages has no redirects) and names it as canonical; the theme redirects navigation within the site and the dev server's first load. The nav links to the What-is pages and stays lit anywhere in its project; the sitemap leaves the homes out. The strip of the homes' hero art goes: nothing of those pages shows any more. --- .vitepress/config.mts | 35 ++++++++++++++++++++++++++--------- .vitepress/theme/index.ts | 29 +++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/.vitepress/config.mts b/.vitepress/config.mts index 757a0ebb0..c88f9b7ce 100644 --- a/.vitepress/config.mts +++ b/.vitepress/config.mts @@ -21,10 +21,18 @@ const projects = ["clice", "catter", "xclang"]; // next to .vitepress: srcDir is not set, so it is the site root. const zhProjects = projects.filter((p) => fs.existsSync(new URL(`../zh/${p}`, import.meta.url))); +/** A project's pages in a language: zh, if the project publishes zh. */ +function projectBase(p: string, lang: "en" | "zh"): string { + return lang === "zh" && zhProjects.includes(p) ? `/zh/${p}` : `/${p}`; +} + +// A project opens on its "What is" page: its home page only redirects +// there (see transformPageData and the theme's setup). function projectNav(lang: "en" | "zh"): DefaultTheme.NavItem[] { return projects.map((p) => ({ text: p, - link: lang === "zh" && zhProjects.includes(p) ? `/zh/${p}/` : `/${p}/`, + link: `${projectBase(p, lang)}/guide/what-is-${p}`, + activeMatch: `^${projectBase(p, lang)}/`, })); } @@ -34,7 +42,12 @@ export default defineConfigWithTheme + items.filter((item) => !new RegExp(`^/?(zh/)?(${projects.join("|")})/?$`).test(item.url)), + }, head: [ // Browsers cache favicons apart from the page; bump v when the icons change. ["link", { rel: "icon", type: "image/png", sizes: "32x32", href: "/favicon-32.png?v=2" }], @@ -63,11 +76,16 @@ export default defineConfigWithTheme en.replace(/\/$/, "") === p); + const target = home ? `${isZh ? "/zh" : ""}/${home}/guide/what-is-${home}` : ""; const head = [ ["meta", { property: "og:title", content: title && title !== "clice.io" ? `${title} | clice.io` : "clice.io" }], ["meta", { property: "og:url", content: url(isZh ? path : en) }], ["meta", { property: "og:locale", content: isZh ? "zh_CN" : "en_US" }], - ["link", { rel: "canonical", href: url(isZh ? path : en) }], + ["link", { rel: "canonical", href: target ? `${SITE}${target}` : url(isZh ? path : en) }], // No share image: a link preview shows the page's title and description. ["meta", { name: "twitter:card", content: "summary" }], ]; @@ -81,13 +99,12 @@ export default defineConfigWithTheme { diff --git a/.vitepress/theme/index.ts b/.vitepress/theme/index.ts index 99e40af95..f7cfdad61 100644 --- a/.vitepress/theme/index.ts +++ b/.vitepress/theme/index.ts @@ -1,6 +1,6 @@ // https://vitepress.dev/guide/custom-theme -import { h } from 'vue' -import { onContentUpdated, type Theme } from 'vitepress' +import { h, onMounted } from 'vue' +import { onContentUpdated, useRouter, type Theme } from 'vitepress' import DefaultTheme from 'vitepress/theme' import BlogPostList from './components/BlogPostList.vue' import HomeLanding from './components/HomeLanding.vue' @@ -22,6 +22,16 @@ import '@fontsource/jetbrains-mono/400.css' import '@fontsource/jetbrains-mono/700.css' import './style.css' +// A project's home page redirects to its "What is" page: the built page +// refreshes to it (see transformPageData in config.mts); a navigation within +// the site, and the dev server, go there here. +const PROJECT_HOME = /^(\/zh)?\/(clice|catter|xclang)\/?$/ + +function entryFor(path: string): string { + const m = PROJECT_HOME.exec(path) + return m ? `${m[1] ?? ''}/${m[2]}/guide/what-is-${m[2]}` : '' +} + export default { extends: DefaultTheme, Layout: () => { @@ -32,6 +42,21 @@ export default { }) }, setup() { + const router = useRouter() + const before = router.onBeforeRouteChange + router.onBeforeRouteChange = (to) => { + const entry = entryFor(new URL(to, location.href).pathname) + if (entry) { + void router.go(entry) + return false + } + return before?.(to) + } + onMounted(() => { + const entry = entryFor(location.pathname) + if (entry) location.replace(entry) + }) + // A page's `