From dd2d6be5c173715dd96635ef1ab7fee93d84ff81 Mon Sep 17 00:00:00 2001 From: FjellOverflow Date: Fri, 25 Oct 2024 22:52:51 +0300 Subject: [PATCH] feat: global search --- package.json | 4 +- pnpm-lock.yaml | 55 ++++++++++++++++ src/components/layout/Header.astro | 8 +++ src/layouts/PageLayout.astro | 6 +- src/layouts/PostLayout.astro | 3 +- src/pages/search.astro | 101 +++++++++++++++++++++++++++++ 6 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 src/pages/search.astro diff --git a/package.json b/package.json index c744735..0ff388e 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "check": "pnpm lint && astro check", "build": "astro build", "preview": "pnpm build && astro preview", - "release": "pnpm check && commit-and-tag-version --sign" + "release": "pnpm check && commit-and-tag-version --sign", + "postbuild": "pagefind --site dist" }, "dependencies": { "@astrojs/check": "^0.9.4", @@ -42,6 +43,7 @@ "astro": "^4.16.7", "mdast-util-to-string": "^4.0.0", "medium-zoom": "^1.1.0", + "pagefind": "^1.1.1", "reading-time": "^1.5.0", "satori": "^0.11.2", "sharp": "^0.33.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 49668b8..cd5f3c6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -47,6 +47,9 @@ importers: medium-zoom: specifier: ^1.1.0 version: 1.1.0 + pagefind: + specifier: ^1.1.1 + version: 1.1.1 reading-time: specifier: ^1.5.0 version: 1.5.0 @@ -674,6 +677,31 @@ packages: '@oslojs/encoding@1.1.0': resolution: {integrity: sha512-70wQhgYmndg4GCPxPPxPGevRKqTIJ2Nh4OkiMWmDAVYsTQ+Ta7Sq+rPevXyXGdzr30/qZBnyOalCszoMxlyldQ==} + '@pagefind/darwin-arm64@1.1.1': + resolution: {integrity: sha512-tZ9tysUmQpFs2EqWG2+E1gc+opDAhSyZSsgKmFzhnWfkK02YHZhvL5XJXEZDqYy3s1FAKhwjTg8XDxneuBlDZQ==} + cpu: [arm64] + os: [darwin] + + '@pagefind/darwin-x64@1.1.1': + resolution: {integrity: sha512-ChohLQ39dLwaxQv0jIQB/SavP3TM5K5ENfDTqIdzLkmfs3+JlzSDyQKcJFjTHYcCzQOZVeieeGq8PdqvLJxJxQ==} + cpu: [x64] + os: [darwin] + + '@pagefind/linux-arm64@1.1.1': + resolution: {integrity: sha512-H5P6wDoCoAbdsWp0Zx0DxnLUrwTGWGLu/VI1rcN2CyFdY2EGSvPQsbGBMrseKRNuIrJDFtxHHHyjZ7UbzaM9EA==} + cpu: [arm64] + os: [linux] + + '@pagefind/linux-x64@1.1.1': + resolution: {integrity: sha512-yJs7tTYbL2MI3HT+ngs9E1BfUbY9M4/YzA0yEM5xBo4Xl8Yu8Qg2xZTOQ1/F6gwvMrjCUFo8EoACs6LRDhtMrQ==} + cpu: [x64] + os: [linux] + + '@pagefind/windows-x64@1.1.1': + resolution: {integrity: sha512-b7/qPqgIl+lMzkQ8fJt51SfguB396xbIIR+VZ3YrL2tLuyifDJ1wL5mEm+ddmHxJ2Fki340paPcDan9en5OmAw==} + cpu: [x64] + os: [win32] + '@pkgjs/parseargs@0.11.0': resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==} engines: {node: '>=14'} @@ -2956,6 +2984,10 @@ packages: package-json-from-dist@1.0.1: resolution: {integrity: sha512-UEZIS3/by4OC8vL3P2dTXRETpebLI2NiI5vIrjaD/5UtrkFX/tNbwjTSRAGC/+7CAo2pIcBaRgWmcBBHcsaCIw==} + pagefind@1.1.1: + resolution: {integrity: sha512-U2YR0dQN5B2fbIXrLtt/UXNS0yWSSYfePaad1KcBPTi0p+zRtsVjwmoPaMQgTks5DnHNbmDxyJUL5TGaLljK3A==} + hasBin: true + pako@0.2.9: resolution: {integrity: sha512-NUcwaKxUxWrZLpDG+z/xZaCgQITkA/Dv4V/T6bw7VON6l1Xz/VnrBqrYjZQ12TamKHzITTfOEIYUj48y2KXImA==} @@ -4767,6 +4799,21 @@ snapshots: '@oslojs/encoding@1.1.0': {} + '@pagefind/darwin-arm64@1.1.1': + optional: true + + '@pagefind/darwin-x64@1.1.1': + optional: true + + '@pagefind/linux-arm64@1.1.1': + optional: true + + '@pagefind/linux-x64@1.1.1': + optional: true + + '@pagefind/windows-x64@1.1.1': + optional: true + '@pkgjs/parseargs@0.11.0': optional: true @@ -7617,6 +7664,14 @@ snapshots: package-json-from-dist@1.0.1: {} + pagefind@1.1.1: + optionalDependencies: + '@pagefind/darwin-arm64': 1.1.1 + '@pagefind/darwin-x64': 1.1.1 + '@pagefind/linux-arm64': 1.1.1 + '@pagefind/linux-x64': 1.1.1 + '@pagefind/windows-x64': 1.1.1 + pako@0.2.9: {} parent-module@1.0.1: diff --git a/src/components/layout/Header.astro b/src/components/layout/Header.astro index 0835f55..ef310be 100644 --- a/src/components/layout/Header.astro +++ b/src/components/layout/Header.astro @@ -44,6 +44,11 @@ export const isNavItem = (item: HeaderItem): item is NavItemType => )) } +
  • + + + +
  • {config.modeToggle && }
  • @@ -51,6 +56,9 @@ export const isNavItem = (item: HeaderItem): item is NavItemType =>
    {config.modeToggle && } + + +
    diff --git a/src/layouts/PageLayout.astro b/src/layouts/PageLayout.astro index 89b3517..f1e044e 100644 --- a/src/layouts/PageLayout.astro +++ b/src/layouts/PageLayout.astro @@ -14,6 +14,7 @@ export type Props = BaseLayoutProps & { scrollProgress: boolean activeHeaderLink: string scrollToTop: boolean + searchable: boolean }> } @@ -22,7 +23,8 @@ const { frontmatter } = Astro.props const { scrollProgress = config.scrollProgress, scrollToTop = config.scrollToTop, - activeHeaderLink + activeHeaderLink, + searchable = false } = frontmatter --- @@ -35,7 +37,7 @@ const { -
    +
    diff --git a/src/layouts/PostLayout.astro b/src/layouts/PostLayout.astro index 3e8d919..ea09be4 100644 --- a/src/layouts/PostLayout.astro +++ b/src/layouts/PostLayout.astro @@ -28,7 +28,8 @@ const frontmatter: PageLayoutProps['frontmatter'] = { ...post.data, openGraphImage: post.data.openGraphImage || `/posts/${post.slug}.png`, activeHeaderLink: 'Blog', - scrollProgress: true + scrollProgress: true, + searchable: true } const publishedStr = toDateString(post.data.publishedDate) diff --git a/src/pages/search.astro b/src/pages/search.astro new file mode 100644 index 0000000..686d449 --- /dev/null +++ b/src/pages/search.astro @@ -0,0 +1,101 @@ +--- +import PageLayout, { + type Props as PageLayoutProps +} from '@/layouts/PageLayout.astro' + +const frontmatter: PageLayoutProps['frontmatter'] = { + title: 'Search', + activeHeaderLink: '/search' +} +--- + + +

    {frontmatter.title}

    + + + + +

    {''}

    +
      +
      + +