jekyll-og-image 2.1.1 → 3.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: c942bb90210108818a9e9c16c8efe20ff170079a7409bb9f8089907217b38283
4
- data.tar.gz: fa663ffdad61a24c2ed416e105ec6ae2769fe8c5f6dd6cecbcabbc6aeba84121
3
+ metadata.gz: f12db2733443d3e1704f0448ebb4b84c34a15e44d6feeb0ef7b2092802900ae9
4
+ data.tar.gz: 5e8ca788996efab23fdf0f4292ec153b3cf2eae182a2d71946560aa071edf3d7
5
5
  SHA512:
6
- metadata.gz: 8b044a110d16f46019936c724c5aa9d11f4874ffa7436c92b8cdbb6a571be208d155df37bca239fe15b95406644b2b4b5b9ee77c5738e9c1e367131f9ec64af1
7
- data.tar.gz: 446d504388871d526cc79bb80c519243227d0be2a905b4633f018286fc29971266f8a56e227fbc06d151d9ffb078e0d65456872ec83c58bcf6467aa1e0a66745
6
+ metadata.gz: fb99a4f054458c83ff576d41c96401ef6cd38b7abb787d2de3657833d835f4457bf870e5259e7c1446d27b82a90d1cc887b7bc3754a5d689ca3c582b4e2d6e30
7
+ data.tar.gz: fe24fa07b713d0b1001bf1e4d57c5eab8cc162f90544e507f84a5d86a6d3a9027b649a2bd2cb7f8dd33f0f50eb700af1426306372ae876f5cb5b697811ad88fb
data/README.md CHANGED
@@ -1,16 +1,29 @@
1
- # Jekyll OG Image
1
+ <p align="center">
2
+ <img src="assets/icon.svg" alt="Jekyll OG Image" width="128" height="128">
3
+ </p>
2
4
 
3
- A Jekyll plugin to automatically generate open graph images for posts.
5
+ <h1 align="center">Jekyll OG Image</h1>
4
6
 
5
- [![Gem Version](https://badge.fury.io/rb/jekyll-og-image.svg)](https://badge.fury.io/rb/jekyll-og-image)
6
- [![Lint](https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/lint.yml/badge.svg?branch=main)](https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/lint.yml)
7
- [![Tests](https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/tests.yml)
7
+ <p align="center">
8
+ A Jekyll plugin to automatically generate open graph images for posts.
9
+ </p>
10
+
11
+ <p align="center">
12
+ <a href="https://badge.fury.io/rb/jekyll-og-image"><img src="https://badge.fury.io/rb/jekyll-og-image.svg" alt="Gem Version"></a>
13
+ <a href="https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/lint.yml"><img src="https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/lint.yml/badge.svg?branch=main" alt="Lint"></a>
14
+ <a href="https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/tests.yml"><img src="https://github.com/igor-alexandrov/jekyll-og-image/actions/workflows/tests.yml/badge.svg?branch=main" alt="Tests"></a>
15
+ </p>
16
+
17
+ <p align="center">
18
+ <img src="examples/1.png" alt="Example open graph image" width="600">
19
+ </p>
8
20
 
9
21
  ## Installation
10
22
 
11
23
  Add this line to your site's Gemfile:
12
24
 
13
25
  ```ruby
26
+ gem 'jekyll-seo-tag'
14
27
  gem 'jekyll-og-image'
15
28
  ```
16
29
 
@@ -22,13 +35,11 @@ plugins:
22
35
  - jekyll-og-image
23
36
  ```
24
37
 
25
- This plugin requires `libvips` to be installed. If you are using GitHub Pages to host your Jekyll site, don't forget to install `libvips` before running `jekyll build`. See the example below.
38
+ The plugin requires Ruby 3.2 or newer and [libvips](https://www.libvips.org/install.html). If you are using GitHub Pages to host your Jekyll site, don't forget to install `libvips` before running `jekyll build`. See the example below.
26
39
 
27
40
  ``` yaml
28
41
  jobs:
29
42
  build:
30
- needs:
31
- - lint
32
43
  runs-on: ubuntu-latest
33
44
  steps:
34
45
  - name: Checkout
@@ -40,16 +51,6 @@ jobs:
40
51
  ruby-version: .ruby-version
41
52
  bundler-cache: true
42
53
 
43
- - name: Set Node.js 20.x
44
- uses: actions/setup-node@v3
45
- with:
46
- node-version: 20.x
47
-
48
- - name: Run install
49
- uses: borales/actions-yarn@v4
50
- with:
51
- cmd: install
52
-
53
54
  - name: Update apt
54
55
  env:
55
56
  DEBIAN_FRONTEND: noninteractive
@@ -65,19 +66,28 @@ jobs:
65
66
  uses: actions/configure-pages@v5
66
67
 
67
68
  - name: Build with Jekyll
68
- run: ./bin/jekyll build --baseurl "${{ steps.pages.outputs.base_path }}"
69
+ run: bundle exec jekyll build --baseurl "${{ steps.pages.outputs.base_path }}"
69
70
  env:
70
71
  JEKYLL_ENV: production
71
72
 
72
73
  - name: Upload artifact
73
74
  # Automatically uploads an artifact from the './_site' directory by default
74
75
  uses: actions/upload-pages-artifact@v3
75
-
76
76
  ```
77
77
 
78
78
  ## Usage
79
79
 
80
- Jekyll OG Image works together with [jekyll-seo-tag](https://github.com/jekyll/jekyll-seo-tag) plugin. It automatically generates open graph images for posts and inserts them into the posts metadata.
80
+ Jekyll OG Image works together with the [jekyll-seo-tag](https://github.com/jekyll/jekyll-seo-tag) plugin. It generates open graph images for posts and sets them as the `image` in each post's front matter, which jekyll-seo-tag turns into `og:image` and `twitter:image` meta tags. jekyll-seo-tag isn't installed automatically, so add it to your Gemfile yourself, as shown above.
81
+
82
+ ### Regenerating images
83
+
84
+ An image is regenerated when anything that affects it changes: the document's title or metadata, the plugin configuration (site-wide or in front matter), the logo or background image files, or the plugin version. Otherwise the existing image is kept, so builds stay fast and `jekyll serve` doesn't rewrite unchanged files.
85
+
86
+ To track this, the plugin keeps a fingerprint of each image in `.jekyll-og-image.json` inside `output_dir`. Jekyll doesn't publish it. If you commit generated images to your repository, commit this file too; without it, every image is regenerated once.
87
+
88
+ ### Upgrading from 2.x
89
+
90
+ Images used to be named after the document's title or slug, which let two documents with the same title share one image. They are now named after the source file (see `output_dir` below), so every image gets a new path and URL on the first build after upgrading. The old images are left in place. Delete them from `output_dir` if you committed them to your repository.
81
91
 
82
92
  ## Configuration
83
93
 
@@ -85,11 +95,15 @@ The plugin can be configured in the `_config.yml` file or in the post's front ma
85
95
 
86
96
  The following configuration options are available:
87
97
 
98
+ Colors are hex values in `"#RRGGBB"` or `"#RGB"` form. Quote them in YAML, because an unquoted `#` starts a comment.
99
+
100
+ Paths to images (`image.path`, `canvas.background_image`) are relative to the site's source directory. If a file doesn't exist, the plugin logs a warning and generates the image without it.
101
+
88
102
  * `collections` - An array specifying which types of collections to generate images for. Supports `"posts"`, `"pages"`, and the names of any custom collections. Default: `["posts"]`
89
103
 
90
- * `output_dir` – The directory where the generated images will be saved. Images will be placed in subdirectories named after their collection type (e.g., `assets/images/og/posts`, `assets/images/og/pages`). Default: `assets/images/og`
104
+ * `output_dir` – The directory where the generated images will be saved. Images are placed in a subdirectory named after their collection type and named after their source file, e.g. `_posts/2024-02-15-hello.md` → `assets/images/og/posts/2024-02-15-hello.png` and `blog/index.md` → `assets/images/og/pages/blog/index.png`. Default: `assets/images/og`
91
105
 
92
- * `force` – If set to `true`, the plugin will generate an image for every document, even if the document already has an image. Default: `false`
106
+ * `force` – If set to `true`, every image is regenerated on each build, even when it is up to date. Rarely needed, since images are regenerated automatically when they change (see [Regenerating images](#regenerating-images)). Under `jekyll serve`, it applies to the first build only. Default: `false`
93
107
  * `verbose` – If set to `true`, the plugin will output additional information about the image generation process. Default: `false`
94
108
 
95
109
  * `skip_drafts` – If set to `true`, the plugin will skip post drafts when generating images. Default: `true`
@@ -97,10 +111,10 @@ The following configuration options are available:
97
111
  * `canvas` – The canvas configuration options:
98
112
  * `background_color` – The background color of the canvas. Default: `#FFFFFF`
99
113
  * `background_image` – The background image of the canvas. Default: `nil`
100
- * `width` – The width of the generated image in pixels. Default: `1200`
114
+ * `width` – The width of the generated image in pixels. The layout is designed for a 1200px-wide canvas and scales with this width: margins, text sizes, and the logo and border sizes below are all given for 1200px. Default: `1200`
101
115
  * `height` – The height of the generated image in pixels. Default: `600`
102
116
 
103
- * `header` – The header configuration options:
117
+ * `header` – The header configuration options. A title too long for the space above the metadata line is shrunk to fit.
104
118
  * `font_family` – The font family of the header text. Default: `Helvetica, Bold`
105
119
  * `color` – The color of the header text. Default: `#2f313d`
106
120
  * `prefix` – Text to prepend to the title. Default: `""`
@@ -110,8 +124,14 @@ The following configuration options are available:
110
124
  * `font_family` – The font family of the content text. Default: `Helvetica, Regular`
111
125
  * `color` – The color of the content text. Default: `#535358`
112
126
 
127
+ * `description` – Text drawn below the title, such as a summary or the first sentence of the post. It wraps, shrinks to fit above the metadata line, and is left out when a long title leaves no room for it. Not drawn unless `source` is set.
128
+ * `source` – Where the text comes from: `excerpt` for the document's excerpt as plain text, or the name of a front matter field such as `description`. Default: `nil`
129
+ * `sentences` – Keep only this many sentences from the start of the text. Default: `nil`, which keeps it all
130
+ * `font_family` – The font family of the description text. Default: `Helvetica, Regular`
131
+ * `color` – The color of the description text. Default: `#535358`
132
+
113
133
  * `border_bottom` – The border bottom configuration options:
114
- * `width` – The width of the border bottom. Default: `20`
134
+ * `width` – The width of the border bottom. Default: `0`
115
135
  * `fill` – The array of colors to fill the border bottom. Default: `["#000000"]`
116
136
 
117
137
  * `domain` – The domain name to use in the image. Default: `nil`
@@ -122,7 +142,7 @@ The following configuration options are available:
122
142
  * `height` – The height of the logo in pixels. Default: `150`
123
143
  * `radius` – The radius for rounded corners on the logo. Default: `50`
124
144
  * `position` – The position of the logo as `{x, y}` coordinates. Default: `{x: 80, y: 100}`
125
- * `gravity` – The gravity anchor for logo positioning (nw, n, ne, w, e, sw, s, se). Default: `ne`
145
+ * `gravity` – The gravity anchor for logo positioning (nw, ne, sw, se). Default: `ne`
126
146
 
127
147
  * `metadata` – The metadata configuration options:
128
148
  * `fields` – Array of metadata fields to display. Available options: `"date"`, `"tags"`, or any custom front matter field. Default: `["date", "tags"]`
@@ -259,6 +279,21 @@ This configuration:
259
279
  - Uses a pipe separator between metadata fields
260
280
  - Formats dates as "15 February 2024"
261
281
 
282
+ ### Description
283
+
284
+ ```yaml
285
+ # _config.yml
286
+ og_image:
287
+ domain: "igor.works"
288
+ description:
289
+ source: "excerpt"
290
+ sentences: 1
291
+ ```
292
+
293
+ ![Example 5](examples/5.png)
294
+
295
+ This draws the first sentence of each post's excerpt below the title. Use `source: "description"` to read a `description` field from front matter instead, and set `og_image.description` in a post's front matter to change it for that post.
296
+
262
297
 
263
298
  ## Contributing
264
299
 
data/assets/icon.png ADDED
Binary file
data/assets/icon.svg ADDED
@@ -0,0 +1,39 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
2
+ <defs>
3
+ <linearGradient id="bg" x1="0" y1="0" x2="0" y2="1">
4
+ <stop offset="0" stop-color="#3a3d4c"/>
5
+ <stop offset="1" stop-color="#1f2029"/>
6
+ </linearGradient>
7
+ <linearGradient id="logo" x1="0" y1="0" x2="1" y2="1">
8
+ <stop offset="0" stop-color="#EDA895"/>
9
+ <stop offset="1" stop-color="#D51F06"/>
10
+ </linearGradient>
11
+ <clipPath id="card">
12
+ <rect x="48" y="148" width="416" height="248" rx="24"/>
13
+ </clipPath>
14
+ </defs>
15
+
16
+ <rect width="512" height="512" rx="112" fill="url(#bg)"/>
17
+
18
+ <rect x="96" y="108" width="320" height="248" rx="24" fill="#ffffff" opacity="0.18"/>
19
+ <rect x="72" y="128" width="368" height="248" rx="24" fill="#ffffff" opacity="0.36"/>
20
+
21
+ <g clip-path="url(#card)">
22
+ <rect x="48" y="148" width="416" height="248" fill="#ffffff"/>
23
+
24
+ <rect x="84" y="184" width="224" height="30" rx="15" fill="#2f313d"/>
25
+ <rect x="84" y="226" width="148" height="30" rx="15" fill="#2f313d"/>
26
+
27
+ <rect x="360" y="184" width="68" height="68" rx="18" fill="url(#logo)"/>
28
+
29
+ <rect x="84" y="290" width="112" height="12" rx="6" fill="#a4a4ab"/>
30
+ <rect x="84" y="316" width="168" height="12" rx="6" fill="#a4a4ab"/>
31
+ <rect x="360" y="290" width="68" height="12" rx="6" fill="#a4a4ab"/>
32
+
33
+ <rect x="48" y="364" width="83.2" height="32" fill="#820C02"/>
34
+ <rect x="131.2" y="364" width="83.2" height="32" fill="#A91401"/>
35
+ <rect x="214.4" y="364" width="83.2" height="32" fill="#D51F06"/>
36
+ <rect x="297.6" y="364" width="83.2" height="32" fill="#DE3F24"/>
37
+ <rect x="380.8" y="364" width="83.2" height="32" fill="#EDA895"/>
38
+ </g>
39
+ </svg>
data/examples/5.png ADDED
Binary file
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # require "anyway_config"
4
-
5
3
  class JekyllOgImage::Configuration
6
4
  Canvas = Data.define(:background_color, :background_image, :width, :height) do
7
5
  def initialize(background_color: "#FFFFFF", background_image: nil, width: 1200, height: 600)
@@ -23,13 +21,23 @@ class JekyllOgImage::Configuration
23
21
 
24
22
  Border = Data.define(:width, :fill) do
25
23
  def initialize(width: 0, fill: nil)
26
- fill.is_a?(Array) ? fill : [ fill ]
27
- super(width: width, fill: fill)
24
+ super(width: width, fill: Array(fill || "#000000"))
28
25
  end
29
26
  end
30
27
 
31
28
  Image = Data.define(:path, :width, :height, :radius, :position, :gravity) do
32
- def initialize(path: nil, width: 150, height: 150, radius: 50, position: { x: 80, y: 100 }, gravity: :ne)
29
+ def initialize(path: nil, width: 150, height: 150, radius: 50, position: {}, gravity: :ne)
30
+ # Values read from YAML have string keys and string gravity
31
+ position = { x: 80, y: 100 }.merge(position.transform_keys(&:to_sym))
32
+
33
+ super(path: path, width: width, height: height, radius: radius, position: position, gravity: gravity.to_sym)
34
+ end
35
+ end
36
+
37
+ # Text below the header, read from a front matter field or the document's
38
+ # excerpt, optionally cut to its first sentences. Not drawn unless source is set.
39
+ Description = Data.define(:source, :sentences, :font_family, :color) do
40
+ def initialize(source: nil, sentences: nil, font_family: "Helvetica, Regular", color: "#535358")
33
41
  super
34
42
  end
35
43
  end
@@ -44,7 +52,7 @@ class JekyllOgImage::Configuration
44
52
  @raw_config = raw_config
45
53
  end
46
54
 
47
- def merge!(other)
55
+ def merge(other)
48
56
  config = Jekyll::Utils.deep_merge_hashes(
49
57
  @raw_config,
50
58
  other.to_h
@@ -86,26 +94,22 @@ class JekyllOgImage::Configuration
86
94
  end
87
95
 
88
96
  def canvas
89
- @raw_config["canvas"] ? Canvas.new(**Jekyll::Utils.symbolize_hash_keys(@raw_config["canvas"])) : Canvas.new
97
+ build_section(Canvas, "canvas")
90
98
  end
91
99
 
92
100
  def header
93
- @raw_config["header"] ? Header.new(**Jekyll::Utils.symbolize_hash_keys(@raw_config["header"])) : Header.new
101
+ build_section(Header, "header")
94
102
  end
95
103
 
96
104
  def content
97
- @raw_config["content"] ? Content.new(**Jekyll::Utils.symbolize_hash_keys(@raw_config["content"])) : Content.new
105
+ build_section(Content, "content")
98
106
  end
99
107
 
100
108
  def image
101
- if @raw_config["image"].is_a?(String)
102
- # Legacy support: if image is just a string, convert it to the new format
103
- Image.new(path: @raw_config["image"])
104
- elsif @raw_config["image"]
105
- Image.new(**Jekyll::Utils.symbolize_hash_keys(@raw_config["image"]))
106
- else
107
- Image.new
108
- end
109
+ # Legacy support: if image is just a string, convert it to the new format
110
+ return Image.new(path: @raw_config["image"]) if @raw_config["image"].is_a?(String)
111
+
112
+ build_section(Image, "image")
109
113
  end
110
114
 
111
115
  def domain
@@ -113,15 +117,35 @@ class JekyllOgImage::Configuration
113
117
  end
114
118
 
115
119
  def border_bottom
116
- @raw_config["border_bottom"] ? Border.new(**Jekyll::Utils.symbolize_hash_keys(@raw_config["border_bottom"])) : nil
120
+ build_section(Border, "border_bottom") if @raw_config["border_bottom"]
117
121
  end
118
122
 
119
- def margin_bottom
120
- 80 + (border_bottom&.width || 0)
123
+ def description
124
+ build_section(Description, "description")
121
125
  end
122
126
 
123
-
124
127
  def metadata
125
- @raw_config["metadata"] ? Metadata.new(**Jekyll::Utils.symbolize_hash_keys(@raw_config["metadata"])) : Metadata.new
128
+ build_section(Metadata, "metadata")
129
+ end
130
+
131
+ private
132
+
133
+ def build_section(klass, name)
134
+ value = @raw_config[name]
135
+ return klass.new unless value
136
+
137
+ unless value.is_a?(Hash)
138
+ raise Jekyll::Errors::InvalidConfigurationError, "og_image.#{name} must be a set of options, got #{value.inspect}"
139
+ end
140
+
141
+ options = Jekyll::Utils.symbolize_hash_keys(value)
142
+ unknown = options.keys - klass.members
143
+
144
+ if unknown.any?
145
+ raise Jekyll::Errors::InvalidConfigurationError,
146
+ "Unknown og_image.#{name} option: #{unknown.join(", ")}. Valid options: #{klass.members.join(", ")}"
147
+ end
148
+
149
+ klass.new(**options)
126
150
  end
127
151
  end
@@ -5,14 +5,22 @@ class JekyllOgImage::Element::Base
5
5
 
6
6
  private
7
7
 
8
+ VALID_GRAVITY.each do |gravity|
9
+ define_method("gravity_#{gravity}?") do
10
+ @gravity == gravity
11
+ end
12
+ end
13
+
8
14
  def hex_to_rgb(input)
9
15
  case input
10
- when String
11
- input.match(/#(..)(..)(..)/)[1..3].map(&:hex)
12
16
  when Array
13
17
  input
18
+ when /\A#\h{3}\z/
19
+ input[1..].chars.map { |digit| (digit * 2).hex }
20
+ when /\A#\h{6}\z/
21
+ input[1..].scan(/\h\h/).map(&:hex)
14
22
  else
15
- raise ArgumentError, "Unknown input #{input.inspect}"
23
+ raise ArgumentError, "Invalid color #{input.inspect}, expected a hex color like \"#FFFFFF\" or \"#FFF\""
16
24
  end
17
25
  end
18
26
 
@@ -31,18 +39,9 @@ class JekyllOgImage::Element::Base
31
39
  end
32
40
 
33
41
  def composite_with_gravity(canvas, overlay, x, y)
34
- if gravity_nw?
35
- canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
36
- elsif gravity_ne?
37
- x = canvas.width - overlay.width - x
38
- canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
39
- elsif gravity_sw?
40
- y = canvas.height - overlay.height - y
41
- canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
42
- elsif gravity_se?
43
- x = canvas.width - overlay.width - x
44
- y = canvas.height - overlay.height - y
45
- canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
46
- end
42
+ x = canvas.width - overlay.width - x if gravity_ne? || gravity_se?
43
+ y = canvas.height - overlay.height - y if gravity_sw? || gravity_se?
44
+
45
+ canvas.composite(overlay, :over, x: [ x ], y: [ y ]).flatten
47
46
  end
48
47
  end
@@ -7,7 +7,7 @@ class JekyllOgImage::Element::Border < JekyllOgImage::Element::Base
7
7
  def initialize(size, position: :bottom, fill: "#000000")
8
8
  @size = size
9
9
  @position = position
10
- @fill = fill
10
+ @fill = Array(fill)
11
11
 
12
12
  validate_position!
13
13
  end
@@ -15,14 +15,11 @@ class JekyllOgImage::Element::Border < JekyllOgImage::Element::Base
15
15
  def apply_to(canvas, &block)
16
16
  border = Vips::Image.black(*dimensions(canvas))
17
17
 
18
- parts(canvas).each.with_index do |part, index|
18
+ parts(canvas).each do |part|
19
19
  image = Vips::Image.black(part.width, part.height).ifthenelse([ 0, 0, 0 ], part.rgb)
20
+ x, y = vertical? ? [ 0, part.offset ] : [ part.offset, 0 ]
20
21
 
21
- if vertical?
22
- border = border.composite(image, :over, x: [ 0 ], y: [ part.offset ]).flatten
23
- else
24
- border = border.composite(image, :over, x: [ part.offset ], y: [ 0 ]).flatten
25
- end
22
+ border = border.composite(image, :over, x: [ x ], y: [ y ]).flatten
26
23
  end
27
24
 
28
25
  result = block.call(canvas, border) if block_given?
@@ -40,10 +37,6 @@ class JekyllOgImage::Element::Border < JekyllOgImage::Element::Base
40
37
 
41
38
  private
42
39
 
43
- def hex_to_rgb(hex)
44
- hex.match(/#(..)(..)(..)/)[1..3].map { |x| x.hex }
45
- end
46
-
47
40
  def dimensions(canvas)
48
41
  if vertical?
49
42
  [ @size, canvas.height ]
@@ -53,21 +46,15 @@ class JekyllOgImage::Element::Border < JekyllOgImage::Element::Base
53
46
  end
54
47
 
55
48
  def parts(canvas)
56
- if @fill.is_a?(Array)
57
- width, height = vertical? ? [ @size, (canvas.height / @fill.size) ] : [ (canvas.width / @fill.size), @size ]
58
-
59
- @fill.map.with_index do |item, index|
60
- Part.new(
61
- rgb: hex_to_rgb(item),
62
- width: width,
63
- height: height,
64
- offset: index * (vertical? ? height : width)
65
- )
66
- end
67
- else
68
- length = vertical? ? canvas.height : canvas.width
49
+ length = vertical? ? canvas.height : canvas.width
69
50
 
70
- [ Part.new(rgb: hex_to_rgb(@fill), length: length, offset: 0) ]
51
+ @fill.map.with_index do |color, index|
52
+ # Stripe edges are rounded down, so the last stripe absorbs any remainder
53
+ from = length * index / @fill.size
54
+ to = length * (index + 1) / @fill.size
55
+ width, height = vertical? ? [ @size, to - from ] : [ to - from, @size ]
56
+
57
+ Part.new(rgb: hex_to_rgb(color), width: width, height: height, offset: from)
71
58
  end
72
59
  end
73
60
 
@@ -75,10 +62,6 @@ class JekyllOgImage::Element::Border < JekyllOgImage::Element::Base
75
62
  @position == :left || @position == :right
76
63
  end
77
64
 
78
- def horizontal?
79
- @position == :top || @position == :bottom
80
- end
81
-
82
65
  def validate_position!
83
66
  unless %i[left right top bottom].include?(@position)
84
67
  raise ArgumentError, "Invalid position: #{@position.inspect}"
@@ -14,8 +14,6 @@ class JekyllOgImage::Element::Canvas < JekyllOgImage::Element::Base
14
14
 
15
15
  @canvas = @canvas.composite(overlay, :over, x: [ 0 ], y: [ 0 ]).flatten
16
16
  end
17
-
18
- @canvas
19
17
  end
20
18
 
21
19
  def image(source, **opts, &block)
@@ -18,13 +18,14 @@ class JekyllOgImage::Element::Image < JekyllOgImage::Element::Base
18
18
  image = image.flatten
19
19
  end
20
20
 
21
- image = round_corners(image) if @radius
22
-
23
21
  if @width && @height
24
22
  ratio = calculate_ratio(image, @width, @height, :min)
25
23
  image = image.resize(ratio)
26
24
  end
27
25
 
26
+ # Rounded after resizing, so the radius is in output pixels whatever the source size
27
+ image = round_corners(image) if @radius
28
+
28
29
  result = block.call(canvas, image) if block_given?
29
30
 
30
31
  x, y = result ? [ result.fetch(:x, 0), result.fetch(:y, 0) ] : [ 0, 0 ]
@@ -34,12 +35,6 @@ class JekyllOgImage::Element::Image < JekyllOgImage::Element::Base
34
35
 
35
36
  private
36
37
 
37
- VALID_GRAVITY.each do |gravity|
38
- define_method("gravity_#{gravity}?") do
39
- @gravity == gravity
40
- end
41
- end
42
-
43
38
  def round_corners(image)
44
39
  mask = %(
45
40
  <svg viewBox="0 0 #{image.width} #{image.height}">
@@ -1,10 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class JekyllOgImage::Element::Text < JekyllOgImage::Element::Base
4
- def initialize(message, gravity: :nw, width: nil, dpi: nil, color: "#000000", font: nil)
4
+ MARKUP_ESCAPES = { "&" => "&amp;", "<" => "&lt;", ">" => "&gt;" }.freeze
5
+
6
+ # Vips::Image.text parses its input as Pango markup, so plain text has to be escaped
7
+ def self.escape_markup(message)
8
+ message.to_s.gsub(/[&<>]/, MARKUP_ESCAPES)
9
+ end
10
+
11
+ def initialize(message, gravity: :nw, width: nil, height: nil, dpi: nil, color: "#000000", font: nil)
5
12
  @message = message
6
13
  @gravity = gravity
7
14
  @width = width
15
+ @height = height
8
16
  @dpi = dpi
9
17
  @color = color
10
18
  @font = font
@@ -22,7 +30,12 @@ class JekyllOgImage::Element::Text < JekyllOgImage::Element::Base
22
30
 
23
31
  params[:wrap] = :word if wrap_supported?
24
32
 
25
- text = Vips::Image.text(@message, **params)
33
+ text = Vips::Image.text(self.class.escape_markup(@message), **params)
34
+
35
+ # Too tall: given a width and height instead of a dpi, libvips picks the largest dpi that fits
36
+ if @height && text.height > @height && height_supported?
37
+ text = Vips::Image.text(self.class.escape_markup(@message), **params.except(:dpi), height: @height)
38
+ end
26
39
 
27
40
  text = text
28
41
  .new_from_image(hex_to_rgb(@color))
@@ -38,10 +51,9 @@ class JekyllOgImage::Element::Text < JekyllOgImage::Element::Base
38
51
 
39
52
  private
40
53
 
41
- VALID_GRAVITY.each do |gravity|
42
- define_method("gravity_#{gravity}?") do
43
- @gravity == gravity
44
- end
54
+ def height_supported?
55
+ # Vips::Image.text fits text to a height since vips 8.8.0
56
+ Vips.at_least_libvips?(8, 8)
45
57
  end
46
58
 
47
59
  def wrap_supported?
@@ -1,60 +1,67 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "cgi"
4
+ require "digest"
5
+
3
6
  class JekyllOgImage::Generator < Jekyll::Generator
4
7
  safe true
5
8
 
6
9
  def generate(site)
7
10
  config = JekyllOgImage.config
11
+ @missing_files = Set.new
12
+ @file_digests = {}
13
+ manifest = JekyllOgImage::Manifest.new(File.join(site.source, config.output_dir))
14
+
15
+ # `jekyll serve` reuses this generator for every rebuild. Forcing only the first
16
+ # build stops rewritten images from triggering the watcher in an endless loop.
17
+ @first_build = !@built
18
+ @built = true
8
19
 
9
20
  config.collections.each do |type|
10
- process_collection(site, type, config)
21
+ process_collection(site, type, config, manifest)
11
22
  end
23
+
24
+ manifest.save
12
25
  end
13
26
 
14
27
  private
15
28
 
16
- def process_collection(site, type, config)
29
+ def process_collection(site, type, config, manifest)
17
30
  Jekyll.logger.info "Jekyll Og Image:", "Processing type: #{type}" if config.verbose?
18
31
 
19
- items = get_items_for_collection(site, type)
20
- return if items.empty?
21
-
22
- base_output_dir = File.join(config.output_dir, type)
23
- absolute_output_dir = File.join(site.config["source"], base_output_dir)
24
- FileUtils.mkdir_p absolute_output_dir
25
-
26
- items.each do |item|
32
+ get_items_for_collection(site, type).each do |item|
27
33
  if item.respond_to?(:draft?) && item.draft? && config.skip_drafts?
28
34
  Jekyll.logger.info "Jekyll Og Image:", "Skipping draft: #{item.data['title']}" if config.verbose?
29
35
  next
30
36
  end
31
37
 
32
- fallback_basename = if item.respond_to?(:basename_without_ext)
33
- item.basename_without_ext
34
- # rubocop:disable Layout/ElseAlignment # Disabled due to RuboCop error in v3.3.0
35
- else
36
- # rubocop:enable Layout/ElseAlignment
37
- File.basename(item.name, File.extname(item.name))
38
- end
38
+ item_config = config.merge(item.data["og_image"] || {})
39
+ next unless item_config.enabled?
39
40
 
40
- slug = item.data["slug"] || Jekyll::Utils.slugify(item.data["title"] || fallback_basename)
41
- image_filename = "#{slug}.png"
42
- absolute_image_path = File.join(absolute_output_dir, image_filename)
43
- relative_image_path = File.join("/", base_output_dir, image_filename) # Use leading slash for URL
41
+ image_path = File.join(type, "#{image_basename(item)}.png")
42
+ site_image_path = File.join(config.output_dir, image_path)
43
+ absolute_image_path = File.join(site.source, site_image_path)
44
+ digest = image_digest(site, item, item_config)
44
45
 
45
- if !File.exist?(absolute_image_path) || config.force?
46
+ force = @first_build && item_config.force?
47
+
48
+ if force || !File.exist?(absolute_image_path) || !manifest.fresh?(image_path, digest)
46
49
  Jekyll.logger.info "Jekyll Og Image:", "Generating image #{absolute_image_path}" if config.verbose?
47
- generate_image_for_document(site, item, absolute_image_path, config)
50
+ FileUtils.mkdir_p(File.dirname(absolute_image_path))
51
+ generate_image_for_document(site, item, absolute_image_path, item_config)
48
52
  else
49
- Jekyll.logger.info "Jekyll Og Image:", "Skipping image generation for #{relative_image_path} as it already exists." if config.verbose?
53
+ Jekyll.logger.info "Jekyll Og Image:", "Skipping #{site_image_path}, it is up to date" if config.verbose?
50
54
  end
51
55
 
52
- register_static_file(site, base_output_dir, image_filename, config) if File.exist?(absolute_image_path)
56
+ next unless File.exist?(absolute_image_path)
57
+
58
+ manifest.record(image_path, digest)
59
+ register_static_file(site, site_image_path, config)
53
60
 
54
61
  item.data["image"] ||= {
55
- "path" => relative_image_path,
56
- "width" => JekyllOgImage.config.canvas.width,
57
- "height" => JekyllOgImage.config.canvas.height,
62
+ "path" => File.join("/", site_image_path), # Use leading slash for URL
63
+ "width" => item_config.canvas.width,
64
+ "height" => item_config.canvas.height,
58
65
  "alt" => item.data["title"]
59
66
  }
60
67
  end
@@ -65,7 +72,7 @@ class JekyllOgImage::Generator < Jekyll::Generator
65
72
  when "posts"
66
73
  site.posts.docs
67
74
  when "pages"
68
- site.pages.reject { |page| !page.html? }
75
+ site.pages.select(&:html?)
69
76
  else
70
77
  if site.collections.key?(type)
71
78
  site.collections[type].docs
@@ -76,104 +83,209 @@ class JekyllOgImage::Generator < Jekyll::Generator
76
83
  end
77
84
  end
78
85
 
79
- def register_static_file(site, base_output_dir, image_filename, config)
80
- relative_path = File.join("/", base_output_dir, image_filename)
86
+ # The source file's path without its collection directory and extension, which is
87
+ # unique within the site: "_posts/2024-01-01-hello.md" => "2024-01-01-hello",
88
+ # "blog/index.md" => "blog/index".
89
+ def image_basename(item)
90
+ path = item.relative_path.delete_prefix("/")
91
+ path = path.delete_prefix("#{item.collection.relative_directory}/") if item.respond_to?(:collection) && item.collection
92
+ path = path.delete_suffix(File.extname(path))
93
+
94
+ path.split("/").map { |segment| slugify_path_segment(segment) }.join("/")
95
+ end
96
+
97
+ def slugify_path_segment(segment)
98
+ slug = Jekyll::Utils.slugify(segment)
99
+ # A name made only of characters slugify removes, e.g. emoji
100
+ slug.empty? ? Digest::SHA256.hexdigest(segment)[0, 12] : slug
101
+ end
102
+
103
+ # Changes whenever anything that affects the rendered image changes
104
+ def image_digest(site, item, config)
105
+ inputs = [
106
+ JekyllOgImage::VERSION,
107
+ config.canvas.to_h,
108
+ config.header.to_h,
109
+ config.content.to_h,
110
+ config.image.to_h,
111
+ config.border_bottom&.to_h,
112
+ config.metadata.to_h,
113
+ config.description.to_h,
114
+ config.domain,
115
+ item.data["title"],
116
+ description_for(item, config),
117
+ config.metadata.fields.map { |field| metadata_value_for(item, field, config) },
118
+ file_digest(site, config.image.path),
119
+ file_digest(site, config.canvas.background_image)
120
+ ]
121
+
122
+ Digest::SHA256.hexdigest(JSON.generate(inputs))
123
+ end
124
+
125
+ def file_digest(site, path)
126
+ return unless path
127
+
128
+ absolute_path = File.join(site.source, path)
129
+ @file_digests[absolute_path] ||= Digest::SHA256.file(absolute_path).hexdigest if File.file?(absolute_path)
130
+ end
131
+
132
+ def register_static_file(site, site_image_path, config)
133
+ relative_path = File.join("/", site_image_path)
81
134
  return if site.static_files.any? { |file| file.relative_path == relative_path }
82
135
 
136
+ # Leading slash in dir, as Jekyll's reader does, so relative_path matches the check above
83
137
  static_file = Jekyll::StaticFile.new(
84
138
  site,
85
139
  site.source,
86
- base_output_dir,
87
- image_filename
140
+ File.dirname(relative_path),
141
+ File.basename(relative_path)
88
142
  )
89
143
 
90
144
  site.static_files << static_file
91
- Jekyll.logger.info "Jekyll Og Image:", "Added #{base_output_dir}/#{image_filename} to static files" if config.verbose?
145
+ Jekyll.logger.info "Jekyll Og Image:", "Added #{site_image_path} to static files" if config.verbose?
92
146
  end
93
147
 
94
- def generate_image_for_document(site, item, path, base_config)
95
- config = base_config.merge!(item.data["og_image"] || {})
96
-
97
- return unless config.enabled?
148
+ def generate_image_for_document(site, item, path, config)
149
+ layout = JekyllOgImage::Layout.new(config)
98
150
 
99
151
  canvas = generate_canvas(site, config)
100
- canvas = add_border_bottom(canvas, config) if config.border_bottom
101
- canvas = add_image(canvas, File.read(File.join(site.config["source"], config.image.path)), config) if config.image.path
102
- canvas = add_header(canvas, item, config)
103
- canvas = add_metadata(canvas, item, config)
104
- canvas = add_domain(canvas, item, config) if config.domain
152
+ canvas = add_border_bottom(canvas, config, layout) if config.border_bottom
153
+ logo = read_source_file(site, config.image.path, "og_image.image.path") if config.image.path
154
+ canvas = add_image(canvas, logo, config, layout) if logo
155
+ canvas, header_height = add_header(canvas, item, config, layout)
156
+ canvas = add_description(canvas, item, config, layout, header_height)
157
+ canvas = add_metadata(canvas, item, config, layout)
158
+ canvas = add_domain(canvas, config, layout) if config.domain
105
159
 
106
160
  canvas.save(path)
107
161
  end
108
162
 
109
163
  def generate_canvas(site, config)
110
- background_image = if config.canvas.background_image
111
- bg_path = File.join(site.config["source"], config.canvas.background_image.gsub(/^\//, ""))
112
- File.exist?(bg_path) ? File.read(bg_path) : nil
164
+ if config.canvas.background_image
165
+ background_image = read_source_file(site, config.canvas.background_image, "og_image.canvas.background_image")
113
166
  end
114
167
 
115
- JekyllOgImage::Element::Canvas.new(JekyllOgImage.config.canvas.width, JekyllOgImage.config.canvas.height,
168
+ JekyllOgImage::Element::Canvas.new(config.canvas.width, config.canvas.height,
116
169
  background_color: config.canvas.background_color,
117
170
  background_image: background_image
118
171
  )
119
172
  end
120
173
 
121
- def add_border_bottom(canvas, config)
122
- canvas.border(config.border_bottom.width,
174
+ def read_source_file(site, path, option)
175
+ absolute_path = File.join(site.source, path)
176
+ return File.binread(absolute_path) if File.file?(absolute_path)
177
+
178
+ # Warn once per build, not once per document
179
+ Jekyll.logger.warn "Jekyll Og Image:", "#{option} file not found: #{path}, skipping it" if @missing_files.add?(path)
180
+ nil
181
+ end
182
+
183
+ def add_border_bottom(canvas, config, layout)
184
+ canvas.border(layout.border_width,
123
185
  position: :bottom,
124
186
  fill: config.border_bottom.fill
125
187
  )
126
188
  end
127
189
 
128
- def add_image(canvas, image_data, config)
129
- image_config = config.image
190
+ def add_image(canvas, image_data, config, layout)
130
191
  canvas.image(image_data,
131
- gravity: image_config.gravity,
132
- width: image_config.width,
133
- height: image_config.height,
134
- radius: image_config.radius
135
- ) { |_canvas, _text| { x: image_config.position[:x], y: image_config.position[:y] } }
192
+ gravity: config.image.gravity,
193
+ width: layout.logo_width,
194
+ height: layout.logo_height,
195
+ radius: layout.logo_radius
196
+ ) { |_canvas, _image| layout.logo_position }
136
197
  end
137
198
 
138
- def add_header(canvas, item, config)
199
+ def add_header(canvas, item, config, layout)
139
200
  title = item.data["title"] || "Untitled"
140
201
  full_title = "#{config.header.prefix}#{title}#{config.header.suffix}"
141
202
 
142
- # Calculate available width for header text to avoid overlap with image
143
- header_width = if config.image.path
144
- # Canvas width - left margin - right margin - image width - spacing
145
- # 1200 - 80 - 80 - image_width - 30 (spacing)
146
- 1040 - config.image.width - 30
147
- else
148
- 1040
149
- end
150
-
151
- canvas.text(full_title,
152
- width: header_width,
203
+ header_height = 0
204
+ canvas = canvas.text(full_title,
205
+ width: layout.header_width,
206
+ height: layout.header_max_height(content_line_height(config, layout)),
153
207
  color: config.header.color,
154
- dpi: 400,
208
+ dpi: layout.header_dpi,
155
209
  font: config.header.font_family
156
- ) { |_canvas, _text| { x: 80, y: 100 } }
210
+ ) do |_canvas, text|
211
+ header_height = text.height
212
+ { x: layout.margin, y: layout.header_top }
213
+ end
214
+
215
+ [ canvas, header_height ]
157
216
  end
158
217
 
159
- def add_metadata(canvas, item, config)
160
- metadata_text = metadata_text_for(item, config)
161
- return canvas if metadata_text.empty?
218
+ # Fits the description between the header and the metadata line, shrinking it
219
+ # if needed, and leaves it out when there isn't room for a single line.
220
+ def add_description(canvas, item, config, layout, header_height)
221
+ description = description_for(item, config)
222
+ return canvas if description.nil? || description.empty?
223
+
224
+ max_height = layout.description_max_height(header_height, content_line_height(config, layout))
225
+ return canvas if max_height < description_line_height(config, layout)
226
+
227
+ canvas.text(description,
228
+ width: layout.header_width,
229
+ height: max_height,
230
+ color: config.description.color,
231
+ dpi: layout.description_dpi,
232
+ font: config.description.font_family
233
+ ) { |_canvas, _text| { x: layout.margin, y: layout.description_top(header_height) } }
234
+ end
235
+
236
+ def description_line_height(config, layout)
237
+ Vips::Image.text("Ay", dpi: layout.description_dpi, font: config.description.font_family).height
238
+ end
239
+
240
+ # Plain text of the configured front matter field or of the excerpt, cut to the
241
+ # configured number of sentences
242
+ def description_for(item, config)
243
+ source = config.description.source
244
+ return if source.nil? || source.to_s.empty?
245
+
246
+ text = source == "excerpt" ? excerpt_text(item) : item.data[source]&.to_s
247
+ return if text.nil?
162
248
 
163
- metadata_width = metadata_width_for(config)
249
+ text = text.gsub(/\s+/, " ").strip
250
+ sentences = config.description.sentences
251
+ sentences ? first_sentences(text, sentences) : text
252
+ end
253
+
254
+ # Excerpts are Markdown with Liquid when generators run, so convert them to
255
+ # HTML and keep only the text.
256
+ def excerpt_text(item)
257
+ excerpt = item.data["excerpt"]
258
+ return if excerpt.nil?
259
+
260
+ markup = (excerpt.respond_to?(:content) ? excerpt.content : excerpt).to_s
261
+ markup = markup.gsub(/\{%.*?%\}|\{\{.*?\}\}/m, "")
262
+ converter = item.site.find_converter_instance(Jekyll::Converters::Markdown)
263
+ html = converter.convert(markup)
264
+ # Block tags separate words, inline tags such as <em> and <code> don't
265
+ html = html.gsub(%r{</?(?:p|br|div|li|ol|ul|h[1-6]|blockquote|pre|table|tr|td|th)\b[^>]*>}i, " ")
266
+ CGI.unescapeHTML(html.gsub(/<[^>]*>/, ""))
267
+ end
268
+
269
+ def first_sentences(text, count)
270
+ text.split(/(?<=[.!?])\s+(?=["“'‘(\[]?[[:upper:][:digit:]])/).first(count.to_i).join(" ")
271
+ end
272
+
273
+ def add_metadata(canvas, item, config, layout)
274
+ metadata_text = metadata_text_for(item, config, layout)
275
+ return canvas if metadata_text.empty?
164
276
 
165
277
  canvas.text(metadata_text,
166
278
  gravity: :sw,
167
- width: metadata_width,
279
+ width: metadata_width_for(config, layout),
168
280
  color: config.content.color,
169
- dpi: 150,
281
+ dpi: layout.content_dpi,
170
282
  font: config.content.font_family
171
- ) { |_canvas, _text| { x: 80, y: config.margin_bottom } }
283
+ ) { |_canvas, _text| { x: layout.margin, y: layout.bottom } }
172
284
  end
173
285
 
174
- def metadata_text_for(item, config)
286
+ def metadata_text_for(item, config, layout)
175
287
  metadata_parts = []
176
- metadata_width = metadata_width_for(config)
288
+ metadata_width = metadata_width_for(config, layout)
177
289
 
178
290
  config.metadata.fields.each do |field|
179
291
  metadata_value = metadata_value_for(item, field, config)
@@ -181,7 +293,7 @@ class JekyllOgImage::Generator < Jekyll::Generator
181
293
 
182
294
  if field == "description"
183
295
  candidate_text = (metadata_parts + [ metadata_value ]).join(config.metadata.separator)
184
- next unless metadata_text_fits_single_line?(candidate_text, metadata_width, config)
296
+ next unless metadata_text_fits_single_line?(candidate_text, metadata_width, config, layout)
185
297
  end
186
298
 
187
299
  metadata_parts << metadata_value
@@ -195,7 +307,7 @@ class JekyllOgImage::Generator < Jekyll::Generator
195
307
  when "date"
196
308
  item.respond_to?(:date) && item.date ? item.date.strftime(config.metadata.date_format) : nil
197
309
  when "tags"
198
- return nil unless item.data["tags"]&.is_a?(Array) && item.data["tags"].any?
310
+ return nil unless item.data["tags"].is_a?(Array) && item.data["tags"].any?
199
311
 
200
312
  item.data["tags"].map { |tag| "##{tag}" }.join(" ")
201
313
  else
@@ -204,34 +316,38 @@ class JekyllOgImage::Generator < Jekyll::Generator
204
316
  end
205
317
  end
206
318
 
207
- def metadata_width_for(config)
208
- config.domain ? 600 : 1040
319
+ # The metadata line shares the bottom row with the domain
320
+ def metadata_width_for(config, layout)
321
+ return layout.content_width unless config.domain
322
+
323
+ layout.content_width - render_content_text(config.domain, config, layout).width - layout.gap
209
324
  end
210
325
 
211
- def metadata_text_fits_single_line?(text, metadata_width, config)
212
- options = {
213
- width: metadata_width,
214
- dpi: 150,
215
- font: config.content.font_family,
216
- align: :low
217
- }
218
- options[:wrap] = :word if Vips.at_least_libvips?(8, 14)
326
+ def metadata_text_fits_single_line?(text, metadata_width, config, layout)
327
+ rendered_height = render_content_text(text, config, layout, width: metadata_width).height
219
328
 
220
- single_line_height = Vips::Image.text("Ay", **options).height
221
- rendered_height = Vips::Image.text(text, **options).height
329
+ rendered_height <= (content_line_height(config, layout) * 1.6)
330
+ end
222
331
 
223
- rendered_height <= (single_line_height * 1.6)
332
+ def content_line_height(config, layout)
333
+ render_content_text("Ay", config, layout).height
224
334
  end
225
335
 
336
+ def render_content_text(text, config, layout, width: nil)
337
+ options = { dpi: layout.content_dpi, font: config.content.font_family, align: :low }
338
+ options[:width] = width if width
339
+ options[:wrap] = :word if width && Vips.at_least_libvips?(8, 14)
340
+
341
+ Vips::Image.text(JekyllOgImage::Element::Text.escape_markup(text), **options)
342
+ end
226
343
 
227
- def add_domain(canvas, _item, config)
228
- y_pos = config.margin_bottom
229
344
 
345
+ def add_domain(canvas, config, layout)
230
346
  canvas.text(config.domain,
231
347
  gravity: :se,
232
348
  color: config.content.color,
233
- dpi: 150,
349
+ dpi: layout.content_dpi,
234
350
  font: config.content.font_family
235
- ) { |_canvas, _text| { x: 80, y: y_pos } }
351
+ ) { |_canvas, _text| { x: layout.margin, y: layout.bottom } }
236
352
  end
237
353
  end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Positions and sizes for an image. Everything, including the pixel sizes in the
4
+ # configuration (logo, border), is designed for a 1200px-wide canvas and scaled
5
+ # with the canvas width, so any canvas size keeps the same proportions.
6
+ class JekyllOgImage::Layout
7
+ REFERENCE_WIDTH = 1200.0
8
+
9
+ def initialize(config)
10
+ @config = config
11
+ @scale = config.canvas.width / REFERENCE_WIDTH
12
+ end
13
+
14
+ # Left, right and bottom margin
15
+ def margin
16
+ scaled(80)
17
+ end
18
+
19
+ def header_top
20
+ scaled(100)
21
+ end
22
+
23
+ def header_dpi
24
+ scaled(400)
25
+ end
26
+
27
+ def content_dpi
28
+ scaled(150)
29
+ end
30
+
31
+ def description_dpi
32
+ scaled(200)
33
+ end
34
+
35
+ # Space between the header and whatever is next to or below it
36
+ def gap
37
+ scaled(30)
38
+ end
39
+
40
+ def border_width
41
+ scaled(@config.border_bottom&.width || 0)
42
+ end
43
+
44
+ # Distance from the bottom edge to the metadata line, above the border
45
+ def bottom
46
+ margin + border_width
47
+ end
48
+
49
+ def logo_width
50
+ scaled(@config.image.width)
51
+ end
52
+
53
+ def logo_height
54
+ scaled(@config.image.height)
55
+ end
56
+
57
+ def logo_radius
58
+ scaled(@config.image.radius) if @config.image.radius
59
+ end
60
+
61
+ def logo_position
62
+ { x: scaled(@config.image.position[:x]), y: scaled(@config.image.position[:y]) }
63
+ end
64
+
65
+ def content_width
66
+ @config.canvas.width - 2 * margin
67
+ end
68
+
69
+ # Leaves room for the logo next to the header
70
+ def header_width
71
+ @config.image.path ? content_width - logo_width - gap : content_width
72
+ end
73
+
74
+ # How tall the header can be without reaching the metadata line
75
+ def header_max_height(metadata_line_height)
76
+ @config.canvas.height - header_top - bottom - metadata_line_height - gap
77
+ end
78
+
79
+ # Where the description starts, below a header of the given height
80
+ def description_top(header_height)
81
+ header_top + header_height + gap
82
+ end
83
+
84
+ # How tall the description can be between the header and the metadata line
85
+ def description_max_height(header_height, metadata_line_height)
86
+ @config.canvas.height - description_top(header_height) - bottom - metadata_line_height - gap
87
+ end
88
+
89
+ private
90
+
91
+ def scaled(value)
92
+ (value * @scale).round
93
+ end
94
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ # Remembers a digest of the inputs each image was generated from, so images are
6
+ # regenerated only when something that affects them changes.
7
+ #
8
+ # Stored as a dotfile in the output directory, which Jekyll never publishes.
9
+ class JekyllOgImage::Manifest
10
+ FILENAME = ".jekyll-og-image.json"
11
+
12
+ def initialize(dir)
13
+ @path = File.join(dir, FILENAME)
14
+ @previous = load
15
+ @current = {}
16
+ end
17
+
18
+ def fresh?(image_path, digest)
19
+ @current.fetch(image_path) { @previous[image_path] } == digest
20
+ end
21
+
22
+ def record(image_path, digest)
23
+ @current[image_path] = digest
24
+ end
25
+
26
+ # Writes the images recorded during this build. Entries for images that weren't
27
+ # recorded (deleted or disabled documents) are dropped.
28
+ def save
29
+ entries = @current.sort.to_h
30
+ return if entries == @previous
31
+
32
+ FileUtils.mkdir_p(File.dirname(@path))
33
+ File.write(@path, JSON.pretty_generate(entries))
34
+ end
35
+
36
+ private
37
+
38
+ def load
39
+ JSON.parse(File.read(@path))
40
+ rescue Errno::ENOENT, JSON::ParserError
41
+ {}
42
+ end
43
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllOgImage
4
- VERSION = "2.1.1"
4
+ VERSION = "3.1.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-og-image
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Alexandrov
@@ -9,20 +9,6 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: jekyll-seo-tag
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: '2.8'
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "~>"
24
- - !ruby/object:Gem::Version
25
- version: '2.8'
26
12
  - !ruby/object:Gem::Dependency
27
13
  name: zeitwerk
28
14
  requirement: !ruby/object:Gem::Requirement
@@ -43,14 +29,14 @@ dependencies:
43
29
  requirements:
44
30
  - - "~>"
45
31
  - !ruby/object:Gem::Version
46
- version: 2.2.0
32
+ version: '2.2'
47
33
  type: :runtime
48
34
  prerelease: false
49
35
  version_requirements: !ruby/object:Gem::Requirement
50
36
  requirements:
51
37
  - - "~>"
52
38
  - !ruby/object:Gem::Version
53
- version: 2.2.0
39
+ version: '2.2'
54
40
  - !ruby/object:Gem::Dependency
55
41
  name: jekyll
56
42
  requirement: !ruby/object:Gem::Requirement
@@ -71,20 +57,6 @@ dependencies:
71
57
  - - "<"
72
58
  - !ruby/object:Gem::Version
73
59
  version: '5.0'
74
- - !ruby/object:Gem::Dependency
75
- name: csv
76
- requirement: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: '0'
81
- type: :development
82
- prerelease: false
83
- version_requirements: !ruby/object:Gem::Requirement
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- version: '0'
88
60
  email:
89
61
  - igor.alexandrov@gmail.com
90
62
  executables: []
@@ -97,10 +69,13 @@ files:
97
69
  - LICENSE.txt
98
70
  - README.md
99
71
  - Rakefile
72
+ - assets/icon.png
73
+ - assets/icon.svg
100
74
  - examples/1.png
101
75
  - examples/2.png
102
76
  - examples/3.png
103
77
  - examples/4.png
78
+ - examples/5.png
104
79
  - lib/jekyll-og-image.rb
105
80
  - lib/jekyll_og_image.rb
106
81
  - lib/jekyll_og_image/configuration.rb
@@ -110,6 +85,8 @@ files:
110
85
  - lib/jekyll_og_image/element/image.rb
111
86
  - lib/jekyll_og_image/element/text.rb
112
87
  - lib/jekyll_og_image/generator.rb
88
+ - lib/jekyll_og_image/layout.rb
89
+ - lib/jekyll_og_image/manifest.rb
113
90
  - lib/jekyll_og_image/version.rb
114
91
  homepage: https://github.com/igor-alexandrov/jekyll-og-image
115
92
  licenses:
@@ -130,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
107
  - !ruby/object:Gem::Version
131
108
  version: '0'
132
109
  requirements: []
133
- rubygems_version: 4.0.3
110
+ rubygems_version: 4.0.20
134
111
  specification_version: 4
135
112
  summary: Jekyll plugin to generate GitHub-style open graph images
136
113
  test_files: []