-
Notifications
You must be signed in to change notification settings - Fork 0
/
algolia-queries.ts
83 lines (76 loc) · 1.49 KB
/
algolia-queries.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
interface PageNode {
readonly id: string;
readonly frontmatter: {
title: string;
slug: string;
cover: {
publicURL: string;
};
tags: string[];
};
readonly excerpt: string;
}
interface PageQueryResult {
readonly data: {
pages: {
edges: {
node: PageNode;
}[];
};
};
}
interface AlgoliaRecord {
readonly objectID: string;
readonly title: string;
readonly slug: string;
readonly excerpt: string;
readonly cover: {
publicURL: string;
};
readonly tags: string[];
}
interface AlgoliaQuery {
readonly query: string;
readonly transformer: (result: PageQueryResult) => AlgoliaRecord[];
readonly indexName: string;
readonly settings: {
attributesToSnippet: string[];
};
}
const pageQuery = `{
pages: allMdx {
edges {
node {
id
frontmatter {
title
slug
cover {
publicURL
}
tags
}
excerpt(pruneLength: 120)
}
}
}
}`;
function pageToAlgoliaRecord({ node }: { node: PageNode }): AlgoliaRecord {
const { id, frontmatter, excerpt } = node;
return {
objectID: id,
...frontmatter,
excerpt,
};
}
const queries: AlgoliaQuery[] = [
{
query: pageQuery,
transformer: ({ data }) => {
return data.pages.edges.map(pageToAlgoliaRecord);
},
indexName: process.env.GATSBY_ALGOLIA_INDEX_NAME ?? "Posts",
settings: { attributesToSnippet: [`excerpt:20`] },
},
];
export default queries;