48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
type ArchivePathArgs = {
|
|
/** Archive page's slug in this locale, e.g. 'artykuly'. */
|
|
archiveSlug: string
|
|
locale: string
|
|
/** 1-based. Page 1 is omitted from the URL. */
|
|
page?: number
|
|
}
|
|
|
|
/**
|
|
* Path to an archive listing: /pl/artykuly, /pl/artykuly?page=2.
|
|
*
|
|
* Pagination goes in the query string rather than the path. A path segment
|
|
* (/pl/artykuly/2) would be ambiguous with an entry slugged "2", and the
|
|
* alternative (/pl/artykuly/strona/2) drags in yet another localized segment to
|
|
* configure and translate. Google has understood ?page= for years, and
|
|
* rel=next/prev — the reason people used to prefer path segments — was retired.
|
|
*/
|
|
export function buildArchivePath({ archiveSlug, locale, page = 1 }: ArchivePathArgs): string {
|
|
const base = `/${locale}/${archiveSlug}`
|
|
return page > 1 ? `${base}?page=${page}` : base
|
|
}
|
|
|
|
type EntryPathArgs = {
|
|
/** Archive page's slug in this locale, e.g. 'artykuly'. */
|
|
archiveSlug: string
|
|
/** Entry's slug in this locale, e.g. 'moj-post'. */
|
|
entrySlug: string
|
|
locale: string
|
|
}
|
|
|
|
/** Path to a single entry: /pl/artykuly/moj-post. */
|
|
export function buildEntryPath({ archiveSlug, entrySlug, locale }: EntryPathArgs): string {
|
|
return `/${locale}/${archiveSlug}/${entrySlug}`
|
|
}
|
|
|
|
/**
|
|
* Reads a page number out of a query parameter.
|
|
*
|
|
* Anything that isn't a positive integer is page 1 — `?page=abc`, `?page=-5`,
|
|
* `?page=1.5` and a repeated `?page=1&page=2` all have to resolve to something,
|
|
* and silently showing the first page beats a 500 on a URL a crawler invented.
|
|
*/
|
|
export function parsePageParam(value: string | string[] | undefined): number {
|
|
const raw = Array.isArray(value) ? value[0] : value
|
|
const n = Number(raw)
|
|
return Number.isInteger(n) && n > 0 ? n : 1
|
|
}
|