mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 16:51:37 +00:00
Add gists to algolia search
This commit is contained in:
86
scripts/gists/index.mjs
Normal file
86
scripts/gists/index.mjs
Normal 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;
|
Reference in New Issue
Block a user