class Kramdown::Converter::Preview

A custom kramdown HTML converter for getting the HTML preview for a post

Public Instance Methods

convert_img(element, indent) click to toggle source

An override of the convert_img tag which converts all image sources to pull from the CarrierWare cache location if an uploader exists with the image's filename. Or the Base64 contents of a downloaded image are replaced in the src attribute if the image was downloaded for the post

Params: el::the image element to convert to html indent::the indent of the HTML

Calls superclass method
# File lib/services/kramdown_service.rb, line 21
def convert_img(element, indent)
  formatted_filename = File.basename(element.attr['src']).tr(' ', '_')
  uploader = PostImageManager.instance.uploaders.find { |x| x.filename == formatted_filename }
  if uploader
    element.attr['src'] = "/uploads/tmp/#{uploader.preview.cache_name}"
  else
    downloaded_image = PostImageManager.instance.downloaded_images
                                       .find { |x| File.basename(x.filename) == File.basename(element.attr['src']) }
    if downloaded_image
      extension = File.extname(downloaded_image.filename)
      extension[0] = ''
      element.attr['src'] = "data:image/#{extension};base64,#{downloaded_image.contents}"
    end
  end

  super(element, indent)
end