Method: ApplicationHelper#strip_markdown

Defined in:
app/helpers/application_helper.rb

#strip_markdown(markdown) ⇒ String

Strip Markdown formatting out of a text string to use in plain-text only environments. This isn’t a perfect way to strip out Markdown, so it should only be used for non-critical things like page descriptions - things that will later be supplemented by the full formatted content.

Parameters:

  • markdown (String)

    The Markdown string to strip.

Returns:

  • (String)

    The plain-text equivalent.



121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/application_helper.rb', line 121

def strip_markdown(markdown)
  # Remove block-level formatting: headers, hr, references, images, HTML tags
  markdown = markdown.gsub(/(?:^#+ +|^-{3,}|^\[[^\]]+\]: ?.+$|^!\[[^\]]+\](?:\([^)]+\)|\[[^\]]+\])$|<[^>]+>)/, '')

  # Remove inline formatting: bold, italic, strike etc.
  markdown = markdown.gsub(/[*_~]+/, '')

  # Remove links and inline images but replace them with their text/alt text.
  markdown.gsub(/!?\[([^\]]+)\](?:\([^)]+\)|\[[^\]]+\])/, '\1')
end