Skip to content

Commit 5eaa504

Browse files
committed
refactor(docs): extract BASE_PATH constant and improve locale redirect
- Extract BASE_PATH as single source of truth for all path references - Add LOCALE_MAP for extensible language-to-locale mapping - Generate redirect script dynamically, eliminating hardcoded paths - Handle navigator.language being null/undefined - Preserve query parameters during redirect - Restore link: '/en/' for proper VitePress language switching - Use BASE_PATH in favicon and og:image URLs Benefits: - Changing base path requires only one edit - Adding new locales requires only LOCALE_MAP update - More robust handling of edge cases
1 parent 01748a3 commit 5eaa504

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

docs/.vitepress/config.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
import { defineConfig } from 'vitepress'
22

3+
// Site configuration constants - single source of truth
4+
const BASE_PATH = '/cpp-high-performance-guide/'
5+
const DEFAULT_LOCALE = 'en'
6+
const LOCALE_MAP: Record<string, string> = {
7+
zh: 'zh', // Chinese -> /zh/
8+
// All other languages default to /en/
9+
}
10+
11+
// Generate locale redirect script with dynamic base path
12+
function generateLocaleRedirectScript(): string {
13+
return `(()=>{const b='${BASE_PATH}',k='lr',s=sessionStorage,p=location.pathname;if(s.getItem(k))return;if(p!==b&&p!==b.slice(0,-1))return;s.setItem(k,'1');const l=(navigator.language||'${DEFAULT_LOCALE}').toLowerCase().slice(0,2),t=LOCALE_MAP[l]||'${DEFAULT_LOCALE}';location.replace(b+t+'/'+location.search)})()`
14+
.replace('LOCALE_MAP', JSON.stringify(LOCALE_MAP))
15+
}
16+
317
export default defineConfig({
418
title: 'C++ High Performance Guide',
519
description:
620
'Runnable C++20 performance engineering examples covering CMake, memory layout, SIMD, concurrency, and profiling.',
7-
base: '/cpp-high-performance-guide/',
21+
base: BASE_PATH,
822
cleanUrls: true,
923
lastUpdated: true,
1024
head: [
11-
['link', { rel: 'icon', type: 'image/svg+xml', href: '/cpp-high-performance-guide/favicon.svg' }],
25+
['link', { rel: 'icon', type: 'image/svg+xml', href: `${BASE_PATH}favicon.svg` }],
1226
['meta', { name: 'theme-color', content: '#5c7cfa' }],
1327
['meta', { property: 'og:type', content: 'website' }],
1428
['meta', { property: 'og:site_name', content: 'C++ High Performance Guide' }],
1529
['meta', { property: 'og:title', content: 'C++ High Performance Guide' }],
1630
['meta', { property: 'og:description', content: 'Runnable C++20 performance engineering examples and learning docs.' }],
17-
['meta', { property: 'og:image', content: 'https://lessup.github.io/cpp-high-performance-guide/logo.svg' }],
31+
['meta', { property: 'og:image', content: `https://lessup.github.io${BASE_PATH}logo.svg` }],
1832
['meta', { name: 'twitter:card', content: 'summary_large_image' }],
1933
['meta', { name: 'keywords', content: 'C++, C++20, performance, SIMD, cache optimization, concurrency, CMake, benchmark' }],
2034
// Auto-redirect based on browser language (runs before page renders)
21-
['script', {}, `(()=>{if(sessionStorage.getItem('locale-redirect-done'))return;const p=window.location.pathname;if(p==='/cpp-high-performance-guide/'||p==='/cpp-high-performance-guide'){sessionStorage.setItem('locale-redirect-done','true');const l=navigator.language.toLowerCase();window.location.replace(l.startsWith('zh')?'/cpp-high-performance-guide/zh/':'/cpp-high-performance-guide/en/')}})()`],
35+
['script', {}, generateLocaleRedirectScript()],
2236
],
2337
themeConfig: {
2438
logo: '/logo.svg',
@@ -43,6 +57,7 @@ export default defineConfig({
4357
root: {
4458
label: 'English',
4559
lang: 'en',
60+
link: '/en/',
4661
themeConfig: {
4762
nav: [
4863
{

0 commit comments

Comments
 (0)