vuepress2使用docsearch插件facetFilters多个lang参数,导致搜索不到内容

242 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

vuepress2使用docsearch插件的时候facetFilterslang会多一个lang:zh-CN的参数,导致搜索不到内容。 在这里插入图片描述 vuepress2下使用docsearch的依赖逻辑:plugin-docsearchdocsearchalgoliasearch 只需要修改打开node_modules/@vuepress/plugin-docsearch/lib/client/components/Docsearch.js文件 删掉lang:${lang.value}即可 在这里插入图片描述 删除脚本

cd node_modules/@vuepress/plugin-docsearch/lib/client/components/

# 去掉 lang 对象

sed -i .js s/\`lang:\$\{lang.value\}\`,//g Docsearch.js

目前不清楚是plugin-docsearch的bug还是我的algolia配置文件问题。

Github上有相关的问题 Docsearch not working,不过没太看明白,没找到解决方案。

我的配置项目,可以参考以下

   docsearchPlugin({
      // 配置项
      appId: '***',
      apiKey: '***',
      indexName: 'jaqi.note',
      contextualSearch: true,
      searchParameters: {
      },
      locales: {
        '/': {
          lang: 'zh-CN',
          placeholder: '搜索文档',
          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: {
                noResultsText: '无法找到相关结果',
                suggestedQueryText: '你可以尝试查询',
                reportMissingResultsText: '你认为该查询应该有结果?',
                reportMissingResultsLinkText: '点击反馈',
              },
            },
          },
        },
      },
    })
new Crawler({
  appId: "***",
  apiKey: "***",
  rateLimit: 8,
  startUrls: ["https://jaqi.gitee.io/jaqi.note"],
  sitemaps: ["https://jaqi.gitee.io/jaqi.note"],
  ignoreCanonicalTo: false,
  exclusionPatterns: [],
  discoveryPatterns: ["https://jaqi.gitee.io/jaqi.note/**"],
  schedule: "at 02:00 every 1 day",
  actions: [
    {
      indexName: "jaqi.note",
      pathsToMatch: ["https://jaqi.gitee.io/jaqi.note/**"],
      recordExtractor: ({ $, helpers }) => {
        // @vuepress/theme-default 的选项
        return helpers.docsearch({
          recordProps: {
            lvl0: {
              selectors: ".sidebar-heading.active",
              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",
            lvl6: ".theme-default-content h6",
            content: ".theme-default-content p, .theme-default-content li",
          },
          indexHeadings: true,
        });
      },
    },
  ],
  initialIndexSettings: {
    "jaqi.note": {
      attributesForFaceting: ["type", "lang"],
      attributesToRetrieve: ["hierarchy", "content", "anchor", "url"],
      attributesToHighlight: ["hierarchy", "hierarchy_camel", "content"],
      attributesToSnippet: ["content:10"],
      camelCaseAttributes: ["hierarchy", "hierarchy_radio", "content"],
      searchableAttributes: [
        "unordered(hierarchy_radio_camel.lvl0)",
        "unordered(hierarchy_radio.lvl0)",
        "unordered(hierarchy_radio_camel.lvl1)",
        "unordered(hierarchy_radio.lvl1)",
        "unordered(hierarchy_radio_camel.lvl2)",
        "unordered(hierarchy_radio.lvl2)",
        "unordered(hierarchy_radio_camel.lvl3)",
        "unordered(hierarchy_radio.lvl3)",
        "unordered(hierarchy_radio_camel.lvl4)",
        "unordered(hierarchy_radio.lvl4)",
        "unordered(hierarchy_radio_camel.lvl5)",
        "unordered(hierarchy_radio.lvl5)",
        "unordered(hierarchy_radio_camel.lvl6)",
        "unordered(hierarchy_radio.lvl6)",
        "unordered(hierarchy_camel.lvl0)",
        "unordered(hierarchy.lvl0)",
        "unordered(hierarchy_camel.lvl1)",
        "unordered(hierarchy.lvl1)",
        "unordered(hierarchy_camel.lvl2)",
        "unordered(hierarchy.lvl2)",
        "unordered(hierarchy_camel.lvl3)",
        "unordered(hierarchy.lvl3)",
        "unordered(hierarchy_camel.lvl4)",
        "unordered(hierarchy.lvl4)",
        "unordered(hierarchy_camel.lvl5)",
        "unordered(hierarchy.lvl5)",
        "unordered(hierarchy_camel.lvl6)",
        "unordered(hierarchy.lvl6)",
        "content",
      ],
      distinct: true,
      attributeForDistinct: "url",
      customRanking: [
        "desc(weight.pageRank)",
        "desc(weight.level)",
        "asc(weight.position)",
      ],
      ranking: [
        "words",
        "filters",
        "typo",
        "attribute",
        "proximity",
        "exact",
        "custom",
      ],
      highlightPreTag: '<span class="algolia-docsearch-suggestion--highlight">',
      highlightPostTag: "</span>",
      minWordSizefor1Typo: 3,
      minWordSizefor2Typos: 7,
      allowTyposOnNumericTokens: false,
      minProximity: 1,
      ignorePlurals: true,
      advancedSyntax: true,
      attributeCriteriaComputedByMinProximity: true,
      removeWordsIfNoResults: "allOptional",
    },
  },
});

示例