{{/* gotmplfmt-ignore-all */ -}}

{{/*
Renders a description list of the pages in the given section.

Render a subset of the pages in the section by specifying a predefined filter,
and whether to include those pages.

Filters are defined in the data directory, in the file named page_filters. Each
filter is an array of paths to a file, relative to the root of the content
directory. Hugo will throw an error if the specified filter does not exist, or
if any of the pages in the filter do not exist.

@param {string} path The path to the section.
@param {string} [filter=""] The name of filter list.
@param {string} [filterType=""] The type of filter, either include or exclude.
@param {string} [titlePrefix=""] The string to prepend to the link title.

@example {{% render-list-of-pages-in-section path=/methods/resource %}}
@example {{% render-list-of-pages-in-section path=/functions/images filter=some_filter filterType=exclude %}}
@example {{% render-list-of-pages-in-section path=/functions/images filter=some_filter filterType=exclude titlePrefix=foo %}}
*/}}

{{- /* Initialize. */}}
{{- $filter := or "" (.Get "filter" | lower) }}
{{- $filterType := or (.Get "filterType") "none" | lower }}
{{- $filteredPages := slice }}
{{- $titlePrefix := or (.Get "titlePrefix") "" }}

{{- /* Build slice of filtered pages. */}}
{{- with $filter }}
	{{- with index hugo.Data.page_filters . }}
		{{- range . }}
			{{- with site.GetPage . }}
				{{- $filteredPages = $filteredPages | append . }}
			{{- else }}
				{{- errorf "The %q shortcode was unable to find %q as specified in the page_filters data file. See %s" $.Name . $.Position }}
			{{- end }}
		{{- end }}
	{{- else }}
		{{- errorf "The %q shortcode was unable to find the %q filter in the page_filters data file. See %s" $.Name . $.Position }}
	{{- end }}
{{- end }}

{{- /* Render. */}}
{{- with $sectionPath := .Get "path" }}
	{{- with site.GetPage . }}
		{{- with .RegularPages }}
			{{- range $page := .ByTitle }}
				{{- if or
					(and (eq $filterType "include") (in $filteredPages $page))
					(and (eq $filterType "exclude") (not (in $filteredPages $page)))
					(eq $filterType "none")
				}}
					{{- $linkTitle := .LinkTitle }}
					{{- with $titlePrefix }}
						{{- $linkTitle = printf "%s%s" . $linkTitle }}
					{{- end }}
					{{- /* Use page Path as the link destination for render hook to resolve correctly. */}}
					{{- if $page.Params.isFunctionOrMethod }}
[`{{ $linkTitle }}`]({{ $page.Path }}){{/* Do not indent. */}}
					{{- else }}
[{{ $linkTitle }}]({{ $page.Path }}){{/* Do not indent. */}}
					{{- end }}
: {{ $page.Description }}{{/* Do not indent. */}}
				{{ end }}
			{{- end }}
		{{- else }}
			{{- warnf "The %q shortcode found no pages in the %q section. See %s" $.Name $sectionPath $.Position }}
		{{- end }}
	{{- else }}
		{{- errorf "The %q shortcode was unable to find %q. See %s" $.Name $sectionPath $.Position }}
	{{- end }}
{{- else }}
	{{- errorf "The %q shortcode requires a 'path' parameter indicating the path to the section. See %s" $.Name $.Position }}
{{- end }}
