開發程式代碼 #
outdated-warning.html #
layouts/partials/outdated-warning.html
{{/* 站點預設(可在 params 設,缺省也可) */}}
{{ $siteEnabled := site.Params.outdatedWarning.enabledByDefault | default false }}
{{ $siteDays := site.Params.outdatedWarning.days | default 365 }}
{{ $useLastmod := site.Params.outdatedWarning.useLastmod | default true }}
{{/* 文章級開關:front matter > 站點預設 */}}
{{ $hasOutdated := isset .Params "outdated" }}
{{ $enabled := cond $hasOutdated (.Param "outdated") $siteEnabled }}
{{/* 文章級門檻:front matter > 站點預設 */}}
{{ $limit := .Param "outdated_days" }}
{{ if not $limit }}{{ $limit = $siteDays }}{{ end }}
{{/* 計算基準日:優先 lastmod(可由 useLastmod 關閉) */}}
{{ $base := .Date }}
{{ if $useLastmod }}
{{ if gt .Lastmod.Unix 0 }}{{ $base = .Lastmod }}{{ end }}
{{ end }}
{{/* 相差天數(整數) */}}
{{ $days := div (sub now.Unix $base.Unix) 86400 }}
{{ if and $enabled (gt $days $limit) }}
<div class="arx-outdated" role="note" aria-live="polite">
<strong>提醒:</strong>
本篇文章從發布到現在已經隔了 <b>{{ $days }}</b> 天,內容可能過期,請自行甄別喔 👉👈
</div>
{{ end }}
custom.css #
assets/css/custom.css
/* Outdated banner */
.arx-outdated{
margin: 1rem 0 1.25rem;
padding: .875rem 1rem;
border-radius: .75rem;
font-size: .95rem;
border: 1px solid #fecaca; /* red-200 */
background: #fee2e2; /* red-100 */
color: #991b1b; /* red-800 */
}
.dark .arx-outdated{
border-color: rgba(248,113,113,.35); /* red-400/35 */
background: rgba(127,29,29,.25); /* red-900/25 */
color: #fecaca; /* red-200 */
}
設定全站預設(可選) #
config/_default/params.toml #
[outdatedWarning]
enabledByDefault = false # 預設關閉;需要時在文章頭開啟
days = 365 # 預設門檻(可在文章覆寫)
useLastmod = true # 用 lastmod 計算,否則用 date
在單篇頁面模板「插入」這個 partial(自動顯示) #
目的:讓它自動出現在文章內容的最上方(不必每篇手動放短碼)。
路徑 A(常見情況): 把 Blowfish 主題的 single.html 複製出來再加一行。
找到:themes/blowfish/layouts/_default/single.html
複製到你專案:layouts/_default/single.html
在那個檔案中,找到輸出內容的位置(通常會有 {{ .Content }}
或某個包含內容的 partial),然後在它的上方插入:
{{ define "main" }}
<!-- 文章標題、meta 等 -->
{{ partial "outdated-warning.html" . }} <!-- ← 加這行 -->
{{ .Content }}
{{ end }}
如何使用 #
---
title: "我的文章"
date: 2024-12-01
lastmod: 2025-07-01
outdated: true # ← 開啟
outdated_days: 30 # ← 超過 30 天才顯示
---
省略行為:
沒寫 outdated → 走全站 enabledByDefault(上面預設是 false)。
沒寫 outdated_days → 走全站 days(預設 365)。