Class: Slackdown

Inherits:
Object
  • Object
show all
Defined in:
app/models/slackdown.rb

Overview

Converts GitHub-flavored Markdown to Slack-flavored Markdown

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markdown) ⇒ Slackdown

Returns a new instance of Slackdown.



5
6
7
# File 'app/models/slackdown.rb', line 5

def initialize(markdown)
  @markdown = markdown.to_s
end

Instance Attribute Details

#markdownObject (readonly)

Returns the value of attribute markdown.



3
4
5
# File 'app/models/slackdown.rb', line 3

def markdown
  @markdown
end

Instance Method Details

#convertObject Also known as: to_s



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/slackdown.rb', line 9

def convert
  # Remove the source name from fenced code blocks
  slackdown = markdown.gsub(/^```(ruby|diff|css|json|sql|html|xml|coffee(?:script)?|javascript|js|bash)/, "```")

  # Convert `__` and `**` to `*`
  slackdown = slackdown.gsub(/(?<!_)__(?!_)/, "*")
                       .gsub(/(?<!\*)\*\*(?!\*)/, "*")

  # Replace images with their URLs: Slack will unfurl them
  slackdown = slackdown.gsub(/<img[^>]*src="([^"]+)"[^>]*>/, "\\1")
  slackdown
end