正在尝试为Docpad网站设置Lunr全文搜索插件.我做错了什么

Trying to set up Lunr full-text search plugin for Docpad site. What am I doing wrong?

本文关键字:搜索 插件 什么 错了 文搜索 Docpad Lunr 设置 网站      更新时间:2023-09-26

我正在使用Docpad构建一个静态网站。我正试图为全文网站搜索设置Lunr插件,但我一直收到这个错误:

$ docpad run

info: Welcome to DocPad v6.78.1 (local installation: /PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad)
info: Plugins: cleanurls, eco, livereload, lunr, marked, moment, partials, rss, sitemap, stylus
info: Environment: development
info: DocPad listening to http://127.0.0.1:9778/ on directory /PATH/TO/WEBSITE/REPOSITORY/out
info: LiveReload listening to new socket on channel /docpad-livereload
ReferenceError: partial is not defined
  at Object.eval (<anonymous>:41:18)
  at Object.eval (<anonymous>:51:8)
  at eval (<anonymous>:53:6)
  at Function.eco.render (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad-plugin-eco/node_modules/eco/lib/index.js:26:25)
  at EcoPlugin.render (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad-plugin-eco/out/eco.plugin.js:23:32)
  at ambi (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:57:27)
  at Task.<anonymous> (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/event-emitter-grouped/out/lib/event-emitter-grouped.js:45:23)
  at ambi (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:55:18)
  at fireMethod (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:397:23)
  at b (domain.js:183:18)
  at Domain.run (domain.js:123:23)
  at Task.fire (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:435:27)
  at Object._onImmediate (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:452:26)
  at processImmediate [as _immediateCallback] (timers.js:354:15)
info: Generating...
error: The action completed successfully
error: Something went wrong while rendering: /PATH/TO/WEBSITE/REPOSITORY/src/render/search.html.eco
The error follows:
ReferenceError: partial is not defined
  at Object.eval (<anonymous>:41:18)
  at Object.eval (<anonymous>:51:8)
  at eval (<anonymous>:53:6)
  at Function.eco.render (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad-plugin-eco/node_modules/eco/lib/index.js:26:25)
  at EcoPlugin.render (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad-plugin-eco/out/eco.plugin.js:23:32)
  at ambi (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:57:27)
  at Task.<anonymous> (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/event-emitter-grouped/out/lib/event-emitter-grouped.js:45:23)
  at ambi (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/ambi/out/lib/ambi.js:55:18)
  at fireMethod (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:397:23)
  at b (domain.js:183:18)
  at Domain.run (domain.js:123:23)
  at Task.fire (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:435:27)
  at Object._onImmediate (/PATH/TO/WEBSITE/REPOSITORY/node_modules/docpad/node_modules/taskgroup/out/lib/taskgroup.js:452:26)
  at processImmediate [as _immediateCallback] (timers.js:354:15)

我阅读了Lunr插件的存储库README,但我并不总是确定将配置属性放在哪里。但这是我提出的docpad.coffee属性。

plugins:
        lunr:
            indexes:
                myIndex:
                    collection: [ "story", "meta" ]
                    resultsTemplate: "src/partials/search-excerpt.html.eco"

并且在src/partials/search-excerpt.html.eco 中搜索结果是部分的

<div>
    <a href="<%= post.url %>"><%= post.title %></a>
    <span>posted on <%= post.date %></span>
</div>

以及src/render/search.html.eco 中的搜索结果页面

---
title: "search results"
---
<%- partial( "header" ) %>
<%- @getLunrSearchPage( "myIndex" ) %>
<%- partial( "footer" ) %>

我还没有抽出时间尝试在网站模板中插入搜索栏,因为基础知识已经突破了。我做错了什么?如有任何帮助,我们将不胜感激。

我从来没有让这部分工作过。我的决定过去和现在都做得很好——我直接修改源代码(我知道,这是一种糟糕的做法,如果你愿意,你也可以派生项目)。因此,在您的项目中,您有node_modules/docpad-plugin-lunr/out/lunrdoc.js,并且有一个resultsTemplate属性,您可以修改

我从未让Lunr插件工作过,但我认为我已经想出了一个更好、更简单的解决方案。

我最终做的是一个简单的HTML表单,它在DuckDuckGo上对我的网站进行自定义搜索——尽管我相信你可以很容易地调整它来使用你喜欢的任何搜索引擎。

<!--
    Here, method="post" hides the query parameters, so the code doesn't
    appear in the search results page URL. You can change attribute
    `method` to "get" if you want all the parameters to show in the URL
    bar when users go to the search page.
-->
<form action="https://duckduckgo.com/" method="post">
    <!--
        Input elements with type="hidden" won't render on your page, but
        they send extra parameters when users submit the form. For 
        example, this will set DuckDuckGo to show search results for
        *your* website.
    -->
    <input type="hidden" name="sites" value="YOURDOMAIN.URL" />
    <!--
        DuckDuckGo has a variety of parameters you can set to customize 
        the look and feel of the search results page. See 
        <https://duckduckgo.com/params> for a full list.
        For example, this will set the page to use DuckDuckGo's 
        dark theme.
    -->
    <input type="hidden" name="kae" value="d" />
    <!--
        label the search bar with, say, a magnifying lens icon, or just 
        the word "Search." the `for` attribute has to match the `id` 
        attribute of the input element with type="search" coming up after 
        this.
    -->
    <label for="search-bar">
        <img src="/PATH/TO/SEARCH/ICON.jpg" alt="Search" />
    </label>
    <!--
        The actual search bar. This sends the user's search terms to 
        DuckDuckGo. You can also add a `placeholder` attribute here to 
        show placeholder text until the user starts typing in the search 
        bar.
    -->
    <input id="search-bar" type="search" name="q" />
    <button id="search-button" type="submit" title="search provided by DuckDuckGo">Go</button>
</form>