class SlackSmartBot
def transform_to_slack_markdown(markdown_text)
code_block_regex = /```(.*?)```/m
preserved_code_blocks = markdown_text.scan(code_block_regex).map(&:first)
code_block_placeholder_text = ""
while markdown_text.include?(code_block_placeholder_text)
code_block_placeholder_text = "CODE_BLOCK_PLACEHOLDER_#{"6:x".gen}"
end
transformed_text = markdown_text.gsub(code_block_regex, code_block_placeholder_text)
transformed_text.gsub!(/^\* (.*)$/, '• \1') transformed_text.gsub!(/^\s*d+. (.*)$/, '\1.') transformed_text.gsub!(/!\[(.*?)\]\((.*?)\)/, '\1') transformed_text.gsub!(/\[(.*?)\]\((.*?)\)/, '<\2|\1>') transformed_text.gsub!(/\*\*(.*?)\*\*/, '*\1*') transformed_text.gsub!(/__(.*?)__/, '*\1*') transformed_text.gsub!(/^\s*#+.*\*/) { |match| match.gsub("*", "") }
transformed_text.gsub!(/^\s*####(#+) (.*)$/) { "\n" + "#{":black_small_square:" * ($1.length + 1)} *#{$2}*" }
transformed_text.gsub!(/^\s*#### (.*)$/, "\n" + ':black_small_square: *\1*') transformed_text.gsub!(/^\s*### (.*)$/, "\n" + ':small_orange_diamond: *\1*') transformed_text.gsub!(/^\s*## (.*)$/, "\n" + ':small_blue_diamond: *\1*') transformed_text.gsub!(/^\s*# (.*)$/, "\n" + ':small_red_triangle: *\1*')
preserved_code_blocks.each { |block| transformed_text.sub!(code_block_placeholder_text, "```#{block}```") }
return transformed_text
end
end