Skip to content

Commit f49dcef

Browse files
committed
Also generate social image for blog posts
1 parent 6358dda commit f49dcef

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

_plugins/social_images.rb

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,53 @@ def generate(site)
2929
Jekyll.logger.info('Skipping social image generation')
3030
return
3131
end
32-
guides = Dir.glob(File.join(site.source, '_guides', '*.adoc'))
32+
generate_images(Dir.glob(File.join(site.source, '_guides', '*.adoc')), site)
33+
generate_images(Dir.glob(File.join(site.source, '_posts', '*.adoc')), site)
34+
end
35+
36+
def split_text_into_lines(text)
37+
lines = []
38+
words = text.split(' ')
39+
current_line = ''
40+
41+
words.each do |word|
42+
if current_line.length + word.length <= 32
43+
current_line += (current_line == '' ? '' : ' ') + word
44+
else
45+
lines.push(current_line)
46+
current_line = word
47+
end
48+
end
49+
50+
lines.push(current_line) unless current_line.empty?
51+
52+
lines
53+
end
54+
55+
private
56+
57+
def generate_images(files, site)
3358
output_dir = 'assets/images/social'
3459
FileUtils.mkdir_p(File.join(site.source, output_dir))
3560

36-
guides.each do |guide_file|
61+
files.each do |guide_file|
3762
basename = File.basename(guide_file, '.adoc')
3863
if basename.start_with?('_')
3964
next
4065
end
4166
title = extract_title(guide_file)
67+
# Skip if title is empty
68+
if (title.nil? || title.empty?)
69+
next
70+
end
4271
output_file = File.join(site.source, output_dir, "#{basename}.png")
4372
# Skip if the file already exists
4473
if File.exist?(output_file)
4574
next
4675
end
4776

77+
Jekyll.logger.info("Generating social image for '#{title}' in #{output_file}")
78+
4879
# Generate the SVG image
4980
svg_image_str = generate_svg_string(title)
5081

@@ -65,38 +96,16 @@ def generate(site)
6596
# Change the last parameters to change the position of the generated image
6697
png_image.compose!(ChunkyPNG::Image.from_blob(b.string), 0, 80)
6798

68-
Jekyll.logger.info("Generating social image to #{output_file}")
6999
# Save the composed image to the output file
70100
png_image.save(output_file)
71101
end
72102
end
73103

74-
def split_text_into_lines(text)
75-
lines = []
76-
words = text.split(' ')
77-
current_line = ''
78-
79-
words.each do |word|
80-
if current_line.length + word.length <= 32
81-
current_line += (current_line == '' ? '' : ' ') + word
82-
else
83-
lines.push(current_line)
84-
current_line = word
85-
end
86-
end
87-
88-
lines.push(current_line) unless current_line.empty?
89-
90-
lines
91-
end
92-
93-
private
94-
95104
def generate_svg_string(title)
96105
idx = 90
97106
font_size = 30
98107
tspan_elements = ''
99-
split_text_into_lines(title).each_with_index do |line, index|
108+
split_text_into_lines(title.gsub('&', '&amp;')).each_with_index do |line, index|
100109
tspan_elements += "<tspan x='50%' y='#{idx}'>#{line}</tspan>"
101110
idx += font_size + 10
102111
end

0 commit comments

Comments
 (0)