#!/usr/bin/env node // Builds the Pagefind index that Nextra's docs loads from // /_pagefind/. The root layout renders a fixed , so the Pagefind // CLI would index every page as English and leave Chinese text unsegmented. // Rewrite each page's lang from its /{lang}/ route prefix first, so Pagefind // writes one correctly tokenized index per language. import { readdir, readFile, rm } from "node:fs/promises"; import path from "node:path"; import process from "node:process"; import { fileURLToPath } from "node:url"; import * as pagefind from "pagefind"; const SITE_DIR = ".next/server/app"; const OUTPUT_DIR = "public/_pagefind"; const LOCALE_BY_PREFIX = { en: "en-US", zh: "zh-CN" }; async function* htmlFiles(dir) { for (const entry of await readdir(dir, { withFileTypes: true })) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { yield* htmlFiles(fullPath); } else if (entry.name.endsWith(".html")) { yield fullPath; } } } export function localeForPage(relativePath) { const prefix = relativePath.split(/[/\\.]/)[0]; return LOCALE_BY_PREFIX[prefix]; } export function withHtmlLang(html, locale) { return html.replace( /]*?)\blang="[^"]*"/, ` { console.error(error); process.exitCode = 1; }); }