More things
This commit is contained in:
@@ -35,6 +35,22 @@ export function languageName(code: string): string {
|
||||
return LANGUAGES.find((l) => l.code === code)?.name ?? code.toUpperCase()
|
||||
}
|
||||
|
||||
/**
|
||||
* Which language version to open an article in when the address asks for
|
||||
* none: the reader's own language when the article speaks it, else English
|
||||
* as the wiki's common tongue, else null — nothing to pick, so whatever
|
||||
* version already stands for the article answers.
|
||||
*/
|
||||
export function preferredLanguage(
|
||||
available: readonly (string | undefined)[],
|
||||
reader: string
|
||||
): string | null {
|
||||
const codes = new Set(available.map((c) => (c ?? '').trim().toLowerCase()))
|
||||
const wanted = reader.trim().toLowerCase()
|
||||
if (codes.has(wanted)) return wanted
|
||||
return codes.has(DEFAULT_LANGUAGE) ? DEFAULT_LANGUAGE : null
|
||||
}
|
||||
|
||||
/** The first language no version of an article has been written in yet. */
|
||||
export function nextFreeLanguage(taken: readonly string[]): string {
|
||||
const takenCodes = taken.map((c) => c.trim().toLowerCase())
|
||||
|
||||
@@ -3,15 +3,21 @@
|
||||
An article route is a page of the open book drawn by ArticleBookStack —
|
||||
pages laid out side by side, older ones parked with their spines showing —
|
||||
while the article edit page and the new-article page remain plain,
|
||||
full-height pages. An article lives at /wiki/<slug>; one of its language
|
||||
versions at /wiki/<lang>/<slug>, e.g. /wiki/fr/bayes-theorem. */
|
||||
full-height pages. Every language version of an article has an address of
|
||||
its own — /wiki/<lang>/<identity>, English no different from the rest, so
|
||||
/wiki/en/bayes-theorem and /wiki/fr/bayes-theorem sit side by side. The one
|
||||
short form, /wiki/<slug> (what a wiki link to [[Bayes Theorem]] writes, and
|
||||
what an old bookmark holds), names no language: the open-article middleware
|
||||
reads which document the slug speaks and turns the address into that
|
||||
version's own. */
|
||||
|
||||
const READER_PATH = /^\/wiki\/([^/]+?)\/?$/
|
||||
const VERSION_READER_PATH = /^\/wiki\/([a-z]{2})\/([^/]+?)\/?$/
|
||||
const WIKI_SUB_PATH = /^\/wiki\/[^/]+\/[^/]+/
|
||||
|
||||
/** What a reader address names: the article's identity, and which language
|
||||
* version the address asks for (null for the article's own version). */
|
||||
* version the address asks for (null for the short form, which asks for
|
||||
* whichever version the slug itself speaks). */
|
||||
export interface ReaderLocation {
|
||||
identity: string
|
||||
lang: string | null
|
||||
@@ -37,12 +43,15 @@ export function wikiSlugFromPath(path: string): string | null {
|
||||
}
|
||||
|
||||
/**
|
||||
* The address of one language version: the canonical document lives at the
|
||||
* article's own URL, translations at /wiki/<lang>/<identity>.
|
||||
* The address one language version lives at: /wiki/<lang>/<identity>, the
|
||||
* article's identity being the canonical version's slug — so every version of
|
||||
* one article shares the last part of its address and differs only by the
|
||||
* language in front. Documents carry their own stored slug, which no address
|
||||
* needs: the wiki finds a version by (identity, language).
|
||||
*/
|
||||
export function versionHref(identity: string, version: { language?: string; slug: string }): string {
|
||||
if (version.slug === identity) return `/wiki/${encodeURIComponent(identity)}`
|
||||
return `/wiki/${encodeURIComponent(version.language ?? 'en')}/${encodeURIComponent(identity)}`
|
||||
export function versionHref(identity: string, language?: string): string {
|
||||
const code = language?.trim().toLowerCase() || DEFAULT_LANGUAGE
|
||||
return `/wiki/${encodeURIComponent(code)}/${encodeURIComponent(identity)}`
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user