-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgenerateAlgoliaItems.ts
58 lines (48 loc) · 1.99 KB
/
generateAlgoliaItems.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Algoliasearch from 'algoliasearch';
import Invariant from 'invariant';
import { categoriesToMainCategory, categorySlugToCategory } from './utils/categories';
import { splitContent, trimExcerpt } from './utils/excerpt';
import { getFingerprint } from './utils/fingerprint';
import { toHtml } from './utils/markdown';
import { readAllPosts } from './utils/posts';
import { seriesSlugToSeries } from './utils/series';
async function run() {
Invariant(!!process.env.ALGOLIA_UPDATE_API_KEY, 'ALGOLIA_UPDATE_API_KEY is not set');
const data = await readAllPosts({ includePages: true, includeCommentsCount: false });
const client = Algoliasearch('QB2FWHH99M', process.env.ALGOLIA_UPDATE_API_KEY);
const index = client.initIndex('typeofweb_prod');
const algoliaItems = await Promise.all(
data.posts.map(async (p) => {
const category =
'categories' in p.data
? categoriesToMainCategory(p.data.categories)
: 'category' in p.data
? categorySlugToCategory(p.data.category)
: null;
const series = typeof p.data.series === 'string' ? seriesSlugToSeries(p.data.series) : p.data.series;
const [excerpt, content] = splitContent(p.content);
const compiledContent = (await toHtml(content, { excerpt: false, parseOembed: false })).toString('utf-8');
const compiledExcerpt = await toHtml(excerpt, { excerpt: true, parseOembed: false });
return {
objectID: p.data.permalink,
title: p.data.title,
date: p.data.date,
type: p.data.type,
permalink: p.data.permalink,
authors: p.data.authors,
seo: p.data.seo,
excerpt: trimExcerpt(compiledExcerpt),
content: compiledContent.replace(/<[^>]+>/g, ''),
img: p.data.thumbnail,
category,
series,
};
}),
);
const results = await index.saveObjects(algoliaItems);
console.log(`${results.objectIDs.length} items indexed`);
}
run().catch((err) => {
console.error(err);
process.exit(1);
});