From a8e051b2586f373203c205d4a2200e2f7887e388 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 4 Dec 2026 12:36:31 -0500 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E9=A1=B9=E7=9B=AE=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E5=88=87=E6=8D=A2=E4=B8=AD=E8=8B=B1=E6=96=87?= =?UTF-8?q?=EF=BC=8C=E5=81=9A=E6=B3=95=E8=B7=9F=20Angular-iSharkFly-Com=20?= =?UTF-8?q?=E4=B8=80=E6=A0=B7=EF=BC=9A=E4=B8=AD=E6=96=87=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E8=BF=98=E5=9C=A8=E5=8E=9F=E6=9D=A5=E7=9A=84=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=EF=BC=8C=E8=8B=B1=E6=96=87=E9=A1=B5=E9=9D=A2=E6=94=BE=E5=9C=A8?= =?UTF-8?q?=20/en/=20=E4=B8=8B=E9=9D=A2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vitepress/config.mts | 340 ++++++++++++------ .../components/NavBarLanguageSwitcher.vue | 72 ++++ .../components/NavScreenLanguageSwitcher.vue | 85 +++++ .vitepress/theme/languageSwitcher.ts | 62 ++++ .vitepress/theme/style.css | 10 + docs/en/index.md | 61 ++++ 6 files changed, 526 insertions(+), 104 deletions(-) create mode 100644 .vitepress/theme/components/NavBarLanguageSwitcher.vue create mode 100644 .vitepress/theme/components/NavScreenLanguageSwitcher.vue create mode 100644 .vitepress/theme/languageSwitcher.ts create mode 100644 docs/en/index.md diff --git a/.vitepress/config.mts b/.vitepress/config.mts index 3a850c3..ad28094 100644 --- a/.vitepress/config.mts +++ b/.vitepress/config.mts @@ -1,5 +1,12 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; import { defineConfig, type DefaultTheme } from 'vitepress'; +const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); +const srcDir = path.join(rootDir, 'docs'); + +const EN_PREFIX = '/en'; const SITE_URL = 'https://docs.isharkfly.com/'; const FORUM_URL = 'https://www.isharkfly.com'; const REPO_URL = 'https://src.sharkdat.com/iSharkFly-Docs/docs-isharkfly-com'; @@ -57,12 +64,220 @@ const zhSearchText = { }, }; +// 中文侧边栏;英文侧边栏由 mapSidebar 从这里生成。 +const sidebar: DefaultTheme.SidebarItem[] = [ + { + text: '计算机相关技术', + items: [ + { + text: '程序语言', + collapsed: true, + items: [ + { + text: 'Java', link: '/programming-language/java/', collapsed: true, + items: [ + {text: '核心编程', link: '/programming-language/java/core/'}, + + ] + }, + {text: 'Python', link: '/programming-language/python/'}, + {text: 'TypeScript', link: '/programming-language/typescript/'}, + {text: 'Mumps', link: '/programming-language/mumps/'} + ] + }, + { + text: '框架和库', + collapsed: true, + link: '/framework/', + items: [ + { + text: 'Spring', + collapsed: true, + items: [ + { + text: 'Java', link: '/programming-language/java/', collapsed: true, + items: [ + {text: '核心编程', link: '/programming-language/java/core/'}, + + ] + }, + {text: 'Python', link: '/programming-language/python/'}, + {text: 'Mumps', link: '/programming-language/mumps/'} + ] + }, + { + text: 'Node', + collapsed: true, + items: [ + {text: 'Yarn', link: '/framework/node/yarn', collapsed: true,}, + ] + }, + ] + }, + { + text: 'DevOps', + collapsed: true, + link: '/dev-ops/', + items: [ + {text: 'OS', link: '/dev-ops/os/linux'}, + { + text: '数据库', link: '/dev-ops/database/', + items: [ + {text: 'PostgreSQL', link: '/dev-ops/database/postgresql.md'}, + {text: 'MariaDB/MySQL', link: '/dev-ops/database/mariadb.md'}, + ] + }, + {text: 'Docker', link: '/dev-ops/container/docker.md'}, + + ] + }, + { text: 'AI', collapsed: true, link: '/ai/'} + ] + }, + { + text: '面试和职场', + collapsed: true, + link: '/work/', + items: [ + {text: '面试', link: '/work/interview/'}, + {text: '职场', link: '/work/workplace/'}, + {text: '认证', link: '/work/certifications/'} + ] + }, + { + text: '人文随笔', + collapsed: true, + link: '/misc/', + items: [ + {text: '人文', link: '/misc/culture/', + items: [ + {text: '湘南地区村落', link: '/misc/culture/yong-zhou'} + ] + }, + {text: '随笔', link: '/misc/essay/'} + ] + }, + { + text: '站点导航', + collapsed: true, + link: '/navigation/' + } +]; + +const enThemeText: DefaultTheme.Config = { + outline: { level: [2, 3], label: 'On this page' }, + footer: { + message: 'Released under the MIT License.', + copyright: `Copyright © 2025-${new Date().getFullYear()} iSharkFly.com`, + }, +}; + +// 侧边栏只维护中文一份,英文侧边栏的文字由下表翻译,没有列出的保持原文。 +const enSidebarText: Record = { + '计算机相关技术': 'Computer Technology', + '程序语言': 'Programming Languages', + '核心编程': 'Core Programming', + '框架和库': 'Frameworks & Libraries', + '数据库': 'Databases', + '面试和职场': 'Interviews & Career', + '面试': 'Interviews', + '职场': 'Workplace', + '认证': 'Certifications', + '人文随笔': 'Culture & Essays', + '人文': 'Culture', + '湘南地区村落': 'Villages of Southern Hunan', + '随笔': 'Essays', + '站点导航': 'Site Navigation', +}; + +// 列出某个目录下的 .md 页面(相对路径);根目录下跳过 en、public。 +function listPages(dir: string, rel = ''): string[] { + if (!fs.existsSync(dir)) + return []; + return fs.readdirSync(dir, { withFileTypes: true }).flatMap(entry => { + const entryRel = rel + entry.name; + if (entry.isDirectory()) { + if (!rel && ['en', 'public'].includes(entry.name)) + return []; + return listPages(path.join(dir, entry.name), `${entryRel}/`); + } + return entry.name.endsWith('.md') ? [entryRel] : []; + }); +} + +const pagesByLanguage = { + zh: listPages(srcDir), + en: listPages(path.join(srcDir, 'en')), +}; +const enPages = new Set(pagesByLanguage.en); + +// 站内链接对应的 .md 页面,例如 /work/ -> work/index.md,/ai/9537-mac -> ai/9537-mac.md。 +function linkToPages(link: string): string[] { + const p = link.replace(/^\//, '').replace(/[#?].*$/, ''); + if (p === '' || p.endsWith('/')) + return [`${p}index.md`]; + if (p.endsWith('.md')) + return [p]; + return [`${p}.md`, `${p}/index.md`]; +} + +// 英文版有这一页就指向 /en/...,否则仍指向中文页面,避免 404。 +function enLink(link: string) { + if (!link.startsWith('/')) + return link; + return linkToPages(link).some(page => enPages.has(page)) ? EN_PREFIX + link : link; +} + +function mapSidebar( + items: DefaultTheme.SidebarItem[], + mapLink: (link: string) => string, + mapText: (text: string) => string, +): DefaultTheme.SidebarItem[] { + return items.map(item => ({ + ...item, + ...(item.text ? { text: mapText(item.text) } : {}), + ...(item.link ? { link: mapLink(item.link) } : {}), + ...(item.items ? { items: mapSidebar(item.items, mapLink, mapText) } : {}), + })); +} + // https://vitepress.dev/reference/site-config export default defineConfig({ srcDir: 'docs', lang: 'zh-CN', title: 'iSharkFly 文档和手册', description: 'iSharkFly - Docs', + locales: { + root: { + label: '简体中文', + lang: 'zh-CN', + title: 'iSharkFly 文档和手册', + description: 'iSharkFly - Docs', + themeConfig: { + ...zhThemeText, + nav: [ + { text: '主页', link: '/' }, + isharkflyNav(true), + ], + sidebar, + }, + }, + en: { + label: 'English', + lang: 'en-US', + link: `${EN_PREFIX}/`, + title: 'iSharkFly Docs', + description: 'iSharkFly - Docs', + themeConfig: { + ...enThemeText, + nav: [ + { text: 'Home', link: `${EN_PREFIX}/` }, + isharkflyNav(false), + ], + sidebar: mapSidebar(sidebar, enLink, text => enSidebarText[text] ?? text), + }, + }, + }, cleanUrls: true, lastUpdated: false, ignoreDeadLinks: true, @@ -104,122 +319,39 @@ export default defineConfig({ theme: { light: 'github-light', dark: 'github-dark' }, }, themeConfig: { - ...zhThemeText, // https://vitepress.dev/reference/default-theme-config - nav: [ - { text: '主页', link: '/' }, - isharkflyNav(true), - ], search: { provider: 'local', options: { - translations: zhSearchText, + locales: { + root: { translations: zhSearchText }, + }, }, }, - sidebar: [ - { - text: '计算机相关技术', - items: [ - { - text: '程序语言', - collapsed: true, - items: [ - { - text: 'Java', link: '/programming-language/java/', collapsed: true, - items: [ - {text: '核心编程', link: '/programming-language/java/core/'}, - - ] - }, - {text: 'Python', link: '/programming-language/python/'}, - {text: 'TypeScript', link: '/programming-language/typescript/'}, - {text: 'Mumps', link: '/programming-language/mumps/'} - ] - }, - { - text: '框架和库', - collapsed: true, - link: '/framework/', - items: [ - { - text: 'Spring', - collapsed: true, - items: [ - { - text: 'Java', link: '/programming-language/java/', collapsed: true, - items: [ - {text: '核心编程', link: '/programming-language/java/core/'}, - - ] - }, - {text: 'Python', link: '/programming-language/python/'}, - {text: 'Mumps', link: '/programming-language/mumps/'} - ] - }, - { - text: 'Node', - collapsed: true, - items: [ - {text: 'Yarn', link: '/framework/node/yarn', collapsed: true,}, - ] - }, - ] - }, - { - text: 'DevOps', - collapsed: true, - link: '/dev-ops/', - items: [ - {text: 'OS', link: '/dev-ops/os/linux'}, - { - text: '数据库', link: '/dev-ops/database/', - items: [ - {text: 'PostgreSQL', link: '/dev-ops/database/postgresql.md'}, - {text: 'MariaDB/MySQL', link: '/dev-ops/database/mariadb.md'}, - ] - }, - {text: 'Docker', link: '/dev-ops/container/docker.md'}, - - ] - }, - { text: 'AI', collapsed: true, link: '/ai/'} - ] - }, - { - text: '面试和职场', - collapsed: true, - link: '/work/', - items: [ - {text: '面试', link: '/work/interview/'}, - {text: '职场', link: '/work/workplace/'}, - {text: '认证', link: '/work/certifications/'} - ] - }, - { - text: '人文随笔', - collapsed: true, - link: '/misc/', - items: [ - {text: '人文', link: '/misc/culture/', - items: [ - {text: '湘南地区村落', link: '/misc/culture/yong-zhou'} - ] - }, - {text: '随笔', link: '/misc/essay/'} - ] - }, - { - text: '站点导航', - collapsed: true, - link: '/navigation/' - } - ], socialLinks: [ { icon: 'git', link: `${REPO_URL}.git` }, ], editLink: undefined, + languageSwitcher: { + enPrefix: EN_PREFIX, + pages: pagesByLanguage, + }, } as DefaultTheme.Config, vite: { build: { chunkSizeWarningLimit: 4096 }, + resolve: { + // 用自定义的语言切换组件替换默认主题的翻译菜单(与 Angular-iSharkFly-Com 一致): + // 对应语言没有当前页面时跳到该语言首页,而不是 404。 + alias: [ + { + find: /^.*\/VPNavBarTranslations\.vue$/, + replacement: fileURLToPath(new URL('./theme/components/NavBarLanguageSwitcher.vue', import.meta.url)), + }, + { + find: /^.*\/VPNavScreenTranslations\.vue$/, + replacement: fileURLToPath(new URL('./theme/components/NavScreenLanguageSwitcher.vue', import.meta.url)), + }, + ], + }, }, }); diff --git a/.vitepress/theme/components/NavBarLanguageSwitcher.vue b/.vitepress/theme/components/NavBarLanguageSwitcher.vue new file mode 100644 index 0000000..25b79b4 --- /dev/null +++ b/.vitepress/theme/components/NavBarLanguageSwitcher.vue @@ -0,0 +1,72 @@ + + + + + diff --git a/.vitepress/theme/components/NavScreenLanguageSwitcher.vue b/.vitepress/theme/components/NavScreenLanguageSwitcher.vue new file mode 100644 index 0000000..a1f7575 --- /dev/null +++ b/.vitepress/theme/components/NavScreenLanguageSwitcher.vue @@ -0,0 +1,85 @@ + + + + + diff --git a/.vitepress/theme/languageSwitcher.ts b/.vitepress/theme/languageSwitcher.ts new file mode 100644 index 0000000..2a3bd12 --- /dev/null +++ b/.vitepress/theme/languageSwitcher.ts @@ -0,0 +1,62 @@ +import { computed } from 'vue'; +import { useData } from 'vitepress'; + +type Reading = 'zh' | 'en'; + +type SwitcherConfig = { + enPrefix: string; + // 每种语言已有的页面(相对该语言根目录的 .md 路径),用来判断切换后对应页面是否存在。 + pages: Record; +}; + +export type SwitcherLink = { text: string; link: string; lang?: string; active: boolean }; + +const READING_LANGUAGES = [ + { key: 'zh', text: '简体中文', lang: 'zh-CN' }, + { key: 'en', text: 'English', lang: 'en-US' }, +] as const; + +function pageUrl(base: string, page: string) { + return base + page.replace(/(^|\/)index\.md$/, '$1').replace(/\.md$/, ''); +} + +export function useLanguageSwitcher() { + const { page, theme, hash } = useData(); + const config = computed(() => theme.value.languageSwitcher as SwitcherConfig); + + const current = computed(() => { + let rest = page.value.relativePath; + const enDir = config.value.enPrefix.replace(/^\//, '') + '/'; + const reading: Reading = rest.startsWith(enDir) ? 'en' : 'zh'; + if (reading === 'en') + rest = rest.slice(enDir.length); + return { reading, page: rest }; + }); + + // 对应语言有同一页面就跳到该页面,否则跳到该语言的首页。 + function linkFor(reading: Reading, keepHash: boolean) { + const base = reading === 'en' ? config.value.enPrefix + '/' : '/'; + const pages = config.value.pages[reading] ?? []; + if (!pages.includes(current.value.page)) + return base; + return pageUrl(base, current.value.page) + (keepHash ? hash.value : ''); + } + + const isZh = computed(() => current.value.reading === 'zh'); + + const readingLanguages = computed(() => READING_LANGUAGES.map(reading => ({ + text: reading.text, + lang: reading.lang, + link: linkFor(reading.key, true), + active: reading.key === current.value.reading, + }))); + + const labels = computed(() => isZh.value + ? { menu: '切换语言', reading: '阅读语言' } + : { menu: 'Change language', reading: 'Language' }); + + const currentLabel = computed(() => + READING_LANGUAGES.find(reading => reading.key === current.value.reading)!.text); + + return { readingLanguages, labels, currentLabel, isZh }; +} diff --git a/.vitepress/theme/style.css b/.vitepress/theme/style.css index 1a61cb1..fd3076d 100644 --- a/.vitepress/theme/style.css +++ b/.vitepress/theme/style.css @@ -137,3 +137,13 @@ --docsearch-primary-color: var(--vp-c-brand-1) !important; } + +/** + * Language switcher + * 默认主题“…”菜单里的翻译分组会直接链接到另一语言的同名页面(英文页面多数还没有,会 404), + * 这里把它隐藏,改由 NavBarLanguageSwitcher 显示。 + * -------------------------------------------------------------------------- */ + +.VPNavBarExtra .group.translations { + display: none; +} diff --git a/docs/en/index.md b/docs/en/index.md new file mode 100644 index 0000000..507f05e --- /dev/null +++ b/docs/en/index.md @@ -0,0 +1,61 @@ +--- +# https://vitepress.dev/reference/default-theme-home-page +layout: home + +hero: + name: "iSharkFly Docs" + text: "Docs" + tagline: Welcome to iSharkFly's documentation and handbooks + actions: + - theme: brand + text: Programming Languages + link: /programming-language/index + - theme: alt + text: Frameworks & Libraries + link: /framework + - theme: alt + text: Products + link: /product + - theme: alt + text: DevOps + link: /dev-ops/ + - theme: alt + text: Career & Interviews + link: /work + - theme: alt + text: Culture + link: /misc/culture + - theme: alt + text: Essays + link: /misc/essay + +features: + - title: iSharkFly Docs Project + details: Notes, handbooks and tutorials on software development, collected by iSharkFly. + - title: Built with VitePress + details: The site is generated with VitePress and deployed on Cloudflare Workers. + - title: Quick Navigation + details: See the quick navigation links at the bottom of this page for related sites. + link: /navigation +--- + +::: info +Most articles are currently available in Chinese only. Links on this page open the Chinese version. +::: + +## Contact + +| Channel | Contact | +|---------|---------| +| Email | [yhu@sharkdat.com](mailto:yhu@sharkdat.com) | +| QQ / WeChat | 103899765 | +| Forum | https://www.isharkfly.com/ | + +## Quick Navigation + +| Site | URL | Note | +|------|-----|------| +| DOCS.ISHARKFLY.COM | [https://docs.isharkfly.com/](https://docs.isharkfly.com/) | This handbook | +| iSharkFly | https://www.isharkfly.com/c/software-development/5 | Front-end frameworks and software development discussions | +| TypeScript | https://typescript.ossez.com/ | TypeScript documentation and handbook | +| Mumps | https://mumps.sharkfly.com/ | Mumps documentation and handbook | -- 2.54.0