diff --git a/lib/utils/index.ts b/lib/utils/index.ts
index b7fd467..c0b9acd 100644
--- a/lib/utils/index.ts
+++ b/lib/utils/index.ts
@@ -20,20 +20,16 @@ export const getHeadings = (source: string) => {
const regex = /
(.*?)<\/h2>/g;
const linkRegx = /id="(.*?)"/;
- if (source.match(regex)) {
- return source.match(regex)?.map((heading) => {
- const headingText = heading
- .replace(//, '')
- .replace(/<\/h2>/, '');
+ return source.match(regex)?.map((heading) => {
+ const headingText = heading
+ .replace(//, '')
+ .replace(/<\/h2>/, '');
- const link = '#' + heading.match(linkRegx)?.[1];
+ const link = '#' + heading.match(linkRegx)?.[1];
- return {
- text: headingText,
- link,
- };
- });
- }
-
- return [];
+ return {
+ text: headingText,
+ link,
+ };
+ });
};
diff --git a/package.json b/package.json
index e419f9f..6a6f468 100644
--- a/package.json
+++ b/package.json
@@ -17,6 +17,7 @@
"@mdx-js/loader": "^2.1.1",
"@mdx-js/react": "^2.1.0",
"@next/mdx": "^12.1.5",
+ "algoliasearch": "^4.13.0",
"classnames": "^2.3.1",
"next": "12.1.0",
"next-compose-plugins": "^2.2.1",
@@ -36,10 +37,12 @@
"@types/node": "17.0.21",
"@types/react": "17.0.41",
"autoprefixer": "^10.4.4",
+ "dotenv": "^16.0.0",
"eslint": "8.11.0",
"eslint-config-next": "12.1.0",
"gray-matter": "^4.0.3",
"jest": "^27.5.1",
+ "nanoid": "^3.3.2",
"postcss": "^8.4.12",
"tailwindcss": "^3.0.23",
"typescript": "4.6.2"
diff --git a/scripts/build-search.mjs b/scripts/build-search.mjs
index 95e0798..9cb27ce 100644
--- a/scripts/build-search.mjs
+++ b/scripts/build-search.mjs
@@ -2,45 +2,50 @@ import { config } from 'dotenv';
import algoliasearch from 'algoliasearch/lite.js';
import fs from 'fs';
import path from 'path';
-import matter from 'gray-matter';
-
-export const sortByDate = ({ date: a }, { date: b }) => {
- if (a < b) {
- return 1;
- } else if (a > b) {
- return -1;
- } else {
- return 0;
- }
-};
+import { nanoid } from 'nanoid';
/**
- * Read post meta info with gray-matter.
+ * Build post information for Algolia search.
* @param filename
* @returns
*/
-const readFileMeta = (filename) => {
- 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 postLists = () => {
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() {
@@ -55,9 +60,7 @@ async function main() {
}
try {
- const posts = await postLists();
- // All objects must have an unique objectID
- posts.forEach((p) => (p.objectID = p.slug));
+ const posts = postLists();
// initialize the client with your environment variables
const client = algoliasearch(
@@ -84,9 +87,10 @@ async function main() {
}
}
-// (async () => {
-// const posts = await postLists();
-// console.log(posts);
-// })();
+function test() {
+ const posts = postLists();
+ posts.map((p) => console.log(p));
+}
+// test();
main();
diff --git a/yarn.lock b/yarn.lock
index c41142b..5a42525 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5597,6 +5597,11 @@ nanoid@^3.1.30, nanoid@^3.3.1:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.1.tgz#6347a18cac88af88f58af0b3594b723d5e99bb35"
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:
version "1.0.2"
resolved "https://registry.npmmirror.com/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806"