Skip to content

Commit

Permalink
feat: add algolia update script
Browse files Browse the repository at this point in the history
  • Loading branch information
oriverk committed Mar 26, 2024
1 parent 9165879 commit 02565bd
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
export default defineConfig({
site: "https://oriverk.dev",
publicDir: "./public",
// add data-astro-prefetch to anchor element
prefetch: true,
integrations: [
svelte(),
Expand Down
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"script:feed": "node script/getFeed.mjs",
"script:github": "node -r dotenv/config script/getGitHubUserContent.mjs",
"script:cv": "node -r dotenv/config script/getGitHubRepositoryContent.mjs",
"algolia": "node -r dotenv/config script/algolia.mjs",
"astro": "astro",
"sync": "astro sync",
"dev": "astro dev",
Expand Down Expand Up @@ -50,13 +51,16 @@
"clsx": "^2.1.0",
"date-fns": "^3.6.0",
"dotenv": "^16.4.5",
"gray-matter": "^4.0.3",
"hast": "^1.0.0",
"mdast": "^3.0.0",
"rehype-katex": "^7.0.0",
"remark": "^15.0.1",
"remark-comment": "^1.0.0",
"remark-github-alerts": "^0.1.0-beta.3",
"remark-math": "^6.0.0",
"rss-parser": "^3.13.0",
"strip-markdown": "^6.0.0",
"svelte": "^4.2.12",
"unified": "^11.0.4",
"unist-util-inspect": "^8.0.0",
Expand Down
103 changes: 103 additions & 0 deletions script/algolia.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import path from "path"
import fs from "fs-extra";
import { remark } from "remark";
import strip from "strip-markdown";
import matter from "gray-matter";
import algoliasearch from "algoliasearch";

const adminApiKey = process.env.SECRET_ALGOLIA_ADMIN_KEY;
const appId = process.env.PUBLIC_ALGOLIA_APP_ID;
const indexName = process.env.PUBLIC_ALGOLIA_INDEX_BLOG;
const POSTS_PATH = "src/content/blog";

/**
* @param {string} str
* @returns
*/
async function parseMarkdown(str) {
if (!str) return;
const result = await remark().use(strip).process(str)
return result.toString()
}

/**
* @param {string} dir
* @returns string[]
*/
function getAllFiles(dir) {
const result = fs.readdirSync(dir).reduce((files, file) => {
const name = path.join(dir, file);
const isDirectory = fs.statSync(name).isDirectory();
// biome-ignore lint/performance/noAccumulatingSpread: <explanation>
return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name];
}, []);

return result;
}

/**
* @param localFilePath
* ex: /home/oriverk/Codes/oriverk/blog/docs/2022/memo.md
* @returns
*/
async function getPost(localFilePath) {
const source = fs.readFileSync(localFilePath).toString();
const { content, data } = matter(source);
const {
title,
create = "",
update = "",
published = false,
tags = [],
} = data;
const regexp = new RegExp(`${POSTS_PATH}\/|.mdx?$`, "g");
// ex: 2022/20220505-ubuntu2204
const id = localFilePath.replace(regexp, "");
return {
objectID: id,
id,
title,
create,
update,
tags: tags.map((tag) => tag.toLowerCase()).sort() || "",
content: await parseMarkdown(content),
published,
};
}

async function getPosts() {
const fileNames = getAllFiles(POSTS_PATH)
.filter((path) => /\.mdx?$/.test(path));
const promise = fileNames.map(async (fileName) => await getPost(fileName));
const posts = (await Promise.all(promise))
.filter(({ published }) => published)
.sort((a, b) => {
return (
new Date(b.update ?? b.create).getDate() -
new Date(a.update ?? b.create).getDate()
);
});
return posts;
}

async function updateAlgolia(appId, adminApiKey, data) {
const client = algoliasearch(appId, adminApiKey);
const index = client.initIndex(indexName);

try {
await index
.saveObjects(data, {
autoGenerateObjectIDIfNotExist: true,
})
.then(({ objectIDs }) => {
console.log("post data for was successfull pushed to Algolia!");
console.log(objectIDs);
});
} catch (error) {
console.log("Error occurred!");
console.error(error.message);
}
}

const posts = await getPosts();
await updateAlgolia(appId, adminApiKey, posts);

0 comments on commit 02565bd

Please sign in to comment.