本文整理了 Demius 主题中最常用的两个文章增强组件:内置的链接卡片短代码 linkcard,以及外链跳转中转功能 linkRedirect。照着示例复制即可快速开箱。

链接卡片短代码(linkcard)

主题内置的链接卡片短代码是 linkcard。开启方式如下:

  1. hugo.toml 中打开样式与默认配置:

    [params.linkCard]
      enable = true
      defaultType = "auto"
      openInNewTab = true
      showArticleInfo = true
      showArticleDate = true
      showArticleSummary = true
      showUrl = true
    
  2. 在 Markdown 中直接引用短代码,内部链接会自动读取文章的标题、摘要与日期:

    {{< linkcard url="/posts/helloworld/" >}}
    
  3. 常用可选参数:

    • type: auto / internal / external
    • newtab, showinfo, showdate, showsummary, showurl
    • title, summary, date, ref
  4. 组合示例(自定义外链卡片):

    {{< linkcard
        url="https://gohugo.io/documentation/"
        title="Hugo 官方文档"
        summary="配置、模板语法与部署指南"
        type="external"
        showurl="false"
    >}}
    

外链跳转中转功能

中转页面由 content/go.mdlayout = "go")和 /themes/demius/layouts/go.html 驱动,前端脚本会自动把文章内容区域的外链改写为 /go/?goUrl=...。使用流程:

  1. 配置 params.linkRedirect

    [params.linkRedirect]
      enable = true
      pagePath = "/go/"
      countdown = 3
      showCountdown = true
      showButton = true
      safeMessage = "😃 💡倒计时结束自动跳转,也可自行点击跳转,请注意您的账号和财产安全"
      elementWhitelist = [".post-content"]
      pageWhitelist = [".*"]
      skipPatterns = [
        # "https://example\\.com/.*"
      ]
    
  2. 保持 content/go.md 存在即可,无需在正文中额外操作。若某些链接不想被中转,可在 Markdown 中添加 data-skip-link-redirect 属性,或把匹配规则写进 skipPatterns / safeWhitelist

小提示:如果你需要在特定页面禁用中转,直接把对应的 URL 正则加入 skipPatterns,或者给链接添加 data-skip-link-redirect 即可。