diff --git a/CHANGELOG.md b/CHANGELOG.md index fde0018..cc43f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to `markdown-pdf-export`. The format follows ## [Unreleased] +### Changed + +- Studio's **Help** links open the documentation in the browser's language when the site has it: French, German or Spanish (the site is now in all three). Other languages open the English pages. Studio itself stays in English; inside an editor's panel the editor picks the language, as before. + ## [2.11.0] - 2026-11-28 ### Added diff --git a/lib/__tests__/studio-links.test.mjs b/lib/__tests__/studio-links.test.mjs index 118b3c7..cb13fe1 100644 --- a/lib/__tests__/studio-links.test.mjs +++ b/lib/__tests__/studio-links.test.mjs @@ -4,6 +4,7 @@ import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { DOCS_URL } from "../types.mjs"; +import { DOCS_LANGUAGES, localDocsUrl } from "../../studio/model.mjs"; // Studio's help links point at the documentation site built from docs/. A renamed page or heading // must not leave a link to nowhere. @@ -46,6 +47,42 @@ test("every Studio documentation link names an existing page and heading", () => } }); +test("Studio knows every language of the documentation site", () => { + const site = fs + .readdirSync(path.join(root, "docs/.vitepress/theme/i18n")) + .filter((f) => /^[a-z]{2}\.json$/.test(f) && f !== "en.json") + .map((f) => f.slice(0, 2)); + assert.deepEqual([...DOCS_LANGUAGES].sort(), site.sort()); +}); + +test("every Studio documentation link exists in each language, with its section", () => { + for (const language of DOCS_LANGUAGES) { + for (const english of links) { + const url = localDocsUrl(english, language); + assert.ok(url.startsWith(`${DOCS_URL}${language}/`), `${english} in ${language}: ${url}`); + const file = pageFile(url); + assert.ok(fs.existsSync(path.join(root, file)), `${url}: no ${file}`); + const hash = new URL(url).hash.slice(1); + if (!hash) continue; + // Translated headings keep the English anchor: `## Polices {#fonts}`. + const anchors = [...read(file).matchAll(/^#{2,4} .*\{#([^}\s]+)\}\s*$/gm)].map((m) => m[1]); + assert.ok(anchors.includes(hash), `${url}: no heading {#${hash}} in ${file}`); + } + } +}); + +test("Studio's script sends its documentation links through the browser's language", () => { + const app = read("studio/app.mjs"); + // An editor's panel picks the language itself: Studio leaves the links alone there. + assert.match(app, /const docsIn = host \? "" : docsLanguageOf\(/); + // The links in the page, rewritten at start. + assert.match(app, /querySelectorAll\('a\[href\^="https:\/\/markdown-pdf\.matinfo\.tech\/"\]'\)\) \{\s*link\.href = docs\(link\.href\);/); + // Every link the script writes itself goes through docs(). + for (const m of app.matchAll(/(.{0,5})"(https:\/\/markdown-pdf\.matinfo\.tech\/[^"]+)"/g)) { + assert.ok(m[1].endsWith("docs("), `studio/app.mjs: wrap "${m[2]}" in docs()`); + } +}); + test("Studio's documentation links open in a new tab without a referrer", () => { const html = read("studio/index.html"); const anchors = html.match(/]*>/g) ?? []; diff --git a/lib/__tests__/studio-model.test.mjs b/lib/__tests__/studio-model.test.mjs index 0efe232..4cb7183 100644 --- a/lib/__tests__/studio-model.test.mjs +++ b/lib/__tests__/studio-model.test.mjs @@ -4,9 +4,11 @@ import { FIRST_LAYOUT, LAYOUTS, LOGO, + docsLanguageOf, draftToSettings, emptyDraft, lookAccent, + localDocsUrl, lookDefaults, matchLayout, renderText, @@ -270,3 +272,19 @@ test("theme colour: kept as written, and left alone by looks without an accent", assert.deepEqual(draftToSettings(settingsToDraft(clean, ctx), clean, ctx), clean); assert.equal(settingsToDraft({ accent_color: "red; }" }, ctx).accent, "", "invalid value ignored"); }); + +test("documentation links follow the browser's language when the site has it", () => { + assert.equal(docsLanguageOf("fr"), "fr"); + assert.equal(docsLanguageOf("de-CH"), "de"); + assert.equal(docsLanguageOf("es-419"), "es"); + for (const other of ["en-US", "pt-BR", "zh", "", undefined]) assert.equal(docsLanguageOf(other), "", String(other)); + + const site = "https://markdown-pdf.matinfo.tech"; + assert.equal(localDocsUrl(`${site}/advanced/styling#fonts`, "fr"), `${site}/fr/advanced/styling#fonts`); + assert.equal(localDocsUrl(`${site}/reference/header-footer`, "de"), `${site}/de/reference/header-footer`); + assert.equal(localDocsUrl(`${site}/advanced/styling#fonts`, ""), `${site}/advanced/styling#fonts`); + assert.equal(localDocsUrl(`${site}/fr/help`, "es"), `${site}/fr/help`, "already in a language"); + assert.equal(localDocsUrl(`${site}/schema/settings.schema.json`, "fr"), `${site}/schema/settings.schema.json`); + assert.equal(localDocsUrl(`${site}/logo.png`, "fr"), `${site}/logo.png`); + assert.equal(localDocsUrl("https://example.com/help", "fr"), "https://example.com/help"); +}); diff --git a/studio/app.mjs b/studio/app.mjs index bf915ec..c879619 100644 --- a/studio/app.mjs +++ b/studio/app.mjs @@ -5,11 +5,13 @@ import { LOGO, MARGINS, ZONES, + docsLanguageOf, draftToSettings, emptyDraft, lookAccent, lookDefaults, layoutsIn, + localDocsUrl, matchLayout, renderText, settingsToDraft, @@ -55,6 +57,14 @@ const host = window.studioHost ?? null; const words = host?.text ?? {}; +// Help links open the documentation in the browser's language when the site has it. An editor +// showing Studio in its own panel picks the language itself, so Studio leaves its links alone. +const docsIn = host ? "" : docsLanguageOf(navigator.languages?.[0] ?? navigator.language); +const docs = (url) => localDocsUrl(url, docsIn); +for (const link of document.querySelectorAll('a[href^="https://markdown-pdf.matinfo.tech/"]')) { + link.href = docs(link.href); +} + /** * A text in the editor's language, with `{0}`, `{1}`… replaced by the values * given. The English text is a literal here, or marked with toTranslate where @@ -442,7 +452,7 @@ function syncNotices() { } `, el("a", { class: "help", - href: "https://markdown-pdf.matinfo.tech/reference/header-footer", + href: docs("https://markdown-pdf.matinfo.tech/reference/header-footer"), target: "_blank", rel: "noopener noreferrer", text: say("Header and footer reference"), diff --git a/studio/model.mjs b/studio/model.mjs index 6fec193..07b6b6b 100644 --- a/studio/model.mjs +++ b/studio/model.mjs @@ -366,3 +366,34 @@ export function renderText(text, sample = {}) { }; return String(text).replace(/\{(\w+)(?::[^}]*)?\}/g, (all, name) => values[name] ?? all); } + +// ── documentation links ───────────────────────────────────────────────────── + +const DOCS_SITE = "https://markdown-pdf.matinfo.tech"; + +/** + * The languages the documentation site is written in besides English. Each has every English + * page at the same path under its own folder (/fr/advanced/styling), with the same section + * anchors. lib/__tests__/studio-links.test.mjs keeps this list in step with docs/. + */ +export const DOCS_LANGUAGES = ["fr", "de", "es"]; + +/** Files every language shares, at the same path: the settings schema, images, sample PDFs, logo. */ +const DOCS_SHARED = /^\/(?:(?:schema|img|samples)\/|logo\.png$)/; + +/** The documentation's language for a browser language ("fr", "de-CH"…): "fr", or "" for English. */ +export function docsLanguageOf(tag) { + const language = String(tag ?? "").toLowerCase().split("-")[0]; + return DOCS_LANGUAGES.includes(language) ? language : ""; +} + +/** + * A page of the documentation site in `language` ("fr"; "" keeps English). Section anchors stay; + * shared files, pages already in a language and other sites are returned as they are. + */ +export function localDocsUrl(url, language) { + if (!language || !String(url).startsWith(`${DOCS_SITE}/`)) return url; + const rest = url.slice(DOCS_SITE.length); + const inLanguage = DOCS_LANGUAGES.some((l) => new RegExp(`^/${l}(?:[/#?]|$)`).test(rest)); + return inLanguage || DOCS_SHARED.test(rest) ? url : `${DOCS_SITE}/${language}${rest}`; +}