• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

easyops-cn / docusaurus-search-local / 16433921444

22 Jul 2025 03:34AM UTC coverage: 60.246%. First build
16433921444

Pull #530

github

web-flow
Merge 3d2ac4e49 into 13de464a1
Pull Request #530: Fix non-deterministic search-index.json generation

421 of 714 branches covered (58.96%)

Branch coverage included in aggregate %.

26 of 28 new or added lines in 3 files covered. (92.86%)

658 of 1077 relevant lines covered (61.1%)

7.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/docusaurus-search-local/src/server/utils/postBuildFactory.ts
1
import fs from "fs";
2
import path from "path";
3
import util from "util";
4
import {
5
  ProcessedPluginOptions,
6
  PostBuildData,
7
  SearchDocument,
8
} from "../../shared/interfaces";
9
import { buildIndex } from "./buildIndex";
10
import { debugInfo } from "./debug";
11
import { processDocInfos } from "./processDocInfos";
12
import { scanDocuments } from "./scanDocuments";
13

14
const writeFileAsync = util.promisify(fs.writeFile);
×
15

16
export function postBuildFactory(
17
  config: ProcessedPluginOptions,
18
  searchIndexFilename: string
19
) {
20
  return async function postBuild(buildData: PostBuildData): Promise<void> {
×
21
    debugInfo("gathering documents");
×
22

23
    const data = processDocInfos(buildData, config);
×
24

25
    debugInfo("parsing documents");
×
26

27
    for (const versionData of data) {
×
28
      // Give every index entry a unique id so that the index does not need to store long URLs.
29
      const allDocuments = await scanDocuments(versionData.paths, config);
×
30

31
      debugInfo("building index");
×
32

33
      const docsByDirMap = new Map<string, SearchDocument[][]>();
×
34
      const {
35
        searchContextByPaths,
36
        hideSearchBarWithNoSearchContext,
37
        useAllContextsWithNoSearchContext,
38
      } = config;
×
39
      if (searchContextByPaths) {
×
40
        const { baseUrl } = buildData;
×
41
        const rootAllDocs: SearchDocument[][] = [];
×
42
        if (!hideSearchBarWithNoSearchContext) {
×
43
          docsByDirMap.set("", rootAllDocs);
×
44
        }
45
        let docIndex = 0;
×
46
        for (const documents of allDocuments) {
×
47
          rootAllDocs[docIndex] = [];
×
48
          for (const doc of documents) {
×
49
            if (doc.u.startsWith(baseUrl)) {
×
50
              const uri = doc.u.substring(baseUrl.length);
×
51
              const matchedPaths: string[] = [];
×
52
              for (const _path of searchContextByPaths) {
×
53
                const path = typeof _path === "string" ? _path : _path.path;
×
54
                if (uri === path || uri.startsWith(`${path}/`)) {
×
55
                  matchedPaths.push(path);
×
56
                }
57
              }
58
              for (const matchedPath of matchedPaths) {
×
59
                let dirAllDocs = docsByDirMap.get(matchedPath);
×
60
                if (!dirAllDocs) {
×
61
                  dirAllDocs = new Array(allDocuments.length);
×
62
                  docsByDirMap.set(matchedPath, dirAllDocs);
×
63
                }
64
                let dirDocs = dirAllDocs[docIndex];
×
65
                if (!dirDocs) {
×
66
                  dirAllDocs[docIndex] = dirDocs = [];
×
67
                }
68
                dirDocs.push(doc);
×
69
              }
70
              if (
×
71
                matchedPaths.length > 0 &&
×
72
                !useAllContextsWithNoSearchContext
73
              ) {
74
                continue;
×
75
              }
76
            }
77
            rootAllDocs[docIndex].push(doc);
×
78
          }
79
          docIndex++;
×
80
        }
81
      } else {
82
        docsByDirMap.set("", allDocuments);
×
83
      }
84

NEW
85
      for (const [k, allDocs] of Array.from(docsByDirMap.entries()).sort(([a], [b]) => a.localeCompare(b))) {
×
86
        const searchIndex = buildIndex(allDocs, config);
×
87

88
        debugInfo(`writing index (/${k}) to disk`);
×
89

90
        await writeFileAsync(
×
91
          path.join(
92
            versionData.outDir,
93
            searchIndexFilename.replace(
94
              "{dir}",
95
              k === "" ? "" : `-${k.replace(/\//g, "-")}`
×
96
            )
97
          ),
98
          JSON.stringify(searchIndex),
99
          { encoding: "utf8" }
100
        );
101

102
        debugInfo(`index (/${k}) written to disk successfully!`);
×
103
      }
104
    }
105
  };
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc