17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/document_to_rich_html/html_formatter.rb', line 17
def self.format(content)
doc = Nokogiri::HTML.fragment(content)
doc.css('img').each do |img|
next if img['src'].nil? || !img['src'].start_with?('data:')
unless img['data-trix-attachment']
content_type = img['src'][/^data:(.*?);/, 1] || 'application/octet-stream'
extension = content_type.split('/')[1] || 'bin'
img['data-trix-attachment'] = {
contentType: content_type,
filename: "image.#{extension}",
filesize: img['src'].length,
height: 'auto',
width: 'auto',
url: img['src']
}.to_json
end
img['data-trix-attributes'] ||= '{"presentation":"gallery"}'
end
sanitized_html = Sanitize.fragment(doc.to_html, SANITIZER_CONFIG)
doc = Nokogiri::HTML.fragment(sanitized_html)
doc.css('p, h1, h2, h3, h4, h5, h6, ul, ol, blockquote, pre, table, tr, td, th, img')
.each { |node| node['data-trix-content-type'] = node.name }
doc.to_html
end
|