redcarpet icon indicating copy to clipboard operation
redcarpet copied to clipboard

bullet list requires newline

Open mawise opened this issue 6 years ago • 2 comments

This is an example that doesn't seem to render properly. If bullets don't have an empty line before them, they are parsed as stars:

irb(main):020:0> markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
=> #<Redcarpet::Markdown:0x007fe666240348 @renderer=#<Redcarpet::Render::HTML:0x007fe666240370>>
irb(main):021:0> puts markdown.render(str)
<p>text
* bullet1
* bullet1</p>
=> nil
irb(main):022:0> puts str
text
* bullet1
* bullet1

If I add an extra newline, it works fine:

irb(main):025:0> puts markdown.render(str2)
<p>text</p>

<ul>
<li>bullet1</li>
<li>bullet1</li>
</ul>
=> nil
irb(main):026:0> puts str2
text

* bullet1
* bullet1
=> nil

mawise avatar Aug 19 '19 16:08 mawise

You need to pass lax_spacing: true to support not requiring newlines in places like that.

johnfairh avatar Aug 20 '19 09:08 johnfairh

I have lax_spacing true and I still get bad behaviors with hard_wrap. I ultimately had to create a new renderer that overrode preprocess to do this:

class RedcarpetCustom < Redcarpet::Render::HTML
  def preprocess(doc)
    doc.gsub("\n", "\n\n")
  end
end

and it fixed a ton of odd behaviors.

I'd even tried overriding the linebreak but it did nothing. In the HTML you could see that it was putting the next word on a new line (so the new line was making it through the process) but there weren't any HTML elements to actually cause that to happen when rendered by a browser so it seems it isn't triggering linebreak() on "\n"

  def linebreak()
    "<br/>"
  end

I ultimately removed the above method once I'd written the preprocess routine.

My ApplicationHelper code:

  def markdown(text)
    markdown = Redcarpet::Markdown.new(RedcarpetCustom,
                                       no_intra_emphasis: true,
                                       lax_spacing: true,
                                       underline: true,
                                       hard_wrap: true)
    markdown.render(text).html_safe
  end

nuclearspike avatar Sep 10 '19 05:09 nuclearspike