Update algolia search

This commit is contained in:
Defectink
2022-04-18 14:56:28 +08:00
parent 0fd635f2b2
commit e0a293ac2e
4 changed files with 61 additions and 53 deletions

View File

@ -20,20 +20,16 @@ export const getHeadings = (source: string) => {
const regex = /<h2 id="(.*?)">(.*?)<\/h2>/g; const regex = /<h2 id="(.*?)">(.*?)<\/h2>/g;
const linkRegx = /id="(.*?)"/; const linkRegx = /id="(.*?)"/;
if (source.match(regex)) { return source.match(regex)?.map((heading) => {
return source.match(regex)?.map((heading) => { const headingText = heading
const headingText = heading .replace(/<h2 id="(.*?)">/, '')
.replace(/<h2 id="(.*?)">/, '') .replace(/<\/h2>/, '');
.replace(/<\/h2>/, '');
const link = '#' + heading.match(linkRegx)?.[1]; const link = '#' + heading.match(linkRegx)?.[1];
return { return {
text: headingText, text: headingText,
link, link,
}; };
}); });
}
return [];
}; };

View File

@ -17,6 +17,7 @@
"@mdx-js/loader": "^2.1.1", "@mdx-js/loader": "^2.1.1",
"@mdx-js/react": "^2.1.0", "@mdx-js/react": "^2.1.0",
"@next/mdx": "^12.1.5", "@next/mdx": "^12.1.5",
"algoliasearch": "^4.13.0",
"classnames": "^2.3.1", "classnames": "^2.3.1",
"next": "12.1.0", "next": "12.1.0",
"next-compose-plugins": "^2.2.1", "next-compose-plugins": "^2.2.1",
@ -36,10 +37,12 @@
"@types/node": "17.0.21", "@types/node": "17.0.21",
"@types/react": "17.0.41", "@types/react": "17.0.41",
"autoprefixer": "^10.4.4", "autoprefixer": "^10.4.4",
"dotenv": "^16.0.0",
"eslint": "8.11.0", "eslint": "8.11.0",
"eslint-config-next": "12.1.0", "eslint-config-next": "12.1.0",
"gray-matter": "^4.0.3", "gray-matter": "^4.0.3",
"jest": "^27.5.1", "jest": "^27.5.1",
"nanoid": "^3.3.2",
"postcss": "^8.4.12", "postcss": "^8.4.12",
"tailwindcss": "^3.0.23", "tailwindcss": "^3.0.23",
"typescript": "4.6.2" "typescript": "4.6.2"

View File

@ -2,45 +2,50 @@ import { config } from 'dotenv';
import algoliasearch from 'algoliasearch/lite.js'; import algoliasearch from 'algoliasearch/lite.js';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import matter from 'gray-matter'; import { nanoid } from 'nanoid';
export const sortByDate = ({ date: a }, { date: b }) => {
if (a < b) {
return 1;
} else if (a > b) {
return -1;
} else {
return 0;
}
};
/** /**
* Read post meta info with gray-matter. * Build post information for Algolia search.
* @param filename * @param filename
* @returns * @returns
*/ */
const readFileMeta = (filename) => { const postLists = () => {
const markdownWithMeta = fs.readFileSync(
path.join('pages/p', filename),
'utf-8'
);
const slug = filename.replace(/\.mdx$/, '');
const { data: meta } = matter(markdownWithMeta);
return {
slug,
...{ ...meta },
};
};
/**
* Read all posts with matter info.
* @returns
*/
export const postLists = async () => {
const files = fs.readdirSync(path.join('pages/p')); const files = fs.readdirSync(path.join('pages/p'));
const posts = files.map(readFileMeta).sort(sortByDate);
return posts; const myPosts = [];
files.map((f, fi) => {
const content = fs.readFileSync(path.join('pages/p', f), 'utf-8');
// const { data: meta, content } = matter(markdownWithMeta);
const slug = f.replace(/\.mdx$/, '');
const regex = /^#{2}(?!#)(.*)/gm;
content.match(regex)?.map((heading, i) => {
myPosts.push({
content: null,
hierarchy: {
lvl0: 'Post',
lvl1: slug,
lvl2: heading.substring(3),
},
type: 'lvl2',
objectID: `${nanoid()}-https://rua.plus/p/${slug}`,
url: 'https://rua.plus/p/' + slug,
});
});
myPosts.push({
content: null,
hierarchy: {
lvl0: 'Post',
lvl1: slug,
},
type: 'lvl1',
objectID: `${nanoid()}-https://rua.plus/p/${slug}`,
url: 'https://rua.plus/p/' + slug,
});
});
return myPosts;
}; };
async function main() { async function main() {
@ -55,9 +60,7 @@ async function main() {
} }
try { try {
const posts = await postLists(); const posts = postLists();
// All objects must have an unique objectID
posts.forEach((p) => (p.objectID = p.slug));
// initialize the client with your environment variables // initialize the client with your environment variables
const client = algoliasearch( const client = algoliasearch(
@ -84,9 +87,10 @@ async function main() {
} }
} }
// (async () => { function test() {
// const posts = await postLists(); const posts = postLists();
// console.log(posts); posts.map((p) => console.log(p));
// })(); }
// test();
main(); main();

View File

@ -5597,6 +5597,11 @@ nanoid@^3.1.30, nanoid@^3.3.1:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.1.tgz#6347a18cac88af88f58af0b3594b723d5e99bb35" resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.1.tgz#6347a18cac88af88f58af0b3594b723d5e99bb35"
integrity sha512-n6Vs/3KGyxPQd6uO0eH4Bv0ojGSUvuLlIHtC3Y0kEO23YRge8H9x1GCzLn28YX0H66pMkxuaeESFq4tKISKwdw== integrity sha512-n6Vs/3KGyxPQd6uO0eH4Bv0ojGSUvuLlIHtC3Y0kEO23YRge8H9x1GCzLn28YX0H66pMkxuaeESFq4tKISKwdw==
nanoid@^3.3.2:
version "3.3.2"
resolved "https://registry.npmmirror.com/nanoid/-/nanoid-3.3.2.tgz#c89622fafb4381cd221421c69ec58547a1eec557"
integrity sha512-CuHBogktKwpm5g2sRgv83jEy2ijFzBwMoYA60orPDR7ynsLijJDqgsi4RDGj3OJpy3Ieb+LYwiRmIOGyytgITA==
napi-build-utils@^1.0.1: napi-build-utils@^1.0.1:
version "1.0.2" version "1.0.2"
resolved "https://registry.npmmirror.com/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806" resolved "https://registry.npmmirror.com/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806"