-
Notifications
You must be signed in to change notification settings - Fork 189
/
orama.ts
189 lines (171 loc) · 4.86 KB
/
orama.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { Data, Page } from "lume/core/file.ts";
import { walkSync } from "@std/fs/walk";
import { parseExample } from "./examples.page.tsx";
export interface OramaDocument {
path: string;
title: string;
content: string;
section: string;
category: string;
}
export function generateDocumentsForPage(page: Page<Data>): OramaDocument[] {
const documents: OramaDocument[] = [];
const headers = page.document!.querySelectorAll(
"main h1, main h2, main h3, main h4, main h5, main h6",
);
for (const header of headers) {
let headerContent = "";
for (const childNode of header.childNodes) {
if (childNode.classList?.contains("header-anchor")) {
break;
}
headerContent += childNode.textContent?.trim() + " ";
}
let bodyContent = "";
let bodySibling = header.tagName == "H1"
? header.nextElementSibling!.firstElementChild
: header.nextElementSibling;
while (
bodySibling &&
!["H1", "H2", "H3", "H4", "H5", "H6"].includes(bodySibling.tagName)
) {
bodyContent += bodySibling.textContent?.trim() + "\n";
bodySibling = bodySibling.nextElementSibling;
}
documents.push({
path: page.data.url + (header.id ? `#${header.id}` : ""),
title: headerContent.trim(),
content: bodyContent,
section: page.data.title ?? "",
category: page.data.url.startsWith("/runtime/")
? "Runtime"
: (page.data.url.startsWith("/deploy/") ? "Deploy" : "Subhosting"),
});
}
return documents;
}
export async function generateDocumentsForSymbols(): Promise<OramaDocument[]> {
const documents = [];
for (const kind of ["deno", "web", "node"]) {
(globalThis as any).window = globalThis;
delete (globalThis as any).DENO_DOC_SEARCH_INDEX;
await import(`./reference_gen/gen/${kind}/search_index.js`);
const index = (globalThis as any as {
DENO_DOC_SEARCH_INDEX: {
nodes: {
name: string;
url: string;
doc: string;
category: string;
file: string;
}[];
};
}).DENO_DOC_SEARCH_INDEX;
delete (globalThis as any).DENO_DOC_SEARCH_INDEX;
delete (globalThis as any).window;
console.log(
"[orama] Inserting %d symbols for %s",
index.nodes.length,
kind,
);
for (const node of index.nodes) {
documents.push({
path: new URL(node.url, `https://docs.deno.com/api/${kind}/`).pathname
.replace(/\.html$/, ""),
title: node.name,
content: node.doc,
section: `API > ${kind}${node.file !== "." ? ` > ${node.file}` : ""}${
node.category ? ` > ${node.category}` : ""
}`,
category: "Reference",
});
}
}
console.log("[orama] Total %d symbols", documents.length);
return documents;
}
export async function generateDocumentsForExamples(): Promise<OramaDocument[]> {
const files = [...walkSync("./examples/", {
exts: [".ts"],
})];
return await Promise.all(files.map(async (file) => {
const content = await Deno.readTextFile(file.path);
const parsed = parseExample(file.name, content);
const label = file.name.replace(".ts", "");
return {
path: `/examples/${label}`,
title: parsed.title,
content: parsed.description,
section: parsed.group,
category: "Examples",
} as OramaDocument;
}));
}
export async function clear(
apiKey: string,
indexId: string,
) {
const res = await fetch(
`https://api.oramasearch.com/api/v1/webhooks/${indexId}/snapshot`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: "[]",
},
);
if (!res.ok) {
console.error(`Orama clear failed`, res.status, await res.text());
} else {
console.log(`🚀 Orama clear succeeded`);
}
}
export async function notify(
apiKey: string,
indexId: string,
documents: OramaDocument[],
label: string,
) {
const res = await fetch(
`https://api.oramasearch.com/api/v1/webhooks/${indexId}/notify`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({ upsert: documents }),
},
);
if (!res.ok) {
console.error(
`Orama notify '${label}' failed`,
res.status,
await res.text(),
);
} else {
console.log(`🚀 Orama notify '${label}' succeeded`);
}
}
export async function deploy(
apiKey: string,
indexId: string,
) {
const resp = await fetch(
`https://api.oramasearch.com/api/v1/webhooks/${indexId}/deploy`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
},
);
if (!resp.ok) {
console.error("Orama deploy failed", resp.status, await resp.text());
} else {
console.log("🚀 Orama deploy succeeded");
}
}