class ContentfulRails::MarkdownRenderer

A custom renderer for Redcarpet, which adds options for manipulating images through the URL of the image, and building links based on the content of the link.

You can subclass this in your application, to add or manipulate specific markup to elements in the markdown.

Public Class Methods

new(opts) click to toggle source
Calls superclass method
# File lib/contentful_rails/markdown_renderer.rb, line 12
def initialize(opts)
  @options = opts
  @image_parameters = {
    w: @options[:image_options][:width], # width in px
    h: @options[:image_options][:height], # height in px
    fit: @options[:image_options][:fit], # crop, scale, pad, thumb
    f: @options[:image_options][:focus], # top, right, bottom, left, top_right, top_left, bottom_right, bottom_left, face
    fm: @options[:image_options][:format], # jpg, png, webp
    bg: @options[:image_options][:background_colour], # no idea what the options are, but 'transparent' works for pngs
    r: @options[:image_options][:corner_radius], # corner radius in px
    q: @options[:image_options][:quality]
  }.delete_if { |_, v| v.nil? }
  super
end

Public Instance Methods

image(link, title, alt_text) click to toggle source

Image tag wrapper for forwarding Image API options

# File lib/contentful_rails/markdown_renderer.rb, line 28
def image(link, title, alt_text)
  # add the querystring to the image
  if @image_parameters.present?
    prefix = link.include?('?') ? '&' : '?'
    link += "#{prefix}#{@image_parameters.to_query}"
  end
  # return a content tag
  content_tag(:img, nil, src: link.to_s, alt: alt_text, title: title)
end