Help with omitting _index.md from readDir, getting subdirs and their pages' categories

Here is my dir structure.

-- content/
---- about/
---- blog/
---- work/
------ anim/
-------- anim-01/
------------ _index.md
-------- anim-02/
------------ _index.md
------ art/
-------- art-01/
------------ _index.md

I have a short code based off the example here in themes/site/layouts/shortcodes/gallery.html.

My themes shortcodes/gallery.html:

{{- $pathURL := .Get "pathURL" -}}
{{- $path := .Get "path" -}}
{{- $sections := readDir $path -}}
{{- range $sections | intersect (where $sections ".Name" "!=" "_index.md") }}
<a href="{{ $pathURL }}{{ .Name | relURL }}">{{ .Name }}</a>
{{- end }}

an example subdir /work/anim/anim-01/_index.md

---

title: 'Animation 01'

categories:

- Animation
- Drawing
- Traditional
---

**animation** dolor sit amet...

My work/_index.md:

---
title: 'Work'
date: ''
---

**Work** Quisque mattis volutpat lorem vitae feugiat. 

{{< gallery path="/content/work/" pathURL="/" >}}

which produces “_index.md anim art code” at the bottom, live in work/.

Now to the issue. I’d like to make the following arrays:

$dirs array which contains the the entries inside the $files array excluding the initial “_index.md” entry.

$subdirs array which contains the subdirs of the $dirs array, “anim-01, anim-02, art-01” etc.

$categories array that contains the categories inside each subdir [Animation, Drawing, Traditional], [Animation, Digital]".

Ultimately I’d like this structure in my shortcode:

<a href="{{ $dirs }}/{{ $subdirs }}" class="{{ $categories }}">anim/anim-01/</a>

A literal:

<a href="anim/anim-01/" class="animation drawing traditional">anim/anim-01/</a>

I’m pretty confused how to go about it and would appreciate any help.

Hi,

I’m not sure I understand what are you trying to do? If you are trying to list the images in your content, it would probably be easier to use Page Resources than readDir.

No, I’m not trying to pull images from the content, though I will need to down the line. I managed to output the proper dirs “anim, art”, omitting “_index.md” with this revised shortcode:

{{- $pathURL := .Get "pathURL" -}}
{{- $path := .Get "path" -}}
{{- $sections := readDir $path -}}
{{- range $sections | intersect (where $sections ".Name" "!=" "_index.md") }}
<a href="{{ $pathURL }}{{ .Name | relURL }}">{{ .Name }}</a>
{{- end }}

Now I need to get their subdirs and subsequent categories inside their relative _index.md’s.

Ultimately I’ll have example code like this:

<a href="anim/anim-01/" class="animation drawing traditional">anim/anim-01/</a>
<a href="anim/anim-02/" class="animation digital">anim/anim-02/</a>
<a href="art/art-01/" class="art sketch">art/art-01/</a>

Yes, I should have used page resources.

{{- range where .Site.Pages "Section" "work" }} 
<a href="{{ .Permalink }}">{{ .Name }}</a> 
{{- end }}

Was all I needed. Outputs this:

Animation 01 Animation 02 Code 01 Code 02 Sketch 01 Sketch 02 Work

However, I want to omit that last “Work” in the list.

If the pages you want to list are leaf bundles (ie index.md not _index.md) you could try .Site.RegularPages in your range.

Hello Again,

Really appreciate your help. How would I list the sub-sections of the section “work”?

content/
  - work/
    -- animation/
    -- art/
    -- code/

In this case, I want to display “animation”, “art”, and “code”. Ultimately I’d like to inject each subsection name into each link’s class.

{{- range where .Site.Pages "Section" "work" }} 
<a href="{{ .Permalink }}" class="{{ \*subsection name*\ }}">{{ .Name }}</a> 
{{- end }}

resulting in:

<a href="work/animation/anim-01" class="animation">Animation 01</a> 
<a href="work/animation/anim-02" class="animation">Animation 02</a> 
...
<a href="work/code/code-01" class="code">Code 02</a>

I ended up making a custom param for each page called “category” and adding it to the link’s class. If there’s an alternative way, please let me know. Otherwise, this issue’s been resolved.

I suggest you read about “nested sections” in the documentation.

I must be misunderstanding something as I did read the “nested sections” in the docs. Using the breadcrumb navigation, it lists all the sections from the root folders inside content dir.

.Section
The section this content belongs to. Note: For nested sections, this is the first path
element in the directory, for example, /blog/funny/mypost/ => blog.

Reading it over, I don’t understand how I would pull “funny” or “mypost” if it goes back to the root folder “blog”.

I currently have this:

{{ range where .Site.RegularPages "Section" "work" }}
<a href="{{ .Permalink }}" class="{{ echoParam .Params "category" -}}">
    {{ .Name }}
</a>
{{ end }}

But it requires me to add the param category to each individual index of each subsection.