【问题标题】:Using BlueCloth how can I turn off markdown for part of the text?使用 BlueCloth 如何关闭部分文本的降价?
【发布时间】:2013-04-02 23:47:06
【问题描述】:

我们在 Rails 网站上使用 Bluecloth 进行文本格式设置,但我遇到了一些电子邮件地址的问题。当地址中有双下划线时,BlueCloth 会将其转换为 EM 标签。

在 Rails 控制台中:

BlueCloth::new("bob_smith@foo.com").to_html
"<p>bob_smith@foo.com</p>"

带有一个下划线的电子邮件地址可以正常工作,但是当地址中有两个下划线时,下划线将更改为 em 标签:

BlueCloth::new("bob_jones_smith@foo.com").to_html
"<p>bob<em>jones</em>smith@foo.com</p>"

我需要其余文本的格式,但我需要关闭电子邮件地址的格式。我觉得我以前可以使用 notextile 标签,但这似乎不再起作用了。

BlueCloth::new("<notextile> bob_jones_smith@foo.com </notextile>").to_html
"<p><notextile> bob<em>jones</em>smith@foo.com </notextile></p>"

有人知道如何处理吗?我们使用的是 bluecloth 2.2.0 版。

【问题讨论】:

    标签: ruby-on-rails markdown bluecloth


    【解决方案1】:

    一种选择是将:relaxed 设置切换为true

    BlueCloth.new('bob_jones_smith@foo.com', relaxed: true).to_html
    

    The option documentation is available online 但选项名称及其映射到底层 C 库中选项的方式并不总是显而易见的。

    如果您想在某些地方需要字内强调而不是其他地方的文本使用默认选项,您可以explicitly escape the underscores

    escaped_email = 'bob_jones_smith@foo.com'.gsub('_', '\_')
    BlueCloth.new(escaped_email).to_html
    

    【讨论】:

      【解决方案2】:

      为什么不使用字符串插值?

      BlueCloth::new("don't format this: %s").to_html % 'bob_jones_smith@foo.com' 
      => "<p>don't format this: bob_jones_smith@foo.com</p>"
      

      除非您不知道要将原始字符串保存在哪里,否则这将是……奇怪的。

      【讨论】:

      • 感谢您的回复,这是个好主意,但内容来自外部来源,因此我无法控制。
      猜你喜欢
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 2020-12-03
      • 2014-11-07
      相关资源
      最近更新 更多