项目作者: algolia

项目描述 :
A Gatsby theme to add Algolia search into your Gatsby project
高级语言: JavaScript
项目地址: git://github.com/algolia/gatsby-theme-algolia.git
创建时间: 2019-05-03T11:37:21Z
项目社区:https://github.com/algolia/gatsby-theme-algolia

开源协议:

下载


gatsby-theme-algolia

gatsby-theme-algolia helps you add Algolia search into your Gatsby project.

It helps you in two ways:

  1. Push your data to Algolia server
  2. List your data with a search box

gatsby-theme-algolia is a wrapper of gatsby-plugin-algolia and react-instantsearch. If you are already familiar with them, it is recommended that you use them directly.

Table of Contents

Installation

  1. # with npm
  2. npm install gatsby-theme-algolia --save-dev
  3. # with yarn
  4. yarn add gatsby-theme-algolia -D

Getting Started

This guide assumes you already have an account at algolia and have created an index.

Push your data to Algolia server

gatsby-config.js

  1. module.exports = {
  2. // ...
  3. __experimentalThemes: [{ resolve: 'gatsby-theme-algolia' }],
  4. };

Environment variables

You need these environment variables:

  1. GATSBY_ALGOLIA_APP_ID
  2. GATSBY_ALGOLIA_ADMIN_API_KEY
  3. GATSBY_ALGOLIA_SEARCH_API_KEY
  4. GATSBY_ALGOLIA_INDEX_NAME

You can grab that information from API Keys menu at algolia.

In case of Netlify, you can specify them at Settings > Build & deploy > Build environment variables.

If you want to test on your local machine, create a file named .env on your project root and put like this:

  1. GATSBY_ALGOLIA_APP_ID=xxx
  2. GATSBY_ALGOLIA_ADMIN_API_KEY=xxx
  3. GATSBY_ALGOLIA_SEARCH_API_KEY=xxx
  4. GATSBY_ALGOLIA_INDEX_NAME=xxx

Since GATSBY_ALGOLIA_ADMIN_API_KEY is a secret information, you don’t want to expose it to public. Run the following to avoid commiting the credentials.

  1. echo '.env' >> .gitignore

Push it

Run the following command to push data to Algolia server.

  1. # with npm
  2. npm run build
  3. # with yarn
  4. yarn build

Check the dashboard if the data is well stored. At the dashboard, you need to go to Configuration and configure Searchable attributes properly.

Open your index page file(maybe src/pages/index.js or src/components/index.js).

Import the components:

  1. import { SearchWrapper, SearchResult, SearchBox } from 'gatsby-theme-algolia';

Add the components:

  1. <SearchWrapper>
  2. <SearchBox ></SearchBox>
  3. <SearchResult ></SearchResult>
  4. </SearchWrapper>

That’s it. Now you will see the list of your data and the list will be filtered as you type a query on the search box.

Customization

Specify what to index

You can specify what kind of data to push to Algolia server.

You can add things to the query or change how transformer behaves.

  1. {
  2. resolve: 'gatsby-theme-algolia',
  3. options: {
  4. queries: [
  5. {
  6. query: `
  7. {
  8. allMarkdownRemark {
  9. edges {
  10. node {
  11. excerpt
  12. timeToRead
  13. frontmatter {
  14. title
  15. date
  16. }
  17. fields {
  18. slug
  19. }
  20. }
  21. }
  22. }
  23. }
  24. `,
  25. transformer: ({ data }) =>
  26. data.allMarkdownRemark.edges.map(
  27. ({
  28. node: {
  29. excerpt,
  30. timeToRead,
  31. frontmatter: { title, date },
  32. fields: { slug },
  33. },
  34. }) => ({
  35. title,
  36. timeToRead,
  37. date,
  38. description: excerpt,
  39. path: slug,
  40. })
  41. ),
  42. },
  43. ],
  44. },
  45. }

Alter the UI

Of course, you want to alter the default UI.

You can simply put a css file to override the default style.

Or you can pass a custom component to replace the default list item with.

  1. const hitComponent = ({
  2. hit: { title, timeToRead, date, description, path },
  3. }) => (
  4. <article>
  5. <h2>
  6. <a href={path}>{title}</a>
  7. </h2>
  8. <p>{description}</p>
  9. <p>
  10. {date} {timeToRead}min(s)
  11. </p>
  12. </article>
  13. );
  14. ...
  15. <SearchWrapper>
  16. <div className="header">
  17. <h1>Test Website</h1>
  18. <SearchBox ></SearchBox>
  19. </div>
  20. <div className="body">
  21. <SearchResult hitComponent={hitComponent} ></SearchResult>
  22. </div>
  23. </SearchWrapper>

Contribution

Any contribution is welcomed. You can file an issue for suggestion or even create a pull request for whatever you want.