Class: MarkdownTopicConverter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/markdown.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(text) ⇒ Object


92
93
94
# File 'lib/markdown.rb', line 92

def self.convert(text)
  instance.convert(text)
end

.format(raw) ⇒ Object


96
97
98
# File 'lib/markdown.rb', line 96

def self.format(raw)
  instance.format(raw)
end

Instance Method Details

#convert(text) ⇒ Object


100
101
102
# File 'lib/markdown.rb', line 100

def convert(text)
  @converter.render(text)
end

#format(raw) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/markdown.rb', line 104

def format(raw)
  text = raw.clone
  return '' if text.blank?

  convert_bbcode_img(text)
  users = normalize_user_mentions(text)

  # 如果 ``` 在刚刚换行的时候 Redcapter 无法生成正确,需要两个换行
  text.gsub!("\n```", "\n\n```")

  result = convert(text)
  doc = Nokogiri::HTML.fragment(result)
  link_mention_floor(doc)
  link_mention_user(doc, users)
  replace_emoji(doc)

  return doc.to_html.strip
rescue => e
  Rails.logger.error "MarkdownTopicConverter.format ERROR: #{e}"
  return text
end