Skip to content

Commit c10f4d0

Browse files
committed
Update: index blogs for search
1 parent bef7071 commit c10f4d0

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

.github/workflows/publish.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ jobs:
2626
client_email: ${{ secrets.CLIENT_EMAIL }}
2727
private_key_id: ${{ secrets.PRIVATE_KEY_ID }}
2828
client_cert_url: ${{ secrets.CLIENT_CERT_URL }}
29+
MEILISEARCH_API_KEY: ${{ secrets.MEILISEARCH_API_KEY }}
30+
MEILISEARCH_HOST: ${{ secrets.MEILISEARCH_HOST }}

publish/index.js

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import firebaseAdmin from "firebase-admin";
22
import fs from "fs";
33
import path from "path";
4+
import { MeiliSearch } from "meilisearch";
5+
46
import { parseMarkdown } from "./parser.js";
57

8+
const client = new MeiliSearch({
9+
host: process.env.MEILISEARCH_HOST,
10+
apiKey: process.env.MEILISEARCH_API_KEY,
11+
});
12+
613
firebaseAdmin.initializeApp({
714
credential: firebaseAdmin.credential.cert({
815
type: "service_account",
@@ -44,46 +51,66 @@ async function main() {
4451
const blogFileNames = getBlogsFileNames();
4552

4653
for (let filename of blogFileNames) {
47-
const fileNameWithoutExtension = filename.split(".")[0].split("/").pop();
48-
const blogExists = (
49-
await firestore.collection("blogs").doc(fileNameWithoutExtension).get()
50-
).exists;
54+
try {
55+
const fileNameWithoutExtension = filename.split(".")[0].split("/").pop();
56+
const blogExists = (
57+
await firestore.collection("blogs").doc(fileNameWithoutExtension).get()
58+
).exists;
5159

52-
const blog = getBlogFileContent(filename);
60+
const blog = getBlogFileContent(filename);
5361

54-
const { source, content, data } = await parseMarkdown({ markdown: blog });
62+
const { source, content, data } = await parseMarkdown({ markdown: blog });
5563

56-
if (!blogExists) {
57-
await firestore
58-
.collection("blogs")
59-
.doc(fileNameWithoutExtension)
60-
.set(
61-
{
62-
source,
63-
content: content,
64-
...data,
65-
dateCreated: data.dateCreated.toUTCString(),
66-
dateUpdated: new Date().toUTCString(),
67-
likes: 0,
68-
link: fileNameWithoutExtension,
69-
},
70-
{ merge: true },
71-
);
72-
} else {
73-
await firestore
74-
.collection("blogs")
75-
.doc(fileNameWithoutExtension)
76-
.set(
64+
if (!blogExists) {
65+
await firestore
66+
.collection("blogs")
67+
.doc(fileNameWithoutExtension)
68+
.set(
69+
{
70+
source,
71+
content: content,
72+
...data,
73+
dateCreated: data.dateCreated.toUTCString(),
74+
dateUpdated: new Date().toUTCString(),
75+
likes: 0,
76+
link: fileNameWithoutExtension,
77+
},
78+
{ merge: true },
79+
);
80+
} else {
81+
await firestore
82+
.collection("blogs")
83+
.doc(fileNameWithoutExtension)
84+
.set(
85+
{
86+
source,
87+
content: content,
88+
...data,
89+
dateCreated: data.dateCreated.toUTCString(),
90+
dateUpdated: new Date().toUTCString(),
91+
link: fileNameWithoutExtension,
92+
},
93+
{ merge: true },
94+
);
95+
}
96+
97+
try {
98+
await client.index("blogs").addDocuments([
7799
{
78-
source,
79-
content: content,
80-
...data,
81-
dateCreated: data.dateCreated.toUTCString(),
82-
dateUpdated: new Date().toUTCString(),
100+
id: fileNameWithoutExtension,
101+
title: data.title,
102+
content,
103+
author: data.author,
104+
tags: data.tags,
83105
link: fileNameWithoutExtension,
106+
dateCreated: new Date(data.dateCreated).toISOString(),
107+
description: data.description,
84108
},
85-
{ merge: true },
86-
);
109+
]);
110+
} catch (error) {}
111+
} catch (error) {
112+
console.log("Error with file: " + filename);
113+
console.error(error);
87114
}
88115
}
89116
}

0 commit comments

Comments
 (0)