简体   繁体   English

Redcarpet / Bluecloth不允许标题?

[英]Redcarpet/Bluecloth not allowing headers?

Is there a way to use either Redcarpet or Bluecloth such that when it interpolates the markdown it won't make any headers? 有没有办法使用Redcarpet或Bluecloth,这样当它插入降价时它不会产生任何标题?

For example: 例如:

#header 1

yields: 收益率:

header 1 标题1

header 1 (preferred) 标题1(首选)

And: 和:

##header 2

yields: 收益率:

header 2 标题2

header 2 (preferred) 标题2(首选)

Well, you can escape characters in Markdown: 好吧,你可以在Markdown中转义字符:

# header 1
\# header 1

## header 2
\## header 2

...gives: ...给:

header 1 标题1

# header 1 #header 1

header 2 标题2

## header 2 ##标题2

If you don't want to have to do this, or you're parsing other people's Markdown and don't have a choice, I would recommend pre-processing the incoming Markdown to do the above for you: 如果您不想这样做,或者您正在解析其他人的Markdown并且没有选择,我建议您预先处理收到的Markdown以执行上述操作:

def pound_filter text
  text.gsub /^#/, '\#'
end

Using Redcarpet you can verify that it works: 使用Redcarpet,您可以验证它是否有效:

text = <<-END
  # Hello
  ## World
END

Markdown.new(text.to_html)
# =>  <h1>Hello</h1>
#
#     <h2>World</h2>

Markdown.new(pound_filter text).to_html
# =>  <p># Hello
#     ## World</p>

Of course since a line break in HTML doesn't actually render as such--it will appear as one line: 当然,因为HTML中的换行符实际上不会这样呈现 - 它将显示为一行:

# Hello ## World" # 你好,世界”

...you might want to augment that: ......你可能想要增加:

def pound_filter text
  text.gsub( /((\A^)|([^\A]^))#/ ) {|match| "\n" == match[0] ? "\n\n\\#" : '\#' }
end

pound_filter text
# =>  \# Hello
#
#     \## World

Markdown.new(pound_filter text).to_html
# =>  <p>\# Hello</p>
#
#     <p>\## World</p>

This last would appear as: 这最后会显示为:

# Hello # 你好

## World ##世界

Unfortunately you eventually get into weird territory like this, where a heading is inside a quote: 不幸的是,你最终会进入这样一个奇怪的领域,其中标题在引号内:

> ## Heading

...but I leave that as an exercise to the reader. ......但我把它作为练习留给读者。

Saw a similar solution here that went like this: 在这里看到类似的解决方案:

class RenderWithoutWrap < Redcarpet::Render::HTML
  def postprocess(full_document)
    Regexp.new(/\A<p>(.*)<\/p>\Z/m).match(full_document)[1] rescue full_document
  end
end

It removes all <p> & </p> tags. 它会删除所有<p></p>标记。 I used it like that and it worked. 我就这样使用它并且它起作用了。 I placed that class in a new file called /config/initializers/render_without_wrap.rb . 我将该类放在名为/config/initializers/render_without_wrap.rb的新文件中。 You could do something similar for all <h1> - <h6> tags 你可以为所有<h1> - <h6>标签做类似的事情

class RenderWithoutHeaders < Redcarpet::Render::HTML
  def postprocess(full_document)
    Regexp.new(/\A<h1>(.*)<\/h1>\Z/m).match(full_document)[1] rescue full_document
    Regexp.new(/\A<h2>(.*)<\/h2>\Z/m).match(full_document)[1] rescue full_document
    Regexp.new(/\A<h3>(.*)<\/h3>\Z/m).match(full_document)[1] rescue full_document
    ...(you get the idea)
  end
end

You could then use it like this 然后你可以像这样使用它

def custom_markdown_parse(text)
  markdown = Redcarpet::Markdown.new(RenderWithoutHeaders.new(
    filter_html: true,
    hard_wrap: true,
    other_options: its_your_call
  ))
  markdown.render(text).html_safe
end

I haven't tested it, but it's an idea. 我没有测试过,但这是一个想法。

1. You should be able to escape your markdown source text with backslashes: 1.您应该能够使用反斜杠转义降价源文本:

\# not a header

2. You could also monkey-patch it: 你也可以修补它:

module RedCloth::Formatters::HTML

  [:h1, :h2, :h3, :h4, :h5, :h6].each do |m|
    define_method(m) do |opts|
      "#{opts[:text]}\n"
    end
  end

end

鉴于对Markdown预解析很困难,并且Markdown允许插入HTML,那么如何从生成的HTML中删除标题元素呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM