Skip to content

vitepress配置使用algolia

vitepress三方教程

algolia网址

algolia控制台

algolia爬虫api

申请使用Algolia DocSearch

docsearch 申请地址

提交申请表单不成功需要科学上网(似乎不申请也可自己创建账号添加索引、爬虫等)

申请成功后,Algolia官方会给你填写的邮箱发送邮件,提醒你接受邀请

这里有几点需要注意:

  • 您必须是该网站的所有者,或至少有更新其内容的权限
  • 你的网站必须是公开的
  • 你的网站必须是一个开源项目或技术博客的技术文档,不授权于商业内容
  • 你的网站必须到生产环境

TIP

如果是点击邮件里的邀请地址,Algolia会默认创建好一个应用,且应用中已经有一个索引,如果没有,自己单独创建一个即可。

algolia控制台优化配置

配置语言优化

配置vitepress

配置 .vitepress/config.js中的themeConfig选项中的search

appId和apiKey

indexName

js
const themeConfig = {
    /**
     * 其它themeConfig配置
     */
    // 搜索
    search: {
        provider: 'algolia',
        options: {
            // appId 和 apiKey看上面图片地方复制
            appId: 'NDR4MJ654N',
            apiKey: 'c76b36eaab0b39fbda9a6cb1804f3301',
            // 上面图片中的indexName
            indexName: 'shangjundragon',
            // 网站首页 爬虫起始页面
            start_urls: ["https://shangjundragon.netlify.app"],
            // vitepress 需要配置生成sitemap.xml 会在根路径自动生成sitemap.xml
            sitemaps: ['https://shangjundragon.netlify.app/sitemap.xml'],
            searchParameters: {   // 新增搜索参数
                hitsPerPage: 10,          // 每页结果数
                attributesToSnippet: ['content:30'],  // 从内容中截取片段
                snippetEllipsisText: '...',          // 截断符号
                distinct: true,           // 去重相似结果
                typoTolerance: 'min',     // 容错级别
                advancedSyntax: true      // 启用高级搜索语法
            },
            translations: {
                button: {
                    buttonText: '搜索文档',
                    buttonAriaLabel: '搜索文档'
                },
                modal: {
                    searchBox: {
                        resetButtonTitle: '清除查询条件',
                        resetButtonAriaLabel: '清除查询条件',
                        cancelButtonText: '取消',
                        cancelButtonAriaLabel: '取消'
                    },
                    startScreen: {
                        recentSearchesTitle: '搜索历史',
                        noRecentSearchesText: '没有搜索历史',
                        saveRecentSearchButtonTitle: '保存至搜索历史',
                        removeRecentSearchButtonTitle: '从搜索历史中移除',
                        favoriteSearchesTitle: '收藏',
                        removeFavoriteSearchButtonTitle: '从收藏中移除'
                    },
                    errorScreen: {
                        titleText: '无法获取结果',
                        helpText: '你可能需要检查你的网络连接'
                    },
                    footer: {
                        selectText: '选择',
                        navigateText: '切换',
                        closeText: '关闭',
                        searchByText: '搜索提供者'
                    },
                    noResultsScreen: {
                        reportMissingResultsText: '你认为该有搜索结果?',
                        reportMissingResultsLinkText: '点击反馈',
                        noResultsText: '暂时没有找到相关内容',
                        suggestedQueryText: '试试这些关键词:',
                        suggestedQueries: ['入门指南', 'API 参考', '常见问题'],
                    }
                }
            }
        }
    },

}

export default themeConfig

ok

/src/technology/dateblog/2025/04/20250425-vitepress%E9%85%8D%E7%BD%AE%E4%BD%BF%E7%94%A8algolia.html