Tags & archives

Tags (many-to-many), categories (many-to-one), and yearly archives are computed directly from page metadata. Normalize fields with @tola/schema, aggregate with @tola/collection, and emit the index and term pages in loops.

Normalize tags

Inconsistent casing, leading whitespace, or duplicates in metadata split one tag across two pages. Clean them inside the schema:

typst
#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: "Uncategorized"),
  tags: s.optional(clean-tags, default: ()),
  draft: s.optional(bool, default: false),
))

Tag pages

Use group-by-keys from @tola/collection:0.0.0 for many-to-many grouping. Keep display names separate from URL slugs and run index-by over the slugs so collisions (such as C++ and C) fail the build instead of colliding silently:

typst
#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))

// Fail if two tags collapse to the same slug
#let _ = index-by(tag-entries, key: entry => entry.slug)

// Tag index /tags/
#document(output-of("/tags/"), format: "html", title: "Tags")[
  #html.h1[All tags]
  #html.ul[
    #for entry in tag-entries {
      html.li[
        #html.a(href: output-url(entry.output), entry.name)
        (#entry.posts.len())
      ]
    }
  ]
]

// Per-tag pages /tags/<slug>/
#for entry in tag-entries {
  document(entry.output, format: "html", title: "Tag: " + entry.name)[
    #html.h1[Tag: #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)
        ]
      }
    ]
  ]
}

Link from a post header back to each tag page with slugify(tag):

typst
#for tag in page.meta.tags {
  html.a(href: output-url(output-of("/tags/" + slugify(tag) + "/")), class: "tag", "#" + tag)
}

Yearly archives

Categories and year archives are one-to-one groupings via group-by. Date values parsed by @tola/schema expose .year, .month, and .day:

typst
#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: "Archive")[
  #html.h1[Archive]
  #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)
          ]
        }
      ]
    ]
  ]
]

Use select-members from @tola/collection:0.0.0 to find posts sharing any ("any") or all ("all") tags with the current post:

typst
#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()))
}

Sort candidates by overlap count first if you want posts with more shared tags ranked higher.

Search this site

Use the arrow keys to move through results.

Type to search.