vuepress2.x文档和集成algolia配置过程

此为升级经验输出,主要针对于vuepress2.x集成algolia的过程

背景

文档和官网是acp输出能力的体现之一,原基于vuepress版本为0.x版本,中间做了部分升级,也走了一些问题点,这里同步做个记录。

集成找到资料比较多的方式是按vuepress官网提的使用邮件发送,然后回复,但是经过几次之后,发现周期慢,而且貌似最后也没有见key和index发到邮箱里面。

考虑左右,使用api文档上传的方式,但是vuepress集成的时候,algolia提供了相应的配置,同时可以集成github action进行提交,集成效果如下:

升级集成的仓库地址如下:

https://github.com/alinesno-cloud/alinesno-cloud-platform-press

集成思路

这里自己三种方式集成,这里做一下记录,主要是:

  • shell脚本集成
  • github action集成

Shell脚本集成

shell脚本集成是比较快速的,这里主要几个步骤:

  • 注册官网,获取到key和appid
  • 获取docsearch,配置config.json
  • vuepress集成验证

注册官网

官网地址是:https://www.algolia.com/ref/docsearch 按着步骤注册即可,同时生成应用和获取apikey,官网操作相对比较简单,这里就不过多阐述。

这里需要注意的一点是,index会自动生成,所以只需要key就可以(注意是admin-key)

获取docsearch配置

这里主要在linux上操作可能会比较简单,我这里是mac系统,windows的可以使用github action集成验证,这里是docsearch的下载地址:

https://github.com/algolia/docsearch-scraper

下载之后,生成config.json文件,注意新手尽量使用下面的config.json配置,之前也是验证了几个方式都不太顺利,如下:

{
  "index_name": "acp_linesno",
  "start_urls": [
    {
      "url": "http://alinesno-platform.linesno.com/",
      "selectors_key": "v2",
      "tags": [
        "v2"
      ]
    }
  ],
  "stop_urls": [],
  "selectors": {
    "v2": {
      "lvl0": {
        "selector": ".sidebar-heading.active",
        "global": true,
        "default_value": "Documentation"
      },
      "lvl1": ".theme-default-content h1",
      "lvl2": ".theme-default-content h2",
      "lvl3": ".theme-default-content h3",
      "lvl4": ".theme-default-content h4",
      "lvl5": ".theme-default-content h5",
      "text": ".theme-default-content p, .theme-default-content li",
      "lang": {
        "selector": "/html/@lang",
        "type": "xpath",
        "global": true
      }
    }
  },
  "scrape_start_urls": false,
  "strip_chars": " .,;:#",
  "custom_settings": {
    "attributesForFaceting": [
      "lang",
      "tags"
    ]
  }
}

其实algolia官网也有配置参考,上面是针对于v2版本做过调整,参考的config仓库是这个:

https://github.com/algolia/docsearch-configs

获取到配置之后,运行docsearch文件,第一次运行的时候,会提交让你填写appid和key,主要保存在.env文件里面,然后提交官网索引,生成效果如下:

./docsearch run ./config.json

生成之后,可以在官网的地址查看生成的索引情况,注意上面的应用选择和索引选择,如下:

官网提供了一个测试的办法,进行验证,如下:

./docsearch playground ./config.jso

会生成一个简单的测试界面,这里就不过多的粘贴。

vuepress集成验证

这里集成主要参考官网的链接就可,具体链接地址如下:

https://v2.vuepress.vuejs.org/zh/reference/plugin/docsearch.html

修改好apiid和key,下面是我的配置,也是参考vuepress2官方的:

appId: 'HAT6A1ER66',
apiKey: '1c5e0970f29dd7423d668b1fd245a7e2',
indexName: 'acp_linesno',
searchParameters: {
  facetFilters: ['tags:v2'],  # 注意这个地方
},

github action集成

这里主要参考基线的代码,这里就不做过多的阐述,主要集成的workflow.yaml文件可以从这里获取:

https://github.com/alinesno-cloud/alinesno-cloud-platform-press/blob/2.1.0/.github/workflows/docIndex.yaml

注意一点是,上面是创建了一个.algolia文件来进行配置。

其它

以下即集成algolia的教程,主要参考的资料地址如下:

  • 文件配置:https://github.com/algolia/docsearch-configs
  • 生成索引:https://github.com/algolia/docsearch-scraper
  • 文档配置:https://docsearch.algolia.com/docs/legacy/run-your-own/