-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathorama_docs_reindex.ts
113 lines (101 loc) · 2.95 KB
/
orama_docs_reindex.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
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
import { pooledMap } from "@std/async/pool";
import { stripSplitBySections } from "@deno/gfm";
import { extractYaml } from "@std/front-matter";
import GitHubSlugger from "github-slugger";
import TOC from "../frontend/docs/toc.ts";
import { join } from "@std/path";
const index = Deno.env.get("ORAMA_DOCS_INDEX_ID");
const auth = Deno.env.get("ORAMA_DOCS_PRIVATE_API_KEY");
export interface OramaDocsHit {
path: string;
header: string;
headerParts: string[];
slug: string;
content: string;
}
const ORAMA_URL = "https://api.oramasearch.com/api/v1/webhooks";
// Clear the index
const resp1 = await fetch(`${ORAMA_URL}/${index}/snapshot`, {
method: "POST",
headers: {
authorization: `Bearer ${auth}`,
"Content-Type": "application/json",
},
body: JSON.stringify([]),
});
if (!resp1.ok) {
throw new Error(
`Failed to clear index: ${resp1.status} ${await resp1.text()}`,
);
}
// fill the index
const path = "frontend/docs/";
const results = pooledMap(
10,
TOC,
async (entry) => {
const file = await Deno.readTextFile(
join(path, entry.id + ".md"),
);
const {
body,
attrs,
// deno-lint-ignore no-explicit-any
} = extractYaml<any>(file);
const slugger = new GitHubSlugger();
const sections = stripSplitBySections(body);
if (sections[0].header === "" && sections[0].content !== "") {
sections[0].header = attrs.title ?? entry.title;
sections[0].depth = 1;
} else if (sections[0].header === "" && sections[0].content === "") {
sections[0].header = attrs.title ?? entry.title;
sections[0].content = attrs.description;
sections[0].depth = 1;
}
return sections.map((section, i) => {
const headerParts: string[] = [section.header];
let currentDepth = section.depth;
for (let j = i; currentDepth > 1 && j >= 0; j--) {
if (sections[j].depth < currentDepth) {
headerParts.unshift(sections[j].header);
currentDepth = sections[j].depth;
}
}
return {
path: entry.id,
header: section.header,
headerParts,
slug: slugger.slug(section.header),
content: section.content,
} satisfies OramaDocsHit;
});
},
);
const entries = (await Array.fromAsync(results)).flat();
const resp2 = await fetch(`${ORAMA_URL}/${index}/notify`, {
method: "POST",
headers: {
authorization: `Bearer ${auth}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ "upsert": entries }),
});
if (!resp2.ok) {
throw new Error(
`Failed to upsert index: ${resp2.status} ${await resp2.text()}`,
);
}
// deploy the index
const resp3 = await fetch(`${ORAMA_URL}/${index}/deploy`, {
method: "POST",
headers: {
authorization: `Bearer ${auth}`,
"Content-Type": "application/json",
},
});
if (!resp3.ok) {
throw new Error(
`Failed to deploy index: ${resp3.status} ${await resp3.text()}`,
);
}