0

I can't seem to get the list function to work when I pass in a string to kramdown:

For example, this is my text:

email_body = "Hi, Tim! Hopefully you get this. I just got your email   address. \n\n My email is x@xcom. \n\n For example, you could tell me: * 3 hours from now\n * 2 days from now at 5pm\n * Wednesday afternoon\n"

email_body = Kramdown::Document.new(email_body).to_html

email_body = "<p>Hi, Tim! Hopefully you get this. I just got your email   address. \n\n My email is x@xcom. \n\n For example, you could tell me: * 3 hours from now\n * 2 days from now at 5pm\n * Wednesday afternoon\n</p>"

I can't get it to turn into proper HTML based on Markdown/kramdown (e.g. insert <ul> and proper line breaks.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
Satchel
  • 16,414
  • 23
  • 106
  • 192

1 Answers1

1

It might be easier if we took your body text and formatted it as a heredoc:

email_body = <<BODY
Hi, Tim! Hopefully you get this. I just got your email   address.     

 My email is x@xcom.     

 For example, you could tell me: * 3 hours from now    
 * 2 days from now at 5pm    
 * Wednesday afternoon    
BODY

Notice that each of your subsequent lines start with a space? Normal paragraphs should not be indented. Also to start a list you need a blank line like starting new paragraphs. So as a heredoc what you actually want looks like:

email_body = <<BODY
Hi, Tim! Hopefully you get this. I just got your email   address.

My email is x@xcom.

For example, you could tell me:

* 3 hours from now
* 2 days from now at 5pm
* Wednesday afternoon
BODY

Or as a single line: email_body_single_line = "Hi, Tim! Hopefully you get this. I just got your email address.\n\nMy email is x@xcom.\n\nFor example, you could tell me:\n\n* 3 hours from now\n* 2 days from now at 5pm\n* Wednesday afternoon\n"

Then you get closer to your expected output:

output = Kramdown::Document.new(email_body).to_html
=> "<p>Hi, Tim! Hopefully you get this. I just got your email   address.</p>\n\n<p>My email is x@xcom.</p>\n\n<p>For example, you could tell me:</p>\n\n<ul>\n  <li>3 hours from now</li>\n  <li>2 days from now at 5pm</li>\n  <li>Wednesday afternoon</li>\n</ul>\n"
Charlie
  • 7,181
  • 1
  • 35
  • 49
  • thanks -- would I be able to format something with extra spaces and such programmatically the right way? it comes from an XML file but since it's text, it has extra spaces and and stuff to help for readability. – Satchel May 04 '15 at 21:00
  • Assuming your input is being generated, you'd have to adjust the template generating your input. Or perform some other manipulation on the input being put into Kramdown. These things are possible, but spaces have meaning in markdown, so there's no blanket one size fits all approach. – Charlie May 04 '15 at 21:05
  • I see -- would the rule be there should be no spaces before any line? Should it explicitly have \n for end of lines? Thanks @charlie – Satchel May 05 '15 at 16:33