|
1 | 1 | import { readFileSync } from "node:fs"; |
2 | 2 | import { globSync } from "glob"; |
3 | | -import { load } from "cheerio"; |
| 3 | +import { generalPurposeCrawler } from "@orama/crawly"; |
4 | 4 | import "dotenv/config"; |
5 | 5 |
|
6 | 6 | const ORAMA_PRIVATE_API_KEY = process.env.ORAMA_PRIVATE_API_KEY; |
7 | 7 | const ORAMA_PRIVATE_INDEX_ID = process.env.ORAMA_PRIVATE_INDEX_ID; |
8 | 8 |
|
9 | 9 | const baseURL = new URL("../dist", import.meta.url).pathname; |
10 | 10 | const HTMLFiles = globSync("**/*.html", { cwd: baseURL }); |
11 | | -const headers = ["h1", "h2", "h3", "h4", "h5", "h6"]; |
12 | 11 |
|
13 | 12 | const pagesToIndex = HTMLFiles.flatMap((file) => { |
14 | 13 | const path = file.replace(/\.html$/, ""); |
15 | 14 | const pageContent = readFileSync( |
16 | 15 | new URL(`../dist/${file}`, import.meta.url), |
17 | 16 | "utf8" |
18 | 17 | ); |
19 | | - const $ = load(pageContent); |
20 | 18 |
|
21 | | - const results = []; |
22 | | - const title = $("h1").first().text().trim(); |
23 | | - let currentSection = ""; |
24 | | - let currentContent = ""; |
25 | | - |
26 | | - $("*").each(function () { |
27 | | - const isHeader = $(this).is(headers.join(", ")); |
28 | | - if (isHeader) { |
29 | | - if (currentSection && currentContent) { |
30 | | - results.push({ |
31 | | - title: title, |
32 | | - content: currentContent.trim(), |
33 | | - path: getPath(path), |
34 | | - category: getCategory(getPath(path)), |
35 | | - section: currentSection, |
36 | | - }); |
37 | | - } |
38 | | - if (!$(this).is("h1")) { |
39 | | - currentSection = $(this) |
40 | | - .clone() |
41 | | - .children() |
42 | | - .remove("script") |
43 | | - .end() |
44 | | - .text() |
45 | | - .trim(); |
46 | | - } |
47 | | - currentContent = ""; |
48 | | - } else if ($(this).is("p")) { |
49 | | - currentContent += `${$(this).clone().children().remove("script").end().text().trim()} `; |
50 | | - } |
51 | | - }); |
52 | | - |
53 | | - if (currentSection && currentContent) { |
54 | | - results.push({ |
55 | | - title: title, |
56 | | - content: currentContent.trim(), |
57 | | - path: getPath(path), |
58 | | - category: getCategory(getPath(path)), |
59 | | - section: currentSection, |
60 | | - }); |
61 | | - } |
62 | | - |
63 | | - return results; |
| 19 | + const productionDocsURL = `https://docs.solidjs.com/${path}`; |
| 20 | + return generalPurposeCrawler(productionDocsURL, pageContent); |
64 | 21 | }); |
65 | 22 |
|
66 | | -function getPath(path) { |
67 | | - return path.endsWith("index") ? path.replace(/index$/, "") : path; |
68 | | -} |
69 | | - |
70 | | -function getCategory(path) { |
71 | | - const category = path.split("/")[0]; |
72 | | - return category === "index" ? "" : category; |
73 | | -} |
74 | | - |
75 | 23 | async function emptyIndex() { |
76 | 24 | await fetch( |
77 | 25 | `https://api.oramasearch.com/api/v1/webhooks/${ORAMA_PRIVATE_INDEX_ID}/snapshot`, |
|
0 commit comments