mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-06 04:21:55 +00:00
chore: add v1 of server search
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -18,4 +18,5 @@ pnpm-debug.log*
|
||||
# macOS-specific files
|
||||
.DS_Store
|
||||
.idea/
|
||||
.eslintcache
|
||||
.eslintcache
|
||||
.vercel
|
||||
|
||||
42
api/search.ts
Normal file
42
api/search.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
|
||||
// import {getAllPosts} from '../src/utils/get-all-posts';
|
||||
|
||||
import exportedIndex from "../searchIndex";
|
||||
import flex from "flexsearch";
|
||||
|
||||
const { Document } = flex;
|
||||
|
||||
const document = new Document({
|
||||
context: true,
|
||||
document: {
|
||||
id: "slug",
|
||||
index: [
|
||||
"slug",
|
||||
"title",
|
||||
"excerpt",
|
||||
"description",
|
||||
"contentMeta",
|
||||
"tags",
|
||||
"authorsMeta",
|
||||
],
|
||||
store: ["slug", "title", "excerpt", "description", "tags", "authorsMeta"],
|
||||
},
|
||||
});
|
||||
|
||||
const keys = Object.keys(exportedIndex);
|
||||
|
||||
let cache = null;
|
||||
|
||||
// const posts = getAllPosts('en');
|
||||
|
||||
export default async (_req: VercelRequest, res: VercelResponse) => {
|
||||
if (!cache) {
|
||||
cache = await Promise.all(
|
||||
keys.map((key) => {
|
||||
return document.import(key, exportedIndex[key]);
|
||||
})
|
||||
);
|
||||
}
|
||||
res.send(document.search("Angular", { enrich: true }));
|
||||
};
|
||||
@@ -31,21 +31,7 @@ export default defineConfig({
|
||||
ssr: {
|
||||
external: ["svgo"],
|
||||
},
|
||||
plugins: [
|
||||
{
|
||||
...copy({
|
||||
hook: "options",
|
||||
flatten: false,
|
||||
targets: [
|
||||
{
|
||||
src: "content/**/*",
|
||||
dest: "public/content",
|
||||
},
|
||||
],
|
||||
}),
|
||||
enforce: "pre",
|
||||
},
|
||||
],
|
||||
plugins: [],
|
||||
},
|
||||
markdown: {
|
||||
mode: "md",
|
||||
|
||||
1130
package-lock.json
generated
1130
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -26,6 +26,7 @@
|
||||
"host:local": "cd dist && ws --http2 --compress",
|
||||
"format": "prettier -w . --cache --plugin-search-dir=.",
|
||||
"lint": "eslint . --ext .js,.ts,.astro",
|
||||
"search-index": "node --experimental-loader esbuild-node-loader src/build-scripts/search-index.ts",
|
||||
"tsc": "tsc --noEmit && astro check",
|
||||
"test": "",
|
||||
"prepare": "husky install"
|
||||
@@ -35,18 +36,22 @@
|
||||
"@remark-embedder/core": "^3.0.1",
|
||||
"@remark-embedder/transformer-oembed": "^3.0.0",
|
||||
"@types/classnames": "^2.3.1",
|
||||
"@types/flexsearch": "^0.7.3",
|
||||
"@types/node": "^18.7.18",
|
||||
"@types/parse5": "^7.0.0",
|
||||
"@types/uuid": "^8.3.4",
|
||||
"@typescript-eslint/eslint-plugin": "^5.38.0",
|
||||
"@typescript-eslint/parser": "^5.38.0",
|
||||
"@vercel/node": "^2.5.18",
|
||||
"astro": "^1.3.0",
|
||||
"astro-icon": "^0.7.3",
|
||||
"classnames": "^2.3.2",
|
||||
"dayjs": "^1.11.5",
|
||||
"esbuild-node-loader": "^0.8.0",
|
||||
"eslint": "^8.23.1",
|
||||
"eslint-plugin-astro": "^0.19.0",
|
||||
"eslint-plugin-jsx-a11y": "^6.6.1",
|
||||
"flexsearch": "^0.7.21",
|
||||
"gatsby-remark-embedder": "^6.0.1",
|
||||
"gray-matter": "^4.0.3",
|
||||
"hast": "^1.0.0",
|
||||
|
||||
34
searchIndex.js
Normal file
34
searchIndex.js
Normal file
File diff suppressed because one or more lines are too long
53
src/build-scripts/search-index.ts
Normal file
53
src/build-scripts/search-index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import flex from "flexsearch";
|
||||
|
||||
const { Document } = flex;
|
||||
|
||||
import { getAllPosts } from "../utils/get-all-posts";
|
||||
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { PostInfo } from "types/PostInfo";
|
||||
|
||||
export const createIndex = async () => {
|
||||
const posts = getAllPosts("en");
|
||||
const document = new Document<PostInfo, Array<keyof PostInfo>>({
|
||||
context: true,
|
||||
document: {
|
||||
id: "slug",
|
||||
index: [
|
||||
"slug",
|
||||
"title",
|
||||
"excerpt",
|
||||
"description",
|
||||
"contentMeta",
|
||||
"tags",
|
||||
"authorsMeta",
|
||||
],
|
||||
store: ["slug", "title", "excerpt", "description", "tags", "authorsMeta"],
|
||||
},
|
||||
});
|
||||
|
||||
posts.forEach((post) => {
|
||||
document.add(post.slug, post);
|
||||
});
|
||||
|
||||
const exportedIndex: Record<string | number, PostInfo> = {};
|
||||
await document.export((key, data) => {
|
||||
exportedIndex[key] = data;
|
||||
});
|
||||
|
||||
// Temporary hotpatch for issue with `export` async
|
||||
// @see https://github.com/nextapps-de/flexsearch/pull/253
|
||||
await new Promise<void>((resolve) => setTimeout(() => resolve(), 1000));
|
||||
|
||||
return exportedIndex;
|
||||
};
|
||||
|
||||
const index = await createIndex();
|
||||
|
||||
const js = `const index = ${JSON.stringify(index)};
|
||||
|
||||
export default index;
|
||||
`;
|
||||
|
||||
fs.writeFileSync(path.resolve(process.cwd(), "./searchIndex.js"), js);
|
||||
@@ -20,6 +20,7 @@ export interface RawPostInfo {
|
||||
export interface PostInfo extends RawPostInfo {
|
||||
slug: string;
|
||||
locale: Languages;
|
||||
contentMeta: string;
|
||||
Content: MarkdownInstance<never>["Content"];
|
||||
authorsMeta: UnicornInfo[];
|
||||
licenseMeta: LicenseInfo;
|
||||
|
||||
@@ -17,7 +17,7 @@ export const rehypeUnicornPopulatePost: Plugin<
|
||||
}
|
||||
|
||||
const fileContents = readFileSync(file.path, "utf8");
|
||||
const { data: frontmatter } = matter(fileContents);
|
||||
const { data: frontmatter, content } = matter(fileContents);
|
||||
|
||||
const directorySplit = file.path.split(path.sep);
|
||||
|
||||
@@ -73,5 +73,6 @@ export const rehypeUnicornPopulatePost: Plugin<
|
||||
setData("authorsMeta", authorsMeta);
|
||||
setData("license", license);
|
||||
setData("frontmatterBackup", frontmatter);
|
||||
setData("contentMeta", content);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user