Search code examples
rubymarkdownmaruku

Rail 3 escaping maruku


I am using Maruku with my RoR3 app. But the problem is that when i use the h(text) method to escape the text from the database before i use Maruku it escapes > to > so Maruku wont see this as a blockquote.

But i still want to escape the rest of the text so my question is how can i make this work?

I don't want to disable the escaping but i don't want it to escape >


Solution

  • The following method takes html_encoded multiline strings and replaces all maruku blockquote elements that have been converted to html entity codes back to >

    For the purpose of this implementation a maruku blockquote line is defined as a line beginning with one or more > sequences separated with optional whitespace.

    def maruku_escape(text)
      text.gsub(/^([\s]*\>)+/) {|match| match.gsub(/\>/, '>')}
    end
    

    The following test string was used

    test_text = "<b>A bold tag</b>
    <span>Some text in a span</span>
    
    Some Markdown
    > Blockquote 1
      > > nested blockquote 1
      > > nested blockquote 2
      >> nested blockquote 3 with no spaces
    
    
    Some plain text with an invalid blockquote > Some blockquote text
    <i>The end in italics<i>"
    

    And using this as follows maruku_text = maruku_escape(ERB::Util.html_escape(test_text))

    Gave the following results

    result =  "&lt;b&gt;A bold tag&lt;/b&gt;
    &lt;span&gt;Some text in a span&lt;/span&gt;
    
    Some Markdown
    > Blockquote 1
      > > nested blockquote 1
      > > nested blockquote 2
      >> nested blockquote 3 with no spaces
    
    
    Some plain text with an invalid blockquote &gt; Some blockquote text
    &lt;i&gt;The end in italics&lt;i&gt;
    "