PageFind for Hugo, ignoring certain pages instead of selectors

I just found out about PageFind for Hugo and within minutes I was able to set it up. I read the docs and it seems it only supports excluding selectors. Configuring how content is indexed | Pagefind

Since it works based off of elements seemingly and there isn’t a way for me to modify my templates where certain md files won’t be part of search. My idea is as follows but I can’t seem to figure out the syntax in Hugo (in the list.html files):

{{ $slicePagesToIgnore := slice "/about" "/foo" "/bar/baz" }}
{{ range $slicePagesToIgnore }}
    {{ if eq .RelPermalink . }}
        div< id="content" data-pagefind-ignore>
    {{ else }}
        <div id="content">
    {{ end }}
{{ end }}

I get the error layouts/page/single.html <.RelPermalink>: can't evaluate field RelPermalink in type string. Seems RelParmaLink is available for .Page only.

What do I need to do?

I would do something like this instead:

content/posts/post-1.md

+++
title = 'Post 1'
date = 2023-01-26T14:09:47-08:00
draft = false
search_ignore = true
+++

layouts/_default/list.html

{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>

    {{ if .Params.search_ignore }}
      <div id="content" data-pagefind-ignore>
    {{ else }}
      <div id="content">
    {{ end }}
    {{ .Content }}
    </div>

  {{ end }}
{{ end }}

layouts/_default/single.html

{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ if .Params.search_ignore }}
    <div id="content" data-pagefind-ignore>
  {{ else }}
    <div id="content">
  {{ end }}
  {{ .Content }}
  </div>
{{ end }}

If you would prefer to specify the ignored pages in your site configuration, you can cascade the value down to the desired pages:

[[cascade]]
search_ignore = true

[cascade._target]
path = "{/posts/post-1.md,/posts/post-2.md}"

Make sure you separate the path values with a comma only (no space before or after the comma).

1 Like

Thank you very much. That’s a better solution. Much appreciated. Cheers.

If anyone would like to see it in action, test it out, perhaps give feedback, or see how the search works, or is interested in implementing it themselves,

https://www.psychedelicsdaily.com/ live here, search box under the menu bar.

I’ll have to disable to widgets/partials, then rebuild site, then rebuild my search for pagefind. That way I can get cleaner/better results.

If you search LSD, it shows pretty much every article, even non LSD articles that have a link to the LSD archives in a partial rendered in a page as if you type ‘LSD’, every instance of this string is listed.

Cheers.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.