dankim
(Dan Kim)
February 14, 2022, 4:18pm
#1
I tried extending Trix — https://codepen.io/javan/pen/ZmbWGP .
h2 tags are rendered in Trix but are not saved to the database and all changes are lost on refresh.
See also:
tarellel
(Brandon Hicks)
February 14, 2022, 11:46pm
#2
I haven’t really dug into it yet (I’m still at work at the moment)
But I’d assume you need to override or append to ActionTexts sanitize allowed_tags
list.
mattr_accessor(:sanitizer) { Rails::Html::Sanitizer.safe_list_sanitizer.new }
mattr_accessor(:allowed_tags) { sanitizer.class.allowed_tags + [ ActionText::Attachment.tag_name, "figure", "figcaption" ] }
mattr_accessor(:allowed_attributes) { sanitizer.class.allowed_attributes + ActionText::Attachment::ATTRIBUTES }
mattr_accessor(:scrubber)
def render_action_text_content(content)
self.prefix_partial_path_with_controller_namespace = false
sanitize_action_text_content(render_action_text_attachments(content))
end
def sanitize_action_text_content(content)
sanitizer.sanitize(content.to_html, tags: allowed_tags, attributes: allowed_attributes, scrubber: scrubber).html_safe
end
def render_action_text_attachments(content)
content.render_attachments do |attachment|
unless attachment.in?(content.gallery_attachments)
attachment.node.tap do |node|
node.inner_html = render_action_text_attachment attachment, locals: { in_gallery: false }
end
end
dankim
(Dan Kim)
February 15, 2022, 12:42pm
#3
Hi Brandon! Thank you for the reply!
Rails::Html::Sanitizer.safe_list_sanitizer.new.class.allowed_tags
already includes h2–h6, so appending h2 to this list doesn’t yield any result. It looks like Action Text supports an even narrower subset of tags.
sdubois
(Sébastien Dubois)
February 22, 2022, 7:37am
#4
I don’t know about saving in the database but you might find some inspiration to customize Trix here:
It would be great though, if this could be more easily supported out of the box.
hfpp2012
(qiuzhi99)
February 23, 2022, 12:28pm
#5
it is so awesome, I am using it in production environment.