Skip to content

批量给文章添加封面

批量给文章添加封面

目录

[toc]

需求1-formatter自动注入文章封面功能

bash
formatter自动注入文章封面功能:

1.可以设置个开关,由用户自己决定是否开启次功能;

2.图片路径可以是本地(和banner壁纸同一个路径,例如/img/bg/1.png),也可以是在线url(将封面壁纸上传到自己图床后,然后把多个壁纸 url写到一个列表里)。然后随机自动注入到每一篇md或者新创建的md。

实现次功能的好处是:

封面图自动注入后,就可以解放双手,不用手动单独给每篇新建的md添加封面了,首页文章排版一致,都有封面,不然有的有封面,有的忘记加封面了,就很丑。

解决办法(测试成功)

  • 环境

警告

次配置适用于Teek@1.2.0-2025.5.12版本(其它版本可自行测试)。

  • 编辑docs\.vitepress\config.mts文件
ts
    // 自动格式formatter插件 添加文章封面图
    autoFrontmatterOption: {
      transform: frontmatter => {
       // 如果文件本身存在了 coverImg,则不生成
       if (frontmatter.coverImg) return;
        
       // 随机获取 coverImg
       const list = [
        "https://onedayxyy.cn/images/1.webp",
        "https://onedayxyy.cn/images/2.webp",
        "https://onedayxyy.cn/images/3.webp",
        "https://onedayxyy.cn/images/4.webp",
        "https://onedayxyy.cn/images/5.webp",
        "https://onedayxyy.cn/images/6.webp",
        "https://onedayxyy.cn/images/7.webp",
        "https://onedayxyy.cn/images/8.webp",
        "https://onedayxyy.cn/images/9.webp",
        "https://onedayxyy.cn/images/10.webp",
        "https://onedayxyy.cn/images/11.webp",
        "https://onedayxyy.cn/images/12.webp",
        "https://onedayxyy.cn/images/13.webp",
        "https://onedayxyy.cn/images/14.webp",
        "https://onedayxyy.cn/images/15.webp",
        "https://onedayxyy.cn/images/16.webp",
        "https://onedayxyy.cn/images/17.webp",
        "https://onedayxyy.cn/images/18.webp",
        "https://onedayxyy.cn/images/19.webp",
       ];
        
       const coverImg = list[Math.floor(Math.random() * list.length)];
        
       const transformResult = { ...frontmatter, coverImg };
        
       return Object.keys(transformResult).length ? transformResult : undefined;
      },
    },

image-20250515211217424

  • 运行,观察效果(完美)

image-20250515211247023

需求2-Teek能实现 “批量操作front matter工具”吗?

bash
Teek能实现 “批量操作front matter工具”吗?

需求背景:
现在我从网上剽了10张好看的妹纸图片,想让这些好看的妹纸做我的博客所有文章的封面,但是昨天已经用frontmatter 自动生成了壁纸,此时该怎么办呢?(感觉简便的方法  就是 只能利用autofrontmatter先删除coverImg信息,再重新生成coverImg信息)

解决办法1(测试成功)

利用vscode的批量替换功能;(经实际测试可实现我的需求😜)

image-20250515210816804

  • 效果

image-20250515210829200

最近更新