Search 默认
提供一个助手函数以搜索其他页面。
配置参数
- name string
The helper name
Default:"search"
描述
这个插件注册了 search
助手函数,用于从其他页面搜索页面。它对于构建菜单或其他导航功能非常有用。
安装
此插件默认安装。🎉
搜索页面
search.pages()
函数返回一个页面数组,你可以对其进行过滤和排序。
要按标签搜索,只需将标签名称作为第一个参数包含进来,用空格分隔。例如,要搜索包含 post
和 html
标签的所有页面,你可以执行 search.pages("post html")
:
<ul>
{{ for post of search.pages("post html") }}
<li>{{ post.title }}</li>
{{ /for }}
</ul>
你可以使用引号来搜索包含空格的标签。例如,要按标签 post
和 static site generator
搜索:
<ul>
{{ for post of search.pages("post 'static site generator'") }}
<li>{{ post.title }}</li>
{{ /for }}
</ul>
使用感叹号来搜索不包含特定标签的页面。例如,要搜索带有 "post" 标签但不包含 "html" 标签的页面:
<ul>
{{ for post of search.pages("post !html") }}
<li>{{ post.title }}</li>
{{ /for }}
</ul>
排序页面
第二个参数是用于排序的值。默认情况下,页面按 date
排序,但你可以使用任何字段。例如,如果你想按标题排序:
<ul>
{{ for post of search.pages("post html", "title") }}
<li>{{ post.title }}</li>
{{ /for }}
</ul>
注意:你可以使用点表示法按任何子字段排序。例如:header.title
。
排序允许指定多个字段。例如,让我们按 "order" 和 "title" 字段排序:
{{ for post of search.pages("post html", "order title") }}
...
{{ /for }}
默认情况下,排序是升序的,但这可以通过在字段名后附加 =desc
来更改:
{{ for post of search.pages("post html", "order=asc title=desc") }}
...
{{ /for }}
从 Lume 2.3.0 开始,有两个新的排序选项,对于带有重音符号或不同大小写的字符串很有用:asc-locale
和 desc-locale
。在底层,它使用 localeCompare 比较方法,因此这些选项必须仅与字符串一起使用。
{{ for post of search.pages("post html", "order=asc title=desc-locale") }}
...
{{ /for }}
限制结果数量
search.pages()
的第三个参数允许限制结果的数量。你可以使用正数返回前 n
个结果,或使用负数返回最后 n
个结果:
<!-- 获取前 3 个值 -->
{{ for post of search.pages("post html", "order title", 3) }}
...
{{ /for }}
<!-- 获取最后 3 个值 -->
{{ for post of search.pages("post html", "order title", -3) }}
...
{{ /for }}
按字段过滤
你不仅可以按标签过滤页面,还可以按你想要的任何其他字段过滤。例如,要搜索所有 menu
值为 true
的页面,只需包含查询 menu=true
:
{{ for option of search.pages("menu=true") }}
<a href="{{ option.url }}">
{{ option.title }}
</a>
{{ /for }}
条件的可用操作符有:
=
搜索匹配项,例如menu=true
。字符串true
和false
会自动转换为布尔值。undefined
和null
也会被转换,因此你可以使用keyname=undefined
过滤没有值的页面。具有数值的字符串也会转换为数字。^=
搜索以另一个值开头的值。例如,所有以字母A
开头的类别:category^=A
。$=
搜索以另一个值结尾的值。例如,所有以字母b
结尾的类别:category$=b
。*=
搜索包含另一个值的值。例如,所有标题包含字符串security
的标题:title*=security
。<
,>
,<=
,>=
搜索小于或大于另一个值的值。例如,所有级别大于 2 的页面:level>2
。
你可以使用点表示法,甚至可以将查询与标签结合使用。例如,假设你想选择所有 taxonomy.category=sport
且带有标签 football
的页面:
{{ for post of search.pages("taxonomy.category=sport football") }}
<a href="{{ post.url }}">
{{ post.title }}
</a>
{{ /for }}
否定条件
你可以在操作符前加上 !
字符来否定条件。例如,menu=true
返回 menu
变量为 true
的页面,而 menu!=true
返回 menu
变量不为 true
的页面。
所有操作符都接受 NOT 操作符。例如 category!^=A
(类别不以字母 A
开头的页面),或 title!*=security
(标题不包含单词 "security" 的页面)。
或者,你可以在条件的开头加上 !
字符。例如,!menu=true
等同于 menu!=true
,而 !category^=A
等同于 category!^=A
。
使用 |
进行 OR 条件
你可以使用管道字符 |
为任何条件分配多个值。例如,如果你想搜索具有标签 html
或 css
的页面,你可以使用 search.pages("html|css")
。你可以使用空格和管道组合 AND 和 OR。例如,要搜索所有带有标签 post
并且带有标签 html
或 css
之一的页面:search.pages("post html|css")
。
OR 条件可以与其他字段一起使用。例如,要搜索标题包含单词 "html"、"css" 或 "javascript" 的页面:search.pages("title*=html|css|javascript")
。
搜索单个页面
search.page()
函数与 search.pages()
非常相似,但只返回找到的第一个页面。请注意,limit
参数不可用。
搜索上一页和下一页
如果当前页面属于页面列表(例如,同一标签下的页面列表),你可以获取此列表中的上一页和下一页。为此,我们有函数 search.previousPage()
和 search.nextPage()
。语法与 search.pages()
相同,但第一个参数是当前页面的 URL。让我们看一个例子:
<h2>更多标记为 "html" 的文章</h2>
{{ set post = search.previousPage(url, "html") }}
{{ if post }}
<a href="{{ post.url }}" rel="prev">← {{ post.title }}</a>
{{ /if }}
{{ set post = search.nextPage(url, "html") }}
{{ if post }}
<a href="{{ post.url }}" rel="next">{{ post.title }} →</a>
{{ /if }}
获取键的所有值
values()
函数返回特定键找到的所有值,删除重复项。例如,假设你的页面有变量 author
,并且你想列出所有作者:
<strong>作者列表:</strong>
<ul>
{{ for author of search.values("author") }}
<li>
{{ author }}
</li>
{{ /for }}
</ul>
使用第二个参数来过滤页面以获取值。例如,要获取类别为 sport
的页面的作者:search.values("author", "category=sport")
。
搜索数据
data
函数返回与源目录中任何文件或目录关联的数据。这对于获取存储在任何目录的任何 _data
中的数据很有用。例如:
{{ set companyData = search.data("about/the-company") }}
搜索文件
files()
函数允许搜索将复制到 dest
文件夹的任何文件,并返回其 URL。它接受正则表达式或带有 glob 表达式的字符串。例如,要搜索所有 CSS 文件:
本站点使用以下 CSS 文件:
<ul>
{{ for file of search.files("*.css") }}
<a href="{{ file }}">
{{ file }}
</a>
{{ /for }}
</ul>