Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.21 #125

Merged
merged 17 commits into from
Jul 4, 2024
Merged

1.21 #125

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 10 additions & 2 deletions .vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import snippetPlugin from "markdown-it-vuepress-code-snippet-enhanced";
import defineVersionedConfig from "vitepress-versioning-plugin";

import { loadLocales } from "./i18n";
import { getLocalisedSidebar, loadLocales, processExistingEntries } from "./i18n";
import { transformItems, transformPageData } from "./transform";
import { DefaultTheme } from "vitepress";
import { readdirSync } from "node:fs";
import { resolve } from "node:path";

// https://vitepress.dev/reference/site-config
// https://www.npmjs.com/package/vitepress-versioning-plugin
Expand Down Expand Up @@ -51,10 +54,15 @@ export default defineVersionedConfig(
transformPageData,

versioning: {
latestVersion: "1.20.4",
latestVersion: "1.21",
rewrites: {
localePrefix: "translated",
},
sidebars: {
sidebarContentProcessor(sidebar: DefaultTheme.SidebarMulti) {
return processExistingEntries(sidebar);
},
}
},
},
__dirname
Expand Down
104 changes: 74 additions & 30 deletions .vitepress/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, readdirSync, readFileSync } from "fs";
import { resolve } from "path/posix";
import { LocaleConfig } from "vitepress";
import { DefaultTheme, LocaleConfig } from "vitepress";

import DevelopSidebar from "./sidebars/develop";
import PlayersSidebar from "./sidebars/players";
Expand Down Expand Up @@ -34,43 +34,86 @@ export const getWebsiteResolver = (localeFolder: string) =>
export const getSidebarResolver = (localeFolder: string) =>
getTranslationsResolver(localeFolder, "sidebar_translations.json");

export function processExistingEntries(sidebar: DefaultTheme.SidebarMulti): DefaultTheme.SidebarMulti {
// Get locales from __dirname/../translated/* folder names.
const localeFolders = readdirSync(resolve(__dirname, "..", "translated"), { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

// Get all versioning entries that match <locale>/<version>
// aka, does not match <locale>/players or <locale>/develop or /develop or /players

const keys = Object.keys(sidebar);
const versionedEntries = keys.filter((key) => {
return !key.includes("/players") && !key.includes("/develop") && localeFolders.some((locale) => key.includes(locale));
});

const versionsMet = new Set<string>();
// Now, ensure that <locale>/<version>/players and <locale>/<version>/develop are included in the sidebar. Use <version>/players and <version>/develop as the key.
for (const entry of versionedEntries) {
const split = entry.split("/");
const locale = split[1];
const version = split[2];

versionsMet.add(version);

const playersToCopy = JSON.parse(JSON.stringify(sidebar[`/${version}/players/`]));
const developToCopy = JSON.parse(JSON.stringify(sidebar[`/${version}/develop/`]));

sidebar[`/${locale}/${version}/players/`] = getLocalisedSidebar(playersToCopy, locale);
sidebar[`/${locale}/${version}/develop/`] = getLocalisedSidebar(developToCopy, locale);

// Delete the original entry.
delete sidebar[entry];
}

for (const version of versionsMet) {
// @ts-ignore
sidebar[`/${version}/players/`] = getLocalisedSidebar(sidebar[`/${version}/players/`], "root");
// @ts-ignore
sidebar[`/${version}/develop/`] = getLocalisedSidebar(sidebar[`/${version}/develop/`], "root");
}

return sidebar;
}

export function getLocalisedSidebar(
sidebar: Fabric.SidebarItem[],
localeCode: string
): Fabric.SidebarItem[] {
const sidebarResolver = getSidebarResolver(localeCode);
const localisedSidebar = JSON.parse(JSON.stringify(sidebar));

for (const item of localisedSidebar) {
if (item.translatable === false) {
continue;
}

item.text = sidebarResolver(item.text);

if (item.link && localeCode !== "root" && item.process !== false) {
item.link = `/${localeCode}${item.link}`;
}

if (item.items) {
item.items = getLocalisedSidebar(item.items, localeCode);
}
}

return localisedSidebar;
}

/**
* Generates translated sidebars for a given root directory and sidebars.
*
* @param sidebars - An object containing sidebars to translate, keyed by URL.
* @param dirname - The root directory to generate translated sidebars for.
* @returns An object containing translated sidebars, keyed by locale URL.
*/
function generateTranslatedSidebars(
export function generateTranslatedSidebars(
sidebars: { [url: string]: Fabric.SidebarItem[] },
dirname: string
): { [localeUrl: string]: Fabric.SidebarItem[] } {
function getLocalisedSidebar(
sidebar: Fabric.SidebarItem[],
localeCode: string
): Fabric.SidebarItem[] {
const sidebarResolver = getSidebarResolver(localeCode);
const localisedSidebar = JSON.parse(JSON.stringify(sidebar));

for (const item of localisedSidebar) {
if (item.translatable === false) {
continue;
}

item.text = sidebarResolver(item.text);

if (item.link && localeCode !== "root" && item.process !== false) {
item.link = `/${localeCode}${item.link}`;
}

if (item.items) {
item.items = getLocalisedSidebar(item.items, localeCode);
}
}

return localisedSidebar;
}

const translatedSidebars: { [key: string]: Fabric.SidebarItem[] } = {};

for (const key of Object.keys(sidebars)) {
Expand Down Expand Up @@ -117,6 +160,7 @@ function generateTranslatedThemeConfig(localeCode: string): Fabric.ThemeConfig {
if (localeCode === "root") {
return null;
} else {
// @ts-ignore
return crowdinOverrides[localeCode] ?? localeCode.split("_")[0];
}
};
Expand Down Expand Up @@ -313,8 +357,8 @@ export function loadLocales(dirname: string): LocaleConfig<Fabric.ThemeConfig> {
language === region.toLowerCase()
? localeNameInEnglish.of(language)!
: localeNameInEnglish.of(language)! +
" - " +
regionNameInEnglish.of(region);
" - " +
regionNameInEnglish.of(region);

const localisedName =
localeNameInLocale.of(language)![0].toUpperCase() +
Expand Down
Empty file.
Loading