Rendering raw html with Redcarpet and Markdown

I use Redcarpet as a Markdown rendering tool, and I would like to be able to display html or any text with <and> without parsing it.

Here is an illustration of what is about to happen:

User enters

I *want* to write <code> 

The source of this comment when sent by the server should be

 I <em>want</em> to write &lt;code&gt; 

The problem is that the rendering outputs exit html when parsing Markdown, I get:

 I &lt;em&gt;want&lt;/em&gt; to write &lt;code&gt; 

Therefore, I cannot distinguish between the html that people send to the server and the html created by the Redcarpet renderer. If I do .html_safe on this, my markdown will be interpreted, but also by the user-entered html, which should not.

Any idea on how to fix this? Note that the idea would be to display (but not to parse) the html entered by the user, even if the user did not use the inverse `elements, as expected, with a markdown.

Here is the corresponding bit of code:

 # this is our markdown helper used in our views def markdown(text, options=nil) options = [:no_intra_emphasis => true, ...] renderer = MarkdownRenderer.new(:filter_html => false, ...) markdown = Redcarpet::Markdown.new(renderer, *options) markdown.render(text).html_safe end 
+4
source share
1 answer

If you understand correctly, you just want <code> as plain text, and not as an HTML element.

To do this, you need to avoid < and > with a backslash:

 I *want* to write \<code\> 
+1
source

Source: https://habr.com/ru/post/1438006/


All Articles