deno.land / x / manual@v1.28.2 / .tools / main.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
import { createFetchRequester } from "@algolia/requester-fetch";import algoliasearch from "algoliasearch";import dax from "dax";import { MarkdownRecord, toRecords } from "markdown_records";import { config } from "std/dotenv/mod.ts";
interface Section { name: string; children?: { [child: string]: string; };}
interface TableOfContents { [section: string]: Section;}
const MANUAL_INDEX = "manual";
dax.logStep("Generate manual search records...");
dax.logStep("Parsing manual toc...");
const toc: TableOfContents = (await import("../toc.json", { assert: { type: "json" } })).default;
dax.logLight(` ${Object.keys(toc).length} sections in toc.`);
interface SearchRecordHierarchy { [lvl: string]: string | null;}
type SearchRecord = Omit<MarkdownRecord, "hierarchy"> & { objectID: string; docPath: string; level: number; hierarchy: SearchRecordHierarchy;};
function mergeHierarchy( docHierarchy: string[], recordHierarchy: string[],): [SearchRecordHierarchy, number] { const entries: [string, string | null][] = []; let count = 0; for (const item of docHierarchy) { entries.push([`lvl${count++}`, item]); } if ( docHierarchy[docHierarchy.length - 1].toLowerCase() === recordHierarchy[0]?.toLowerCase() ) { recordHierarchy.shift(); } for (const item of recordHierarchy) { entries.push([`lvl${count++}`, item]); } const level = count; while (count < 7) { entries.push([`lvl${count++}`, null]); } return [Object.fromEntries(entries) as SearchRecordHierarchy, level];}
function markdownToSearch( docHierarchy: string[], docPath: string, { hierarchy: recordHierarchy, ...record }: MarkdownRecord, idx: number,): SearchRecord { const objectID = `${docPath}-${idx}`; const [hierarchy, level] = mergeHierarchy(docHierarchy, recordHierarchy); return { objectID, level, docPath, hierarchy, ...record, };}
let searchRecords: SearchRecord[] = [];
for (const [id, section] of Object.entries(toc)) { let content: string; try { content = await Deno.readTextFile(`../${id}.md`); } catch (err) { dax.logError(`Error attempting to read "/${id}.md".`); console.log(err); continue; } const docPath = `/manual/${id}`; const hierarchy = [section.name]; dax.logLight(` generating "${docPath}"...`); const records = (await toRecords(content)) .map((record, i) => markdownToSearch(hierarchy, docPath, record, i)); searchRecords = searchRecords.concat(records); if (section.children) { for (const [childId, child] of Object.entries(section.children)) { let content: string; try { content = await Deno.readTextFile(`../${id}/${childId}.md`); } catch (err) { dax.logError(`Error attempting to read "/${id}/${childId}.md".`); console.log(err); continue; } const docPath = `/manual/${id}/${childId}`; const hierarchy = [section.name, child]; dax.logLight(` generating "${docPath}"...`); const records = (await toRecords(content)) .map((record, i) => markdownToSearch(hierarchy, docPath, record, i)); searchRecords = searchRecords.concat(records); } }}
dax.logStep(`Uploading ${searchRecords.length} search records to algolia...`);
await config({ export: true });const appId = Deno.env.get("ALGOLIA_APP_ID") ?? "";const apiKey = Deno.env.get("ALGOLIA_API_KEY") ?? "";const requester = createFetchRequester();const app = algoliasearch(appId, apiKey, { requester });
dax.logLight(` deleting objects from "${MANUAL_INDEX}"...`);const manualIndex = app.initIndex(MANUAL_INDEX);await manualIndex.clearObjects();
dax.logLight(` uploading objects to "${MANUAL_INDEX}"...`);const response = await app.multipleBatch( searchRecords.map((body) => ({ indexName: MANUAL_INDEX, action: "addObject", body, })),);
dax.logLight(` uploaded ${response.objectIDs.length}.`);
dax.logStep("Done.");// algolia holds open things for some reason...Deno.exit(0);
manual

Version Info

Tagged at
a year ago