aboutsummaryrefslogtreecommitdiff
path: root/static/js/pagefind.index.cjs
blob: 1fea6dc94966c24ac109f08d8261fe4c19326593 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Import the path and fs modules to fetch the search index
const path = require("path");
const fs = require("fs");

async function createIndex() {
  // Dynamically import the pagefind module
  const pagefind = await import("pagefind");

  removeOldIndex();

  console.log("Creating index...");

  const publicFolder = path.join(__dirname, "../../public");

  const files = fs.readdirSync(publicFolder);
  let langArray = [];

  files.forEach((file) => {
    if (file.startsWith("search_index")) {
      langArray.push(file.split(".")[1]);
    }
  });

  const { index } = await pagefind.createIndex();
  // Assuming index, fs, and path are already defined and properly imported

  // Convert each lang in langArray to a promise that performs the desired operations
  const promises = langArray.map((lang) =>
    (async () => {
      const filePath = path.join(
        __dirname,
        "../../public/search_index." + lang + ".json"
      );

      // Read the file content synchronously (consider using async readFile for better performance)
      const fileContent = fs.readFileSync(filePath);
      const data = JSON.parse(fileContent);

      // Add each record to the index asynchronously
      for (const record of data) {
        await index.addCustomRecord({
          url: record.url,
          content: record.body,
          language: lang,
          meta: {
            title: record.title,
            description: record.meta,
          },
        });
      }
    })()
  );

  // Execute all promises concurrently
  await Promise.all(promises)
    .then(async () => {
      // Write the index files to disk
      const { errors } = await index.writeFiles({
        outputPath: "./static/js/",
      });
      if (errors.length > 0) {
        console.log("Errors: ", errors);
      }
    })
    .then(async () => {
      // Edit the pagefind to convert from MJS to CJS
      const pagefindPath = path.join(__dirname, "pagefind.js");//source pagefind from node module
      let pagefindContent = fs.readFileSync(pagefindPath, "utf8");
      // Remove 'import.meta.url' from the pagefind file and exports
      pagefindContent = pagefindContent
        .replace(
          /initPrimary\(\)\{([^{}]*\{[^{}]*\})*[^{}]*\}/g,
          `initPrimary(){}`
        ) // Remove annoying function
        .replace(/;export\{[^}]*\}/g, "");
      fs.writeFileSync(pagefindPath, pagefindContent);

      // now insert the CJS into the anonymous function within pagefind.search.js
      const pagefind_searchPath = path.join(__dirname, "pagefind.search.js");//file to insert into
      const search_pagefindPath = path.join(__dirname, "search_pagefind.js");//output
      let pagefind_searchContent = fs.readFileSync(pagefind_searchPath, "utf8");
      // Now insert into pagefind.search.js at this location: //insertHere
      pagefind_searchContent = pagefind_searchContent.replace(/\/\/insertHere/g, pagefindContent);
      fs.writeFileSync(search_pagefindPath, pagefind_searchContent);

    })
    .then(async () => {
      await pagefind.close();
    })
    .catch((error) => {
      console.error("An error occurred:", error);
    });
}

function removeOldIndex() {
  // Remove all the old fragment, index and pagefind files
  const indexFolder = path.join(__dirname, "../../static/js/index");
  const fragmentFolder = path.join(__dirname, "../../static/js/fragment");
  clearFolder(indexFolder);
  clearFolder(fragmentFolder);

  // Delete all files in this format pagefind.*.pf_meta
  const staticFolder = path.join(__dirname, "../../static/js");
  const files = fs.readdirSync(staticFolder);
  files.forEach((file) => {
    if (file.startsWith("pagefind") && file.endsWith(".pf_meta")) {
      fs.unlinkSync(path.join(staticFolder, file));
    }
  });
}

function clearFolder(directoryPath) {
  try {
    // Read all the files in the directory
    const files = fs.readdirSync(directoryPath);

    // Iterate over each file and delete it
    for (const file of files) {
      const filePath = path.join(directoryPath, file);
      fs.unlinkSync(filePath);
    }
  } catch (error) {
    // Ignore the error if the directory does not exist, as that is a good behaviour :)
    if (error.code !== 'ENOENT') {
      console.error("An error occurred:", error);
    }
  }
}

module.exports = createIndex;