一个具有强大功能的 vuepress 主题仓库✨

在vuepress或者vdoing主题项目中,其实已经内置了一些第三方插件,比如百度统计,第三方搜索,代码块一件复制,评论区等等。
想看用了什么插件,看package.json就可以。
以下是我的安装的一些依赖

  1. "devDependencies": {
  2. "dayjs": "^1.9.7",
  3. "inquirer": "^7.1.0",
  4. "json2yaml": "^1.1.0",
  5. "vuepress": "1.8.0",
  6. "vuepress-plugin-baidu-autopush": "^1.0.1",
  7. "vuepress-plugin-baidu-tongji": "^1.0.1",
  8. "vuepress-plugin-demo-block": "^0.7.2",
  9. "vuepress-plugin-one-click-copy": "^1.0.2",
  10. "vuepress-plugin-thirdparty-search": "^1.0.2",
  11. "vuepress-plugin-zooming": "^1.1.7",
  12. "vuepress-theme-vdoing": "^1.8.1",
  13. "yamljs": "^0.3.0"
  14. },
  15. "dependencies": {
  16. "busuanzi.pure.js": "^1.0.3",
  17. "valine": "^1.4.14",
  18. "vuepress-plugin-comment": "^0.7.3"
  19. }

我的博客:https://wggz.top

不蒜子 - 页脚统计

VuePress添加不蒜子插件(一个网页计数器)
官方文档

1. 添加依赖

  • yarn安装

    1. yarn add busuanzi.pure.js
  • npm安装

    1. npm install busuanzi.pure.js --save

    2. 修改meta

    head.js 中添加,写法如下:

    1. ['meta', {name: 'referrer', content: 'no-referrer-when-downgrade'}],

    在 Chrome 85 版本中,为了保护用户的隐私,默认的 Referrer Policy 则变成了 strict-origin-when-cross-origin
    所以必须加入此部分代码,否则文章统计访问量的数据则不正确

    3. 代码

    在本地主题的Components文件夹中,新建Busuanzi.vue文件 ```javascript

  1. <a name="JkkNL"></a>
  2. ### 4. 修改Footer.vue文件
  3. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/12828909/1640006170787-c9e5b6da-e21e-46ba-823c-cc081c9e5c69.png#clientId=u7a0a1aaf-c121-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=313&id=u136f3138&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1300&originWidth=2144&originalType=binary&ratio=1&rotation=0&showTitle=false&size=249684&status=done&style=none&taskId=ua7f24804-bd16-4616-b702-8ebad367bc3&title=&width=516)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12828909/1640006205517-da7ea90f-2d52-45ed-ba0a-779dbf6dcf54.png#clientId=u7a0a1aaf-c121-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=324&id=u2e9207ae&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1338&originWidth=2112&originalType=binary&ratio=1&rotation=0&showTitle=false&size=217873&status=done&style=none&taskId=u1c81742c-ba81-48bd-9164-9fbe26581cd&title=&width=512)<br />完事儿~<br />**效果**<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/12828909/1640006267671-eee169ea-3161-4bfa-b24f-64178dd8bb1c.png#clientId=u7a0a1aaf-c121-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=84&id=u31165495&margin=%5Bobject%20Object%5D&name=image.png&originHeight=256&originWidth=890&originalType=binary&ratio=1&rotation=0&showTitle=false&size=31856&status=done&style=shadow&taskId=ua2be9401-0634-46a1-8938-a1e1e700c18&title=&width=292)
  4. <a name="BxQTD"></a>
  5. ## 不蒜子 - 页面pv统计
  6. ArticleInfo.vue添加如下代码,替换掉原先的阅读量统计代码
  7. ```javascript
  8. <div
  9. id="busuanzi_container_site_pv"
  10. class="icon iconfont icon-eye"
  11. title="浏览量"
  12. >
  13. <span id="busuanzi_value_page_pv"></span>
  14. </div>

在.vuepress中的themeConfig.js中,配置

  1. isShowReadTime: true, // 是否展示阅读时间
  2. // wordsPerMinute: 100, // 每分钟阅读的字数 默认是 300
  3. // isShowReadCount: true, // 是否开启阅读量统计

效果
image.png

鼠标点击爱心

基本上,下载vdoing这个主题,都会自带这个插件,不过作者没有开启
image.png
我们把它开启即可,很简单
plugins.js中,添加如下代码

  1. [require('../plugins/love-me'), { // 鼠标点击爱心特效
  2. color: '#fb7299', // 爱心颜色,默认随机色
  3. excludeClassName: 'theme-vdoing-content' // 要排除元素的class, 默认空''
  4. }],

阅读时间/阅读量

可看这里

创建如下
image.png
index.js

  1. const readingTime = require('./read-time')
  2. const readingTimePlugin = (options, context) => {
  3. return {
  4. name: "reading-time",
  5. extendPageData($page){
  6. $page.readingTime = readingTime(
  7. $page._strippedContent,
  8. options.wordsPerMinute || context.themeConfig.wordsPerMinute || 300
  9. );
  10. },
  11. };
  12. };
  13. module.exports = readingTimePlugin;

read-time.js

  1. // see:https://github.com/vuepress-theme-hope/vuepress-theme-hope/tree/v1/packages/reading-time
  2. // 匹配所有的英文单词
  3. const getWords = (data) =>
  4. data.match(/[\w\d\s\u00C0-\u024F]+/giu) || [];
  5. // 匹配中文
  6. const getChinese = (data) =>
  7. data.match(/[\u4E00-\u9FA5]/gu) || [];
  8. const getWordNumber = (data) =>
  9. getWords(data).reduce(
  10. (accumulator, word) =>
  11. accumulator + (word.trim() === "" ? 0 : word.trim().split(/\s+/u).length),
  12. 0
  13. ) + getChinese(data).length;
  14. // data -> 插件函数上下文提供的页面的元数据
  15. // wordsPerMinute 配置项 每分钟读取多少字 默认是300
  16. const readingTime = (
  17. data,
  18. wordsPerMinute = 300
  19. ) => {
  20. debugger
  21. const words = getWordNumber(data || "");
  22. return {
  23. minutes: Math.round(words / wordsPerMinute ),
  24. words,
  25. };
  26. };
  27. module.exports = readingTime

plugins.js中添加

  1. [require('../plugins/read')], // 阅读时间插件

themeConfig.js中添加

  1. isShowReadTime: true, // 是否展示阅读时间
  2. // wordsPerMinute: 100, // 每分钟阅读的字数 默认是 300
  3. isShowReadCount: true, // 是否开启阅读量统计

效果
image.png

Gitalk评论插件

Gitalk实现静态博客无后台评论系统
Gitalk demoGitalk github官方文档
使用一个别人已经开发好的 vuepress-plugin-comment插件来帮助我们把Gitalk应用到vuepress搭建的静态博客。

如果下载的是vdoing主题项目,那么就不用操心了。只需要去Github申请到Client ID 和 Client Secret。替换到原先作者的Client ID,Client Secret,仓库名称等信息就可以了

1. 安装

  1. npm install --save vuepress-plugin-comment

2. 使用

options的配置和Gitalk的配置相同

  1. module.exports = {
  2. plugins: [
  3. [
  4. 'vuepress-plugin-comment',
  5. {
  6. choosen: 'gitalk',
  7. options: {
  8. clientID: 'GitHub Application Client ID',
  9. clientSecret: 'GitHub Application Client Secret',
  10. repo: 'GitHub repo',
  11. owner: 'GitHub repo owner',
  12. admin: ['GitHub repo owner and collaborators, only these guys can initialize github issues'],
  13. distractionFreeMode: false
  14. }
  15. }
  16. ]
  17. ]
  18. }

需要 GitHub Application,如果没有 点击这里申请Authorization callback URL 填写当前使用插件页面的域名。
03-配置vuepress插件 - 图5

申请完成就会得 Client ID 和 Client Secret。然后把对应参数填写到配置中,例:

  1. module.exports = {
  2. plugins: [
  3. [
  4. 'vuepress-plugin-comment',
  5. {
  6. choosen: 'gitalk',
  7. options: {
  8. clientID: 'a6e*******4709b88b',
  9. clientSecret: 'f0e***************beb8b2d54d7241',
  10. repo: 'blog', // GitHub 仓库
  11. owner: 'xugaoyi', // GitHub仓库所有者
  12. admin: ['xugaoyi'], // 对仓库有写权限的人
  13. distractionFreeMode: false
  14. }
  15. }
  16. ]
  17. ]
  18. }

配置好之后重启项目就发现页面上多了一个评论区,说明评论功能实现啦。但还是有一些bug,继续完善它~

3. BUG修复

评论区与博客样式不匹配
解决办法:新增全局样式文件.vuepress/styles/index.styl,写入样式

  1. #vuepress-plugin-comment
  2. max-width $contentWidth
  3. margin 0 auto
  4. padding 2rem 2.5rem
  5. @media (max-width: $MQNarrow)
  6. padding 2rem
  7. @media (max-width: $MQMobileNarrow)
  8. padding 1.5rem

评论区出现 Error: Validation Failed.
问题分析:当页面 链接过长 或 存在中文链接,导致整个链接字符串长度超过50时,会造成请求issues接口失败,出现422状态码。(中文链接会自动转码,变得很长,id参数默认取的是链接,长度不能超过50)

解决办法:手动设置id取值,限制长度不超过50。

  1. {
  2. choosen: 'gitalk',
  3. options: {
  4. ...
  5. id: "<%- (window.location.origin + (frontmatter.to.path || window.location.pathname)).slice(-50) %>", // 页面的唯一标识,长度不能超过50
  6. title: "「评论」<%- document.title %>", // GitHub issue 的标题
  7. labels: ["Gitalk", "Comment"], // GitHub issue 的标签
  8. body:"<%- document.title %>:<%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>" // GitHub issue 的内容
  9. }
  10. }

访问变量时,如 $frontmatterwindow等,请使用 EJS 语法。因为在配置中不能使用回调函数,会被vuepress过滤,因此使用 EJS 语法。 ——插件作者文档说明

切换页面后评论区内容还是上一个页面的评论
解决:id在获取path时使用 frontmatter.to.path,插件内置了 `frontmatter.fromfrontmatter.to

  1. {
  2. choosen: 'gitalk',
  3. options: {
  4. ...
  5. id: "<%- (window.location.origin + (frontmatter.to.path || window.location.pathname)).slice(-50) %>", // 页面的唯一标识,长度不能超过50
  6. title: "「评论」<%- document.title %>", // GitHub issue 的标题
  7. labels: ["Gitalk", "Comment"], // GitHub issue 的标签
  8. body:"<%- document.title %>:<%- window.location.origin + (frontmatter.to.path || window.location.pathname) %>" // GitHub issue 的内容
  9. }
  10. }

参考:使用Gitalk实现静态博客无后台评论系统

百度统计

在.vuepress中找到plugins.js
其中会自带百度统计的插件

  1. [
  2. 'vuepress-plugin-baidu-tongji', // 百度统计
  3. {
  4. hm: 'eb72710daa92c************',
  5. },
  6. ],
  1. 访问百度统计,添加你的网站
  2. 完成验证。复制此处的hm码替换到上述plugins.js中的代码

image.png
完成验证后,即可在百度统计页面看到你的网站的pvuv情况