Jekyll::Hashsert
A jekyll plugin that inserts random hashes of custom length into strings.
Installation
Add this line to your project’s Gemfile inside the jekyll_plugins
group:
group :jekyll_plugins do
# other gems
gem "jekyll-hashsert"
end
Then, enable the plugin by adding it to the plugins
section in the _config.yml
file:
plugins:
# - other plugins
- jekyll-hashsert
And then execute:
bundle install
Usage
This plugin is essentially a liquid filter. You can pass any string to the filter:
{% assign hash = "$" | hashsert %}
{{ hash }}
The $ symbol will be replaced with a random alphanumeric string of length 64
The output for {{ hash }}
in this case will look something similar to this:
5rsd6vg0a9uvlr2c9jbf6m3pscjsd0bc34b7dxo9park26de494ulw7p37mbx0fw
Note: By default, the plugin replaces all the
$
symbols in the input string with alphanumeric string hashes of length64
. Each$
will be replaced with a different hash.
Custom symbol
To use a custom replacement symbol instead of the default $
, please pass it as a parameter to the filter:
{% assign hash = "o_o" | hashsert: "_" %}
{{ hash }}
The _ symbol will be replaced while the surrounding text will be preserved.
Output will look like: o<hash>o.
The output for {{ hash }}
in this case will look something similar to this:
o4m3x2zebyt71xclbajh0qmxb07gwcd8e9lz2mvotls83ksu1ot90e98ckiimyenko
Note: The symbol should only be one character long and should not be alphnumeric.
Custom length
To set a custom length for the generated hash, pass the required number as a 2nd argument, after the symbol:
{% assign hash = "o_o" | hashsert: "_", 5 %}
{{ hash }}
The _ symbol will be replaced with a string of length 5 while the surrounding text will be preserved.
Output will look like: o<alphanumeric string of length 5>o.
The output for {{ hash }}
in this case will look something similar to this:
oxpn6ho
Note: The 2nd argument should only be an integer. If you intend to set a custom length then you must also set a symbol. You may still pass
$
if you wish to retain the default behavior.
Use case
The most prominent use case for this plugin is to insert random alphanumeric characters into strings. For example, GitHub requires hashes to serve versions of open graph images for repositories. To use the latest versions you need a new random hash:
https://opengraph.githubassets.com/<any_hash_number>/<owner>/<repo>
You can use this plugin to generate such urls:
{% assign image = "https://opengraph.githubassets.com/$/gouravkhunger/jekyll-hashsert" | hashsert %}
<img src="{{ image }}" alt="Open graph image for gouravkhunger/jekyll-hashsert GitHub repository" />
The output for {{ image }}
in this case will look something similar to this:
https://opengraph.githubassets.com/eymp8f8j9ji8zjkjbdrfntr9n8asg8a3sbsiwjj6i3l8y1xz3bx93gcj028i8ye6/gouravkhunger/jekyll-hashsert
License
The gem is available as open source under the terms of the MIT License.