在 Hugo HBstack 主题中使用 Algolia

TL;DR

Algolia 没有将 hugo 的 crawler 模板放上去,因为 hugo 是基于主题的,而每个主题使用的 css 都不一样。

如果你使用 hbstack,你可以参考我的 crawler 配置

new Crawler({
  appId: "xxxxxxxxx",  // 更改为你的appId
  apiKey: "xxxxxxxxxx",  // 更改为你的apiKey
  rateLimit: 8,
  maxDepth: 10,
  startUrls: ["https://travisbikkle.github.io/"],
  // 注意这里的多语言链接,和你hugo.yaml中的配置有关系,你可能是单站点,如zh-hans.youdomain.com
  sitemaps: [
    "https://travisbikkle.github.io/zh-hans/sitemap.xml",
    "https://travisbikkle.github.io/zh-hant/sitemap.xml",
    "https://travisbikkle.github.io/en/sitemap.xml",
  ],
  ignoreCanonicalTo: false,
  discoveryPatterns: ["https://travisbikkle.github.io/**"],
  schedule: "every 1 day at 3:00 pm",
  actions: [
    {
      indexName: "xxxxxxxx", // 更改为你的索引名称
      pathsToMatch: ["https://travisbikkle.github.io/**", "!*.xml"],
      recordExtractor: ({ $, helpers }) => {
        return helpers.docsearch({
          recordProps: {
            lvl0: {
              selectors: "",
              defaultValue: "Title",
            },
            lvl1: [
              "h1.hb-blog-post-title",
              ".hb-blog-post-content h1",
              "h1.hb-docs-doc-title",
              ".breadcrumb-item.active a",
            ],
            lvl2: [".hb-blog-post-content h2", ".hb-docs-doc-content h2"],
            lvl3: [".hb-blog-post-content h3", ".hb-docs-doc-content h3"],
            lvl4: [".hb-blog-post-content h4", ".hb-docs-doc-content h4"],
            lvl5: [".hb-blog-post-content h5", ".hb-docs-doc-content h5"],
            content: [
              ".hb-blog-post-content p, .hb-blog-post-content li",
              ".hb-docs-doc-content p, .hb-docs-doc-content li",
            ],
          },
          indexHeadings: true,
          aggregateContent: true,
          recordVersion: "v3",
        });
      },
    },
  ],
  initialIndexSettings: {
    snzhaoyuaio: {
      attributesForFaceting: ["type", "lang"],
      attributesToRetrieve: ["hierarchy", "content", "anchor", "url"],
      attributesToHighlight: ["hierarchy", "content"],
      attributesToSnippet: ["content:10"],
      camelCaseAttributes: ["hierarchy", "content"],
      searchableAttributes: [
        "unordered(hierarchy.lvl0)",
        "unordered(hierarchy.lvl1)",
        "unordered(hierarchy.lvl2)",
        "unordered(hierarchy.lvl3)",
        "unordered(hierarchy.lvl4)",
        "unordered(hierarchy.lvl5)",
        "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",
    },
  },
});

配置

本文假设你已经向 Algolia 申请了 appId 和 appKey。

你应该参考 HBstack 官方网站的 params.yaml 进行配置,建议 fork 一份全局搜索 docsearch,看懂并不难。

禁用 HBStack 中默认的 search 插件

配置好 Algolia 后,按 CTRL K 会弹出两个窗口。

可以全局搜索 search 并在代码中注释掉,包括 go.mod 以及 yaml 配置,重新发布网站即可。