jekyll-templated-og-image 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 50b0f286a3f3c10a3d1a38217511af8ed2f6d9a4d32ae892c80fe9e47a127f91
4
+ data.tar.gz: 330e588e2b3de5e8598f18a6acc5fbbb274c5e5edc98e9b3b3916be9bb52f2d6
5
+ SHA512:
6
+ metadata.gz: 2608360a20dcd95ad91392301b9fd05e7c1e878f5374962ce7cd03e9a7f93c49e8746f4cdb4a7714ce930afbda8344a1ba44387d41b5bfc80899ee44f727cd34
7
+ data.tar.gz: 204dbf90388771915f70085c1795b5fd2363846c41d04667ea8e055f108c1553c02f5038508510b2d561a008e958a88276b0237f1c018280d4ddbf1868035e66
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 catskull
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # jekyll-og-image
2
+
3
+ Jekyll plugin that automatically generates Open Graph images for your posts and pages using headless Chrome.
4
+
5
+ ## Requirements
6
+
7
+ - Ruby >= 3.0
8
+ - Jekyll >= 4.0
9
+ - Chrome or Chromium installed on the machine
10
+
11
+ ## Installation
12
+
13
+ Add to your site's Gemfile. To run in development only:
14
+
15
+ ```ruby
16
+ group :development, :jekyll_plugins do
17
+ gem "jekyll-og-image"
18
+ end
19
+ ```
20
+
21
+ Then run `bundle install`. Do not add it to the `plugins:` list in `_config.yml` — Bundler loads it automatically via the `:jekyll_plugins` group.
22
+
23
+ ## Usage
24
+
25
+ This plugin is intended to run in development only. Generate images locally and commit them to your repository — they will be served in production without the plugin running.
26
+
27
+ Generated images are saved to `assets/og/{slug}.png` and copied back to your source directory so they persist across builds and can be committed. On subsequent builds, existing images are skipped.
28
+
29
+ Since the plugin does not run in production, do not rely on `page.og_image` being set. Instead, derive the image path from the page URL directly — it is always predictable:
30
+
31
+ ```html
32
+ {% assign og_slug = page.url | remove_first: "/" | replace: "/", "-" | remove: ".html" %}
33
+ {% if og_slug == "" %}{% assign og_slug = "index" %}{% endif %}
34
+ <meta property="og:image" content="{{ site.url }}/assets/og/{{ og_slug }}.png">
35
+ ```
36
+
37
+ ## Configuration
38
+
39
+ All configuration is optional. Add an `og_image` key to `_config.yml`:
40
+
41
+ ```yaml
42
+ og_image:
43
+ output_dir: assets/og # where to write generated images
44
+ layouts_dir: _og/layouts # where layout files live
45
+ force: false # set true to regenerate existing images
46
+ quiet: false # set true to suppress log output
47
+ templates:
48
+ - collection: posts
49
+ template: _og/post.html
50
+ - collection: page
51
+ template: _og/page.html
52
+ - collection: recipes
53
+ template: _og/recipe.html
54
+ - path: /recipes/my-special-recipe
55
+ template: _og/special.html
56
+ ```
57
+
58
+ ### Template rules
59
+
60
+ Only documents covered by at least one rule are processed. Each rule matches by either:
61
+
62
+ - `collection` — the collection name: `posts`, `page`, or any custom collection (e.g. `recipes`)
63
+ - `path` — a glob matched against the document's URL (e.g. `/blog/**`)
64
+
65
+ `path` rules always take priority over `collection` rules, regardless of their order in the config. This lets you override the template for a specific page even if it belongs to a collection that has its own rule.
66
+
67
+ If a matching rule has no `template` key, the gem's built-in default template is used.
68
+
69
+ Set `template: false` to skip OG image generation for matched documents:
70
+
71
+ ```yaml
72
+ og_image:
73
+ templates:
74
+ - collection: page
75
+ template: false
76
+ ```
77
+
78
+ ### Custom templates
79
+
80
+ Templates are Liquid files. The following variables are available:
81
+
82
+ Templates have access to the same variables as any Jekyll layout:
83
+
84
+ | Variable | Description |
85
+ |---|---|
86
+ | `page` | All front matter and computed fields for the document (e.g. `page.title`, `page.url`, `page.date`, `page.tags`) |
87
+ | `site` | Site-wide data from `_config.yml` and Jekyll (e.g. `site.title`, `site.posts`, `site.data`) |
88
+
89
+ Templates support layouts via front matter, similar to Jekyll:
90
+
91
+ ```
92
+ _og/
93
+ layouts/
94
+ base.html ← shared structure, renders {{ content }}
95
+ post.html ← post-specific, declares layout: base
96
+ page.html ← page-specific, declares layout: base
97
+ ```
98
+
99
+ **`_og/post.html`:**
100
+ ```html
101
+ ---
102
+ layout: base
103
+ ---
104
+ <div class="post-card">{{ title }}</div>
105
+ ```
106
+
107
+ **`_og/layouts/base.html`:**
108
+ ```html
109
+ <html>
110
+ <body style="width:1200px;height:630px">
111
+ {{ content }}
112
+ </body>
113
+ </html>
114
+ ```
115
+
116
+ Images are captured at 1200×630px, the standard OG image size.
@@ -0,0 +1,21 @@
1
+ module JekyllOgImage
2
+ class Config
3
+ DEFAULTS = {
4
+ "output_dir" => "assets/og",
5
+ "layouts_dir" => "_og/layouts",
6
+ "force" => false,
7
+ "quiet" => false,
8
+ "templates" => []
9
+ }.freeze
10
+
11
+ def initialize(jekyll_config)
12
+ @config = DEFAULTS.merge(jekyll_config.fetch("og_image", {}))
13
+ end
14
+
15
+ def output_dir = @config["output_dir"]
16
+ def layouts_dir = @config["layouts_dir"]
17
+ def force? = @config["force"]
18
+ def quiet? = @config["quiet"]
19
+ def template_rules = @config["templates"]
20
+ end
21
+ end
@@ -0,0 +1,83 @@
1
+ require "ferrum"
2
+ require "fileutils"
3
+ require_relative "config"
4
+ require_relative "template_resolver"
5
+
6
+ module JekyllOgImage
7
+ class Generator < Jekyll::Generator
8
+ safe true
9
+ priority :low
10
+
11
+ def generate(site)
12
+ @site = site
13
+ @config = Config.new(site.config)
14
+ @resolver = TemplateResolver.new(@config.template_rules, site.source, @config.layouts_dir, site)
15
+ @output_dir = File.join(site.dest, @config.output_dir)
16
+ @source_dir = File.join(site.source, @config.output_dir)
17
+
18
+ browser = Ferrum::Browser.new(headless: true, browser_options: { "font-render-hinting" => "none" })
19
+ browser.resize(width: 1200, height: 630)
20
+
21
+ all_docs = site.posts.docs + site.pages + site.collections.values.flat_map(&:docs)
22
+
23
+ documents = []
24
+ @config.template_rules.each do |rule|
25
+ if (collection = rule["collection"])
26
+ case collection
27
+ when "posts" then documents += site.posts.docs
28
+ when "page" then documents += site.pages
29
+ else documents += site.collections[collection]&.docs || []
30
+ end
31
+ elsif (glob = rule["path"])
32
+ documents += all_docs.select { |doc| File.fnmatch(glob, doc.url, File::FNM_PATHNAME) }
33
+ end
34
+ end
35
+ documents.uniq.reject { |doc| doc.url.end_with?(".xml", ".csv") }.each do |doc|
36
+ generate_image(browser, doc)
37
+ end
38
+ ensure
39
+ browser&.quit
40
+ end
41
+
42
+ private
43
+
44
+ def generate_image(browser, doc)
45
+ slug = slug_for(doc)
46
+ output_path = File.join(@output_dir, "#{slug}.png")
47
+ source_path = File.join(@source_dir, "#{slug}.png")
48
+
49
+ if File.exist?(source_path) && !@config.force?
50
+ doc.data["og_image"] = "/#{@config.output_dir}/#{slug}.png"
51
+ return
52
+ end
53
+
54
+ log "OG Image:", "Generating #{slug}.png"
55
+
56
+ html = @resolver.render(doc, @site.site_payload.merge("page" => doc.to_liquid))
57
+ return log("OG Image:", "Skipping #{slug}") if html.nil?
58
+
59
+ FileUtils.mkdir_p(@output_dir)
60
+ browser.go_to("data:text/html;charset=utf-8,#{ERB::Util.url_encode(html)}")
61
+ browser.network.wait_for_idle
62
+ browser.screenshot(path: output_path)
63
+
64
+ FileUtils.mkdir_p(@source_dir)
65
+ FileUtils.cp(output_path, source_path)
66
+
67
+ @site.static_files << Jekyll::StaticFile.new(
68
+ @site, @site.source, @config.output_dir, "#{slug}.png"
69
+ )
70
+
71
+ doc.data["og_image"] = "/#{@config.output_dir}/#{slug}.png"
72
+ log "OG Image:", "Generated #{slug}.png"
73
+ end
74
+
75
+ def log(topic, message)
76
+ Jekyll.logger.info(topic, message) unless @config.quiet?
77
+ end
78
+
79
+ def slug_for(doc)
80
+ doc.url.gsub("/", "-").gsub(/^-|-$/, "").then { |s| s.empty? ? "index" : s }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,93 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=1200, height=630">
6
+ <style>
7
+ *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
8
+
9
+ body {
10
+ width: 1200px;
11
+ height: 630px;
12
+ display: flex;
13
+ flex-direction: column;
14
+ justify-content: flex-end;
15
+ padding: 64px;
16
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
17
+ background: #0f172a;
18
+ color: #f8fafc;
19
+ overflow: hidden;
20
+ }
21
+
22
+ .accent {
23
+ display: block;
24
+ width: 64px;
25
+ height: 6px;
26
+ background: #6366f1;
27
+ border-radius: 3px;
28
+ margin-bottom: 32px;
29
+ }
30
+
31
+ .title {
32
+ font-size: 64px;
33
+ font-weight: 700;
34
+ line-height: 1.1;
35
+ letter-spacing: -0.02em;
36
+ color: #f8fafc;
37
+ max-width: 900px;
38
+ margin-bottom: 40px;
39
+ }
40
+
41
+ .meta {
42
+ display: flex;
43
+ align-items: center;
44
+ gap: 24px;
45
+ }
46
+
47
+ .date {
48
+ font-size: 22px;
49
+ color: #94a3b8;
50
+ font-weight: 500;
51
+ }
52
+
53
+ .tags {
54
+ display: flex;
55
+ gap: 12px;
56
+ flex-wrap: wrap;
57
+ }
58
+
59
+ .tag {
60
+ font-size: 18px;
61
+ font-weight: 600;
62
+ color: #818cf8;
63
+ background: rgba(99, 102, 241, 0.15);
64
+ padding: 4px 14px;
65
+ border-radius: 999px;
66
+ }
67
+
68
+ .site {
69
+ position: absolute;
70
+ top: 64px;
71
+ right: 64px;
72
+ font-size: 20px;
73
+ font-weight: 600;
74
+ color: #475569;
75
+ letter-spacing: 0.04em;
76
+ text-transform: uppercase;
77
+ }
78
+ </style>
79
+ </head>
80
+ <body>
81
+ {% if site.title != "" %}<span class="site">{{ site.title }}</span>{% endif %}
82
+ <span class="accent"></span>
83
+ <h1 class="title">{{ page.title | default: page.url }}</h1>
84
+ <div class="meta">
85
+ {% if page.date %}<span class="date">{{ page.date | date: "%B %-d, %Y" }}</span>{% endif %}
86
+ {% if page.tags.size > 0 %}
87
+ <div class="tags">
88
+ {% for tag in page.tags %}<span class="tag">{{ tag }}</span>{% endfor %}
89
+ </div>
90
+ {% endif %}
91
+ </div>
92
+ </body>
93
+ </html>
@@ -0,0 +1,77 @@
1
+ require "liquid"
2
+ require "yaml"
3
+
4
+ module JekyllOgImage
5
+ class TemplateResolver
6
+ DEFAULT_TEMPLATE_PATH = File.expand_path("template/og-image.html", __dir__)
7
+
8
+ def initialize(rules, site_source, layouts_dir, site)
9
+ @site_source = site_source
10
+ @layouts_dir = File.expand_path(layouts_dir, site_source)
11
+ @site = site
12
+ @rules = rules.sort_by { |r| r["path"] ? 0 : 1 }
13
+ @cache = {}
14
+ end
15
+
16
+ def render(doc, variables)
17
+ path = path_for(doc)
18
+ return nil if path.nil?
19
+ render_file(path, variables)
20
+ end
21
+
22
+ private
23
+
24
+ def render_file(path, variables)
25
+ front_matter, template = @cache[path] ||= parse_template(path)
26
+ content = template.render!(variables, registers: { site: @site })
27
+
28
+ if (layout_name = front_matter["layout"])
29
+ layout_path = File.join(@layouts_dir, "#{layout_name}.html")
30
+ render_file(layout_path, variables.merge(front_matter).merge("content" => content))
31
+ else
32
+ content
33
+ end
34
+ end
35
+
36
+ def parse_template(path)
37
+ front_matter, body = parse_front_matter(File.read(path))
38
+ [front_matter, Liquid::Template.parse(body)]
39
+ end
40
+
41
+ def path_for(doc)
42
+ @rules.each do |rule|
43
+ next unless matches?(rule, doc)
44
+ return nil if rule["template"] == false
45
+ return File.expand_path(rule["template"], @site_source) if rule["template"]
46
+ return DEFAULT_TEMPLATE_PATH
47
+ end
48
+ DEFAULT_TEMPLATE_PATH
49
+ end
50
+
51
+ def matches?(rule, doc)
52
+ if (collection = rule["collection"])
53
+ doc_type(doc) == collection.to_s
54
+ elsif (glob = rule["path"])
55
+ File.fnmatch(glob, doc.url, File::FNM_PATHNAME)
56
+ else
57
+ true
58
+ end
59
+ end
60
+
61
+ def doc_type(doc)
62
+ if doc.respond_to?(:collection)
63
+ doc.collection.label
64
+ else
65
+ "page"
66
+ end
67
+ end
68
+
69
+ def parse_front_matter(raw)
70
+ if raw =~ /\A---\s*\n(.*?\n?)---\s*\n(.*)/m
71
+ [YAML.safe_load($1) || {}, $2]
72
+ else
73
+ [{}, raw]
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,4 @@
1
+ require "jekyll"
2
+ require "jekyll-templated-og-image/config"
3
+ require "jekyll-templated-og-image/template_resolver"
4
+ require "jekyll-templated-og-image/generator"
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-templated-og-image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - catskull
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: jekyll
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '4.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '4.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: ferrum
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.15'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.15'
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - LICENSE
45
+ - README.md
46
+ - lib/jekyll-templated-og-image.rb
47
+ - lib/jekyll-templated-og-image/config.rb
48
+ - lib/jekyll-templated-og-image/generator.rb
49
+ - lib/jekyll-templated-og-image/template/og-image.html
50
+ - lib/jekyll-templated-og-image/template_resolver.rb
51
+ homepage: https://github.com/catskull/jekyll-og-image
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.6.9
70
+ specification_version: 4
71
+ summary: Generate Open Graph images for Jekyll pages and posts
72
+ test_files: []