我正在使用 Prawn-Markup 将 ActionText HTML 转换为 PDF。我需要缩小大图像(而不是调整大小或裁剪),这需要在样式属性中设置宽度,然后 Prawn-Markup 将其转发到 Prawn.
不幸的是,在渲染模板时,Rails 的清理器会删除样式属性。
# app/views/active_storage/blobs/_blob.html.erb
image_tag blob, style: "width: #{blob.metadata[:whidth]}px"
# => "<img style="width: 200px" src="..." />
# app/views/action_text/content/_layout.html.erb
# => "<img src="..." />
呈现 blob 时需要保留样式属性,但我不想禁用整个应用程序的样式清理。
知道如何实现这一目标吗?
编辑: 我能够通过将 actiontext 添加到初始值设定项中的ActionText::Attachment::ATTRIBUTES
来允许style
属性,该属性适用于 blob,但使 actiontext 容易被滥用。仍在搜索...
尝试:
ActionText::Content::ALLOWED_ATTRIBUTES.add 'style'
或:
# config/initializers/action_text.rb
# frozen_string_literal: true
# You can also add a check to make sure the style attribute is only allowed for images:
module ActionText
class Content
ATTRIBUTES = %w[
# ...
].freeze
def sanitize
# ...
ATTRIBUTES << 'style' if node.name == 'img'
# ...
end
end
end