0

当我将字符串传递给时,我似乎无法让列表函数工作kramdown

例如,这是我的文字:

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>"

我无法将其转换为基于 Markdown/kramdown 的正确 HTML(例如插入<ul>和正确的换行符。

4

1 回答 1

1

如果我们获取您的正文并将其格式化为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

请注意,您后续的每一行都以空格开头?正常段落不应缩进。同样要开始一个列表,您需要一个空白行,例如开始新段落。所以作为一个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

或作为单行: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"

然后你更接近你的预期输出:

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"
于 2015-05-03T19:40:45.987 回答