chore(readme): fix language switcher links (closes #7246)

This commit is contained in:
Elian Doran 2025-12-08 12:43:00 +02:00
parent 8920e6e448
commit 5c8df540db
No known key found for this signature in database
4 changed files with 2007 additions and 12 deletions

View File

@ -17,7 +17,8 @@
[![RelativeCI](https://badges.relative-ci.com/badges/Di5q7dz9daNDZ9UXi0Bp?branch=develop)](https://app.relative-ci.com/projects/Di5q7dz9daNDZ9UXi0Bp) [![Translation status](https://hosted.weblate.org/widget/trilium/svg-badge.svg)](https://hosted.weblate.org/engage/trilium/)
<!-- translate:off -->
[English](./README.md) | [Chinese (Simplified)](./docs/README-ZH_CN.md) | [Chinese (Traditional)](./docs/README-ZH_TW.md) | [Russian](./docs/README-ru.md) | [Japanese](./docs/README-ja.md) | [Italian](./docs/README-it.md) | [Spanish](./docs/README-es.md)
<!-- LANGUAGE SWITCHER -->
[English](./docs/README.md) | [Chinese (Simplified Han script)](./docs/README-ZH_CN.md) | [Chinese (Traditional Han script)](./docs/README-ZH_TW.md) | [Spanish](./docs/README-es.md) | [Italian](./docs/README-it.md) | [Japanese](./docs/README-ja.md) | [Romanian](./docs/README-ro.md) | [German](./docs/README-de.md) | [Greek](./docs/README-el.md) | [French](./docs/README-fr.md)
<!-- translate:on -->
Trilium Notes is a free and open-source, cross-platform hierarchical note taking application with focus on building large personal knowledge bases.

3
docs/README.md vendored
View File

@ -17,7 +17,8 @@
[![RelativeCI](https://badges.relative-ci.com/badges/Di5q7dz9daNDZ9UXi0Bp?branch=develop)](https://app.relative-ci.com/projects/Di5q7dz9daNDZ9UXi0Bp) [![Translation status](https://hosted.weblate.org/widget/trilium/svg-badge.svg)](https://hosted.weblate.org/engage/trilium/)
<!-- translate:off -->
[English](../README.md) | [Chinese (Simplified)](./README-ZH_CN.md) | [Chinese (Traditional)](./README-ZH_TW.md) | [Russian](./README-ru.md) | [Japanese](./README-ja.md) | [Italian](./README-it.md) | [Spanish](./README-es.md)
<!-- LANGUAGE SWITCHER -->
[English](../README.md) | [Chinese (Simplified Han script)](./README-ZH_CN.md) | [Chinese (Traditional Han script)](./README-ZH_TW.md) | [Spanish](./README-es.md) | [Italian](./README-it.md) | [Japanese](./README-ja.md) | [Romanian](./README-ro.md) | [German](./README-de.md) | [Greek](./README-el.md) | [French](./README-fr.md)
<!-- translate:on -->
Trilium Notes is a free and open-source, cross-platform hierarchical note taking application with focus on building large personal knowledge bases.

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,8 @@
import { readFile, writeFile } from "fs/promises";
import { readFile, stat, writeFile, } from "fs/promises";
import { join } from "path";
const rootDir = join(__dirname, "../..");
const scriptDir = __dirname;
const rootDir = join(scriptDir, "../..");
const docsDir = join(rootDir, "docs");
/**
@ -11,21 +12,69 @@ const docsDir = join(rootDir, "docs");
* The README in the repo root remains the true base file, but it's a two-step process which requires the execution of this script.
*/
async function handleBaseFile() {
}
async function getLanguageStats() {
const cacheFile = join(scriptDir, ".language-stats.json");
// Try to read from the cache.
try {
const cacheStats = await stat(cacheFile);
const now = new Date();
const oneDay = 24 * 60 * 60 * 1000; // milliseconds
if (cacheStats.mtimeMs < now.getTime() + oneDay) {
console.log("Reading language stats from cache.");
return JSON.parse(await readFile(cacheFile, "utf-8"));
}
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}
}
// Make the request
console.log("Reading language stats from Weblate API.");
const request = await fetch("https://hosted.weblate.org/api/components/trilium/readme/translations/");
const stats = JSON.parse(await request.text());
// Update the cache
await writeFile(cacheFile, JSON.stringify(stats, null, 4));
return stats;
}
async function rewriteLanguageBar(readme: string) {
// Filter languages by their availability.
const languageStats = await getLanguageStats();
const languagesWithCoverage = languageStats.results.filter(language => language.translated_percent > 75);
const languageLinks = languagesWithCoverage.map(language => `[${language.language.name}](./${language.filename})`)
readme = readme.replace(
/<!-- LANGUAGE SWITCHER -->\r?\n.*$/m,
`<!-- LANGUAGE SWITCHER -->\n${languageLinks.join(" | ")}`);
return readme;
}
function rewriteRelativeLinks(readme: string) {
readme = readme.replaceAll("./docs/", "./");
readme = readme.replaceAll("./README.md", "../README.md");
return readme;
}
async function main() {
// Read the README at root level.
const readmePath = join(rootDir, "README.md");
let readme = await readFile(readmePath, "utf-8");
// Rewrite relative links.
readme = readme.replaceAll("./docs/", "./");
readme = readme.replaceAll("./README.md", "../README.md");
// Update the README at root level.
readme = await rewriteLanguageBar(readme);
await writeFile(readmePath, readme);
// Copy it into docs.
// Rewrite relative links for docs/README.md.
readme = rewriteRelativeLinks(readme);
const outputPath = join(docsDir, "README.md");
await writeFile(outputPath, readme);
}
async function main() {
await handleBaseFile();
}
main();