Skip to content

Commit

Permalink
Theme: Add 'file_version_query' Liquid filter
Browse files Browse the repository at this point in the history
Fixes #4.
  • Loading branch information
Krinkle committed Sep 6, 2021
1 parent 98e4c10 commit 39c5c41
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ lang: en
timezone: UTC
# Files that will not be converted or published
exclude:
# Exclude repo docs from site output and search index
# Exclude internal repo files from site output
- docs
- lib
# Avoid the following on GitHub CI:
# > Error: could not read file
# > vendor/jekyll/lib/site_template/_posts/0000-00-00-welcome-to-jekyll.markdown.erb:
# > Invalid date '<%= Time.now.strftime('%Y-%m-%d %H:%M:%S %z') %>':
# > Document does not have a valid date in the YAML front matter.
- vendor
plugins:
- jekyll-theme-amethyst


# Theme settings
Expand Down
2 changes: 1 addition & 1 deletion _layouts/wrapper.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="{{ site.amethyst.favicon | default: '/favicon.svg' | relative_url }}">
<link rel="stylesheet" href="{{ '/assets/styles.css' | relative_url }}" media="screen">
<link rel="stylesheet" href="{{ '/assets/styles.css' | file_version_query: '/_sass/amethyst-variables.scss', '/_sass/amethyst.scss', '/_sass/highlight.scss', '/_sass/custom.scss' | relative_url }}" media="screen">
{% include opengraph.html -%}
<body>
<header class="site-header" role="banner">
Expand Down
39 changes: 39 additions & 0 deletions lib/jekyll-theme-amethyst.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# The "file_version_query" Liquid filter appends a query parameter with a file hash.
#
# Example: Basic
#
# ```
# <img src="{{ "/assets/foo.png" | file_version_query | relative_url }}">
# ```
# ```
# <img src="/assets/foo.png?v=01234567">
# ```
#
# Example: Link to a generated file that may not yet exist, and hash its input files instead.
#
# ```
# <link href="{{ "/assets/style.css" | file_version_query: "/_sass/x.scss", "/_sass/y.scss" }}">
# <link href="/assets/style.css?v=01234567">
# ```
#
require "digest"

module Jekyll
module AmethystFilters
def file_version_query(input, *filenames)
filenames = [input] unless filenames.length > 0
hexes = filenames.map do |filename|
begin
digest = Digest::MD5.file File.join(@context.registers[:site].source, filename)
rescue StandardError => e
digest = Digest::MD5.file File.join(__dir__, "..", filename)
end
digest.hexdigest
end
hex = hexes.length > 1 ? Digest::MD5.hexdigest(hexes.join(" ")) : hexes[0]
"#{input}?v=#{hex[0..7]}"
end
end
end

Liquid::Template.register_filter(Jekyll::AmethystFilters)

0 comments on commit 39c5c41

Please sign in to comment.