Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

3.3 KB
Raw Blame
class BaseMarkdownRenderer < CommonMarker::HtmlRenderer
  include MarkdownRendererUrlSanitizer

  # Email clients drop stylesheets, so tables carry inline styles. Translucent grays suit any
  # background (white email, blue bubble, dark mode); the solid border is Outlook's fallback.
  TABLE_STYLE = 'border-collapse: collapse; margin: 12px 0;'.freeze
  TABLE_CELL_STYLE = 'border: 1px solid #d1d5db; border-color: rgba(127, 127, 127, 0.35); padding: 6px 12px; vertical-align: top;'.freeze
  TABLE_HEADER_STYLE = "#{TABLE_CELL_STYLE} background-color: rgba(127, 127, 127, 0.12); font-weight: 600;".freeze

  def image(node)
    src, title = extract_img_attributes(node)
    sizing_style = extract_image_sizing_style(src)

    render_img_tag(src, title, sizing_style)
  end

  # CommonMarker's table rendering plus inline styles; the ivars are the gem's table state.
  def table(node)
    @alignments = node.table_alignments
    @needs_close_tbody = false
    out(%(<table style="#{TABLE_STYLE}">\n), :children)
    out("</tbody>\n") if @needs_close_tbody
    out("</table>\n")
  end

  # Unaligned cells follow the text direction (browsers center <th>); an empty cell
  # gets an nbsp because clients collapse it to zero height.
  def table_cell(node)
    tag = @in_header ? 'th' : 'td'
    style = @in_header ? TABLE_HEADER_STYLE : TABLE_CELL_STYLE
    text_align = @alignments[@column_index] || 'inherit'
    content = node.first_child ? :children : '&nbsp;'
    out(%(<#{tag} style="#{style} text-align: #{text_align};">), content, "</#{tag}>\n")
    @column_index += 1
  end

  private

  def extract_img_attributes(node)
    [
      sanitized_href(node.url),
      escape_html(node.title)
    ]
  end

  # Drag-resize from the reply editor encodes the chosen width as cw_image_width
  # on the URL; the older message-signature picker uses cw_image_height. Width
  # wins when both are set so the agent's most recent intent is honored.
  def extract_image_sizing_style(src)
    query_params = parse_query_params(src)
    width = sanitize_pixel_value(query_params['cw_image_width']&.first)
    return "width: #{width}; max-width: 100%; height: auto;" if width

    height = sanitize_pixel_value(query_params['cw_image_height']&.first)
    height ? "height: #{height};" : nil
  end

  # Only allow a bounded `<digits>px` value so the decoded query param can't
  # break out of the inline style attribute (HTML attribute injection).
  def sanitize_pixel_value(raw)
    return unless raw =~ /\A(\d+)px\z/

    px = Regexp.last_match(1).to_i
    "#{px}px" if px.between?(1, 2000)
  end

  def parse_query_params(url)
    parsed_url = URI.parse(url)
    CGI.parse(parsed_url.query || '')
  rescue URI::InvalidURIError
    {}
  end

  def render_img_tag(src, title, sizing_style = nil)
    title_attribute = title.present? ? " title=\"#{title}\"" : ''
    # Use inline style instead of HTML width/height attributes: email clients
    # and the in-app Letter view both run images through CSS (e.g. prose /
    # lettersanitizer's `img { height: auto }`) which overrides presentational
    # attributes. Inline style has higher specificity and survives.
    style_attribute = sizing_style ? " style=\"#{sizing_style}\"" : ''

    plain do
      # plain ensures that the content is not wrapped in a paragraph tag
      out("<img src=\"#{src}\"#{title_attribute}#{style_attribute} />")
    end
  end
end