Skip to content

Commit 2a4c91e

Browse files
committed
fix(search): include English pages in the en search index
source.getPages() without a locale argument only returns default-locale (zh) pages, so the static /search.en.json index has been empty since the i18n split. Enumerate pages with getPages("en") and classify English pages by the .en.md(x) file suffix in addition to lang frontmatter, so translations missing lang: en are indexed and excluded from the zh shard.
1 parent 5b7e4ce commit 2a4c91e

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

app/search.en.json/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { pageToIndex, isEnglishPage } from "@/lib/search-index";
55
export const dynamic = "force-static";
66

77
/**
8-
* 英文搜索索引分片:只包含 lang==="en" 的翻译版文档
8+
* 英文搜索索引分片:按 frontmatter 或 .en 文件名识别翻译版文档
99
* 用 Orama 默认英文分词(无需 mandarin tokenizer)。
1010
*/
1111
const api = createSearchAPI("advanced", {
1212
indexes: () =>
13-
Promise.all(source.getPages().filter(isEnglishPage).map(pageToIndex)),
13+
Promise.all(source.getPages("en").filter(isEnglishPage).map(pageToIndex)),
1414
language: "english",
1515
search: {
1616
threshold: 0.3,

lib/search-index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ export async function pageToIndex(page: Page): Promise<AdvancedIndex> {
3939

4040
/**
4141
* 判断一个 fumadocs 页面是否为英文翻译版。
42-
* 翻译版 frontmatter 会声明 `lang: "en"` 且通常 `translatedFrom: "zh"`
42+
* 兼容 frontmatter 语言标记和 Fumadocs 保留的原始 source path
4343
*/
44-
export function isEnglishPage(page: Page): boolean {
44+
export function isEnglishPage(page: { data: unknown; path: string }): boolean {
4545
const lang = (page.data as PageData).lang;
46-
return lang === "en";
46+
return lang === "en" || /\.en\.mdx?$/i.test(page.path);
4747
}

tests/search-index-locale.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
vi.mock("@/lib/source", () => ({ source: { getPages: () => [] } }));
4+
5+
import { isEnglishPage } from "@/lib/search-index";
6+
7+
describe("search index locale classification", () => {
8+
it("classifies English source filenames even without lang frontmatter", () => {
9+
expect(
10+
isEnglishPage({
11+
path: "learn/cs/data-structures/array/index.en.mdx",
12+
data: {},
13+
}),
14+
).toBe(true);
15+
expect(isEnglishPage({ path: "career/example.en.md", data: {} })).toBe(
16+
true,
17+
);
18+
expect(isEnglishPage({ path: "career/example.md", data: {} })).toBe(false);
19+
});
20+
21+
it("retains explicit lang frontmatter classification", () => {
22+
expect(
23+
isEnglishPage({ path: "career/legacy.mdx", data: { lang: "en" } }),
24+
).toBe(true);
25+
});
26+
});

0 commit comments

Comments
 (0)