Match closest resource

In content/author/assets, I place all author images. Each file is named after the author.

Examples: charles-dickens.png, daniel-defoe.jpg

In content/books, I place all my book reviews. These are named after the author and book title.

Examples: charles-dickens-a-tale-of-two-cities.md, daniel-defoe-robinson-crusoe.md

In layouts/books/single.html, I want to show the author’s image alongside the book review. This is straightforward, however, the issue I’m running into is if I have two or more authors with the same name.

Is there a way to match, as in my example: daniel-defoe-robinson-crusoe.md to daniel-defoe.jpg, without using a parameter in front matter?

I’m trying to do something like this (doesn’t work):

  {{.File.BaseFileName}} --> daniel-defoe-robinson-crusoe

  {{$img := (site.GetPage "/author/assets").Resources}}

  {{$img}} --> [charles-dickens.png daniel-defoe.jpg]

  {{$img := $img.GetMatch (printf "%s" .File.BaseFileName)}}

This question wrinkles my brain. So… your problem is having authors with the same name, but you don’t want to handle that definitively with a front matter key, correct?

What code are you using when you don’t have authors with the same name? Maybe we can modify it to check. Also, how are you naming images for authors with the same names?

That’s correct.

Let me elaborate on this. Given a scenario where every author has a unique name, the solution is straightforward as each content/books markdown file has the author specified in the front matter, like so:

+++
author = "Daniel Defoe"
title = "Robinson Crusoe"
+++

To find the author’s image in layouts/books/single.html, I can do:

  {{$img := (site.GetPage "/author/assets").Resources}}
  {{$img := $img.GetMatch (printf "%s.*" (anchorize .Params.author)}}
  {{$img}} --> daniel-defoe.jpg

Now consider the scenario where there can be many authors with the same name. Front matter:

+++
author = "Daniel Defoe"
title = "Robinson Crusoe"
+++

+++
author = "Daniel Defoe"
title = "This is Daniel Defoe No. 2"
+++

And in author/assets, we now have:

daniel-defoe.jpg and daniel-defoe-2.jpg.

Now I can’t use .Params.author as previously in order to find the right image as .Params.author is the same in both cases. And it wouldn’t make sense to name the other author “Daniel Defoe 2” as that’s not their name. So now I need to figure out how to match:

daniel-defoe-robinson-crusoe.md to daniel-defoe.jpg
daniel-defoe-2-this-is-daniel-defoe-no-2.md to daniel-defoe-2.jpg.

Does that make sense?

Yeah, to an extent. When I list humans I use UUIDs, because humans have this horrible behavior of reusing names for unique instances. :slight_smile:

1 Like

Haha! You’re right.

I’ll continue tinkering with this and see if I can come up with a solution. In the meantime, if anyone has any suggestions, let me know please.