尝试为 Docpad 网站设置 Lunr 全文搜索插件。我做错了什么?
2015-03-30
236
我正在使用 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" ) %>
我还没有开始尝试在网站模板中插入搜索栏,因为基础已经坏了。我做错了什么?任何帮助都将不胜感激。
2个回答
我从来没有让这个部分工作得很好。
我的决定是,而且效果很好——我直接修改源代码(我知道,不好的做法,你也可以根据需要分叉该项目)。
因此,在您的项目中,您有
node_modules/docpad-plugin-lunr/out/lunrdoc.js
,并且有一个您可以修改的 resultsTemplate 属性
Lukasz Gornicki
2015-04-03
我从来没有让 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>
2015-04-05