Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}/`,
}));
}

Expand All @@ -34,7 +42,12 @@ export default defineConfigWithTheme<DefaultTheme.Config & { zhProjects?: string
cleanUrls: true,
srcExclude: ["README.md", "art/**", "output/**", "sources/**", "tools/**", "check-translations/**"],
lastUpdated: true,
sitemap: { hostname: SITE },
sitemap: {
hostname: SITE,
// A project's home only redirects to its "What is" page.
transformItems: (items) =>
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" }],
Expand Down Expand Up @@ -63,11 +76,16 @@ export default defineConfigWithTheme<DefaultTheme.Config & { zhProjects?: string
const description =
pageData.frontmatter.description ?? (excerptOf(`${siteConfig.srcDir}/${pageData.filePath}`) || pageData.description);
if (description) pageData.description = description;
// A project's home page goes straight to its "What is" page: GitHub
// Pages has no redirects, so the page refreshes to it before showing
// anything (the theme redirects navigation within the site).
const home = projects.find((p) => 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" }],
];
Expand All @@ -81,13 +99,12 @@ export default defineConfigWithTheme<DefaultTheme.Config & { zhProjects?: string
if (description) {
head.push(["meta", { property: "og:description", content: description }]);
}
pageData.frontmatter.head = [...(pageData.frontmatter.head ?? []), ...head];
// Temporary: hide the mascot art on the project home pages until new
// hero art exists. The pages are synced from the project repos, so the
// image is dropped here rather than in their frontmatter.
if (projects.includes(en.split("/")[0]) && pageData.frontmatter.hero) {
delete pageData.frontmatter.hero.image;
if (target) {
head.push(["meta", { "http-equiv": "refresh", content: `0; url=${target}` }]);
// Nothing of the synced home (hero, feature cards) shows meanwhile.
pageData.frontmatter = { layout: "page", search: false, sidebar: false, aside: false };
}
pageData.frontmatter.head = [...(pageData.frontmatter.head ?? []), ...head];
},
markdown: {
config: (md) => {
Expand Down
29 changes: 27 additions & 2 deletions .vitepress/theme/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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: () => {
Expand All @@ -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 `<video autoplay muted>` (clice's what-is-clice demos) is put
// in by Vue, not by the HTML parser, and Chrome then leaves it paused,
// though a muted video may autoplay: start it once the page is in.
Expand Down
Loading