Is there a way to use either Redcarpet or Bluecloth such that when it interpolates the markdown it won't make any headers?
For example:
#header 1
yields:
header 1
header 1 (preferred)
And:
##header 2
yields:
header 2
header 2 (preferred)
Is there a way to use either Redcarpet or Bluecloth such that when it interpolates the markdown it won't make any headers?
For example:
#header 1
yields:
header 1
header 1 (preferred)
And:
##header 2
yields:
header 2
header 2 (preferred)
On
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. I used it like that and it worked. I placed that class in a new file called /config/initializers/render_without_wrap.rb. You could do something similar for all <h1>-<h6> tags
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.
Well, you can escape characters in Markdown:
...gives:
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:
Using Redcarpet you can verify that it works:
Of course since a line break in HTML doesn't actually render as such--it will appear as one line:
...you might want to augment that:
This last would appear as:
Unfortunately you eventually get into weird territory like this, where a heading is inside a quote:
...but I leave that as an exercise to the reader.