@tola/head

生成 HTML <head> 元数据标签:标题、描述、canonical 链接、Open Graph 与 Twitter 卡片。

导入

typst
#import "@tola/head:0.0.0": *

每个函数均返回 HTML 元素数组,可用 + 拼接后通过 .join() 传给 html.head(...)。

导出成员

head-metadata

typc
let head-metadata(
  site: dictionary,
  charset: none | string = "utf-8",
  description: auto | none | content | string = auto,
  title: auto | none | content | string = auto,
  viewport: none | string = "width=device-width, initial-scale=1",
) = array;

生成基础的 <meta charset>、viewport、<title> 与 <meta name="description"> 标签。

位置参数

  • site dictionary 必填:提供默认标题与描述的站点字典(通常传入 @tola/site 的 site)。

命名参数

  • title auto | none | content | string 默认值: auto:页面标题。auto 取 site.title;none 或空白字符串省略 <title>。
  • description auto | none | content | string 默认值: auto:页面描述。auto 取 site.description;none 或空白字符串省略描述标签。
  • charset none | string 默认值: "utf-8":字符编码,默认 "utf-8";传 none 或空白字符串省略。
  • viewport none | string 默认值: "width=device-width, initial-scale=1":viewport 内容,默认 "width=device-width, initial-scale=1";传 none 或空白字符串省略。

组合完整的 HTML `<head>` 元数据

typst
#import "@tola/head:0.0.0": canonical, head-metadata, open-graph, twitter-card
#import "@tola/site:0.0.0": site

#let url = "https://example.com/guide/"
#let cover = (url: "https://example.com/cover.png", alt: "指南封面")
#let tags = (
  head-metadata(site, title: "指南", description: "快速上手站点配置。")
  + canonical(url)
  + open-graph(title: "指南", url: url, images: (cover,))
  + twitter-card(card: "summary_large_image", title: "指南", image: cover)
)

#document("guide/index.html", title: "指南")[
  #html.html[
    #html.head(tags.join())
    #html.body[指南正文]
  ]
]

canonical

typc
let canonical(
  href: string,
) = array;

生成页面的 <link rel="canonical"> 标签。

位置参数

  • href string 必填:页面的绝对 http:// 或 https:// URL。

open-graph

typc
let open-graph(
  description: none | string = none,
  images: none | array = none,
  kind: string = "website",
  locale: none | string = none,
  site-name: none | string = none,
  title: string = auto,
  url: string = auto,
) = array;

生成社交分享预览所需的 Open Graph <meta property="og:*"> 标签。

命名参数

  • title string 默认值: auto:非空页面标题。
  • kind string 默认值: "website":Open Graph 类型,例如 "website"(默认)或 "article"。
  • url string 默认值: auto:页面的绝对 HTTP(S) URL。
  • images none | array 默认值: none:传 none 省略图片标签,或传入非空的 (url:, alt:) 图片字典数组。
  • description none | string 默认值: none:可选摘要,非空时写入。
  • site-name none | string 默认值: none:可选站点名,非空时写入。
  • locale none | string 默认值: none:可选区域标签,非空时写入。

twitter-card

typc
let twitter-card(
  card: string = auto,
  creator: none | string = none,
  description: none | string = none,
  handle: none | string = none,
  image: none | dictionary = none,
  title: string = auto,
) = array;

生成 Twitter 分享卡片所需的 <meta name="twitter:*"> 标签。

命名参数

  • card string 默认值: auto:卡片类型:"summary" 或 "summary_large_image"。
  • title string 默认值: auto:非空卡片标题。
  • image none | dictionary 默认值: none:可选 (url:, alt:) 字典;当 card 为 "summary_large_image" 时必填。
  • description none | string 默认值: none:可选描述,非空时写入。
  • handle none | string 默认值: none:可选站点账号(twitter:site)。
  • creator none | string 默认值: none:可选作者账号(twitter:creator)。

站内搜索

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

输入关键词开始搜索。