-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
I'm trying to write an image render hook that looks for the presence of smaller image files to fill out the srcset field for responsive images. I've settled on the following naming convention:
image1.jpg
image1_400.jpg
image1_800.jpg
...etc...
where the numbers are the various widths of the image. Not all sizes will be available for all images, so the hook needs to test if it exists and only emit it if it exists.
I've settled on having a site-wide parameter that has a list of the sizes to look for. That way I can have a tool check that parameter and generate the appropriate images.
To generate the path to look for, I am currently doing this:
{{- $path := replaceRE ".[jJ][pP][gG]$" (printf "_%sw%s" $size (path.Ext $src.Name)) $src.Name -}}
which only works for jpg files. I'd like to do something like this:
{{- $path := (printf "%s/%s_%sw%s" (path.Dir $src.Name) (path.BaseName $src.Name) $size (path.Ext $src.Name)) -}}
Which would work for all file extensions.
The File object has BaseFileName which provides this operation, and I don't really care what the operation is named, but I would like to have the operation.
Some test cases (I expect they are similar to the .File.BaseFileName test cases):
path.BaseName "image1.jpg" => image1
path.BaseName "/a/long/path/foo.txt" => foo
path.BaseName "filename.en.jpg" => filename.en