Skip to content

搜索的配置

注意,以前的版本是import {defineConfig} from 'vitepress'

本地

import {defineConfig} from 'vite'
export default defineConfig({
	themeConfig:{
	    search:{
           provider:'local'
        },
	}
})
import {defineConfig} from 'vite'
export default defineConfig({
	themeConfig:{
	    search:{
           provider:'local'
        },
	}
})

使用插件

npm install vitepress-plugin-search -D
npm install vitepress-plugin-search -D
import {defineConfig} from 'vite'
import {SearchPlugin} from 'vitepress-plugin-search'
export default defineConfig({
	vite: {
      plugins: [
        SearchPlugin({
          previewLength: 80, // 这个选项决定了搜索结果预览的长度,单位是字符数
          buttonLabel: "搜索", // 这个选项可以用来改变搜索按钮的标签
          placeholder: "搜索文档", // 这个选项可以用来设置搜索输入框的占位符
          allow: [], // 这是一个数组,你可以在这个数组中指定哪些页面可以被搜索
          ignore: [] // 这也是一个数组,你可以在这个数组中指定哪些页面不被搜索
        })
      ]
    },
})
import {defineConfig} from 'vite'
import {SearchPlugin} from 'vitepress-plugin-search'
export default defineConfig({
	vite: {
      plugins: [
        SearchPlugin({
          previewLength: 80, // 这个选项决定了搜索结果预览的长度,单位是字符数
          buttonLabel: "搜索", // 这个选项可以用来改变搜索按钮的标签
          placeholder: "搜索文档", // 这个选项可以用来设置搜索输入框的占位符
          allow: [], // 这是一个数组,你可以在这个数组中指定哪些页面可以被搜索
          ignore: [] // 这也是一个数组,你可以在这个数组中指定哪些页面不被搜索
        })
      ]
    },
})

使用aligio

网址:https://www.algolia.com/

“Dashboard” 按钮前往后台。这个进不去,比较麻烦,就没搞

我也不知道怎么回事,好像等了两个星期就能搜索到了,他可能需要一段时间来抓取数据

import {defineConfig} from 'vite'
export default defineConfig({
	themeConfig:{
	    algolia: {	//根据注册邮箱的来填写
          appId: "看邮箱",
          apiKey: "看邮箱",
          indexName: "看邮箱",
          placeholder: '请输入关键词',
          buttonText: '搜索',
        },
	}
})
import {defineConfig} from 'vite'
export default defineConfig({
	themeConfig:{
	    algolia: {	//根据注册邮箱的来填写
          appId: "看邮箱",
          apiKey: "看邮箱",
          indexName: "看邮箱",
          placeholder: '请输入关键词',
          buttonText: '搜索',
        },
	}
})
申请Algolia DocSearch服务

访问Algolia官网,填写网站地址、邮箱和开源地址(DocSearch要求网站必须是开源的)。 如果网站地址和开源地址相同,可以直接使用博客的可访问地址。

我的网站地址和开源地址是一样的,我的博客是可以访问的就没必要放到托管平台

回复邮件

最后等待邮件就行了

申请后,Algolia会发送两封邮件,一封确认申请通过,另一封通知爬取完成。这个过程可能需要几天时间。 在项目中,使用defineConfig函数配置Algolia的appId、apiKey和indexName。这些信息来自Algolia发送的邮件。

最后一个还要在根目录添加一个文件crawlerConfig.json
{
    "start_urls": ["你的网址"],
    "rateLimit": 10,
    "maxDepth": 20,
    "selectors": {
      "lvl0": {
        "selector": "",
        "global": true,
        "defaultValue": "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",
      "content": ".theme-default-content p, .theme-default-content li"
    },
    "selectors_exclude": [
      "aside",
      ".page-footer",
      ".next-and-prev-link",
      ".table-of-contents"
    ],
    "scrape_start_urls": false,
    "js_render": true,
    "user_agent": "My Bot 1.0"
  }
{
    "start_urls": ["你的网址"],
    "rateLimit": 10,
    "maxDepth": 20,
    "selectors": {
      "lvl0": {
        "selector": "",
        "global": true,
        "defaultValue": "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",
      "content": ".theme-default-content p, .theme-default-content li"
    },
    "selectors_exclude": [
      "aside",
      ".page-footer",
      ".next-and-prev-link",
      ".table-of-contents"
    ],
    "scrape_start_urls": false,
    "js_render": true,
    "user_agent": "My Bot 1.0"
  }

完成后,等待Algolia的爬虫抓取网站数据。这个过程可能需要一段时间,大约半个月左右。

程序员小洛文档