feat: add image resize support in articles (#14293)

This commit is contained in:
Sivin Varghese
2026-05-19 19:34:43 +05:30
committed by GitHub
parent 6560dbb68d
commit bca95efb82
8 changed files with 57 additions and 8 deletions
+23
View File
@@ -33,8 +33,31 @@ class CustomMarkdownRenderer < CommonMarker::HtmlRenderer
super
end
def image(node)
src = escape_href(node.url)
width = extract_image_width(src)
plain do
out(%(<img src="#{src}"))
out(' alt="', :children, '"')
out(%( title="#{escape_html(node.title)}")) if node.title.present?
out(%( style="width: #{width}; max-width: 100%; height: auto;")) if width
out(' />')
end
end
private
def extract_image_width(src)
query = URI.parse(src).query
raw = query && CGI.parse(query)['cw_image_width']&.first
return unless raw =~ /\A(\d+)px\z/
px = Regexp.last_match(1).to_i
"#{px}px" if px.between?(1, 2000)
rescue URI::InvalidURIError
nil
end
def surrounded_by_empty_lines?(node)
prev_node_empty?(node.previous) && next_node_empty?(node.next)
end