2016-10-27 9 views
0

私はいくつかのHTMLタグをホワイトリストに載せたいと思います。たとえば、<kbd></kbd>のように、すべてのキーボードショートカットにかなりのキーボードアイコンを表示できます。これどうやってするの?RedcarpetのHTMLタグをオプションでフィルタリングする方法は?

以下のコードスニペットは、Markdown文字列を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 

答えて

0

sanitizeと独自のカスタムスクラバークラスを使用してください。

このクラスは、コントローラクラスと同じファイルに格納できます。

renderer = Redcarpet::Render::HTML.new(options) 
markdown = Redcarpet::Markdown.new(renderer, extensions) 
sanitize(markdown.render(markdown_str), scrubber: MarkdownScrubber.new) 
+0

私は 'sanitize'は、コントローラの内部で使用することができないのです。

class MarkdownScrubber < Rails::Html::PermitScrubber def initialize super self.tags = %w(kbd) self.attributes = [] end def skip_node?(node) node.text? end end 

は、その後、あなたがrenderを呼び出すときに使用します。私は 'sanitize'エラーが定義されていません。 Googleの後にちょっと調べたところ、 'sanitize'はActionViewヘルパーhttp://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html –

+0

@LiXinyangだからだ。あなたのビューに電話を入れてください。 – ArtOfCode

関連する問題