node.js - Gatsby Lunr 插件与 Ghost 源

标签 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 更改为 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>
  )
};

有人有想法吗?

最佳答案

通过将解析器更改为:

来修复此问题
   resolvers: {
      GhostPost: {
        title: node => node.title
      }
    }

关于node.js - Gatsby Lunr 插件与 Ghost 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56206791/

相关文章:

node.js - 将 Ghost 博客作为在 Node http-server 上运行的网站的子文件夹运行

ghost-blog - 幽灵博客客户端 ID secret 显示在头部

javascript - html和node.js之间的通信(基础)

node.js - 如何从node.js后端获取客户端的错误对象?

node.js - AWS Codedeploy 错误 : scripts/applicationstart. sh 不可执行。试图使其可执行

gatsby - 将自定义 Modernizr 构建添加到 Gatsby 站点

javascript - Gatsby 当前链接颜色

javascript - Gatsby 导入 babel polyfill

node.js - Mongodb/Node.js findByIdAndUpdate 方法未更新

docker - 如何在家庭 Web 服务器上托管 Ghost 博客