This commit is contained in:
cupcakearmy 2021-12-22 23:33:58 +01:00
parent 44224ffb85
commit b465c067d9
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
4 changed files with 68 additions and 43 deletions

View File

@ -9219,6 +9219,11 @@ export type GQLPostsOneQueryVariables = Exact<{
export type GQLPostsOneQuery = { readonly __typename?: 'RootQuery', readonly post: { readonly __typename?: 'Post', readonly id: string, readonly slug: string, readonly title: string, readonly content: string, readonly status: string, readonly date: string, readonly modified: string, readonly post: { readonly __typename?: 'Post_Post', readonly featured: { readonly __typename?: 'MediaItem', readonly srcSet: string, readonly altText: string, readonly sourceUrl: string } } } };
export type GQLSearchQueryVariables = Exact<{ [key: string]: never; }>;
export type GQLSearchQuery = { readonly __typename?: 'RootQuery', readonly posts: { readonly __typename?: 'RootQueryToPostConnection', readonly nodes: ReadonlyArray<{ readonly __typename?: 'Post', readonly id: string, readonly slug: string, readonly title: string, readonly content: string, readonly status: string, readonly date: string, readonly modified: string, readonly post: { readonly __typename?: 'Post_Post', readonly featured: { readonly __typename?: 'MediaItem', readonly srcSet: string, readonly altText: string, readonly sourceUrl: string } } }> }, readonly projects: { readonly __typename?: 'RootQueryToProjectConnection', readonly nodes: ReadonlyArray<{ readonly __typename?: 'Project', readonly id: string, readonly slug: string, readonly title: string, readonly content: string, readonly status: string, readonly project: { readonly __typename?: 'Project_Project', readonly date: string, readonly link: string, readonly description: string } }> }, readonly works: { readonly __typename?: 'RootQueryToWorkConnection', readonly nodes: ReadonlyArray<{ readonly __typename?: 'Work', readonly id: string, readonly slug: string, readonly title: string, readonly content: string, readonly status: string, readonly work: { readonly __typename?: 'Work_Work', readonly date: string, readonly link: string, readonly role: string, readonly image: { readonly __typename?: 'MediaItem', readonly srcSet: string, readonly altText: string, readonly sourceUrl: string } } }> } };
export const BasePageFragmentDoc = gql`
fragment BasePage on Page {
id
@ -9362,6 +9367,27 @@ export const PostsOneDocument = gql`
}
}
${BasePostFragmentDoc}`;
export const SearchDocument = gql`
query Search {
posts(first: 100) {
nodes {
...BasePost
}
}
projects(first: 100) {
nodes {
...BaseProject
}
}
works(first: 100) {
nodes {
...BaseWork
}
}
}
${BasePostFragmentDoc}
${BaseProjectFragmentDoc}
${BaseWorkFragmentDoc}`;
export type SdkFunctionWrapper = <T>(action: (requestHeaders?:Record<string, string>) => Promise<T>, operationName: string) => Promise<T>;
@ -9399,6 +9425,9 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
},
PostsOne(variables: GQLPostsOneQueryVariables, requestHeaders?: Dom.RequestInit["headers"]): Promise<GQLPostsOneQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<GQLPostsOneQuery>(PostsOneDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'PostsOne');
},
Search(variables?: GQLSearchQueryVariables, requestHeaders?: Dom.RequestInit["headers"]): Promise<GQLSearchQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<GQLSearchQuery>(SearchDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'Search');
}
};
}

View File

@ -125,3 +125,21 @@ query PostsOne($slug: ID!) {
...BasePost
}
}
query Search {
posts(first: 100) {
nodes {
...BasePost
}
}
projects(first: 100) {
nodes {
...BaseProject
}
}
works(first: 100) {
nodes {
...BaseWork
}
}
}

View File

@ -1,51 +1,29 @@
import { SDK } from '$lib/gql'
import type { GQLBasePageFragment } from '$lib/gql/gen'
import lunr from 'lunr'
// import { BaseAttributes, Call, gql, Page } from '$lib/api'
// function removeHTML(s: string) {
// return s.replace(/<.*?>|\s+|&#\d+;/g, ' ').trim()
// }
function removeHTML(s: string) {
return s.replace(/<.*?>|\s+|&#\d+;/g, ' ').trim()
}
// function convertForIdx(type: string, items: Page[]) {
// const keys: (keyof Page)[] = ['title', 'content', 'slug']
// return items.map((item) => ({
// url: `${type}/${item.slug}`,
// data: keys.map((field) => removeHTML(item[field] ?? '')).join(' '),
// }))
// }
// async function getAll() {
// const all = await Call<Record<'posts' | 'projects' | 'works', { nodes: Page[] }>>(gql`
// query {
// posts(first: 1000) {
// nodes {
// ${BaseAttributes}
// }
// }
// projects(first: 1000) {
// nodes {
// ${BaseAttributes}
// }
// }
// works(first: 1000) {
// nodes {
// ${BaseAttributes}
// }
// }
// }
// `)
// return all
// }
function convertForIdx(type: string, items: GQLBasePageFragment[]) {
const keys: (keyof GQLBasePageFragment)[] = ['title', 'content', 'slug']
return items.map((item) => ({
url: `${type}/${item.slug}`,
data: keys.map((field) => removeHTML(item[field] ?? '')).join(' '),
}))
}
export const get = async () => {
// const all = await getAll()
// const converted = Object.entries(all)
// .map(([type, data]) => convertForIdx(type, data.nodes))
// .flat()
const { __typename, ...all } = await SDK.Search()
const converted = Object.entries(all)
.map(([type, data]) => convertForIdx(type, data.nodes))
.flat()
const idx = lunr(function () {
this.ref('url')
this.field('data')
// converted.forEach((doc) => this.add(doc))
converted.forEach((doc) => this.add(doc))
})
return { body: idx }

View File

@ -21,6 +21,7 @@
export let prebuilt: any
let needle: string | null = null
let results: SearchResultItem[] = []
let input: HTMLInputElement
const idx = lunr.Index.load(prebuilt)
@ -34,7 +35,7 @@
}
}
$: if (needle) {
$: if (needle !== null) {
if (typeof window !== 'undefined') {
window.history.replaceState(null, '', `/search?q=${needle ?? ''}`)
}
@ -43,19 +44,18 @@
onMount(() => {
needle = new URLSearchParams(window.location.search).get('q')
input.focus()
})
</script>
<SimplePage title="Search" expanded={false}>
<input bind:value={needle} placeholder="needle" />
<input bind:this={input} bind:value={needle} placeholder="needle" />
{#if needle}
<ul>
{#each results as result (result.ref)}
<SearchResult {result} />
{/each}
</ul>
{:else}
<p>Start typing...</p>
{/if}
</SimplePage>