一步步搭建 VuePress 及优化【插件系列】

介绍

在以前为了搭建 VuePress 的文档,顺带制做了视频教程,现在准备再次搭建一个 VuePress 的项目,一看本身的视频居然有五个小时,天呐,我只是须要过一遍而已,因此从新整理成文档吧,万一未来又要用呢……html

固然,若是您以为文字版不够直观,能够前往观看视频版: 【☛ 视频地址 ☚】 ,当时录制完本地测试后以为声音大小还能够,结果一套录完了才发现声音很小,因此推荐带上耳机。vue

VuePress 文档示例node

插件列表

为了方便的统一管理 plugin,须要对 docs/.vuepress/config.js 进行配置:git

// docs/.vuepress/config.js
const pluginConf = require('../../config/pluginConf.js');

module.exports = {
  plugins: pluginConf,
}
复制代码

插件的不少服务都须要对 head 标签进行修改:web

// docs/.vuepress/config.js
const headConf = require('../../config/headConf.js');

module.exports = {
  head: headConf,
}
复制代码

以后就能够去修改 config/pluginConf.jsconfig/headConf.js 文件了。npm

1. PWA

具体的 PWA 配置介绍能够看 官方文档,对应的 视频(8:20)json

VuePress 的版本会致使使用方式不一致,此处仅介绍 1.x 版本:bash

安装:app

yarn add -D @vuepress/plugin-pwa@next
# OR npm install -D @vuepress/plugin-pwa@next
复制代码

使用:ide

module.exports = {
  '@vuepress/pwa': {
    serviceWorker: true,
    updatePopup: {
      message: "发现新内容可用.",
      buttonText: "刷新",
      // 自定义弹窗
      // popupComponent: 'MySWUpdatePopup',
    }
  },
};
复制代码

PWA NOTES:

serviceWorker 选项仅仅用来控制 service worker,为了让你的网站彻底地兼容 PWA,你须要在 .vuepress/public 提供 Manifest 和 icons,更多细节,请参见 MDN docs about the Web App Manifest. 此外,只有您可以使用 SSL 部署您的站点时才能启用此功能,由于 service worker 只能在 HTTPs 的 URL 下注册。

​ -- VuePress 官网

由于使用的 Github Pages 服务,因此即便使用 CNAME 后也依然保持 SSL 状态。

Manifest 第六个视频其实存在一些问题,在第九个 视频 中解决了,利用 App Manifest Generator 直接生成便可。

参考示例:

{
  "name": "飞跃高山与大洋的鱼",
  "short_name": "山与海",
  "description": "飞跃高山与大洋的鱼的文档",
  "theme_color": "#2196f3",
  "background_color": "#2196f3",
  "display": "standalone",
  "start_url": "index.html",
  "icons": [
    {
      "src": "/icons/icon-72x72.png",
      "sizes": "72x72",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-96x96.png",
      "sizes": "96x96",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-128x128.png",
      "sizes": "128x128",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-144x144.png",
      "sizes": "144x144",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-152x152.png",
      "sizes": "152x152",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png"
    },
    {
      "src": "/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}
复制代码

还须要获取一下 favicons 等:

// config/headConf.js

module.exports = [
  ['link', { rel: 'apple-touch-icon', href: '/apple-touch-icon.png' }],
  ['link', { rel: 'icon', href: '/favicon-32x32.png' }],
  ['link', { rel: 'manifest', href: '/manifest.json' }],
  ['meta', { name: 'theme-color', content: '#ffffff' }],
];
复制代码

2. 评论(待修改)

评论须要按照你的需求来:若是你但愿全部评论能够在 Github 可见,那么使用 Gitalk 吧,正好有一篇新鲜出炉的文章;若是你想要全部非 Github 用户也能够评论的话能够使用 Valine视频 地址。

这边利用的其实 主题的继承 ,经过修改 VuePress 的默认主题来实现须要的功能,在制做视频的时候官网尚未对这个详细的描述,此次更新发现有了新的介绍,因为时间关系及下一个项目不须要评论因此暂时延期处理:

首先修改默认主题下的 Page 组件(这意味着你不能随便使用 npm install 了):

<!-- node_modules/@vuepress/theme-default/components/Page.vue  -->

      </p>
    </div>

    <slot name="bottom"/>
    <Valine></Valine>
  </main>
</template>
复制代码

接着建立 Valine 组件,对于评论组件有如下要求:

  1. README.md 文件中能够关闭评论;
  2. 在不一样的路由显示不一样的评论
<!-- docs/.vuepress/components/Valine.vue -->

<template>
  <div class="ValineComment" v-if="comment">
    <hr>
    <span :id="page.path" class="leancloud-visitors" :data-flag-title="page.title">
      <em class="post-meta-item-text">文章阅读量 </em>
      <i class="leancloud-visitors-count">1000000+</i>
    </span>
    <div id="vcomments"></div>
  </div>
</template>

<script>
export default {
  computed: {
    comment: function () {
      let { comment } = this.$frontmatter;
      if (typeof comment === 'undefined') {
        return true;
      }
      return comment;
    },
    page: function () {
      let { path = '/', title = '首页' } = this.$page;
      return { path, title };
    }
  },
  mounted() {
    this.renderValine()
  },
  watch: {
    '$route': {
      handler: function (to, from) {
        if (to.path !== from.path) {
          this.$nextTick(() => {
            this.renderValine();
          })
        }
      }
    }
  },
  methods: {
    renderValine() {
      if (typeof window !== 'undefined') {
        this.window = window;
        window.AV = require('leancloud-storage');
      }
      const secretKeyConf = require('../../../config/secretKeyConf.js');
      const Valine = require('valine');
      new Valine({
        el: '#vcomments' ,
        appId: secretKeyConf.appId,
        appKey: secretKeyConf.appKey,
        notify:false,
        verify:false,
        avatar:'retro',
        path: window.location.pathname,
        meta: ['nick','mail','link'],
        pageSize: 10,
        visitor: true,
        placeholder: '欢迎留言...' 
      });
    }
  }
}
</script>

<style lang="stylus" scoped>
.ValineComment {
  padding 0 2rem;
}
.leancloud-visitors {
  display inline-block
  margin-bottom 1rem;
}
</style>
复制代码

3. Back-to-top

具体的 Back-to-top 配置介绍能够看 官方文档,对应的 视频

安装:

yarn add -D @vuepress/plugin-back-to-top@next
# OR npm install -D @vuepress/plugin-back-to-top@next
复制代码

使用:

// config/pluginConf.js

module.exports = {
  '@vuepress/back-to-top': true,
};
复制代码

效果图:

4. google-analytics

具体的 google-analytics 配置介绍能够看 官方文档,对应的 视频

你须要去 Google 获取对应的 key

安装:

yarn add -D @vuepress/plugin-google-analytics@next
# OR npm install -D @vuepress/plugin-google-analytics@next
复制代码

使用:

// config/pluginConf.js
// 此处引伸出的隐私问题在最后有说明

const secretKeyConf = require('./secretKeyConf.js');

module.exports = {
  '@vuepress/google-analytics': {
    'ga': secretKeyConf.ga
  }
};
复制代码

效果:

0. 隐藏私密信息

按理说,会了 git 基本上都知道这个功能,然而依然有不少人把本身的私密信息(如密码)上传到 Github 仓库,对应 视频(29:00)

主要是使用 .gitignore 文件来忽略你要上传的文件,举个例子:

// config/secretKeyConf.js

module.exports = secretKeyConf = {
  appId: 'xxxxxx',
  appKey: 'xxxxxx',
  ga: 'xxxxxx',
  googleSearchConsole: 'xxxxxx',
}
复制代码

config/secretKeyConf.js 追加到 .gitignore 文件中

# secretKeyConf
secretKeyConf.js
复制代码

最后

为了方便阅读,因此将内容进行了划分:

  1. VuePress 初始化及发布
  2. VuePress 插件系列
  3. VuePress 自动化

参考资料

  1. VuePress 官网
  2. Valine 官网
相关文章
相关标签/搜索