diff options
Diffstat (limited to 'layouts')
| -rw-r--r-- | layouts/404.html | 4 | ||||
| -rw-r--r-- | layouts/_default/_markup/render-image.html | 4 | ||||
| -rw-r--r-- | layouts/_default/baseof.html | 43 | ||||
| -rw-r--r-- | layouts/_default/list.html | 70 | ||||
| -rw-r--r-- | layouts/_default/rss.xml | 85 | ||||
| -rw-r--r-- | layouts/_default/single.html | 297 | ||||
| -rw-r--r-- | layouts/_default/term.html | 36 | ||||
| -rw-r--r-- | layouts/archives/single.html | 94 | ||||
| -rw-r--r-- | layouts/index.html | 1 | ||||
| -rw-r--r-- | layouts/no-comments/single.html | 136 | ||||
| -rw-r--r-- | layouts/partials/archives-list.html | 30 | ||||
| -rw-r--r-- | layouts/partials/custom_body.html | 5 | ||||
| -rw-r--r-- | layouts/partials/custom_head.html | 11 | ||||
| -rw-r--r-- | layouts/partials/favicon.html | 2 | ||||
| -rw-r--r-- | layouts/partials/footer.html | 1 | ||||
| -rw-r--r-- | layouts/partials/header.html | 4 | ||||
| -rw-r--r-- | layouts/partials/nav.html | 7 | ||||
| -rw-r--r-- | layouts/partials/seo_tags.html | 15 | ||||
| -rw-r--r-- | layouts/partials/style.html | 531 | ||||
| -rw-r--r-- | layouts/partials/toc.html | 91 | ||||
| -rw-r--r-- | layouts/robots.txt | 2 | ||||
| -rw-r--r-- | layouts/shortcodes/archives-list.html | 30 | ||||
| -rw-r--r-- | layouts/shortcodes/recent-posts.html | 55 | ||||
| -rw-r--r-- | layouts/shortcodes/word-count.html | 74 |
24 files changed, 1628 insertions, 0 deletions
diff --git a/layouts/404.html b/layouts/404.html new file mode 100644 index 0000000..15198aa --- /dev/null +++ b/layouts/404.html @@ -0,0 +1,4 @@ +{{ define "title" }}404{{ end }} {{ define "main" }} +<h1>404</h1> +<h2>ʕノ•ᴥ•ʔノ ︵ ┻━┻</h2> +{{ end }} diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html new file mode 100644 index 0000000..bf19bf9 --- /dev/null +++ b/layouts/_default/_markup/render-image.html @@ -0,0 +1,4 @@ +<figure class="image-caption"> + <img src="{{ .Destination | safeURL }}" alt="{{ .Text }}"> + <figcaption>{{ .Text }}</figcaption> +</figure>
\ No newline at end of file diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html new file mode 100644 index 0000000..8729df0 --- /dev/null +++ b/layouts/_default/baseof.html @@ -0,0 +1,43 @@ +<!DOCTYPE html> +<html lang="{{ with .Site.LanguageCode }}{{ . }}{{ else }}en-US{{ end }}"> + +<head> + <meta http-equiv="X-Clacks-Overhead" content="GNU Terry Pratchett" /> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + {{- partial "favicon.html" . -}} + <title>{{ .Title }}</title> + + {{- partial "seo_tags.html" . -}} + <meta name="referrer" content="no-referrer-when-downgrade" /> + + {{ with .OutputFormats.Get "rss" -}} + {{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }} + {{ end -}} + + {{- partial "style.html" . -}} + + <!-- A partial to be overwritten by the user. + Simply place a custom_head.html into + your local /layouts/partials-directory --> + {{- partial "custom_head.html" . -}} +</head> + +<body> + <header> + {{- partial "header.html" . -}} + </header> + <main> + {{- block "main" . }}{{- end }} + </main> + <footer> + {{- partial "footer.html" . -}} + </footer> + + <!-- A partial to be overwritten by the user. + Simply place a custom_body.html into + your local /layouts/partials-directory --> + {{- partial "custom_body.html" . -}} +</body> + +</html> diff --git a/layouts/_default/list.html b/layouts/_default/list.html new file mode 100644 index 0000000..8c1db86 --- /dev/null +++ b/layouts/_default/list.html @@ -0,0 +1,70 @@ +{{ define "main" }} +<content> + {{ if .Site.Params.postSearch }} + <input + id="search-input" + type="text" + placeholder="Search..." + style="margin-top: 16px" + /> + <script> + // 等待 DOM 完全加载后执行 + document.addEventListener('DOMContentLoaded', function () { + // 缓存 DOM 元素 + const searchInput = document.getElementById('search-input'); + const posts = document.querySelectorAll('.blog-posts li'); + const years = document.querySelectorAll('.blog-posts h3'); + + // 更新搜索结果 + function updateSearchResults(searchTerm) { + let visiblePosts = 0; + const displayedYears = new Set(); + posts.forEach(function (post) { + const title = post.querySelector('a').textContent.toLowerCase(); + const year = post.querySelector('time').getAttribute('datetime').split('-')[0]; + if (title.includes(searchTerm)) { + post.style.display = ''; + visiblePosts++; + displayedYears.add(year); + } else { + post.style.display = 'none'; + } + }); + + {{ if .Site.Params.groupByYear }} + years.forEach(function (y) { + const year = y.textContent; + y.style.display = displayedYears.has(year) ? '' : 'none'; + }); + {{ end }} + } + + searchInput.addEventListener('input', function () { + updateSearchResults(this.value.toLowerCase().trim()); + }); + }); + </script> + {{ end }} + <ul class="blog-posts"> + {{ $currentYear := 0 }} {{ range .Pages }} {{ if and (not .Params.hidden) + (not (in .Params.categories "往昔")) }} + + <li> + <span + class="{{ if .Site.Params.groupByYear }} grouped {{ else }} ungrouped {{ end }}" + > + <i> + <time datetime='{{ .Date.Format "2006-01-02" }}' pubdate> + {{ .Date.Format (default "2006-01-02" .Site.Params.dateFormat) }} + </time> + </i> + </span> + <a href="{{ .Permalink }}">{{ .Title }}</a> + </li> + + {{ end }} {{ else }} + <li>No posts yet</li> + {{ end }} + </ul> +</content> +{{ end }} diff --git a/layouts/_default/rss.xml b/layouts/_default/rss.xml new file mode 100644 index 0000000..37ab6ea --- /dev/null +++ b/layouts/_default/rss.xml @@ -0,0 +1,85 @@ +{{- /* Deprecate site.Author.email in favor of site.Params.author.email */}} +{{- $authorEmail := "" }} +{{- with site.Params.author }} + {{- if reflect.IsMap . }} + {{- with .email }} + {{- $authorEmail = . }} + {{- end }} + {{- end }} +{{- else }} + {{- with site.Author.email }} + {{- $authorEmail = . }} + {{- warnf "The author key in site configuration is deprecated. Use params.author.email instead." }} + {{- end }} +{{- end }} + +{{- /* Deprecate site.Author.name in favor of site.Params.author.name */}} +{{- $authorName := "" }} +{{- with site.Params.author }} + {{- if reflect.IsMap . }} + {{- with .name }} + {{- $authorName = . }} + {{- end }} + {{- else }} + {{- $authorName = . }} + {{- end }} +{{- else }} + {{- with site.Author.name }} + {{- $authorName = . }} + {{- warnf "The author key in site configuration is deprecated. Use params.author.name instead." }} + {{- end }} +{{- end }} + +{{- $pctx := . }} +{{- if .IsHome }}{{ $pctx = .Site }}{{ end }} +{{- $pages := slice }} +{{- if or $.IsHome $.IsSection }} +{{- $pages = $pctx.RegularPages }} +{{- else }} +{{- $pages = $pctx.Pages }} +{{- end }} +{{- $limit := .Site.Params.RSS.pageLimit }} +{{- if ge $limit 1 }} +{{- $pages = $pages | first $limit }} +{{- end }} +{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }} +<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> + <channel> + <title>{{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{ . }} on {{ end }}{{ .Site.Title }}{{ end }}</title> + <link>{{ .Permalink }}</link> + <description>Recent content {{ if ne .Title .Site.Title }}{{ with .Title }}in {{ . }} {{ end }}{{ end }}on {{ .Site.Title }}</description> + <generator>Hugo</generator> + <language>{{ site.LanguageCode }}</language> + {{ with $authorEmail }} + <managingEditor>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</managingEditor> + {{ end }} + {{ with $authorEmail }} + <webMaster>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</webMaster> + {{ end }} + {{ with .Site.Params.copyright }} + <copyright>{{ . }}</copyright> + {{ end }} + {{ if not .Date.IsZero }} + <lastBuildDate>{{ (index $pages.ByLastmod.Reverse 0).Lastmod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate> + {{ end }} + {{ if and .Site.Params.RSS.followFeedId .Site.Params.RSS.followUserId }} + <follow_challenge> + <feedId>{{ .Site.Params.RSS.followFeedId }}</feedId> + <userId>{{ .Site.Params.RSS.followUserId }}</userId> + </follow_challenge> + {{ end }} + {{- with .OutputFormats.Get "RSS" }} + {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }} + {{- end }} + {{- range $pages }} + <item> + <title>{{ .Title }}</title> + <link>{{ .Permalink }}</link> + <pubDate>{{ .PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate> + {{- with $authorEmail }}<author>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</author>{{ end }} + <guid>{{ .Permalink }}</guid> + <description>{{ .Content | transform.XMLEscape | safeHTML }}</description> + </item> + {{- end }} + </channel> +</rss>
\ No newline at end of file diff --git a/layouts/_default/single.html b/layouts/_default/single.html new file mode 100644 index 0000000..78814e5 --- /dev/null +++ b/layouts/_default/single.html @@ -0,0 +1,297 @@ +{{ define "main" }} {{ if eq .Type "blog" }} {{ if not .Params.menu }} +<h1>{{ .Title }}</h1> +{{ end }} {{ end }} + +<article class="h-entry"> + <h1 class="post-title p-name"><a href="{{ .RelPermalink }}" style="color:#222222;">{{ .Title }}</a></h1> + +<div class="post-info"> + <time class="post-date dt-published" datetime="{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}"> + {{ .Date.Format "2006年1月2日" }} + </time> + {{ range .Params.categories }} + {{ $url := printf "/categories/%s/" (. | urlize) }} + <a href="{{ $url }}" class="category-link">{{ . }}</a> + {{ end }} +</div> +<div class="e-content"> + {{ .Content }} +</div> +</article> +<a class="u-url" href="{{ .Permalink }}" style="display:none;">Permalink</a> + +<p> + {{ range (.GetTerms "tags") }} + <a href="{{ .Permalink }}">#{{ .LinkTitle }}</a> + {{ end }} +</p> + +{{ $upvoteEnabled := default .Site.Params.upvote .Params.upvote }} +{{ if $upvoteEnabled }} +<div class="upvote-container"> +<small class="upvote"> + <button class="upvote-btn" id="upvote-btn"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" class="css-i6dzq1"> + <polyline points="17 11 12 6 7 11"></polyline> + <polyline points="17 18 12 13 7 18"></polyline> + </svg> + <span class="upvote-count" id="upvote-count">0</span> + </button> +</small> +</div> + +<script> + let hasUpvoted = false; + let upvoteBtn; + let upvoteCount; + + // 页面加载时获取点赞数量 + document.addEventListener('DOMContentLoaded', function() { + const slug = '{{ .Slug }}'; + upvoteBtn = document.getElementById('upvote-btn'); + upvoteCount = document.getElementById('upvote-count'); + getCount(slug); + + // 处理点赞按钮的点击事件 + upvoteBtn.addEventListener('click', handleUpvote); + }); + + // 点赞方法 + async function handleUpvote() { + if (hasUpvoted) { + console.log('You have already upvoted this post!'); + return; + } + const slug = '{{ .Slug }}'; + + // 禁用按钮以防止重复点击 + upvoteBtn.disabled = true; + // 给按钮添加 upvoted 类以赋以点击过的样式 + upvoteBtn.classList.add('upvoted'); + // 更新 upvote-count 的值 +1 + upvoteCount.innerText = parseInt(upvoteCount.innerText) + 1; + + try { + const response = await fetch('{{ .Site.Params.upvoteURL }}upvote', { + method: 'POST', + mode: 'cors', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ postId: slug, diff: 1 }), + }); + + if (response.ok) { + console.log('Upvote successful!'); + hasUpvoted = true; + await getCount(slug, 3); + } else { + console.log('Upvote failed!'); + } + } catch (error) { + console.error('Error: ', error); + } finally { + upvoteBtn.disabled = false; + } + } + + // 获取 Upvote 数量的方法,支持设置重试次数,默认不重试 + async function getCount(slug, retryCount = 0) { + try { + const response = await fetch('{{ .Site.Params.upvoteURL }}count?post=' + slug, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + const data = await response.json(); + + if (data.code === 0) { + const count = data.data.count; + upvoteCount.innerText = count; + hasUpvoted = data.data.hasUpvoted; + if (hasUpvoted) { + upvoteBtn.classList.add('upvoted'); + } else { + upvoteBtn.classList.remove('upvoted'); + } + } else { + console.error('Failed to get upvote count: ', data.msg); + } + } catch (error) { + console.error('Error: ', error); + if (retryCount > 0) { + setTimeout(() => { + getCount(slug, retryCount - 1); + }, 1000); + } + } + } +</script> +{{ end }} + +<!-- Place the TOC at the end to ensure the article content loads first. --> +{{ $tocEnabled := default .Site.Params.toc .Params.toc }} {{ if $tocEnabled }} +<div class="toc">{{ partial "toc.html" . }}</div> +{{ end }} +<hr /> +<details> + <summary> + <p style="display: inline">评论 与 Webmentions</p> + </summary> + <div id="giscus-container"></div> + + <div class="wm"> +<div id="webmentions"></div> +<script src="/js/webmention.min.js" + data-id="webmentions" + data-page-url="https://www.glowisle.me{{ .RelPermalink }}" + data-max-webmentions="50" + data-wordcount="30" + data-sort-by="published" + data-sort-dir="up" + async> +</script> + +<details> + <summary>如何参与 Webmentions 互动?</summary> + <div class="wm-guide-content"> +<div class="webmention-form"> + <p>如果你想回应这篇文章,请在你的博客或社交媒体中链接本页面,然后在下面的表单中提交你的页面链接。</p> + <form id="webmention-submit-form" action="https://webmention.io/www.glowisle.me/webmention" method="post"> + <!-- 自动填充的目标文章链接 --> + <input type="hidden" name="target" value="https://www.glowisle.me{{ .RelPermalink }}"> + + <div> + <label for="webmention-source">你的文章链接:</label> + <input + type="url" + id="webmention-source" + name="source" + required + pattern="https?://.+" + > + <button type="submit" id="webmention-submit-btn">提交</button> + </div> + <div id="webmention-form-feedback"></div> + </form> + <br> +<a href="https://indieweb.org/webmention">关于 Webmention 的更多信息</a> 以及 <a href="https://www.glowisle.me/posts/tear-hypocrisy-apart/">为什么要这么做?</a> +</details> + + + + <script> + /* +src="https://giscus.app/client.js" + data-repo="yingyu5658/yingyu5658.github.io" + data-repo-id="R_kgDOOBetsA" + data-category="Announcements" + data-category-id="DIC_kwDOOBetsM4CoF_Z" + data-mapping="title" + data-strict="0" + data-reactions-enabled="1" + data-emit-metadata="0" + data-input-position="bottom" + data-theme="dark" + data-lang="zh-CN" + crossorigin="anonymous" + async> + + +*/ + function getInitialTheme() { + // 1. 优先检查 localStorage 中的用户偏好 + if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) { + return localStorage.getItem('theme'); + } + // 2. 检查系统偏好 + if (window.matchMedia('(prefers-color-scheme: dark)').matches) { + return 'dark'; + } + // 3. 默认值 + return 'light'; +} + +const currentTheme = getInitialTheme(); +const giscusTheme = currentTheme === 'dark' ? 'dark' : 'light'; + +const giscusScript = document.createElement('script'); +giscusScript.src = 'https://giscus.app/client.js'; +giscusScript.setAttribute('data-repo', 'yingyu5658/yingyu5658.github.io'); +giscusScript.setAttribute('data-repo-id', 'R_kgDOOBetsA'); +giscusScript.setAttribute('data-category', 'Announcements'); +giscusScript.setAttribute('data-category-id', 'DIC_kwDOOBetsM4CoF_Z'); +giscusScript.setAttribute('data-mapping', 'title'); +giscusScript.setAttribute('data-strict', '0'); +giscusScript.setAttribute('data-reactions-enabled', '1'); +giscusScript.setAttribute('data-emit-metadata', '0'); +giscusScript.setAttribute('data-input-position', 'bottom'); +giscusScript.setAttribute('data-theme', giscusTheme); +giscusScript.setAttribute('data-lang', 'zh-CN'); +giscusScript.crossOrigin = 'anonymous'; +giscusScript.async = true; + +document.getElementById('giscus-container').appendChild(giscusScript); + +document.addEventListener('DOMContentLoaded', function() { + const form = document.getElementById('webmention-submit-form'); + const submitBtn = document.getElementById('webmention-submit-btn'); + const feedbackEl = document.getElementById('webmention-form-feedback'); + + if (!form) return; + + form.addEventListener('submit', async function(e) { + e.preventDefault(); + + // 禁用提交按钮防止重复提交 + const originalBtnText = submitBtn.textContent; + submitBtn.disabled = true; + submitBtn.textContent = '发送中...'; + + // 清除之前的反馈信息 + feedbackEl.textContent = ''; + + try { + // 使用 FormData 收集表单数据 + const formData = new FormData(form); + + // 发送 POST 请求 + const response = await fetch(form.action, { + method: 'POST', + headers: { + 'Accept': 'application/json', + }, + body: new URLSearchParams(formData) + }); + + const result = await response.json(); + + if (response.ok) { + // 成功响应 + feedbackEl.textContent = '✅ 提交成功!Webmention 正在处理中。'; + form.reset(); + } else { + // 错误响应 + let errorMsg = '提交失败:'; + if (result.error || result.summary) { + errorMsg += result.error || result.summary; + } + feedbackEl.textContent = errorMsg; + } + } catch (error) { + // 网络错误 + feedbackEl.textContent = '❌ 提交过程中出现网络错误,请稍后重试。'; + } finally { + // 恢复提交按钮状态 + submitBtn.disabled = false; + submitBtn.textContent = originalBtnText; + } + }); +}); + + +</script> + +{{ end }} diff --git a/layouts/_default/term.html b/layouts/_default/term.html new file mode 100644 index 0000000..0d55fc1 --- /dev/null +++ b/layouts/_default/term.html @@ -0,0 +1,36 @@ +{{ define "main" }} +<div class="taxonomy-term"> + <h2>{{ .Title }}</h2> + + <div class="posts-list"> + {{ range .Pages }} + + <li style="list-style-type: none; margin-bottom: 12px"> + <span + class="post-date" + {{ + if + .Site.Params.groupByYear + }} + grouped + {{ + else + }} + ungrouped + {{ + end + }} + > + <time datetime='{{ .Date.Format "2006-01-02" }}' pubdate> + {{ .Date.Format "2006-01-02" }} + </time> + </span> + <a href="{{ .Permalink }}">{{ .Title }}</a> + </li> + + {{ else }} + <p>该分类下还没有文章。</p> + {{ end }} + </div> +</div> +{{ end }} diff --git a/layouts/archives/single.html b/layouts/archives/single.html new file mode 100644 index 0000000..6c43aac --- /dev/null +++ b/layouts/archives/single.html @@ -0,0 +1,94 @@ +{{ define "main" }} +<content> + {{ if .Site.Params.postSearch }} + <input + id="search-input" + type="text" + placeholder="搜索文章..." + style="margin-top: 16px" + /> + <script> + document.addEventListener('DOMContentLoaded', function () { + const searchInput = document.getElementById('search-input'); + const posts = document.querySelectorAll('.blog-posts li'); + const years = document.querySelectorAll('.blog-posts h3'); + + function updateSearchResults(searchTerm) { + let visiblePosts = 0; + const displayedYears = new Set(); + + posts.forEach(function (post) { + const titleLink = post.querySelector('a'); + const timeElement = post.querySelector('time'); + + if (titleLink && timeElement) { + const title = titleLink.textContent.toLowerCase(); + const year = timeElement.getAttribute('datetime').split('-')[0]; + + if (title.includes(searchTerm)) { + post.style.display = ''; + visiblePosts++; + displayedYears.add(year); + } else { + post.style.display = 'none'; + } + } + }); + + {{ if .Site.Params.groupByYear }} + years.forEach(function (y) { + const year = y.textContent; + y.style.display = displayedYears.has(year) ? '' : 'none'; + }); + {{ end }} + + {{ if .Site.Params.showPostCount }} + const countText = `找到 ${visiblePosts} 篇文章`; + const countElement = document.getElementById('post-count'); + if (countElement) { + countElement.innerHTML = countText; + } + {{ end }} + } + + if (searchInput) { + searchInput.addEventListener('input', function () { + updateSearchResults(this.value.toLowerCase().trim()); + }); + } + }); + </script> + {{ end }} {{ $allPosts := where .Site.RegularPages "Type" "eq" "posts" }} {{ + $excludePosts := where $allPosts "Params.categories" "intersect" (slice + "往昔") }} {{ $postPages := $allPosts | complement $excludePosts }} {{ if + .Site.Params.showPostCount }} + <p id="post-count">共有 {{ len $postPages }} 篇文章</p> + {{ end }} + + <ul class="blog-posts"> + {{ if gt (len $postPages) 0 }} {{ $pagesToShow := $postPages.ByDate.Reverse + }} {{ $currentYear := 0 }} {{ range $pagesToShow }} {{ if .Date }} {{ $year + := .Date.Year }} {{ if and (.Site.Params.groupByYear) (ne $year + $currentYear) }} + <h3>{{ $year }}</h3> + {{ $currentYear = $year }} {{ end }} {{ end }} + + <li> + <span + class="{{ if .Site.Params.groupByYear }}grouped{{else}}ungrouped{{end}}" + > + <i> + <time datetime='{{ .Date.Format "2006-01-02" }}' pubdate> + {{ .Date.Format (default "2006-01-02" .Site.Params.dateFormat) }} + </time> + </i> + </span> + <a href="{{ .Permalink }}">{{ .Title }}</a> + </li> + {{ end }} {{ else }} + <li>暂无文章</li> + {{ end }} + </ul> +</content> +<hr /> +{{ end }} diff --git a/layouts/index.html b/layouts/index.html new file mode 100644 index 0000000..d975ecb --- /dev/null +++ b/layouts/index.html @@ -0,0 +1 @@ +{{ define "main" }} {{ .Content }} {{ end }} diff --git a/layouts/no-comments/single.html b/layouts/no-comments/single.html new file mode 100644 index 0000000..58160be --- /dev/null +++ b/layouts/no-comments/single.html @@ -0,0 +1,136 @@ +{{ define "main" }} +{{ if eq .Type "blog" }} +{{ if not .Params.menu }} +<h1>{{ .Title }}</h1> +{{ if .Date }} +<p> + <i> + <time datetime='{{ .Date.Format "2006-01-02" }}' pubdate> + {{ .Date.Format (default "02 Jan, 2006" .Site.Params.dateFormat) }} + </time> + </i> +</p> +{{ end }} +{{ end }} +{{ end }} + +<content> {{ .Content }} </content> + +<p> + {{ range (.GetTerms "tags") }} + <a href="{{ .Permalink }}">#{{ .LinkTitle }}</a> + {{ end }} +</p> + +{{ $upvoteEnabled := default .Site.Params.upvote .Params.upvote }} +{{ if $upvoteEnabled }} +<div class="upvote-container"> +<small class="upvote"> + <button class="upvote-btn" id="upvote-btn"> + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" stroke="currentColor" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" class="css-i6dzq1"> + <polyline points="17 11 12 6 7 11"></polyline> + <polyline points="17 18 12 13 7 18"></polyline> + </svg> + <span class="upvote-count" id="upvote-count">0</span> + </button> +</small> +</div> + +<script> + let hasUpvoted = false; + let upvoteBtn; + let upvoteCount; + + // 页面加载时获取点赞数量 + document.addEventListener('DOMContentLoaded', function() { + const slug = '{{ .Slug }}'; + upvoteBtn = document.getElementById('upvote-btn'); + upvoteCount = document.getElementById('upvote-count'); + getCount(slug); + + // 处理点赞按钮的点击事件 + upvoteBtn.addEventListener('click', handleUpvote); + }); + + // 点赞方法 + async function handleUpvote() { + if (hasUpvoted) { + console.log('You have already upvoted this post!'); + return; + } + const slug = '{{ .Slug }}'; + + // 禁用按钮以防止重复点击 + upvoteBtn.disabled = true; + // 给按钮添加 upvoted 类以赋以点击过的样式 + upvoteBtn.classList.add('upvoted'); + // 更新 upvote-count 的值 +1 + upvoteCount.innerText = parseInt(upvoteCount.innerText) + 1; + + try { + const response = await fetch('{{ .Site.Params.upvoteURL }}upvote', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ postId: slug, diff: 1 }), + }); + + if (response.ok) { + console.log('Upvote successful!'); + hasUpvoted = true; + await getCount(slug, 3); + } else { + console.log('Upvote failed!'); + } + } catch (error) { + console.error('Error: ', error); + } finally { + upvoteBtn.disabled = false; + } + } + + // 获取 Upvote 数量的方法,支持设置重试次数,默认不重试 + async function getCount(slug, retryCount = 0) { + try { + const response = await fetch('{{ .Site.Params.upvoteURL }}count?post=' + slug, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + const data = await response.json(); + + if (data.code === 0) { + const count = data.data.count; + upvoteCount.innerText = count; + hasUpvoted = data.data.hasUpvoted; + if (hasUpvoted) { + upvoteBtn.classList.add('upvoted'); + } else { + upvoteBtn.classList.remove('upvoted'); + } + } else { + console.error('Failed to get upvote count: ', data.msg); + } + } catch (error) { + console.error('Error: ', error); + if (retryCount > 0) { + setTimeout(() => { + getCount(slug, retryCount - 1); + }, 1000); + } + } + } +</script> +{{ end }} + +<!-- Place the TOC at the end to ensure the article content loads first. --> +{{ $tocEnabled := default .Site.Params.toc .Params.toc }} +{{ if $tocEnabled }} +<div class="toc"> +{{ partial "toc.html" . }} +</div> +{{ end }} +{{ end }} diff --git a/layouts/partials/archives-list.html b/layouts/partials/archives-list.html new file mode 100644 index 0000000..b9767f7 --- /dev/null +++ b/layouts/partials/archives-list.html @@ -0,0 +1,30 @@ +{{ $allPages := where .Site.RegularPages "Type" "in" (slice "posts" "blog") }} +{{ $visiblePages := where $allPages "Params.hidden" "!=" true }} +{{ $pagesToShow := $visiblePages.ByDate.Reverse }} + +<div class="archives-content"> + <h2>归档</h2> + + <p>共有 {{ len $pagesToShow }} 篇文章</p> + + <ul class="blog-posts"> + {{ $currentYear := 0 }} + {{ range $pagesToShow }} + {{ $year := .Date.Year }} + {{ if ne $year $currentYear }} + <h3>{{ $year }}</h3> + {{ $currentYear = $year }} + {{ end }} + <li> + <span> + <i> + <time datetime='{{ .Date.Format "2006-01-02" }}'> + {{ .Date.Format "2006-01-02" }} + </time> + </i> + </span> + <a href="{{ .Permalink }}">{{ .Title }}</a> + </li> + {{ end }} + </ul> +</div> diff --git a/layouts/partials/custom_body.html b/layouts/partials/custom_body.html new file mode 100644 index 0000000..529771a --- /dev/null +++ b/layouts/partials/custom_body.html @@ -0,0 +1,5 @@ + <!-- A partial to be overwritten by the user. + Simply place a custom_body.html into + your local /layouts/partials-directory --> + + diff --git a/layouts/partials/custom_head.html b/layouts/partials/custom_head.html new file mode 100644 index 0000000..4ea89a1 --- /dev/null +++ b/layouts/partials/custom_head.html @@ -0,0 +1,11 @@ +<meta name="msvalidate.01" content="2E1AACF009206F2DDBAAD4B98E881460" /> +<link + rel="prefetch" + as="image" + href="https://www.blogsclub.org/badge/www.glowisle.me" +/> +<link href="https://github.com/yingyu5658" rel="me" /> +<link + rel="webmention" + href="https://webmention.io/www.glowisle.me/webmention" +/> diff --git a/layouts/partials/favicon.html b/layouts/partials/favicon.html new file mode 100644 index 0000000..ccf1a5d --- /dev/null +++ b/layouts/partials/favicon.html @@ -0,0 +1,2 @@ +{{ with .Site.Params.favicon }} +<link rel="shortcut icon" href="{{ . | absURL }}" />{{ end }} diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html new file mode 100644 index 0000000..cc5e269 --- /dev/null +++ b/layouts/partials/footer.html @@ -0,0 +1 @@ +<p>© 2024 - 2025 | Made with ❤️ by Verdant.</p> diff --git a/layouts/partials/header.html b/layouts/partials/header.html new file mode 100644 index 0000000..66fa74b --- /dev/null +++ b/layouts/partials/header.html @@ -0,0 +1,4 @@ +<a href="{{ "" | relURL }}" class="title"> + <h1 class="site-name">{{ .Site.Title }}</h1> +</a> +<nav>{{- partial "nav.html" . -}}</nav> diff --git a/layouts/partials/nav.html b/layouts/partials/nav.html new file mode 100644 index 0000000..5b9d089 --- /dev/null +++ b/layouts/partials/nav.html @@ -0,0 +1,7 @@ +<a href="{{ "" | relURL }}">首页</a> +{{ range .Site.Menus.main }} +<a href="{{ .URL }}">{{ .Name }}</a> +{{ end }} +{{ with .Site.GetPage "/blog" }} +<a href="{{ "posts/" | relURL }}">Blog</a> +{{ end }} diff --git a/layouts/partials/seo_tags.html b/layouts/partials/seo_tags.html new file mode 100644 index 0000000..06e90cd --- /dev/null +++ b/layouts/partials/seo_tags.html @@ -0,0 +1,15 @@ +<!-- Primary Meta Tags --> +<meta name="title" content="{{ with .Title }}{{ . }}{{ else }}{{ .Site.Title }}{{ end }}" /> +<meta name="description" content="{{ with .Description }}{{ . }}{{ else }}{{ if .IsPage }}{{ .Summary }}{{ else }}{{ with .Site.Params.Description }}{{ . }}{{ end }}{{ end }}{{ end }}" /> +<meta name="keywords" content="{{ if .IsPage }}{{ range $index, $tag := .Params.tags }}{{ $tag }},{{ end }}{{ else }}{{ range $plural, $terms := .Site.Taxonomies }}{{ range $term, $val := $terms }}{{ printf "%s," $term }}{{ end }}{{ end }}{{ end }}" /> + +<link rel="canonical" href="{{ .Permalink }}"> + +<!-- Open Graph / Facebook --> +{{ template "_internal/opengraph.html" . }} + +<!-- Twitter --> +{{ template "_internal/twitter_cards.html" . }} + +<!-- Microdata --> +{{ template "_internal/schema.html" . }} diff --git a/layouts/partials/style.html b/layouts/partials/style.html new file mode 100644 index 0000000..f108da6 --- /dev/null +++ b/layouts/partials/style.html @@ -0,0 +1,531 @@ +<style> + /* light theme */ + :root { + --width-max: 720px; + --font-primary: "Noto Serif SC", "Source Han Serif SC", serif; + --font-secondary: monospace; + --font-size-primary: 1em; + --font-size-secondary: 0.8em; + --body-bg-color: #ffffff; + --bold-text-color: #222; + --body-text-color: #444; + --link-color: #222; + --link-visited-color: #222; + --table-border-color: #f2f2f2; + --table-th-bg-color: #f2f2f2; + --img-border-color: #f2f2f2; + --code-bg-color: #f2f2f2; + --code-text-color: #222; + --blockquote-border-color: #666; + --blockquote-text-color: #666; + --upvoted-color: #fa8072; + --caption-text-color: #666; + --toc-text-color: #e5e5e5; + --toc-hover-color: #655e5e; + } + + .wm { + border: 1px #d0d7de solid; + border-radius: 0.25em; + padding-top: 0; + padding: 1.5vw; + background-color: #f6f8fa; + margin-top: 1vh; + } + + .wm p { + margin-top: 1px; + } + + .item-link { + white-space: nowrap; + overflow: hidden; + } + + .post-info { + text-align: center; + margin-bottom: 3vh; + } + .category-link { + color: #999; + } + + h1.post-title { + margin-top: 5vh; + margin-bottom: 10px; + text-align: center; + } + + h1.post-title a { + font-size: 30px; + font-weight: 700; + } + + .recent-list { + padding-left: 2vw; + } + + .recent-item { + list-style-type: none; + } + + .post-date { + display: inline; + } + + .post-date, + .post-date-shortcode { + margin-left: auto; + color: #999; + } + + article .post-date::after { + content: "/"; + } + + .site-name:hover { + background-color: transparent; + text-decoration: none; + color: #fff; + background-color: #7e4fa0; + } + + .site-name { + transition: 0.3s; + width: 66px; + border-radius: 5px; + text-transform: uppercase; + } + + @media (prefers-color-scheme: dark) { + :root { + --body-bg-color: #121212; + --bold-text-color: #eee; + --body-text-color: #ddd; + --link-color: #ddd; + --link-visited-color: #c3b1ee; + --table-border-color: #999; + --table-th-bg-color: #999; + --img-border-color: #999; + --code-bg-color: #141414; + --code-text-color: #ddd; + --blockquote-border-color: #ccc; + --blockquote-text-color: #ccc; + --caption-text-color: #aaa; + --toc-text-color: #373737; + --toc-hover-color: #cac3c3; + } + + .wm { + background-color: #161b22; + border-color: #30363d; + color: var(--body-text-color); + padding: 10px; + } + + .wm input { + background-color: #010409; + border: #30363d 1px solid; + color: var(--body-text-color); + outline: none; + transition: border-color 0.2s ease-in-out; + } + + #webmention-source:focus { + border-color: var(--color-accent-fg, #0969da); + box-shadow: 0 0 0 3px rgba(9, 105, 218, 0.1); + } + + .wm button { + color: var(--body-text-color); + background-color: #161b22; + border: #30363d 1px solid; + } + + .post-title a { + color: #fff !important; + } + + .post-summary { + color: var(--text-color-primary) !important; + } + + .post-item { + border-bottom: 1px solid #414141 !important; + } + + .pagination-link { + border: 1px solid #414141 !important; + } + + nav a, + h1.post-title a, + .category-link, + .blog-posts li a { + text-decoration: none !important; + } + + a { + font-weight: 700 !important; + } + + a:hover { + color: #ddd !important; + } + } + + body { + font-family: var(--font-primary); + font-size: var(--font-size-primary); + margin: auto; + padding: 20px; + max-width: var(--width-max); + text-align: left; + background-color: var(--body-bg-color); + word-wrap: break-word; + overflow-wrap: break-word; + line-height: 1.6; + color: var(--body-text-color); + } + + h1, + h2, + h3, + h4, + h5, + h6, + strong, + b { + color: var(--bold-text-color); + } + + h1, + h2, + h3, + h4, + h5, + h6 { + margin: 16px 0; + } + + a { + color: var(--link-color); + cursor: pointer; + text-decoration: underline 0.2px #d4d4d4; + transition-duration: 0.3s; + font-weight: 600; + } + + a:hover { + color: #3273dc; + } + + .posts-list li { + margin-bottom: 12px; + } + + .title { + text-decoration: none; + border: 0; + } + + .title:hover { + text-decoration: none; + } + + .title span { + font-weight: 400; + } + + nav { + margin-bottom: 15px; + } + + nav a { + margin-right: 8px; + } + + textarea { + width: 100%; + font-size: 16px; + } + + input { + font-size: 14px; + } + + article { + line-height: 1.6; + } + + table { + width: 100%; + border-collapse: collapse; + border: 1px solid var(--table-border-color); + border-radius: 4px; + margin-top: 16px; + } + + th, + td { + border: 1px solid var(--table-border-color); + padding: 4px; + } + + th { + background-color: var(--table-th-bg-color); + } + + hr { + border: 0; + border-top: 1px dashed; + } + + img { + max-width: 100%; + display: block; + margin-left: auto; + margin-right: auto; + border: 1px solid var(--img-border-color); + border-radius: 4px; + content-visibility: auto; + loading: lazy; + } + + img[src*="#minipic"] { + max-width: 50%; + margin-left: 0; + margin-right: auto; + } + + .image-caption figcaption { + text-align: center; + font-style: italic; + font-size: 0.8em; + margin-top: 0.6em; + color: var(--caption-text-color); + } + + .image-caption { + margin: auto; + } + + i { + font-style: normal; + } + + time { + font-family: var(--font-secondary); + font-size: 15px; + color: rgb(163, 163, 163); + } + + code { + font-family: var(--font-secondary); + background-color: var(--code-bg-color); + color: var(--code-text-color); + padding: 2px; + border-radius: 4px; + } + + pre code { + display: block; + padding: 16px; + white-space: pre-wrap; + overflow-x: auto; + } + + div.highlight pre { + border-radius: 4px; + } + + div.highlight code { + background-color: var(--code-bg-color); + color: var(--code-text-color); + } + + blockquote { + border-left: 2px solid var(--blockquote-border-color); + color: var(--blockquote-text-color); + margin: 0; + padding-left: 16px; + font-style: normal; + } + + blockquote p { + margin: 0; + } + + footer { + padding: 25px 0; + text-align: left; + font-size: var(--font-size-secondary); + } + + ul li:has(input) { + list-style-type: none; + margin-left: -25.5px; + } + + /* blog post list */ + ul.blog-posts { + list-style-type: none; + padding: unset; + } + + ul.blog-posts li { + display: flex; + margin-bottom: 8px; + } + + ul.blog-posts li span { + flex: 0 0 130px; + } + + ul.blog-posts li span.grouped { + flex: 0 0 80px; + } + + ul.blog-posts li a:visited { + color: var(--link-visited-color); + } + + ul.blog-posts a { + margin-left: 10px; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + } + /* TOC 样式 */ + div.toc { + position: fixed; /* 固定定位 */ + top: 50%; /* 垂直居中 */ + left: calc( + (100vw + var(--width-max)) / 2 + ); /* 水平定位,根据视口宽度和最大内容宽度计算 */ + transform: translateY(-50%); /* 垂直居中调整 */ + width: calc((90vw - var(--width-max)) / 2); /* 宽度计算 */ + max-height: 80vh; /* 最大高度为视口高度的80% */ + overflow-y: auto; /* 垂直方向内容溢出时显示滚动条 */ + border: none; /* 无边框 */ + padding: 0; /* 无内边距 */ + margin: 0; /* 无外边距 */ + z-index: 99; /* 设置堆叠顺序,确保在其他元素之上 */ + + /* 隐藏滚动条 */ + &::-webkit-scrollbar { + /* Webkit 浏览器(Chrome, Safari)滚动条样式 */ + display: none; /* 隐藏滚动条 */ + } + + -ms-overflow-style: none; /* IE 和 Edge 隐藏滚动条 */ + scrollbar-width: none; /* Firefox 隐藏滚动条 */ + } + + .toc-nav { + /* 目录导航容器样式 */ + padding: 1.5rem; /* 内边距 */ + } + + .toc-nav ul { + /* 目录导航无序列表样式 */ + list-style: none; /* 移除列表项标记 */ + padding: 0; /* 移除内边距 */ + margin: 0; /* 移除外边距 */ + } + + .toc-nav li { + /* 目录导航列表项样式 */ + margin: 8px 0; /* 上下外边距 */ + } + + .toc-nav a { + /* 目录链接样式 */ + display: block; /* 块级显示 */ + text-decoration: none; /* 无下划线 */ + color: transparent; /* 默认透明 */ + padding: 0 12px; /* 内边距 */ + transition: all 0.2s ease; /* 所有属性过渡效果 */ + font-size: 0.9rem; /* 字体大小 */ + line-height: 1.4; /* 行高 */ + text-align: left; /* 文本左对齐 */ + white-space: nowrap; /* 禁止换行 */ + overflow: hidden; /* 隐藏溢出内容 */ + text-overflow: ellipsis; /* 显示省略号 */ + max-width: 100%; /* 限制最大宽度 */ + } + + .toc-nav:hover a { + color: var(--toc-text-color); /* hover时显示文字颜色 */ + } + + .toc-nav a::before { + /* 目录链接前的小横线样式 */ + content: ""; /* 生成内容 */ + display: inline-block; /* 行内块级显示 */ + width: 16px; /* 宽度 */ + height: 4px; /* 高度 */ + background-color: var(--toc-text-color); /* 灰色背景 */ + border-radius: 16px; /* 圆角 */ + margin-right: 12px; /* 右外边距 */ + vertical-align: middle; /* 垂直居中对齐 */ + } + + .toc-nav ul ul a::before { + /* 二级目录链接前的小横线样式 */ + width: 12px; /* 显示宽度 */ + margin-right: 16px; /* 调整右外边距,使其与默认 a::before 占据的总宽度一致 (12px + 12px = 24px) */ + } + + .toc-nav ul ul ul a::before { + /* 三级目录链接前的小横线样式 */ + width: 8px; /* 显示宽度 */ + margin-right: 20px; /* 调整右外边距,使其与默认 a::before 占据的总宽度一致 (8px + 16px = 24px) */ + } + + .toc-nav a.active, /* 活跃状态和鼠标悬停状态的目录链接样式 */ + .toc-nav a:hover { + text-decoration: none; /* 无下划线 */ + color: var(--toc-hover-color); /* 文字颜色变为深灰色 */ + } + + .toc-nav a.active::before, /* 活跃状态和鼠标悬停状态的目录链接前小横线样式 */ + .toc-nav a:hover::before { + background-color: var(--toc-hover-color); /* 背景颜色变为深灰色 */ + } + + /* upvote button style */ + button.upvote-btn { + margin: 0; + margin-left: auto; + padding: 0; + border: none; + background: none; + cursor: pointer; + display: flex; + flex-direction: column; + align-items: center; + color: var(--body-text-color); + } + + button.upvoted { + color: var(--upvoted-color); + } + + span.upvote-count { + margin-top: -4px; + font-size: smaller; + } + + @media (max-width: 800px) { + img[src*="#minipic"] { + max-width: 100%; + margin-left: auto; + margin-right: auto; + } + + div.toc { + display: none; + } + } +</style> diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html new file mode 100644 index 0000000..66936a4 --- /dev/null +++ b/layouts/partials/toc.html @@ -0,0 +1,91 @@ +{{/* 根据页面内容生成目录 */}} +{{ if and .TableOfContents (ne .TableOfContents "<nav id=\"TableOfContents\"></nav>") }} +<nav class="toc-nav"> + {{ .TableOfContents }} +</nav> + +<script> + // 为目录添加平滑滚动和当前位置高亮 + document.addEventListener('DOMContentLoaded', function () { + const tocLinks = document.querySelectorAll('.toc-nav a'); + const headings = Array.from(tocLinks).map(link => { + const id = link.getAttribute('href').replace('#', ''); + return document.getElementById(id); + }).filter(h => h); + + // 平滑滚动 + tocLinks.forEach(link => { + link.addEventListener('click', function (e) { + e.preventDefault(); + const targetId = this.getAttribute('href').replace('#', ''); + const target = document.getElementById(targetId); + if (target) { + target.scrollIntoView({ + behavior: 'smooth', + block: 'start' + }); + + // 更新活动状态 + tocLinks.forEach(l => l.classList.remove('active')); + this.classList.add('active'); + } + }); + }); + + // 滚动时更新当前位置 + function updateActiveLink() { + const scrollTop = window.pageYOffset || document.documentElement.scrollTop; + const windowHeight = window.innerHeight; + + for (let i = headings.length - 1; i >= 0; i--) { + const heading = headings[i]; + const rect = heading.getBoundingClientRect(); + + if (rect.top <= 100) { + // 移除所有活动状态 + tocLinks.forEach(link => link.classList.remove('active')); + + // 高亮当前标题 + const activeLink = document.querySelector(`.toc-nav a[href="#${heading.id}"]`); + if (activeLink) { + activeLink.classList.add('active'); + + // 高亮所有父级标题 + let currentLi = activeLink.closest('li'); + while (currentLi) { + const parentLi = currentLi.parentElement.closest('li'); + if (parentLi) { + const parentLink = parentLi.querySelector('a'); + if (parentLink) { + parentLink.classList.add('active'); + } + currentLi = parentLi; + } else { + break; + } + } + } + break; + } + } + } + + // 节流函数 + let ticking = false; + function throttleScroll() { + if (!ticking) { + requestAnimationFrame(function () { + updateActiveLink(); + ticking = false; + }); + ticking = true; + } + } + + window.addEventListener('scroll', throttleScroll); + + // 初始化时更新一次 + updateActiveLink(); + }); +</script> +{{ end }}
\ No newline at end of file diff --git a/layouts/robots.txt b/layouts/robots.txt new file mode 100644 index 0000000..0326f5c --- /dev/null +++ b/layouts/robots.txt @@ -0,0 +1,2 @@ +User-Agent: * +Sitemap: {{ "sitemap.xml" | absURL }} diff --git a/layouts/shortcodes/archives-list.html b/layouts/shortcodes/archives-list.html new file mode 100644 index 0000000..b9767f7 --- /dev/null +++ b/layouts/shortcodes/archives-list.html @@ -0,0 +1,30 @@ +{{ $allPages := where .Site.RegularPages "Type" "in" (slice "posts" "blog") }} +{{ $visiblePages := where $allPages "Params.hidden" "!=" true }} +{{ $pagesToShow := $visiblePages.ByDate.Reverse }} + +<div class="archives-content"> + <h2>归档</h2> + + <p>共有 {{ len $pagesToShow }} 篇文章</p> + + <ul class="blog-posts"> + {{ $currentYear := 0 }} + {{ range $pagesToShow }} + {{ $year := .Date.Year }} + {{ if ne $year $currentYear }} + <h3>{{ $year }}</h3> + {{ $currentYear = $year }} + {{ end }} + <li> + <span> + <i> + <time datetime='{{ .Date.Format "2006-01-02" }}'> + {{ .Date.Format "2006-01-02" }} + </time> + </i> + </span> + <a href="{{ .Permalink }}">{{ .Title }}</a> + </li> + {{ end }} + </ul> +</div> diff --git a/layouts/shortcodes/recent-posts.html b/layouts/shortcodes/recent-posts.html new file mode 100644 index 0000000..c6c1a3e --- /dev/null +++ b/layouts/shortcodes/recent-posts.html @@ -0,0 +1,55 @@ +{{ $pages := where site.RegularPages "Type" "in" site.Params.mainSections }} {{ +$recent := first 5 $pages.ByDate.Reverse }} +<style> + .recent-list { + margin: 0; + } + + .recent-item { + position: relative; + margin-bottom: 8px; + display: flex; + align-items: flex-start; + } + + .item-link { + display: inline-block; + text-decoration: none; + margin-left: 10px; + flex: 1; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + } + + .post-date { + font-size: 0.9em; + color: #666; + display: inline-block; + min-width: 80px; + flex-shrink: 0; + } + + .recent-empty { + text-align: center; + font-style: italic; + } +</style> +<div class="recent-posts"> + {{ if $recent }} + <ul class="recent-list"> + {{ range $index, $page := $recent }} + <li class="recent-item"> + <span class="post-date"> + <time datetime='{{ .Date.Format "2006-01-02" }}'> + {{ .Date.Format (default "2006-01-02" .Site.Params.dateFormat) }} + </time> + </span> + <a href="{{ .RelPermalink }}" class="item-link">{{ .Title }}</a> + </li> + {{ end }} + </ul> + {{ else }} + <p class="recent-empty">暂无文章</p> + {{ end }} +</div> diff --git a/layouts/shortcodes/word-count.html b/layouts/shortcodes/word-count.html new file mode 100644 index 0000000..0621ed9 --- /dev/null +++ b/layouts/shortcodes/word-count.html @@ -0,0 +1,74 @@ +{{ $scratch := newScratch }} +{{ if eq (.Get 0) "posts" }} + {{/* 统计所有博客文章 */}} + {{ range where site.RegularPages "Type" "in" (slice "posts" "jottings" "readings" "tech") }} + {{ $scratch.Add "wordcount" .WordCount }} + {{ end }} +{{ else if eq (.Get 0) "all" }} + {{/* 统计所有内容 */}} + {{ range where site.RegularPages "Type" "in" (slice "posts" "about" "newsgroup" "links" "jottings" "readings" "tech") }} + {{ $scratch.Add "wordcount" .WordCount }} + {{ end }} +{{ end }} + +{{ $count := $scratch.Get "wordcount" }} +{{ if gt $count 0 }} + {{ $count }} 字, + + {{/* 名著数据库(字数单位:汉字) */}} + {{ $classics := slice + (dict "name" "红楼梦" "author" "曹雪芹" "country" "(中)" "words" 731017) + (dict "name" "源氏物语" "author" "紫式部" "country" "(日)" "words" 876000) + (dict "name" "假面的告白" "author" "三岛由纪夫" "country" "(日)" "words" 86000) + (dict "name" "金阁寺" "author" "三岛由纪夫" "country" "(日)" "words" 125000) + (dict "name" "了不起的盖茨比" "author" "菲茨杰拉德" "country" "(美)" "words" 49800) + (dict "name" "傲慢与偏见" "author" "简·奥斯汀" "country" "(英)" "words" 183000) + (dict "name" "呐喊" "author" "鲁迅" "country" "(中)" "words" 152000) + (dict "name" "罗生门" "author" "芥川龙之介" "country" "(日)" "words" 35800) + (dict "name" "老人与海" "author" "海明威" "country" "(美)" "words" 26800) + (dict "name" "简爱" "author" "夏洛蒂·勃朗特" "country" "(英)" "words" 322000) + (dict "name" "三国演义" "author" "罗贯中" "country" "(中)" "words" 640000) + (dict "name" "雪国" "author" "川端康成" "country" "(日)" "words" 78000) + (dict "name" "杀死一只知更鸟" "author" "哈珀·李" "country" "(美)" "words" 187000) + (dict "name" "1984" "author" "乔治·奥威尔" "country" "(英)" "words" 123000) + (dict "name" "围城" "author" "钱钟书" "country" "(中)" "words" 257000) + (dict "name" "我是猫" "author" "夏目漱石" "country" "(日)" "words" 402000) + (dict "name" "飘" "author" "玛格丽特·米切尔" "country" "(美)" "words" 802000) + (dict "name" "呼啸山庄" "author" "艾米莉·勃朗特" "country" "(英)" "words" 282000) + }} + + {{/* 寻找最接近的名著 */}} + {{ $closest := dict "diff" 999999999 "book" (index $classics 0) }} + {{ range $book := $classics }} + {{ $ratio := div (float $count) $book.words }} + {{ if and (ge $ratio 0.8) (le $ratio 1.2) }} + {{ $diff := sub $ratio 1.0 }} + {{ if lt $diff 0 }}{{ $diff = mul $diff -1 }}{{ end }} + {{ if lt $diff $closest.diff }} + {{ $closest = dict "diff" $diff "book" $book }} + {{ end }} + {{ end }} + {{ end }} + + {{/* 输出对比结果 */}} + {{ with $closest.book }} + 约等于 《{{ .name }}》 的{{ div (float $count) .words | printf "%.1f" }}倍 + {{ else }} + {{/* 未找到匹配时显示长度最接近的名著 */}} + {{ $closestBook := index $classics 0 }} + {{ $minDiff := sub $count $closestBook.words }} + {{ if lt $minDiff 0 }}{{ $minDiff = mul $minDiff -1 }}{{ end }} + {{ range $classics }} + {{ $currDiff := sub $count .words }} + {{ if lt $currDiff 0 }}{{ $currDiff = mul $currDiff -1 }}{{ end }} + {{ if lt $currDiff $minDiff }} + {{ $closestBook = . }} + {{ $minDiff = $currDiff }} + {{ end }} + {{ end }} + ≈ {{ $closestBook.name }}{{ div (float $count) $closestBook.words | printf "%.1f" }}倍 + {{ end }} + +{{ else }} + 0字 +{{ end }} |
