// Collections and pagination, both derived from page front matter.
// Sort page summaries by date (newest first) then title.
///|
fn sort_pages(items : Array[Json]) -> Unit {
items.sort_by(fn(a, b) {
let da = str_field(a, "date", "")
let db = str_field(b, "date", "")
if da != db {
db.compare(da)
} else {
str_field(a, "title", "").compare(str_field(b, "title", ""))
}
})
}
///|
/// Append one page to the bucket named `key`, creating the bucket on first use.
fn push_item(
buckets : Map[String, Array[Json]],
key : String,
item : Json,
) -> Unit {
match buckets.get(key) {
Some(items) => items.push(item)
None => buckets.set(key, [item])
}
}
///|
/// Set one key on a JSON object in place. `@core.set` returns a copy, so it
/// cannot be used to build a tree incrementally.
fn put_field(root : Json, key : String, value : Json) -> Unit {
match root {
Json::Object(map) => map.set(key, value)
_ => ()
}
}
///|
/// Derive `all`, named and tag collections from the page set.
///
/// Pages that drive pagination are excluded, and a page with
/// `skipCollection: true` opts out of every collection.
pub fn build_collections(pages : Array[Page]) -> Json {
let root = Json::object(Map([]))
let all : Array[Json] = []
let named : Map[String, Array[Json]] = Map([])
let tagged : Map[String, Array[Json]] = Map([])
for page in pages {
if page.paginated ||
@core.as_int(@core.get(page.data, "paginate")) > 0 ||
bool_field(page.data, "skipCollection") {
continue
}
all.push(@core.set(page.data, "content", Json::null()))
let collection = str_field(page.data, "collection", "")
if collection != "" && collection != "." {
push_item(named, collection, page.data)
}
for tag in list_field(page.data, "tags") {
push_item(tagged, @core.slugify(tag), page.data)
}
}
sort_pages(all)
put_field(root, "all", Json::array(all))
for name, items in named {
sort_pages(items)
match @core.get(root, name) {
Json::Array(existing) => {
// A collection named `all` merges into the built-in one.
for item in items {
existing.push(item)
}
sort_pages(existing)
}
_ => put_field(root, name, Json::array(items))
}
}
if tagged.length() > 0 {
let tags : Map[String, Json] = Map([])
for slug, items in tagged {
sort_pages(items)
tags.set(slug, Json::array(items))
}
put_field(root, "tags", Json::object(tags))
}
root
}
///|
/// Build the `pagination` object exposed to one page of a sliced collection.
pub fn make_pagination(
items : Array[Json],
number : Int32,
page_size : Int32,
total : Int32,
base_url : String,
page_count : Int32,
) -> Json {
@core.jobj([
("page", @core.jnum(number.to_double())),
("size", @core.jnum(page_size.to_double())),
("total", @core.jnum(total.to_double())),
("total_pages", @core.jnum(page_count.to_double())),
("pages", @core.jarr(items)),
(
"prev",
if number > 1 {
@core.jstr(pagination_url(base_url, number - 1))
} else {
Json::null()
},
),
(
"next",
if number < page_count {
@core.jstr(pagination_url(base_url, number + 1))
} else {
Json::null()
},
),
("first", @core.jbool(number == 1)),
("last", @core.jbool(number == page_count)),
])
}
// URL of one pagination page: page 1 is the archive page itself.
///|
fn pagination_url(base : String, number : Int32) -> String {
if number <= 1 {
base
} else {
"\{base}page/\{number}/"
}
}
///|
/// Keep only the pages of one language, recursively.
///
/// Collections are built from every page, so a multi-language site would mix
/// `en` and `zh` posts in one list. Every page receives its own language view,
/// and pages without a `lang` (single-language sites) are always kept.
fn filter_collection(value : Json, lang : String) -> Json {
if lang == "" {
return value
}
match value {
Json::Array(items) => {
let kept : Array[Json] = []
for item in items {
let item_lang = str_field(item, "lang", "")
if item_lang == "" || item_lang == lang {
kept.push(item)
}
}
Json::array(kept)
}
Json::Object(map) => {
let out : Map[String, Json] = Map([])
for key, item in map {
out.set(key, filter_collection(item, lang))
}
Json::object(out)
}
_ => value
}
}