1

I am using redcarpet to render my markdown text. here is the function I am using:

def markdown(text)
    render_options = { hard_wrap: true, filter_html: true }
    markdown_options = { autolink: true, no_intra_emphasis: true }
    markdown = Redcarpet::Markdown.new(
      Redcarpet::Render::HTML.new(render_options), markdown_options
    )
    markdown.render(text).html_safe
end

I am unable to render list items with it. for ex:

I am just a sentense
* list item 1
* list item 2

gets translated to:

<p>
  <p>I am just a sentense<br>
     * list item 1<br>
     * list item 2</p>
</p>

However, if I don't use a sentence to start my text, ex:

# head
* list me
* and me

everything seems to be fine:

<p>
  <h1>head</h1>
   <ul>
     <li>list me</li>
     <li>and me</li>
    </ul>
 </p>

I know redcarpet uses daring fireball but there doesn't seem to be any documentation about problem I am having.

sonalkr132
  • 967
  • 1
  • 9
  • 25
  • 1
    AFAIK that's just markdown - you need the empty line between the leading text and the list – Frederick Cheung Mar 30 '15 at 14:00
  • thanks frederick! That did solve my problem. But github markdown doesn't work like that ie I don't need to add an extra line for it to know that I am going to make a list. Any idea how I can achieve the same? – sonalkr132 Mar 30 '15 at 14:05

1 Answers1

0

It's by design.

A paragraph is simply one or more consecutive lines of text, separated by one or more blank lines.

That said, this is considered a single paragraph and is rendered accordingly. End the paragraph with a blank line and you will get the desired output. Like so:

I am just a sentense

* list item 1
* list item 2

UPD: here is the implementation that GitHub uses. The curious thing is, they're using Redcarpet too. Configuration must be different from yours.

D-side
  • 9,150
  • 3
  • 28
  • 44
  • Thanks D-side! I guess I should have read it more carefully. I want to achieve the markdown effect of github (where one doesn't need to add an extra blank line for list to begin). Do you know how? – sonalkr132 Mar 30 '15 at 14:07
  • @sonalkr132 I vaguely remember seeing a Markdown implementation that does not respect that rule. Particularly, Atom previewer of Markdown renders such a list correctly. It uses `marked`, a speedy JS implementation. – D-side Mar 30 '15 at 14:12
  • [github](https://guides.github.com/features/mastering-markdown/) doesn't respect that rule. You will have to try the github comment box to believe me as the link doesn't have the documentation about my case. but hey.. thanks anyway :) – sonalkr132 Mar 30 '15 at 14:18
  • @sonalkr132 added a link to what's used at GitHub. I would poke the source code to see if there's any possibility to configure Redcarpet to work the same. – D-side Mar 30 '15 at 14:27
  • Weird. I see [nothing fancy](https://github.com/github/markup/blob/master/lib/github/markup/markdown.rb#L11) there. – D-side Mar 30 '15 at 14:28
  • I think it has something to do with the way they take input, `"I am\n* a list"` gets formatted to `"

    I am

    \n
    • a list
    "`. So in the end they are following the rule but not really. lol. I hope you understand.
    – sonalkr132 Mar 30 '15 at 16:21
  • @sonalkr132 this may have something to do with hard wrapping. Try disabling. – D-side Mar 30 '15 at 17:17