xijo/reverse_markdown

Slack flavored Markdown

RummanSadiq opened this issue · 6 comments

Hi, I was recently working with slack integration and had to convert HTML into Slack Flavored Markdown, which is a bit different than GitHub Flavored.

For the urgent need, I forked this project and made the required changes to make it work with Slack. I thought this might be good starting point for me to contribute to open source. If you guys think that there is a use for Slack Flavored Markdown, then I'll be more than happy to open a PR for it.

Slack only allows a small subset of Markdown features. It includes things like Bold, Italic, Ordered List, Unordered List, Quote, Code Block.

@xijo thoughts?

xijo commented

Hi @RummanSadiq,

I'd say it depends on how big the differences between "normal" markdown and slack-flavored are.
Do they ignore other markdown or do we really have to unregister existing converters? Are there any other differences in the handling of the nodes itself?

Anyway it would be great to get you started on open source, so please feel free to submit a PR, we can discuss implementation detail there, right?

I also need to conver HTML to Slack's "mrkdwn". I was just trying to figure out whether I should roll my own, improve https://github.com/everwise/slack_transformer or add a Slack flavor to reverse_markdown.

I would be very curious to see what you did, @RummanSadiq.

@xijo, Slack's markup is quite different from Markdown, although they look very similar at first glance. For example:

  • **bold** in markdown is *bold* in Slack
  • Slack does not support headers
  • Slack does not support lists
  • [name](https://google.com) in markdown is <https://google.com|name> in Slack

Some markup works the same, like `code`, ~strike~, _italic_ and > blockquote.

https://api.slack.com/reference/surfaces/formatting

Hey @gabrielmdeal,

Did you ever get anywhere with your need to convert HTML to Mrkdwn?

I have the same need now and the only libraries available (like slack_transformer) haven't been touched in 3+ years.

Thanks!

I ended up rolling my own. But I do not own the code, so there is nothing I can share. 😞

a quick monkey patch covers the two really different slack mrkdown changes - bold & links

# slackify! # https://github.com/xijo/reverse_markdown/issues/90
module ReverseMarkdown
  module Converters
    class Strong < Base
      def convert(node, state = {})
        content = treat_children(node, state.merge(already_strong: true))
        if content.strip.empty? || state[:already_strong]
          content
        else
          "#{content[/^\s*/]}*#{content.strip}*#{content[/\s*$/]}"
        end
      end
    end

    class A < Base
      def convert(node, state = {})
        name  = treat_children(node, state)
        href  = node['href']
        title = extract_title(node)

        if href.to_s.empty? || name.empty?
          name
        else
          link = "<#{href}#{title}|#{name}>"
          link.prepend(' ') if prepend_space?(node)
          link
        end
      end
    end
  end
end