jekyll-og-image 3.0.0 → 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: 5a95a1847180a7d4565e7e951467ff2293fb88e7182b08a58f362aee77f4a1d9
4
- data.tar.gz: f11ab1b7b52feec3c6052b46884da96dd1973fc863293d74ac36ac7c22142bd4
3
+ metadata.gz: f12db2733443d3e1704f0448ebb4b84c34a15e44d6feeb0ef7b2092802900ae9
4
+ data.tar.gz: 5e8ca788996efab23fdf0f4292ec153b3cf2eae182a2d71946560aa071edf3d7
5
5
  SHA512:
6
- metadata.gz: e49a4a7fb8884c6f3898dae7ec54b8185809d1211544d16ad8cbe8b70a60aa4be81aff27a60f182873b6e62048f2fb9bc402dd92c1f4b3d8f8b1b8ceda411ec3
7
- data.tar.gz: 5879cfbe269524ebf28e08dfd68f79d0e1782471b09f7dd74390133a7c62930a846957ac42e2351414f0a7c3d99738acc571905dc2449034771874fd5a6c2d6f
6
+ metadata.gz: fb99a4f054458c83ff576d41c96401ef6cd38b7abb787d2de3657833d835f4457bf870e5259e7c1446d27b82a90d1cc887b7bc3754a5d689ca3c582b4e2d6e30
7
+ data.tar.gz: fe24fa07b713d0b1001bf1e4d57c5eab8cc162f90544e507f84a5d86a6d3a9027b649a2bd2cb7f8dd33f0f50eb700af1426306372ae876f5cb5b697811ad88fb
data/README.md CHANGED
@@ -124,6 +124,12 @@ Paths to images (`image.path`, `canvas.background_image`) are relative to the si
124
124
  * `font_family` – The font family of the content text. Default: `Helvetica, Regular`
125
125
  * `color` – The color of the content text. Default: `#535358`
126
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
+
127
133
  * `border_bottom` – The border bottom configuration options:
128
134
  * `width` – The width of the border bottom. Default: `0`
129
135
  * `fill` – The array of colors to fill the border bottom. Default: `["#000000"]`
@@ -273,6 +279,21 @@ This configuration:
273
279
  - Uses a pipe separator between metadata fields
274
280
  - Formats dates as "15 February 2024"
275
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
+
276
297
 
277
298
  ## Contributing
278
299
 
data/examples/5.png ADDED
Binary file
@@ -34,6 +34,14 @@ class JekyllOgImage::Configuration
34
34
  end
35
35
  end
36
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")
41
+ super
42
+ end
43
+ end
44
+
37
45
  Metadata = Data.define(:fields, :separator, :date_format) do
38
46
  def initialize(fields: [ "date", "tags" ], separator: " • ", date_format: "%B %d, %Y")
39
47
  super
@@ -112,6 +120,10 @@ class JekyllOgImage::Configuration
112
120
  build_section(Border, "border_bottom") if @raw_config["border_bottom"]
113
121
  end
114
122
 
123
+ def description
124
+ build_section(Description, "description")
125
+ end
126
+
115
127
  def metadata
116
128
  build_section(Metadata, "metadata")
117
129
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "cgi"
3
4
  require "digest"
4
5
 
5
6
  class JekyllOgImage::Generator < Jekyll::Generator
@@ -109,8 +110,10 @@ class JekyllOgImage::Generator < Jekyll::Generator
109
110
  config.image.to_h,
110
111
  config.border_bottom&.to_h,
111
112
  config.metadata.to_h,
113
+ config.description.to_h,
112
114
  config.domain,
113
115
  item.data["title"],
116
+ description_for(item, config),
114
117
  config.metadata.fields.map { |field| metadata_value_for(item, field, config) },
115
118
  file_digest(site, config.image.path),
116
119
  file_digest(site, config.canvas.background_image)
@@ -149,7 +152,8 @@ class JekyllOgImage::Generator < Jekyll::Generator
149
152
  canvas = add_border_bottom(canvas, config, layout) if config.border_bottom
150
153
  logo = read_source_file(site, config.image.path, "og_image.image.path") if config.image.path
151
154
  canvas = add_image(canvas, logo, config, layout) if logo
152
- canvas = add_header(canvas, item, config, layout)
155
+ canvas, header_height = add_header(canvas, item, config, layout)
156
+ canvas = add_description(canvas, item, config, layout, header_height)
153
157
  canvas = add_metadata(canvas, item, config, layout)
154
158
  canvas = add_domain(canvas, config, layout) if config.domain
155
159
 
@@ -196,13 +200,74 @@ class JekyllOgImage::Generator < Jekyll::Generator
196
200
  title = item.data["title"] || "Untitled"
197
201
  full_title = "#{config.header.prefix}#{title}#{config.header.suffix}"
198
202
 
199
- canvas.text(full_title,
203
+ header_height = 0
204
+ canvas = canvas.text(full_title,
200
205
  width: layout.header_width,
201
206
  height: layout.header_max_height(content_line_height(config, layout)),
202
207
  color: config.header.color,
203
208
  dpi: layout.header_dpi,
204
209
  font: config.header.font_family
205
- ) { |_canvas, _text| { x: layout.margin, y: layout.header_top } }
210
+ ) do |_canvas, text|
211
+ header_height = text.height
212
+ { x: layout.margin, y: layout.header_top }
213
+ end
214
+
215
+ [ canvas, header_height ]
216
+ end
217
+
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?
248
+
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(" ")
206
271
  end
207
272
 
208
273
  def add_metadata(canvas, item, config, layout)
@@ -28,6 +28,10 @@ class JekyllOgImage::Layout
28
28
  scaled(150)
29
29
  end
30
30
 
31
+ def description_dpi
32
+ scaled(200)
33
+ end
34
+
31
35
  # Space between the header and whatever is next to or below it
32
36
  def gap
33
37
  scaled(30)
@@ -72,6 +76,16 @@ class JekyllOgImage::Layout
72
76
  @config.canvas.height - header_top - bottom - metadata_line_height - gap
73
77
  end
74
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
+
75
89
  private
76
90
 
77
91
  def scaled(value)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllOgImage
4
- VERSION = "3.0.0"
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: 3.0.0
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Alexandrov
@@ -75,6 +75,7 @@ files:
75
75
  - examples/2.png
76
76
  - examples/3.png
77
77
  - examples/4.png
78
+ - examples/5.png
78
79
  - lib/jekyll-og-image.rb
79
80
  - lib/jekyll_og_image.rb
80
81
  - lib/jekyll_og_image/configuration.rb