Just get the base filename using page resources metadata

First, I’m new to Hugo and teaching myself, so I apologize in advance.

I have a page bundle of images. What I’d like to do is display each image and have it link to a corresponding PDF. The PDF are named the same as the images, so image1.jpg corresponds to image1.pdf. image2.jpg to image2.pdf, etc.

I modified the list layout so I have each image as a link, but using the .Name or .Title resource includes the entire filename - so image1.jpg links to image1.jpg.pdf. I’d like to be able just have the filename, but I’m not sure how to do that. I’ve looked into pattern matching here - Page Resources | Hugo - and it feels like this is the solution, but I don’t know how strip the file extension. So in my _index.md, I have:

resources:
- title: CODE TO JUST GET THE BASE PATH OF FILE
  params:
  src: '**.jpg'

What do I need to do?

Try using strings.TrimSuffix

Something like {{ $base := strings.TrimSuffix ".jpg" .Name }} should give you what you ask.

The file variables also have info about the filename without extension, but I am not sure if the way from a page resource to that information is easy (BaseFileName).

this worked perfectly - thank you!

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