Skip to content

Commit 8edfbd9

Browse files
committed
Preserve the file name as a sub-directory
This makes it easier to see where images originate from.
1 parent e0f7754 commit 8edfbd9

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lib/jekyll/transcode-image-filters.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
module Jekyll
55
module TranscodeImageFilters
6+
7+
# Computes a sanitized name for use as a directory.
8+
# @param name The (file) name to sanitize.
9+
# @return [String]
10+
def _sanitize_name(name)
11+
# Replace invalid characters with `_` (Windows and Unix-safe)
12+
name.gsub(/[<>:"\/\\|?*\n\r]/, '_').strip
13+
end
14+
615
# Computes the name of the file in the cache.
716
# @param absolute_path_source [String] Full path to the source file
817
# @param resolution [String] As an example: 900x900
@@ -35,9 +44,11 @@ def _compute_paths(absolute_path_site, relative_path_source, cache_dir, resoluti
3544
absolute_path_source = File.join(absolute_path_site, relative_path_source)
3645
raise "No file found at #{absolute_path_source}" unless File.readable?(absolute_path_source)
3746

47+
file_name_source = _sanitize_name(File.basename(relative_path_source, ".*"))
48+
file_name_source_sanitized = _sanitize_name(file_name_source)
3849
file_name_destination = _compute_cache_filename(absolute_path_source, resolution, format)
3950

40-
absolute_path_cache = File.join(absolute_path_site, cache_dir)
51+
absolute_path_cache = File.join(absolute_path_site, cache_dir, file_name_source_sanitized)
4152
absolute_path_destination = File.join(absolute_path_cache, file_name_destination)
4253
relative_path_destination = File.join(cache_dir, file_name_destination)
4354

0 commit comments

Comments
 (0)