Ruby on rails 如何让红毯选择性地过滤HTML标签?
我想将一些HTML标记列为白名单,例如Ruby on rails 如何让红毯选择性地过滤HTML标签?,ruby-on-rails,ruby,redcarpet,Ruby On Rails,Ruby,Redcarpet,我想将一些HTML标记列为白名单,例如,这样我就可以为所有键盘快捷键显示漂亮的键盘图标。我该怎么做 下面的代码片段是我当前用于将标记字符串转换为HTML的函数 def markdown_to_html(markdown_str) options = { filter_html: true, link_attributes: { rel: 'nofollow', target: '_blank' }, no_styles: true }
,这样我就可以为所有键盘快捷键显示漂亮的键盘图标。我该怎么做
下面的代码片段是我当前用于将标记字符串转换为HTML的函数
def markdown_to_html(markdown_str)
options = {
filter_html: true,
link_attributes: { rel: 'nofollow', target: '_blank' },
no_styles: true
}
extensions = {
autolink: true,
fenced_code_blocks: true,
footnotes: true,
highlight: true,
no_intra_emphasis: true,
quote: true,
space_after_headers: true,
strikethrough: true,
superscript: true,
tables: true
}
renderer = Redcarpet::Render::HTML.new(options)
markdown = Redcarpet::Markdown.new(renderer, extensions)
markdown.render(markdown_str).html_safe
end
使用
sanitize
和您自己的自定义洗涤器类
该类可以与控制器类放在同一个文件中
class MarkdownScrubber < Rails::Html::PermitScrubber
def initialize
super
self.tags = %w( kbd )
self.attributes = []
end
def skip_node?(node)
node.text?
end
end
我不是
sanitize
可以在控制器内使用。我发现清理未定义错误。在谷歌搜索了一段时间后,我发现,sanitize
是一个ActionView助手@LiXinyang,的确如此。那么,把电话放在你的视野里。
renderer = Redcarpet::Render::HTML.new(options)
markdown = Redcarpet::Markdown.new(renderer, extensions)
sanitize(markdown.render(markdown_str), scrubber: MarkdownScrubber.new)