jekyll-before-after 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 95018375cf799be55f8ea6697b6e75001a0273bf053d6cfd70f584a6c0e31a9e
4
- data.tar.gz: 517c6093fc0789f13d357322d2ec9eeacba39df886e55fc5f054474bfe7fe182
3
+ metadata.gz: c42a5cb516fe5ac8d372d66c9119d622a3d7d52125082d784f588de665ecd290
4
+ data.tar.gz: 33620faaa38df743c8048fc8acedd86257ae30c1cb63ea1d9481d1259a2752f8
5
5
  SHA512:
6
- metadata.gz: ebc5888a4e142f4247ba391cc4528b3a718830bc12fd2a0358a00d30ce307a9b72e723e5600cd0d45a1d9b103ee8e68a8a8277bf87e1db72d1324397596fe47e
7
- data.tar.gz: ae8c0aaf89ee1c78345cfd4c80039dacfa4e32be35a6a6449c7431804726225cca8cc112f8e440a71e043862ae356f65696de0c79a67958b1cbb00310c22a90f
6
+ metadata.gz: cb70b8f431611814abd39a4aae1d33730e2fcee94049110349d448dc9c7b509ed16485a4204db0f78912646e2b0e70b8f23c1e960cc62546d1b940c88a391da3
7
+ data.tar.gz: 0bdd8c56827dbecd6b5040a9cb70701755e324a406dc146bc0dbbdf5c7f42487d797e7d513a3bf35bd7e237f9aa760b82c3f151aa0155f73cbcaa74c1a224560
data/CHANGELOG.md CHANGED
@@ -3,6 +3,32 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project adheres to [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
5
5
 
6
+ ## [1.1.0] - 2026-11-28
7
+
8
+ ### Fixed
9
+ - Pages with only vertical sliders now get the component script and stylesheet. The injector matched the exact string `<img-comparison-slider>` and missed `<img-comparison-slider direction="vertical">`.
10
+ - Attribute values are HTML-escaped. Before, a value like `before='/a" onerror="alert(1)'` could break out of the `src` attribute.
11
+ - Liquid variables now resolve (`before=page.before_image`, `before={{ x }}`). Before, `{{ x }}` rendered as `src="{{"`.
12
+ - Site-relative paths now get the site `baseurl`, like the `relative_url` filter. Full URLs are untouched.
13
+ - A missing or empty `before` or `after` logs a warning naming the page and renders nothing, instead of rendering an empty `src`. An invalid `start` logs a warning and uses the default position. Neither stops the build.
14
+ - The 1.0.0 changelog claim of "better accessibility" and the README's "fully accessible" wording overstated the component. See Added.
15
+
16
+ ### Added
17
+ - `alt_before` and `alt_after` (defaults `Before` and `After`), `width`, `height`, `loading` (default `lazy`), `start` (maps to the component's `value`) and `aria_label` options.
18
+ - `role="slider"`, `aria-valuemin`, `aria-valuemax`, `aria-valuenow` and `aria-orientation` on the slider, with `aria-valuenow` kept in step by a small script that listens for the component's `slide` event. The script is added once per page. It has not been tested in a browser or with a screen reader.
19
+ - `before_after:` settings in `_config.yml`: `inject`, `script_url`, `style_url`, `script_integrity`, `style_integrity`, `aria_sync`.
20
+ - Test suite for the current implementation, a `Rakefile` and a GitHub Actions workflow (Ruby 3.1 to 3.3).
21
+
22
+ ### Changed
23
+ - The component is pinned to img-comparison-slider 7.8.2 (was the floating `@7`) and loaded with `integrity` and `crossorigin="anonymous"`. Version 8.x uses the same markup, but 7.8.2 was kept because it hasn't been tried in a browser here.
24
+ - Images now carry `loading="lazy"` by default. Pass `loading=eager` for images above the fold.
25
+ - Minimum Ruby is 3.0 (was 2.7).
26
+ - README and gemspec no longer say the plugin has no external dependencies. It loads a third-party component from a CDN unless you self-host.
27
+
28
+ ### Removed
29
+ - `BeforeAfterGenerator` and the `site.data['before_after_css']` it set. Nothing used it, and it styled the removed 0.1.0 classes.
30
+ - `Gemfile.lock` from version control, and the nonexistent `_before-after.scss` from the gemspec file list.
31
+
6
32
  ## [1.0.0] - 2026-08-29
7
33
 
8
34
  ### Changed
@@ -25,9 +51,11 @@ This project adheres to [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
25
51
 
26
52
  ### Migration Guide
27
53
  If upgrading from 0.1.0:
28
- - All existing `{% before_after %}` tags work unchanged
29
- - Custom styling should migrate to CSS custom properties (see README)
30
- - No configuration changes needed — it just works
54
+ - `before`, `after` and `orientation=vertical` keep working. These options were removed and are now ignored: `start`, `before_label`, `after_label`, `srcset_before`, `srcset_after` and `sizes` (`start` returned in 1.1.0).
55
+ - `orientation=vertical` now sets `direction="vertical"` on the component. Overlay labels are gone, so any `before_label` and `after_label` text no longer shows.
56
+ - `alt_before` and `alt_after` were ignored in 1.0.0 (alt text was always "Before" and "After"). Fixed in 1.1.0.
57
+ - The `.before-after` classes and `--ba-*` custom properties no longer exist. Style the component with its own properties (`--divider-width`, `--handle-width` and so on, see README).
58
+ - The page now loads a script and stylesheet from unpkg.com, so a strict Content Security Policy needs to allow that host.
31
59
 
32
60
  ## [0.1.0] - 2026-08-22
33
61
 
data/CONTRIBUTING.md CHANGED
@@ -8,7 +8,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/jchanc
8
8
  git clone https://github.com/jchance/jekyll-before-after.git
9
9
  cd jekyll-before-after
10
10
  bundle install
11
- bundle exec ruby -Ilib:test test/test_before_after_tag.rb
11
+ bundle exec rake
12
12
  ```
13
13
 
14
14
  ## Pull Requests
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # jekyll-before-after
2
2
 
3
- A Jekyll plugin providing a `{% before_after %}` Liquid tag for rendering a drag-to-reveal image comparison slider. Built on the proven [img-comparison-slider](https://github.com/sneas/img-comparison-slider) web component. Supports horizontal and vertical orientations, responsive images, keyboard navigation, touch support, and full CSS custom property theming. Pure vanilla JS with zero external dependencies — GitHub Pages compatible.
3
+ A Jekyll plugin that adds a `{% before_after %}` Liquid tag for drag-to-reveal image comparisons. It renders the [img-comparison-slider](https://github.com/sneas/img-comparison-slider) web component (pinned to version 7.8.2) and adds its script and stylesheet to any page that uses a slider. The component is loaded from the unpkg CDN with Subresource Integrity, or from your own files if you self-host (see [Configuration](#configuration)). The plugin works on GitHub Pages only when the site is built by a GitHub Action, since GitHub's own builder doesn't run custom plugins.
4
4
 
5
5
  ## Installation
6
6
 
@@ -28,13 +28,23 @@ Run `bundle install`.
28
28
  %}
29
29
  ```
30
30
 
31
- ### All options
31
+ ### Options
32
32
 
33
33
  | Option | Default | Description |
34
34
  |--------|---------|-------------|
35
- | `before` | _(required)_ | Path to the "before" image |
36
- | `after` | _(required)_ | Path to the "after" image |
35
+ | `before` | _(required)_ | Image shown on the left (horizontal) or top (vertical) |
36
+ | `after` | _(required)_ | Image shown on the right (horizontal) or bottom (vertical) |
37
37
  | `orientation` | `horizontal` | `horizontal` or `vertical` |
38
+ | `start` | `50` | Starting divider position, 0 to 100 |
39
+ | `alt_before` | `Before` | Alt text for the first image (use `""` for a decorative image) |
40
+ | `alt_after` | `After` | Alt text for the second image |
41
+ | `aria_label` | `Before and after image comparison` | Accessible name for the slider |
42
+ | `width`, `height` | _(none)_ | Added to both images, to reserve space and avoid layout shift |
43
+ | `loading` | `lazy` | `lazy` or `eager` |
44
+
45
+ Quoted values (`before="/a.jpg"`) are literal. Unquoted values are Liquid variables, so `before=page.before_image` and `before={{ page.before_image }}` both work. Unquoted words such as `orientation=vertical` also work. Every value is HTML-escaped. Paths that start with `/` get your site's `baseurl` added, the same way the `relative_url` filter does. Full URLs are left alone.
46
+
47
+ If `before` or `after` is missing or empty, the tag logs a warning that names the page and renders nothing. The build carries on.
38
48
 
39
49
  ### Examples
40
50
 
@@ -59,7 +69,7 @@ Run `bundle install`.
59
69
 
60
70
  #### Multiple sliders
61
71
 
62
- Multiple sliders on the same page are fully supported. The library script is injected only once per page.
72
+ You can put several sliders on one page. The script and stylesheet are added once.
63
73
 
64
74
  ## Styling
65
75
 
@@ -78,21 +88,49 @@ img-comparison-slider {
78
88
 
79
89
  See [img-comparison-slider docs](https://img-comparison-slider.sneas.io/) for all available CSS custom properties.
80
90
 
91
+ ## Configuration
92
+
93
+ All settings are optional. Add them under `before_after:` in `_config.yml`:
94
+
95
+ ```yaml
96
+ before_after:
97
+ inject: true # false: don't add the component script and stylesheet
98
+ script_url: https://unpkg.com/img-comparison-slider@7.8.2/dist/index.js
99
+ style_url: https://unpkg.com/img-comparison-slider@7.8.2/dist/styles.css
100
+ script_integrity: "" # sha384-... for your script_url
101
+ style_integrity: "" # sha384-... for your style_url
102
+ aria_sync: true # false: don't add the aria-valuenow script
103
+ ```
104
+
105
+ The built-in URLs come with `integrity` and `crossorigin="anonymous"`. If you set your own `script_url` or `style_url`, the plugin leaves integrity off unless you give it a matching hash. To self-host, copy `dist/index.js` and `dist/styles.css` from the npm package into your site and point the two URLs at them. To load everything yourself, set `inject: false` and include both files in your layout.
106
+
107
+ ## Accessibility
108
+
109
+ The component makes the slider focusable and handles the left and right arrow keys. It doesn't add any slider semantics, so the plugin does:
110
+
111
+ - `role="slider"`, `aria-valuemin="0"`, `aria-valuemax="100"` and `aria-orientation` on the element
112
+ - an accessible name from `aria_label`
113
+ - `aria-valuenow`, set from `start` in the HTML and kept in step with the divider by a small inline script that listens for the component's `slide` event
114
+ - alt text on both images from `alt_before` and `alt_after`
115
+
116
+ The component handles arrow keys in both orientations as left and right only, and it has no visible text labels. Write alt text that says what each image shows. If you set `inject: false` and `aria_sync: false`, `aria-valuenow` stays at the starting value. This hasn't been tested with a screen reader.
117
+
81
118
  ## Interaction
82
119
 
83
- - **Drag** the divider to reveal
84
- - **Click** anywhere on the image to jump the slider to that position
85
- - **Keyboard**: focus the slider and use arrow keys
86
- - **Touch** and stylus supported via Pointer Events API
120
+ - Drag the divider, or click anywhere on the image to jump there
121
+ - Focus the slider and use the arrow keys
122
+ - Touch and mouse are supported
123
+
124
+ The component also supports hover-to-slide (`hover`) and handle-only dragging (`handle`), and lets you replace the handle with a `slot="handle"` element. The plugin doesn't pass these through yet. Overlay labels and `srcset` aren't supported.
87
125
 
88
126
  ## How it works
89
127
 
90
- This plugin wraps the lightweight [img-comparison-slider](https://github.com/sneas/img-comparison-slider) web component, loading it from CDN. The Liquid tag converts your `{% before_after %}` markup into the web component format, and the plugin injects the library script into the page once per build.
128
+ The tag renders an `<img-comparison-slider>` element with two images. After each page renders, the plugin looks for that element (with or without attributes) and adds the script, the stylesheet and the ARIA script to the `<head>` once.
91
129
 
92
130
  ## Requirements
93
131
 
94
132
  - Jekyll >= 3.9
95
- - Ruby >= 2.7
133
+ - Ruby >= 3.0
96
134
  - Modern browser with Web Components support (all modern browsers)
97
135
 
98
136
  ## License
@@ -2,18 +2,18 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "jekyll-before-after"
5
- spec.version = "1.0.0"
5
+ spec.version = "1.1.0"
6
6
  spec.authors = ["Jason Chance"]
7
7
  spec.email = ["jason@jasonchance.com"]
8
8
 
9
9
  spec.summary = "Jekyll plugin providing a before_after Liquid tag for drag-to-reveal image comparison sliders."
10
- spec.description = "A Jekyll plugin that provides a {% before_after %} Liquid tag for rendering a polished, accessible drag-to-reveal image comparison slider. Supports horizontal and vertical orientations, configurable start position, overlay labels, responsive images, keyboard navigation, and full CSS custom property theming. Pure vanilla JS, no external dependencies, GitHub Pages compatible."
10
+ spec.description = "A Jekyll plugin that provides a {% before_after %} Liquid tag for drag-to-reveal image comparisons, built on the img-comparison-slider web component (loaded from a CDN with Subresource Integrity, or self-hosted). Supports horizontal and vertical orientations, start position, alt text, ARIA slider attributes, and Liquid variables and baseurl in image paths."
11
11
  spec.homepage = "https://jasonchance.com/projects/jekyll-before-after/"
12
12
  spec.license = "MIT"
13
13
 
14
- spec.required_ruby_version = ">= 2.7"
14
+ spec.required_ruby_version = ">= 3.0"
15
15
 
16
- spec.files = Dir["lib/**/*", "*.md", "*.txt", "*.gemspec", "_before-after.scss"]
16
+ spec.files = Dir["lib/**/*", "*.md", "*.txt", "*.gemspec"]
17
17
  spec.require_paths = ["lib"]
18
18
 
19
19
  spec.add_runtime_dependency "jekyll", ">= 3.9", "< 5.0"
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+
5
+ module Jekyll
6
+ module BeforeAfter
7
+ COMPONENT_VERSION = "7.8.2"
8
+ DEFAULT_SCRIPT_URL = "https://unpkg.com/img-comparison-slider@#{COMPONENT_VERSION}/dist/index.js"
9
+ DEFAULT_STYLE_URL = "https://unpkg.com/img-comparison-slider@#{COMPONENT_VERSION}/dist/styles.css"
10
+ # sha384 of the files at the URLs above (img-comparison-slider 7.8.2).
11
+ DEFAULT_SCRIPT_INTEGRITY = "sha384-c68xRjt7GB42rgPGgUu/Ks4w+AeRgR/cI9xt3K0YxBBt5yXJYE1VBq39ilNnpqEp"
12
+ DEFAULT_STYLE_INTEGRITY = "sha384-TYCxyqTqCrHZ0yHG2B7VENYnxnIBGWt8GHLLkGegAYCw+1UkvUd0YpsVPrt+8//z"
13
+ DEFAULT_ARIA_LABEL = "Before and after image comparison"
14
+
15
+ # Reads the optional `before_after:` block of _config.yml.
16
+ class Config
17
+ def initialize(site)
18
+ raw = site && site.config["before_after"]
19
+ @raw = raw.is_a?(Hash) ? raw : {}
20
+ end
21
+
22
+ # Add the stylesheet and component script to pages that use a slider.
23
+ def inject?
24
+ @raw["inject"] != false
25
+ end
26
+
27
+ # Add the small script that keeps aria-valuenow in step with the slider.
28
+ def aria_sync?
29
+ @raw["aria_sync"] != false
30
+ end
31
+
32
+ def script_url
33
+ @raw["script_url"] || DEFAULT_SCRIPT_URL
34
+ end
35
+
36
+ def style_url
37
+ @raw["style_url"] || DEFAULT_STYLE_URL
38
+ end
39
+
40
+ # The built-in hashes only describe the built-in files, so a custom URL
41
+ # gets an integrity value only if the site supplies one.
42
+ def script_integrity
43
+ @raw["script_integrity"] || (@raw["script_url"] ? nil : DEFAULT_SCRIPT_INTEGRITY)
44
+ end
45
+
46
+ def style_integrity
47
+ @raw["style_integrity"] || (@raw["style_url"] ? nil : DEFAULT_STYLE_INTEGRITY)
48
+ end
49
+ end
50
+
51
+ def self.escape(value)
52
+ CGI.escapeHTML(value.to_s)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "jekyll"
4
+ require_relative "before_after_config"
5
+
6
+ module Jekyll
7
+ # Adds the img-comparison-slider assets and the ARIA sync script to rendered
8
+ # pages that contain a slider.
9
+ class BeforeAfterScriptInjector
10
+ MARKER = "<!-- img-comparison-slider-injected -->"
11
+ # Matches the opening tag whatever its attributes (e.g. direction="vertical").
12
+ SLIDER_RE = %r{<img-comparison-slider[\s/>]}.freeze
13
+
14
+ # The component sets tabindex and arrow-key handling but no slider
15
+ # semantics. This mirrors its position into aria-valuenow. The component
16
+ # fires a non-bubbling "slide" event, so listen in the capture phase.
17
+ ARIA_SCRIPT = <<~JS
18
+ (function () {
19
+ var sel = 'img-comparison-slider';
20
+ function sync(el) {
21
+ var v = Number(el.value);
22
+ if (isFinite(v)) el.setAttribute('aria-valuenow', String(Math.round(v)));
23
+ }
24
+ document.addEventListener('slide', function (e) {
25
+ if (e.target && e.target.matches && e.target.matches(sel)) sync(e.target);
26
+ }, true);
27
+ function init() {
28
+ var ready = window.customElements ? customElements.whenDefined(sel) : Promise.resolve();
29
+ ready.then(function () {
30
+ Array.prototype.forEach.call(document.querySelectorAll(sel), sync);
31
+ });
32
+ }
33
+ if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
34
+ else init();
35
+ })();
36
+ JS
37
+
38
+ def call(doc)
39
+ output = doc.output
40
+ return if output.nil? || !output.match?(SLIDER_RE)
41
+ return if output.include?(MARKER)
42
+
43
+ config = Jekyll::BeforeAfter::Config.new(doc.site)
44
+ html = build(config)
45
+ return if html.empty?
46
+
47
+ doc.output = output.sub("</head>") { "#{MARKER}\n#{html}</head>" } if output.include?("</head>")
48
+ end
49
+
50
+ private
51
+
52
+ def build(config)
53
+ html = +""
54
+ if config.inject?
55
+ html << "<script type=\"module\" src=\"#{esc(config.script_url)}\"#{sri(config.script_integrity)}></script>\n"
56
+ html << "<link rel=\"stylesheet\" href=\"#{esc(config.style_url)}\"#{sri(config.style_integrity)}>\n"
57
+ end
58
+ html << "<script>\n#{ARIA_SCRIPT}</script>\n" if config.aria_sync?
59
+ html
60
+ end
61
+
62
+ def sri(integrity)
63
+ return "" if integrity.nil? || integrity.to_s.empty?
64
+
65
+ " integrity=\"#{esc(integrity)}\" crossorigin=\"anonymous\""
66
+ end
67
+
68
+ def esc(value)
69
+ Jekyll::BeforeAfter.escape(value)
70
+ end
71
+ end
72
+
73
+ Jekyll::Hooks.register :pages, :post_render do |doc|
74
+ Jekyll::BeforeAfterScriptInjector.new.call(doc)
75
+ end
76
+
77
+ Jekyll::Hooks.register :documents, :post_render do |doc|
78
+ Jekyll::BeforeAfterScriptInjector.new.call(doc)
79
+ end
80
+ end
@@ -1,37 +1,148 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "jekyll"
4
+ require_relative "before_after_config"
4
5
 
5
6
  module Jekyll
6
- BEFORE_AFTER_ATTR_RE = /(\w+)\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+))/
7
+ # key=value pairs: "double", 'single', {{ liquid }} or a bare word/variable.
8
+ BEFORE_AFTER_ATTR_RE = /([A-Za-z_]\w*)\s*=\s*(?:"([^"]*)"|'([^']*)'|\{\{(.*?)\}\}|(\S+))/m.freeze
7
9
 
10
+ # Returns { "key" => [text, kind] } where kind is :literal (quoted) or
11
+ # :variable (bare or {{ }}, resolved against the Liquid context at render).
8
12
  def self.parse_ba_attrs(markup)
9
13
  attrs = {}
10
- markup.scan(BEFORE_AFTER_ATTR_RE) do |key, double, single, bare|
11
- attrs[key] = double || single || bare
14
+ markup.scan(BEFORE_AFTER_ATTR_RE) do |key, double, single, braces, bare|
15
+ attrs[key] =
16
+ if double || single
17
+ [double || single, :literal]
18
+ else
19
+ [(braces || bare).strip, :variable]
20
+ end
12
21
  end
13
22
  attrs
14
23
  end
15
24
 
16
25
  class BeforeAfterTag < Liquid::Tag
17
- def initialize(tag_name, markup, tokens)
26
+ # Options whose bare values may be plain words (orientation=vertical)
27
+ # rather than variable names.
28
+ WORD_OPTIONS = %w[orientation loading start width height].freeze
29
+ ABSOLUTE_URL_RE = %r{\A(?:[a-zA-Z][a-zA-Z0-9+.-]*:|//)}.freeze
30
+
31
+ def initialize(tag_name, markup, parse_context)
18
32
  super
33
+ @parse_context = parse_context
19
34
  @attributes = Jekyll.parse_ba_attrs(markup)
20
35
  end
21
36
 
22
37
  def render(context)
23
- before = @attributes["before"] || ""
24
- after = @attributes["after"] || ""
25
- orient = @attributes["orientation"] || "horizontal"
26
- direction = orient == "vertical" ? ' direction="vertical"' : ""
38
+ before = image_url(context, "before")
39
+ after = image_url(context, "after")
40
+ # Without both images there is nothing to compare. Warn and print
41
+ # nothing, so a stray or half-written tag doesn't stop the build.
42
+ return "" if before.nil? || after.nil?
43
+
44
+ vertical = value(context, "orientation") == "vertical"
45
+ start = start_position(context)
46
+ esc = Jekyll::BeforeAfter.method(:escape)
47
+
48
+ attrs = []
49
+ attrs << 'direction="vertical"' if vertical
50
+ attrs << %(value="#{esc.call(start)}") if start
51
+ attrs << 'role="slider"'
52
+ attrs << %(aria-label="#{esc.call(value(context, "aria_label") || Jekyll::BeforeAfter::DEFAULT_ARIA_LABEL)}")
53
+ attrs << 'aria-valuemin="0"' << 'aria-valuemax="100"'
54
+ attrs << %(aria-valuenow="#{esc.call(start || 50)}")
55
+ attrs << %(aria-orientation="#{vertical ? "vertical" : "horizontal"}")
56
+
57
+ size = ""
58
+ %w[width height].each do |dim|
59
+ v = value(context, dim)
60
+ size += %( #{dim}="#{esc.call(v)}") unless v.nil? || v.empty?
61
+ end
62
+ loading = value(context, "loading")
63
+ loading = "lazy" if loading.nil? || loading.empty?
27
64
 
28
65
  <<~HTML
29
- <img-comparison-slider#{direction}>
30
- <img slot="first" src="#{before}" alt="Before">
31
- <img slot="second" src="#{after}" alt="After">
66
+ <img-comparison-slider #{attrs.join(" ")}>
67
+ <img slot="first" src="#{esc.call(before)}" alt="#{esc.call(alt(context, "alt_before", "Before"))}"#{size} loading="#{esc.call(loading)}">
68
+ <img slot="second" src="#{esc.call(after)}" alt="#{esc.call(alt(context, "alt_after", "After"))}"#{size} loading="#{esc.call(loading)}">
32
69
  </img-comparison-slider>
33
70
  HTML
34
71
  end
72
+
73
+ private
74
+
75
+ def start_given?
76
+ @attributes.key?("start")
77
+ end
78
+
79
+ def start_position(context)
80
+ return nil unless start_given?
81
+
82
+ raw = value(context, "start").to_s.strip
83
+ number = Float(raw, exception: false)
84
+ unless number && number >= 0 && number <= 100
85
+ warn(context, "`start` must be a number from 0 to 100, got #{raw.inspect}; using the default position")
86
+ return nil
87
+ end
88
+
89
+ number == number.to_i ? number.to_i : number
90
+ end
91
+
92
+ # Logs a warning that names the page the tag is on.
93
+ def warn(context, message)
94
+ page = context.registers[:page]
95
+ path = page && page["path"]
96
+ Jekyll.logger.warn "before_after:", [path, message].compact.join(": ")
97
+ end
98
+
99
+ def alt(context, key, default)
100
+ v = value(context, key)
101
+ v.nil? ? default : v
102
+ end
103
+
104
+ # Resolved string for an option, or nil if it was not given (or unset).
105
+ def value(context, key)
106
+ text, kind = @attributes[key]
107
+ return nil if text.nil?
108
+ return text if kind == :literal
109
+
110
+ resolved = Liquid::Variable.new(text, @parse_context).render(context)
111
+ resolved = resolved.to_s unless resolved.nil?
112
+ if (resolved.nil? || resolved.empty?) && WORD_OPTIONS.include?(key) && text.match?(/\A\w+\z/)
113
+ return text
114
+ end
115
+
116
+ resolved
117
+ end
118
+
119
+ def image_url(context, key)
120
+ raw = value(context, key).to_s.strip
121
+ if raw.empty?
122
+ given = @attributes[key]
123
+ reason = given.nil? ? "is missing" : "is empty (#{given[0].inspect} resolved to nothing)"
124
+ warn(context, "`#{key}` #{reason}; skipping this slider " \
125
+ '(e.g. {% before_after before="/a.jpg" after="/b.jpg" %})')
126
+ return nil
127
+ end
128
+
129
+ return raw unless raw.start_with?("/") && !raw.start_with?("//")
130
+
131
+ site = context.registers[:site]
132
+ return raw unless site
133
+
134
+ Jekyll::BeforeAfterTag::Url.new(context).relative_url(raw)
135
+ end
136
+
137
+ # Reuses Jekyll's own relative_url filter so baseurl handling matches
138
+ # the rest of the site.
139
+ class Url
140
+ include Jekyll::Filters::URLFilters
141
+
142
+ def initialize(context)
143
+ @context = context
144
+ end
145
+ end
35
146
  end
36
147
  end
37
148
 
@@ -1,28 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "jekyll/before_after_config"
3
4
  require "jekyll/before_after_tag"
4
-
5
- module Jekyll
6
- class BeforeAfterScriptInjector
7
- def call(doc)
8
- return if doc.output.nil? || !doc.output.include?("<img-comparison-slider>")
9
- return if doc.output.include?("<!-- img-comparison-slider-injected -->")
10
-
11
- script = <<~HTML
12
- <!-- img-comparison-slider-injected -->
13
- <script type="module" src="https://unpkg.com/img-comparison-slider@7/dist/index.js"></script>
14
- <link rel="stylesheet" href="https://unpkg.com/img-comparison-slider@7/dist/styles.css">
15
- HTML
16
-
17
- doc.output.sub!("</head>", "#{script}</head>")
18
- end
19
- end
20
-
21
- Jekyll::Hooks.register :pages, :post_render do |doc|
22
- Jekyll::BeforeAfterScriptInjector.new.call(doc)
23
- end
24
-
25
- Jekyll::Hooks.register :documents, :post_render do |doc|
26
- Jekyll::BeforeAfterScriptInjector.new.call(doc)
27
- end
28
- end
5
+ require "jekyll/before_after_injector"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-before-after
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Chance
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-08-29 00:00:00.000000000 Z
10
+ date: 2026-11-29 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: jekyll
@@ -29,11 +29,11 @@ dependencies:
29
29
  - - "<"
30
30
  - !ruby/object:Gem::Version
31
31
  version: '5.0'
32
- description: A Jekyll plugin that provides a {% before_after %} Liquid tag for rendering
33
- a polished, accessible drag-to-reveal image comparison slider. Supports horizontal
34
- and vertical orientations, configurable start position, overlay labels, responsive
35
- images, keyboard navigation, and full CSS custom property theming. Pure vanilla
36
- JS, no external dependencies, GitHub Pages compatible.
32
+ description: A Jekyll plugin that provides a {% before_after %} Liquid tag for drag-to-reveal
33
+ image comparisons, built on the img-comparison-slider web component (loaded from
34
+ a CDN with Subresource Integrity, or self-hosted). Supports horizontal and vertical
35
+ orientations, start position, alt text, ARIA slider attributes, and Liquid variables
36
+ and baseurl in image paths.
37
37
  email:
38
38
  - jason@jasonchance.com
39
39
  executables: []
@@ -46,7 +46,8 @@ files:
46
46
  - README.md
47
47
  - jekyll-before-after.gemspec
48
48
  - lib/jekyll-before-after.rb
49
- - lib/jekyll/before_after_generator.rb
49
+ - lib/jekyll/before_after_config.rb
50
+ - lib/jekyll/before_after_injector.rb
50
51
  - lib/jekyll/before_after_tag.rb
51
52
  homepage: https://jasonchance.com/projects/jekyll-before-after/
52
53
  licenses:
@@ -59,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
60
  requirements:
60
61
  - - ">="
61
62
  - !ruby/object:Gem::Version
62
- version: '2.7'
63
+ version: '3.0'
63
64
  required_rubygems_version: !ruby/object:Gem::Requirement
64
65
  requirements:
65
66
  - - ">="
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jekyll
4
- class BeforeAfterGenerator < Generator
5
- safe true
6
- priority :highest
7
-
8
- def generate(site)
9
- # Create the CSS as a virtual file that can be included
10
- css_content = %q{<style>
11
- .before-after{position:relative;overflow:hidden;cursor:col-resize;user-select:none;touch-action:none;border-radius:var(--ba-radius, 4px);background:var(--ba-bg, #000);margin-bottom:2rem}
12
- .before-after[data-orientation="vertical"]{cursor:row-resize}
13
- .before-after--dragging{cursor:grabbing}
14
- .before-after__after{position:relative;display:block}
15
- .before-after__after img,.before-after__after picture img{display:block;width:100%;height:auto;pointer-events:none}
16
- .before-after__before{position:absolute;inset:0;z-index:1;clip-path:inset(0 50% 0 0)}
17
- .before-after__before img,.before-after__before picture img{display:block;width:100%;height:100%;object-fit:cover;pointer-events:none}
18
- .before-after__label{position:absolute;bottom:.75rem;z-index:2;padding:.25rem .6rem;font-size:.75rem;font-weight:600;letter-spacing:.04em;text-transform:uppercase;background:var(--ba-label-bg, rgba(0, 0, 0, 0.55));color:var(--ba-label-color, #fff);border-radius:var(--ba-label-radius, 3px);pointer-events:none}
19
- .before-after__label--before{left:.75rem}
20
- .before-after__label--after{right:.75rem}
21
- .before-after__handle{position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);z-index:10;display:flex;align-items:center;justify-content:center;outline:0}
22
- .before-after__handle:focus-visible .before-after__handle-knob{box-shadow:0 0 0 3px var(--ba-focus-ring, rgba(255, 255, 255, 0.8))}
23
- .before-after__handle-line{position:absolute;background:var(--ba-handle-color, #fff);opacity:.85}
24
- .before-after:not([data-orientation="vertical"]) .before-after__handle-line{width:var(--ba-line-width, 2px);top:-200%;bottom:-200%;left:50%;transform:translateX(-50%)}
25
- [data-orientation="vertical"] .before-after__handle-line{height:var(--ba-line-width, 2px);left:-200%;right:-200%;top:50%;transform:translateY(-50%)}
26
- .before-after__handle-knob{position:relative;z-index:1;width:var(--ba-knob-size, 2.75rem);height:var(--ba-knob-size, 2.75rem);border-radius:50%;background:var(--ba-handle-color, #fff);color:var(--ba-knob-icon-color, #333);display:flex;align-items:center;justify-content:center;box-shadow:0 2px 8px rgba(0, 0, 0, 0.35);transition:transform .15s ease, box-shadow .15s ease;cursor:grab}
27
- .before-after__handle-knob svg{width:1.1rem;height:1.1rem}
28
- .before-after--dragging .before-after__handle-knob{cursor:grabbing;transform:scale(1.12);box-shadow:0 4px 16px rgba(0, 0, 0, 0.45)}
29
- [data-orientation="vertical"] .before-after__handle-knob svg{transform:rotate(90deg)}
30
- </style>}
31
-
32
- # Store in site data so layouts can access it
33
- site.data['before_after_css'] = css_content
34
- end
35
- end
36
- end