Skip to content

Commit

Permalink
Resolve locales translate or route path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ihavecoke committed Nov 1, 2023
1 parent dc2a3ec commit bc40a5d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 39 deletions.
83 changes: 46 additions & 37 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DefaultTheme, defineConfig } from 'vitepress';
import { DefaultTheme, defineConfig } from "vitepress";
// @ts-ignore
import docs from '../locales/docs.json';

const convertDocsToZhHK = (docs: Record<string, any>[]) => {};
import docs from "../locales/docs.json";
import * as OpenCC from "opencc-js";
const converter = OpenCC.Converter({ from: "hk", to: "cn" });

/**
* Convert feishu-pages's docs.json into VitePress's sidebar config
Expand All @@ -12,73 +12,82 @@ const convertDocsToZhHK = (docs: Record<string, any>[]) => {};
*/
const convertDocsToSidebars = (
docs: Record<string, any>[],
rootSlug?: string
rootSlug?: string,
) => {
const sidebars: DefaultTheme.SidebarItem[] = [];
const cnLang = rootSlug === "zh-CN";

// Go to root slug
docs = docs.find((doc) => doc.slug === rootSlug)?.children || docs;

docs =
docs.find((doc) => doc.slug === (cnLang ? "zh-HK" : rootSlug))?.children ||
docs;

// zh-HK/guides/crm/dashboards
// -> zh-HK/docs/guides/crm/dashboards
for (const doc of docs) {
if(!doc.meta?.hide){
let sidebar: DefaultTheme.SidebarItem = {
text: doc.title,
link: '/' + doc.slug.replace(/^zh-HK\//, 'zh-HK/docs/'),
};
if (doc.children.length > 0) {
sidebar.items = convertDocsToSidebars(doc.children);
if (!doc.meta?.hide) {
const text = cnLang ? converter(doc.title) : doc.title;

const link = `/${
cnLang ? doc.slug.replace(/^zh-HK\//, "zh-CN/docs/") : doc.slug.replace(/^(en|zh-HK)\//, "$1/docs/")
}`;

let sidebar: DefaultTheme.SidebarItem = {
text,
link,
};
if (doc.children.length > 0) {
sidebar.items = convertDocsToSidebars(doc.children, rootSlug);
sidebar.collapsed = true;
}
sidebars.push(sidebar);
}
sidebars.push(sidebar);}
}

return sidebars;
};

const docsSidebarEN = convertDocsToSidebars(docs, 'en');
const docsSidebarZHCN = convertDocsToSidebars(docs, 'zh-CN');
const docsSidebarZHHK = convertDocsToSidebars(docs, 'zh-HK');
const docsSidebarEN = convertDocsToSidebars(docs, "en");
const docsSidebarZHCN = convertDocsToSidebars(docs, "zh-CN");
const docsSidebarZHHK = convertDocsToSidebars(docs, "zh-HK");

// https://vitepress.dev/reference/site-config
export default defineConfig({
title: 'Longbridge Whale Docs',
base: '/',
description: 'Longbridge Whale Docs',
title: "Longbridge Whale Docs",
base: "/",
description: "Longbridge Whale Docs",
ignoreDeadLinks: true,
locales: {
en: {
label: 'English',
lang: 'en',
label: "English",
lang: "en",
},
'zh-CN': {
label: '简体中文',
lang: 'zh-CN',
"zh-CN": {
label: "简体中文",
lang: "zh-CN",
},
'zh-HK': {
label: '繁体中文',
lang: 'zh-HK',
"zh-HK": {
label: "繁体中文",
lang: "zh-HK",
},
},
cleanUrls: true,
srcExclude: ['SUMMARY.md'],
srcDir: 'locales',
srcExclude: ["SUMMARY.md"],
srcDir: "locales",
themeConfig: {
// https://vitepress.dev/reference/default-theme-config
nav: [
{ text: 'Whale Home', link: 'https://longbridgewhale.com' },
{ text: 'Docs', link: '/zh-HK/docs/guides' },
{ text: "Whale Home", link: "https://longbridgewhale.com" },
{ text: "Docs", link: "/zh-HK/docs/guides" },
],

sidebar: {
en: docsSidebarEN,
'zh-CN': docsSidebarZHCN,
'zh-HK': docsSidebarZHHK,
"zh-CN": docsSidebarZHCN,
"zh-HK": docsSidebarZHHK,
},

socialLinks: [
{ icon: 'github', link: 'https://github.com/longbridgeapp/whale-docs' },
{ icon: "github", link: "https://github.com/longbridgeapp/whale-docs" },
],
},
});
12 changes: 10 additions & 2 deletions .vitepress/theme/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import DefaultTheme from 'vitepress/theme';
import '../../style.scss';

export default DefaultTheme;
import VPBadge from 'vitepress/dist/client/theme-default/components/VPBadge.vue';
const theme = {
...DefaultTheme,
enhanceApp: (ctx) => {
const { app } = ctx;
app.component('Badge', VPBadge);
window.WhaleDocs = ctx;
}
}
export default theme;
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
"opencc-js": "^1.0.5",
"sass": "^1.69.3",
"vitepress": "^1.0.0-rc.22"
},
"devDependencies": {
"prettier": "^3.0.3"
}
}

0 comments on commit bc40a5d

Please sign in to comment.