mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-07 12:57:45 +00:00
92 lines
1.9 KiB
TypeScript
92 lines
1.9 KiB
TypeScript
import Fuse from "fuse.js";
|
|
import * as fs from "fs";
|
|
import * as path from "path";
|
|
import * as api from "utils/api";
|
|
import { PostInfo, CollectionInfo } from "types/index";
|
|
|
|
const posts = api.getPostsByLang("en");
|
|
const collections = api.getCollectionsByLang("en");
|
|
|
|
const createPostIndex = () => {
|
|
return Fuse.createIndex<PostInfo>(
|
|
[
|
|
{
|
|
name: "title",
|
|
weight: 2,
|
|
},
|
|
{
|
|
name: "authorName",
|
|
getFn: (post) => {
|
|
return post.authors
|
|
.map((id) => api.getUnicornById(id, post.locale))
|
|
.join(", ");
|
|
},
|
|
weight: 1.8,
|
|
},
|
|
{
|
|
name: "slug",
|
|
weight: 1.6,
|
|
},
|
|
{
|
|
name: "authorHandles",
|
|
getFn: (post) => {
|
|
return post.authors
|
|
.map((id) => api.getUnicornById(id, post.locale))
|
|
.flatMap((author) => Object.values(author.socials))
|
|
.join(", ");
|
|
},
|
|
weight: 1.2,
|
|
},
|
|
{ name: "tags", weight: 1.5, getFn: (post) => post.tags.join(", ") },
|
|
{ name: "description", weight: 1.2 },
|
|
{ name: "excerpt", weight: 1.2 },
|
|
],
|
|
posts,
|
|
).toJSON();
|
|
};
|
|
|
|
const createCollectionIndex = () => {
|
|
return Fuse.createIndex<CollectionInfo>(
|
|
[
|
|
{
|
|
name: "title",
|
|
weight: 2,
|
|
},
|
|
{
|
|
name: "slug",
|
|
weight: 1.6,
|
|
},
|
|
{
|
|
name: "authorName",
|
|
getFn: (post) => {
|
|
return post.authors
|
|
.map((id) => api.getUnicornById(id, post.locale).name)
|
|
.join(", ");
|
|
},
|
|
weight: 1.8,
|
|
},
|
|
{
|
|
name: "authorHandles",
|
|
getFn: (post) => {
|
|
return post.authors
|
|
.map((id) => api.getUnicornById(id, post.locale))
|
|
.flatMap((author) => Object.values(author.socials))
|
|
.join(", ");
|
|
},
|
|
weight: 1.2,
|
|
},
|
|
{
|
|
name: "description",
|
|
weight: 1.2,
|
|
},
|
|
],
|
|
collections,
|
|
).toJSON();
|
|
};
|
|
|
|
const postIndex = createPostIndex();
|
|
const collectionIndex = createCollectionIndex();
|
|
|
|
const json = JSON.stringify({ postIndex, posts, collectionIndex, collections });
|
|
fs.writeFileSync(path.resolve(process.cwd(), "./api/searchIndex.json"), json);
|