init commit for iPAL-kit plugin
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import type { Field, RelationshipField } from 'payload'
|
||||
|
||||
import type { ContentOption } from './types.js'
|
||||
|
||||
import { archiveFieldName } from './types.js'
|
||||
|
||||
/**
|
||||
* Builds one relationship field per content collection, pointing at the
|
||||
* client's Pages collection.
|
||||
*
|
||||
* Sits alongside the system-page roles (homepage, privacy policy) because it's
|
||||
* the same idea: a page referenced by function rather than by slug. The
|
||||
* assignment is locale-agnostic — one page document — while the resulting path
|
||||
* is locale-aware, since the page's slug is localized. Assign the "Artykuły"
|
||||
* page once and you get /pl/artykuly and /en/articles from its own slugs.
|
||||
*/
|
||||
export function buildArchiveFields(content: ContentOption, pagesSlug: string): Field[] {
|
||||
return content.collections.map((collection): RelationshipField => {
|
||||
const label = collection.label ?? collection.slug
|
||||
|
||||
return {
|
||||
name: archiveFieldName(collection.slug),
|
||||
type: 'relationship',
|
||||
admin: {
|
||||
description: `Page listing ${label}. Its slug becomes the URL segment for entries (e.g. /pl/artykuly/moj-wpis).`,
|
||||
},
|
||||
label: `${label} — archive page`,
|
||||
maxDepth: 1,
|
||||
relationTo: pagesSlug,
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
export type ArchiveEntries<T = Record<string, unknown>> = {
|
||||
docs: T[]
|
||||
hasNextPage: boolean
|
||||
hasPrevPage: boolean
|
||||
/** 1-based. */
|
||||
page: number
|
||||
totalDocs: number
|
||||
totalPages: number
|
||||
}
|
||||
|
||||
type GetArchiveEntriesArgs = {
|
||||
/** Collection to list, e.g. 'posts'. */
|
||||
collection: string
|
||||
/** Relationship depth. Defaults to 1 — enough for a cover image. */
|
||||
depth?: number
|
||||
locale: string
|
||||
/** 1-based. Values below 1 are clamped. */
|
||||
page?: number
|
||||
payload: BasePayload
|
||||
/** Defaults to 10. */
|
||||
perPage?: number
|
||||
/** Payload sort string. Defaults to newest first. */
|
||||
sort?: string
|
||||
/** Extra constraints merged into the query, e.g. { category: { equals: id } }. */
|
||||
where?: Record<string, unknown>
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches one page of a collection's entries for an archive listing.
|
||||
*
|
||||
* Only published documents are returned when the collection has drafts enabled;
|
||||
* Payload's `where` on `_status` is left to the caller, since a collection
|
||||
* without drafts has no such field.
|
||||
*
|
||||
* Returns pagination facts rather than markup — the listing itself belongs to
|
||||
* the client (as a block on the archive page, most likely), because how a list
|
||||
* of posts should look isn't something a plugin can decide.
|
||||
*/
|
||||
export async function getArchiveEntries<T = Record<string, unknown>>({
|
||||
collection,
|
||||
depth = 1,
|
||||
locale,
|
||||
page = 1,
|
||||
payload,
|
||||
perPage = 10,
|
||||
sort = '-createdAt',
|
||||
where,
|
||||
}: GetArchiveEntriesArgs): Promise<ArchiveEntries<T>> {
|
||||
const result = await payload.find({
|
||||
collection: collection as never,
|
||||
depth,
|
||||
limit: perPage,
|
||||
locale: locale as never,
|
||||
page: Math.max(1, page),
|
||||
sort,
|
||||
...(where ? { where: where as never } : {}),
|
||||
})
|
||||
|
||||
return {
|
||||
docs: result.docs as T[],
|
||||
hasNextPage: result.hasNextPage,
|
||||
hasPrevPage: result.hasPrevPage,
|
||||
page: result.page ?? 1,
|
||||
totalDocs: result.totalDocs,
|
||||
totalPages: result.totalPages,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export { buildArchiveFields } from './archiveFields.js'
|
||||
export { getArchiveEntries } from './getArchiveEntries.js'
|
||||
export type { ArchiveEntries } from './getArchiveEntries.js'
|
||||
export { buildArchivePath, buildEntryPath, parsePageParam } from './paths.js'
|
||||
export { resolveRoute } from './resolveRoute.js'
|
||||
export type { ResolvedRoute } from './resolveRoute.js'
|
||||
export type { ContentCollectionOption, ContentOption } from './types.js'
|
||||
export { archiveFieldName } from './types.js'
|
||||
@@ -0,0 +1,47 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import type { ArchiveEntries } from './getArchiveEntries.js'
|
||||
import type { ContentOption } from './types.js'
|
||||
|
||||
import { getArchiveEntries } from './getArchiveEntries.js'
|
||||
import { archiveFieldName } from './types.js'
|
||||
|
||||
type ArchivePage = { id: number | string; slug?: unknown }
|
||||
|
||||
export type ResolvedRoute =
|
||||
/** Locale root — the page assigned as Homepage in System Pages. */
|
||||
| {
|
||||
/** The archive page this entry lives under — its slug is the URL prefix. */
|
||||
archive: Record<string, unknown>
|
||||
collection: string
|
||||
doc: Record<string, unknown>
|
||||
type: 'entry'
|
||||
}
|
||||
/** An ordinary page. */
|
||||
| {
|
||||
collection: string
|
||||
doc: Record<string, unknown>
|
||||
/** Present only when resolved with `withEntries` — metadata doesn't need them. */
|
||||
entries?: ArchiveEntries
|
||||
/** 1-based, from ?page=. */
|
||||
page: number
|
||||
perPage: number
|
||||
type: 'archive'
|
||||
}
|
||||
/** A collection's archive page, e.g. /pl/artykuly. */
|
||||
| { doc: Record<string, unknown>; type: 'home' }
|
||||
/** A single entry, e.g. /pl/artykuly/moj-post. */
|
||||
| { doc: Record<string, unknown>; type: 'page' }
|
||||
|
||||
type ResolveRouteArgs = {
|
||||
content?: ContentOption
|
||||
locale: string
|
||||
/** Page number from the query string (?page=2). Defaults to 1. */
|
||||
page?: number
|
||||
/** Client's Pages collection slug. Defaults to 'pages'. */
|
||||
pagesSlug?: string
|
||||
payload: BasePayload
|
||||
/** Route segments after the locale, e.g. ['artykuly', 'moj-post']. */
|
||||
segments?: string[]
|
||||
/** SiteSettings global slug. Defaults to 'site-settings'. */
|
||||
settingsSlug?: string
|
||||
/**
|
||||
* Fetch the archive's entries too. The page component wants them; metadata
|
||||
* generation doesn't, and would pay for a query it throws away.
|
||||
*/
|
||||
withEntries?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Works out what a URL points at: the home page, an ordinary page, a
|
||||
* collection's archive, or a single entry.
|
||||
*
|
||||
* The trick is that archive prefixes aren't configured anywhere — they're the
|
||||
* slug of whichever page an editor assigned as that collection's archive. So
|
||||
* /pl/artykuly and /en/articles come from one assignment, and renaming the page
|
||||
* moves the whole section.
|
||||
*
|
||||
* Resolution order matters: a first segment matching an archive's slug wins
|
||||
* over an ordinary page of the same name, because an archive *is* a page and
|
||||
* would otherwise shadow its own entries.
|
||||
*
|
||||
* Depth beyond {archive}/{entry} isn't supported — categories in the path would
|
||||
* make canonical and hreflang ambiguous (the same entry reachable under several
|
||||
* URLs).
|
||||
*/
|
||||
export async function resolveRoute({
|
||||
content,
|
||||
locale,
|
||||
page = 1,
|
||||
pagesSlug = 'pages',
|
||||
payload,
|
||||
segments,
|
||||
settingsSlug = 'site-settings',
|
||||
withEntries = false,
|
||||
}: ResolveRouteArgs): Promise<null | ResolvedRoute> {
|
||||
const settings = (await payload.findGlobal({
|
||||
slug: settingsSlug,
|
||||
depth: 1,
|
||||
locale: locale as never,
|
||||
})) as Record<string, unknown>
|
||||
|
||||
// Locale root → Homepage from System Pages.
|
||||
if (!segments?.length) {
|
||||
const homepage = settings.homepage
|
||||
if (homepage && typeof homepage === 'object') {
|
||||
return { type: 'home', doc: homepage as Record<string, unknown> }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Which archive (if any) does the first segment name?
|
||||
const [first, ...rest] = segments
|
||||
|
||||
for (const collection of content?.collections ?? []) {
|
||||
const archive = settings[archiveFieldName(collection.slug)]
|
||||
if (!archive || typeof archive !== 'object') {continue}
|
||||
|
||||
const archiveDoc = archive as ArchivePage
|
||||
if (archiveDoc.slug !== first) {continue}
|
||||
|
||||
// /pl/artykuly → the archive page itself.
|
||||
if (rest.length === 0) {
|
||||
const perPage = collection.perPage ?? 10
|
||||
return {
|
||||
type: 'archive',
|
||||
collection: collection.slug,
|
||||
doc: archiveDoc as Record<string, unknown>,
|
||||
page,
|
||||
perPage,
|
||||
...(withEntries
|
||||
? {
|
||||
entries: await getArchiveEntries({
|
||||
collection: collection.slug,
|
||||
locale,
|
||||
page,
|
||||
payload,
|
||||
perPage,
|
||||
}),
|
||||
}
|
||||
: {}),
|
||||
}
|
||||
}
|
||||
|
||||
// /pl/artykuly/moj-post → an entry.
|
||||
if (rest.length === 1) {
|
||||
const found = await payload.find({
|
||||
collection: collection.slug as never,
|
||||
depth: 2,
|
||||
limit: 1,
|
||||
locale: locale as never,
|
||||
where: { slug: { equals: rest[0] } },
|
||||
})
|
||||
const entry = found.docs[0]
|
||||
if (!entry) {return null}
|
||||
|
||||
return {
|
||||
type: 'entry',
|
||||
archive: archiveDoc as Record<string, unknown>,
|
||||
collection: collection.slug,
|
||||
doc: entry as Record<string, unknown>,
|
||||
}
|
||||
}
|
||||
|
||||
// Deeper than {archive}/{entry}.
|
||||
return null
|
||||
}
|
||||
|
||||
// An ordinary page. Joined, so nested slugs ('a/b') keep working.
|
||||
const found = await payload.find({
|
||||
collection: pagesSlug as never,
|
||||
depth: 2,
|
||||
limit: 1,
|
||||
locale: locale as never,
|
||||
where: { slug: { equals: segments.join('/') } },
|
||||
})
|
||||
const doc = found.docs[0]
|
||||
if (!doc) {return null}
|
||||
|
||||
return { type: 'page', doc: doc as Record<string, unknown> }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* A collection whose entries live under an archive page, e.g. blog posts at
|
||||
* /pl/artykuly/moj-post.
|
||||
*
|
||||
* The collection itself belongs to the client — Payload keeps its schema in
|
||||
* code, so adding one is always a code change. What this option adds is the
|
||||
* routing: the plugin exposes an "archive page" assignment in SiteSettings, and
|
||||
* whichever page an editor picks becomes the URL segment. Rename that page from
|
||||
* "Artykuły" to "Wpisy" and the path follows, per locale, with no deploy.
|
||||
*/
|
||||
export type ContentCollectionOption = {
|
||||
/** Slug of the client's collection, e.g. 'posts'. */
|
||||
slug: string
|
||||
/** Admin label for the archive assignment. Defaults to the slug. */
|
||||
label?: string
|
||||
/** Entries per page in listings. Defaults to 10. */
|
||||
perPage?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugin option wiring archive-backed collections into routing.
|
||||
*/
|
||||
export type ContentOption = {
|
||||
collections: ContentCollectionOption[]
|
||||
}
|
||||
|
||||
/**
|
||||
* Field name holding a collection's archive page in SiteSettings.
|
||||
* `posts` → `postsArchive`.
|
||||
*/
|
||||
export function archiveFieldName(collectionSlug: string): string {
|
||||
return `${collectionSlug}Archive`
|
||||
}
|
||||
Reference in New Issue
Block a user