SEO 与订阅源

规范地址(canonical)、社交分享卡片、订阅源和站点地图都依赖站点的公开绝对地址。先在 tola.toml 的 [site] 中配置 origin:

toml
[site]
title = "我的站点"
origin = "https://example.com"
base-path = "/"

当 site.origin 未设置(例如早期草稿或仅本地预览)时,跳过输出 canonical、订阅源和 sitemap.xml,避免生成无效的相对 URL。

元标签

@tola/head:0.0.0 的四个辅助函数都返回 HTML 元素数组,拼接后直接放进 html.head:

typst
#import "@tola/address:0.0.0": output-url
#import "@tola/document:0.0.0": plain-text
#import "@tola/head:0.0.0": canonical, head-metadata, open-graph, twitter-card
#import "@tola/site:0.0.0": site

#let render-head(page, cover-url: none) = {
  let page-title = plain-text(page.meta.title)
  let browser-title = if page.kind == "home" {
    plain-text(site.title)
  } else {
    page-title + " | " + plain-text(site.title)
  }

  let tags = head-metadata(
    site,
    title: browser-title,
    description: page.meta.description,
  )

  if site.origin != none {
    let url = output-url(page.output, origin: site.origin)
    let images = if cover-url != none { (cover-url,) } else { () }
    tags += canonical(url)
    tags += open-graph(
      title: page-title,
      kind: if page.kind == "post" { "article" } else { "website" },
      url: url,
      images: images,
      description: page.meta.description,
      site-name: plain-text(site.title),
      locale: page.locale.tag,
    )
    tags += twitter-card(
      card: if cover-url != none { "summary_large_image" } else { "summary" },
      title: page-title,
      image: cover-url,
      description: page.meta.description,
    )
  }

  tags
}

页面标题如果是富文本 content(例如含行内代码或公式),传给 <title> 和 <meta> 前先用 @tola/document 的 plain-text(...) 提取纯文本。open-graph 与 twitter-card 引用的封面图需要是带 origin 的绝对 URL,且已通过 [assets]、@tola/image 或钩子实际产出到发布目录。

订阅源

在 site.typ 末尾复用已排序、已过滤草稿的 posts 列表调用 @tola/feed:0.0.0:

typst
#import "@tola/document:0.0.0": plain-text
#import "@tola/feed:0.0.0": feed
#import "@tola/site:0.0.0": site

#if site.origin != none and posts.len() > 0 {
  feed(
    output: "feed.xml",
    format: "rss", // 支持 "rss"、"atom"、"json"
    language: site.language.tag,
    title: plain-text(site.title),
    description: "站点更新记录",
    entries: posts.map(post => (
      id: post.meta.at("feed-id", default: "urn:example:" + post.id),
      target: post.output,
      title: plain-text(post.meta.title),
      published: post.meta.published.rfc3339,
      updated: if post.meta.updated != none { post.meta.updated.rfc3339 } else { none },
      summary: post.meta.description,
      content: (document: post.output, id: "article"),
    )),
  )
}

并在 <head> 中加入自动发现标签,让订阅客户端能直接识别地址:

typst
#html.link(
  rel: "alternate",
  type: "application/rss+xml",
  title: plain-text(site.title),
  href: output-url("feed.xml"),
)

订阅条目的 id 应与路由解耦(在元数据里记录固定 feed-id,或用 @tola/uuid 的 v5 基于不可变标识生成),避免调整文章路径后阅读器重复推送。省略 content 只输出摘要;传入 (document: post.output, id: "article") 会抽取对应 HTML 中 id="article" 的元素子树作为正文,排除页眉与导航栏。若条目指向长页面中的某一个小节,target 可写成 (output: "changelog/index.html", fragment: "v0-8-2")。

站点地图

@tola/sitemap:0.0.0 输出标准的 sitemap.xml,只包含整页文档(排除 404.html、分页中间页或显式标记 sitemap: false 的页面):

typst
#import "@tola/sitemap:0.0.0": sitemap
#import "@tola/site:0.0.0": site

#if site.origin != none {
  let indexable = pages.filter(p => p.meta.at("sitemap", default: true))
  sitemap(
    output: "sitemap.xml",
    targets: indexable.map(p => (
      target: p.output,
      lastmod: if p.meta.at("updated", default: none) != none {
        let u = p.meta.updated
        if type(u) == dictionary { u.rfc3339 } else { u }
      } else { none },
    )),
  )
}

站内搜索

可以用上下方向键选择结果。

输入关键词开始搜索。