fix: search index name error

This commit is contained in:
xfy
2025-05-19 20:22:53 +08:00
parent 7b6bf536ea
commit d05a01ea9e
3 changed files with 16 additions and 16 deletions

View File

@ -2,19 +2,17 @@ import { algoliasearch } from 'algoliasearch';
/**
* Generate algolia records.
* @params -t for test.
* @params -g add gitsts to records.
*/
/* @ts-check */
import { config } from 'dotenv';
// import { liteClient } from 'algoliasearch/lite';
import generateGists from './gists/index.mjs';
// import generateGists from './gists/index.mjs';
import { config } from 'dotenv';
import postLists from './posts/index.mjs';
async function generateRecords(gists) {
const records = await postLists();
return gists ? records.concat(await generateGists()) : records;
async function generateRecords() {
return postLists();
}
async function pushAlgolia(gists) {
async function pushAlgolia() {
if (
!process.env.NEXT_PUBLIC_ALGOLIA_APP_ID &&
!process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_ADMIN_KEY
@ -23,7 +21,7 @@ async function pushAlgolia(gists) {
}
try {
const records = await generateRecords(gists);
const records = await generateRecords();
const client = algoliasearch(
process.env.NEXT_PUBLIC_ALGOLIA_APP_ID,
@ -31,7 +29,7 @@ async function pushAlgolia(gists) {
);
const response = await client.clearObjects({ indexName: 'rua' });
console.log('Clean rua index success');
console.log('Clean rua index success', response);
const algoliaResponse = await client.saveObjects({
indexName: 'rua',
objects: records,
@ -46,8 +44,8 @@ async function pushAlgolia(gists) {
}
}
async function test(gists) {
const records = await generateRecords(gists);
async function test() {
const records = await generateRecords();
console.log(records);
}
@ -57,9 +55,8 @@ function main() {
const args = process.argv.slice(2);
const isTest = args.some((arg) => arg === '-t');
const gists = args.some((arg) => arg === '-g');
isTest ? test(gists) : pushAlgolia(gists);
isTest ? test() : pushAlgolia();
}
main();

View File

@ -1,6 +1,7 @@
import path from 'path';
/* @ts-check */
import fs from 'fs/promises';
import matter from 'gray-matter';
const dataPath = 'content/posts';
@ -15,8 +16,9 @@ const postLists = async () => {
const myPosts = [];
const getFiles = async (f) => {
const content = await fs.readFile(path.join(dataPath, f), 'utf-8');
// const { content: meta, content } = matter(markdownWithMeta);
const markdownWithMeta = await fs.readFile(path.join(dataPath, f), 'utf-8');
const { content, data: meta } = matter(markdownWithMeta);
// console.log('meta', meta);
const slug = f.replace(/\.mdx$/, '');
const regex = /^#{2,3}(?!#)(.*)/gm;
@ -40,6 +42,7 @@ const postLists = async () => {
type: `lvl${level}`,
objectID: url,
url,
tag: meta.tags,
};
switch (level) {