Skip to content

Commit 05ee114

Browse files
committed
Fix title extraction algorithm
1 parent f49dcef commit 05ee114

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

_plugins/social_images.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def generate(site)
2929
Jekyll.logger.info('Skipping social image generation')
3030
return
3131
end
32-
generate_images(Dir.glob(File.join(site.source, '_guides', '*.adoc')), site)
3332
generate_images(Dir.glob(File.join(site.source, '_posts', '*.adoc')), site)
33+
generate_images(Dir.glob(File.join(site.source, '_guides', '*.adoc')), site)
3434
end
3535

3636
def split_text_into_lines(text)
@@ -105,7 +105,12 @@ def generate_svg_string(title)
105105
idx = 90
106106
font_size = 30
107107
tspan_elements = ''
108-
split_text_into_lines(title.gsub('&', '&')).each_with_index do |line, index|
108+
# Sanitize title
109+
title = title.gsub(/&/, '&')
110+
title = title.gsub(/</, '&lt;')
111+
title = title.gsub(/>/, '&gt;')
112+
113+
split_text_into_lines(title).each_with_index do |line, index|
109114
tspan_elements += "<tspan x='50%' y='#{idx}'>#{line}</tspan>"
110115
idx += font_size + 10
111116
end
@@ -122,16 +127,27 @@ def generate_svg_string(title)
122127
end
123128

124129
def extract_title(adoc_file)
125-
title = ''
126-
File.open(adoc_file, 'r') do |file|
127-
file.each_line do |line|
128-
if line.start_with? '='
129-
title = line[2..].strip
130+
line_nr = 0
131+
File.readlines(adoc_file).each do |line|
132+
if line_nr == 0
133+
# If line does not start with --- break
134+
unless line.strip.start_with?('---')
130135
break
131136
end
132137
end
138+
if line_nr > 0 && line.strip.start_with?('---')
139+
break;
140+
end
141+
if line.strip.start_with?('title:')
142+
title = line.strip.sub('title:', '').strip
143+
# Remove quotes
144+
title = title.gsub(/\A[\"']|[\"']\z/, '')
145+
return title
146+
end
147+
line_nr += 1
133148
end
134-
title
149+
doc = Asciidoctor.load_file(adoc_file, header_only: true, logger: NullLogger.new)
150+
doc.doctitle
135151
end
136152
end
137153
end

0 commit comments

Comments
 (0)