【问题标题】:Gatsby Lunr Plugin with Ghost source带有 Ghost 源的 Gatsby Lunr 插件
【发布时间】:2019-10-05 23:44:02
【问题描述】:

尝试使用 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>
  )
};

有人有想法吗?

【问题讨论】:

  • 您是否运行gatsby build 来创建索引?
  • 是的。使用 builddevelop 时,索引仍然为空。

标签: node.js gatsby ghost-blog lunrjs


【解决方案1】:

通过将resolvers 更改为:

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

【讨论】:

    猜你喜欢
    • 2019-03-11
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多