Add gists to algolia search

This commit is contained in:
DefectingCat
2022-09-01 11:56:21 +08:00
parent b0b7ed47e9
commit 92b6e52828
4 changed files with 198 additions and 85 deletions

86
scripts/gists/index.mjs Normal file
View File

@ -0,0 +1,86 @@
/* @ts-check */
import { Octokit } from 'octokit';
/**
* Get gists.
* @param {number} page
* @param {number} perPage
* @returns
*/
async function getGists(page, perPage) {
const password = process.env.NEXT_PUBLIC_GITHUB_API;
const octokit = new Octokit({
auth: password,
// @TODO reverse proxy
baseUrl: 'http://api.github.com',
});
return await octokit.rest.gists.list({
page,
per_page: perPage,
});
}
async function generateRecords(gists, result) {
const pushGist = (d) => {
const url = `https://rua.plus/g/${d.id}`;
const files = d.files;
const record = {
content: null,
hierarchy: {
lvl0: 'Gist',
lvl1: d.description,
},
type: `lvl1`,
objectID: url,
url,
};
gists.push(record);
Object.keys(files).map((key) => {
gists.push({
...record,
hierarchy: {
...record.hierarchy,
lvl2: files[key].filename,
},
type: 'lvl2',
});
});
};
result.data.map(pushGist);
}
/**
* Generate all gists search records.
*/
async function generateGists() {
const linkMatch = /<(.*?)>/;
const relMatch = /"(.*?)"/;
const perPage = 50;
const result = await getGists(1, perPage);
const link = result.headers.link?.split(',');
const pageSize = {
prev: null,
next: null,
last: null,
first: null,
};
link.map((l) => {
const text = l.match(relMatch)?.[1];
if (!text) return;
const page = new URLSearchParams(l.match(linkMatch)?.[1].split('?')[1]).get(
'page'
);
pageSize[text] = Number(page);
});
const gists = [];
generateRecords(gists, result);
for (let i = pageSize.next; i <= pageSize.last; i++) {
generateRecords(gists, await getGists(i, perPage));
}
return gists;
}
export default generateGists;