Featured image of post 默认文章YAML Front Matter

默认文章YAML Front Matter

   
文章摘要
小tips……😋

在原作者的基础上略加修改,使用随机图片封面和默认分类标签

随机图片位于/static/images/random/目录下,可以使用 bat 自动重命名为“01”等,需要手动添加 建议压缩到 2M 以下,网上找了许多压缩图片工具大多只支持最大5M的图片,找了半天半天发现本地的file convert反而最好用,虽然压缩超过 3 次后质量压缩严重,但是目前还是可以接受

image-20241219002509620

源码(\archetypes\default.md)奉上

---
title: "{{ replace .Name "-" " " | title }}"
slug: 
description: 
keywords: 
date: {{ .Date }}
lastmod: {{ .Date }}
draft: false
toc: true
weight: false
{{ $id := (add 1 (index (shuffle (seq 1 182)) 0)) -}}
imgid: {{ printf "%03d" $id }}
image: {{ $jsonFile := printf "static/images/random/%03d.json" $id }}{{ with os.ReadFile $jsonFile }}{{ $json := . | unmarshal }}{{ index $json "4399" }}{{ end }}
license: 
comments: false
summary: 小tips……😋
categories: [suibi]
tags: [日常]
ai: false
---

# 前言

1

# 正文

1

解读

---  # 这是YAML前置参数的开始标记
title: "{{ replace .Name "-" " " | title }}"  
# 标题生成逻辑:
# 1. .Name 获取文件名
# 2. replace 将文件名中的"-"替换为空格
# 3. title 将每个单词首字母大写

slug:  # 文章的URL别名,留空则使用文件名
description:  # 文章描述
keywords:  # 文章关键词
date: {{ .Date }}  # 自动插入文章创建时间
lastmod: {{ .Date }}  # 自动插入文章最后修改时间
draft: false  # 是否是草稿,false表示直接发布
toc: true  # 是否显示目录
weight: false  # 文章权重,用于排序

# 下面是随机图片ID生成的核心代码
{{ $id := (add 1 (index (shuffle (seq 1 182)) 0)) -}}
# 1. seq 1 182 生成1到182的序列
# 2. shuffle 随机打乱这个序列
# 3. index ... 0 获取打乱后的第一个数字
# 4. add 1 确保结果在1-182之间
# 5. := 将结果存储在变量$id中

imgid: {{ printf "%03d" $id }}
# 将随机数格式化为三位数,如001、002等

image: {{ $jsonFile := printf "static/images/random/%03d.json" $id }}{{ with os.ReadFile $jsonFile }}{{ $json := . | unmarshal }}{{ index $json "4399" }}{{ end }}
# 1. 构建JSON文件路径:static/images/random/001.json
# 2. 读取对应的JSON文件
# 3. 解析JSON内容
# 4. 获取键名为"4399"的图片URL

license:  # 文章版权声明
comments: false  # 是否开启评论
summary: 小tips……😋  # 文章摘要
categories: [suibi]  # 文章分类
tags: [日常]  # 文章标签
ai: false  # 是否是AI生成的内容
---  # YAML前置参数的结束标记

# 前言  # Markdown格式的文章内容开始
1

# 正文
1
CC BY-NC-SA 4.0 创意的非商业派对入场券
最后更新于 2024-12-27 02:50
晚来天欲雪,能饮一杯无