【问题标题】:How to create custom ghost api for search如何为搜索创建自定义 Ghost api
【发布时间】:2016-07-26 19:29:21
【问题描述】:

想在ghost cms中实现搜索表单。 访问者/用户必须能够在帖子、作者和标签中进行搜索。 如果可能的话,一些 REST api 会像其他公共 ghost api 一样查询到 ghost db 并返回所需的结果。例如下面的 api 获取所有帖子,包括标签和作者。

ghost.url.api('posts', 'slug', mySlugVariable, {include: 'tags, author'});

所以,我想要这样的东西,我可以传递一些字符串并从数据库中获取所有匹配的数据。

【问题讨论】:

    标签: content-management-system ghost-blog ghost


    【解决方案1】:

    我用js解决了。实际上,我没有找到任何好的解决方案,我在他们的 slack 小组上向幽灵团队提出了问题,他们建议我使用 js 解决这个问题。所以这就是我所做的:

    API 调用

    $.get(
    ghost.url.api('posts', { include: 'tags, author' }))
    .done(function(data) {
      localStorage.setItem('posts', JSON.stringify(data));
    })
    .fail(function(err) {
      console.log(err);
    });
    

    将所有数据保存到localStorage

    localStorage.setItem('posts', JSON.stringify(data));
    

    当用户在搜索表单中输入内容时,我会抓取该搜索字符串并过滤与该字符串对应的数据。

    // get search results
    function getSearchResult(string) {
      var postData = JSON.parse(localStorage.getItem('posts'));
      var searchResults = postData.posts.filter(function(post) {
        return post.markdown.match(string)
          || post.title.match(string)
          || post.author.name.match(string);
      });
    
      renderSearchResults(searchResults);
    }
    

    然后根据渲染结果

    function renderSearchResults(posts) {
      // my render code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 2023-04-02
      相关资源
      最近更新 更多