带有Ghost源代码的Gatsby Lunr插件

时间:2019-05-19 10:11:27

标签: node.js gatsby ghost-blog lunrjs

尝试使用gastby-plugin-lunr插件在Gatsby中实现搜索功能。

我的gatsby-config.js

{
  resolve: `gatsby-plugin-lunr`,
  options: {
    languages: [
      {
        name: 'en'
      }
    ],
    fields: [
      { name: 'title', store: true, attributes: { boost: 20 }}
    ],
    resolvers: {
      allGhostPost: {
        title: node => node.title
      }
    }
  }
}

但是我的索引仍然为空。已经尝试将标题节点更改为node.fields.title-仍然无效。

我的搜索组件:

const ContactPage: FunctionComponent = () => {
  const [results, setResults] = useState([]);
  const [query, setQuery] = useState('');

  const search = (event) => {
    const query = event.target.value;
    if (!query || !(window as any).__LUNR__) {
      setResults([]);
    }
    const lunrIndex =  (window as any).__LUNR__['en'];
    const res = lunrIndex.index.search(query);
    setResults(res);
    setQuery(query);
  };

  return (
    <Layout header={<DefaultHeader/>}>
      <input type="text" value={query} onChange={search} />
      <ul>{results.map(page => <li>{page}</li>)}</ul>
    </Layout>
  )
};

有人知道吗?

1 个答案:

答案 0 :(得分:1)

通过将resolvers更改为:

   resolvers: {
      GhostPost: {
        title: node => node.title
      }
    }