标签与归档
标签(多对多)、分类(多对一)和按年归档都属于从页面元数据派生出的集合页。先用 @tola/schema 规范化元数据字段,再用 @tola/collection 聚合,最后循环输出集合索引与各子页。
规范化标签
文章里容易写出大小写不一、首尾带空格或重复的标签。建议在 Schema 校验阶段直接清洗,避免同一个标签裂成两个页面:
#import "@tola/schema:0.0.0" as s
#let clean-tags = s.map(
s.array-of(s.non-empty(s.trim(str))),
tags => {
let seen = (:)
let result = ()
for tag in tags {
let key = lower(tag)
if key in seen { continue }
seen.insert(key, true)
result.push(tag)
}
result
},
)
#let post-schema = s.schema((
title: s.non-empty(s.trim(str)),
published: s.date,
category: s.optional(s.non-empty(s.trim(str)), default: "未分类"),
tags: s.optional(clean-tags, default: ()),
draft: s.optional(bool, default: false),
))标签页
一篇博客可以带有多个标签,使用 @tola/collection:0.0.0 的 group-by-keys 按标签一对多聚合。把显示名和 URL slug 分开处理,并用 index-by 检查是否存在两个不同标签落到同一个 slug 的冲突(例如 C++ 和 C 在 ASCII slug 下可能碰撞):
#import "@tola/address:0.0.0": output-of, output-url, slugify
#import "@tola/collection:0.0.0": group-by-keys, index-by
#let by-tag = group-by-keys(ordered-posts, keys: post => post.meta.tags)
#let tag-entries = by-tag.pairs().map(((name, items)) => (
name: name,
slug: slugify(name),
output: output-of("/tags/" + slugify(name) + "/"),
posts: items,
)).sorted(key: entry => (-entry.posts.len(), entry.name))
// 校验没有两个标签映射到同一个 slug
#let _ = index-by(tag-entries, key: entry => entry.slug)
// 标签总览页 /tags/
#document(output-of("/tags/"), format: "html", title: "标签")[
#html.h1[全部标签]
#html.ul[
#for entry in tag-entries {
html.li[
#html.a(href: output-url(entry.output), entry.name)
(#entry.posts.len())
]
}
]
]
// 每个标签的列表页 /tags/<slug>/
#for entry in tag-entries {
document(entry.output, format: "html", title: "标签:" + entry.name)[
#html.h1[标签:#entry.name]
#html.ul[
#for post in entry.posts {
html.li[
#html.a(href: output-url(post.output), post.meta.title)
#html.time(datetime: post.meta.published.rfc3339, post.meta.published.text)
]
}
]
]
}在单篇博客页眉中,直接用 slugify(tag) 链接回对应标签页:
#for tag in page.meta.tags {
html.a(href: output-url(output-of("/tags/" + slugify(tag) + "/")), class: "tag", "#" + tag)
}年份归档
分类和按年份归档都是一对一归类,用 group-by 即可。post.meta.published 是 @tola/schema 解析后的日期记录,包含 year、month、day 等字段:
#import "@tola/collection:0.0.0": group-by
#let by-year = group-by(ordered-posts, key: post => str(post.meta.published.year))
#let years = by-year.keys().sorted().rev()
#document(output-of("/archive/"), format: "html", title: "归档")[
#html.h1[文章归档]
#for year in years [
#html.section[
#html.h2(id: "y" + year, year)
#html.ul[
#for post in by-year.at(year) {
html.li[
#html.time(datetime: post.meta.published.rfc3339, post.meta.published.text)
#html.a(href: output-url(post.output), post.meta.title)
]
}
]
]
]
]相关文章
需要列出“至少共享一个标签”或“同时具备多个指定标签”的文章时,使用 @tola/collection:0.0.0 的 select-members:
#import "@tola/collection:0.0.0": select-members
#let related-posts(current, all-posts, limit: 5) = {
if current.meta.tags.len() == 0 { return () }
select-members(
all-posts.filter(p => p.output != current.output),
current.meta.tags,
keys: p => p.meta.tags,
match: "any",
).slice(0, calc.min(limit, all-posts.len()))
}match 支持 "any"(命中任意标签)和 "all"(同时包含全部指定标签)。若需按共同标签数量排序,可以先为每篇候选文章计算交集大小再排序截断。