Class: Services::KramdownService

Inherits:
Object
  • Object
show all
Defined in:
lib/services/kramdown_service.rb

Overview

This class contains operations related to the kramdown engine

Constant Summary collapse

DEFAULT_HERO =
'https://source.unsplash.com/collection/145103/'

Instance Method Summary collapse

Instance Method Details

#get_all_image_paths(markdown) ⇒ Object

This method returns an array of all image paths given some markdown

Params:

markdown

text of a markdown post



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/services/kramdown_service.rb', line 80

def get_all_image_paths(markdown)
  document = Kramdown::Document.new(markdown)
  document_descendants = []

  get_document_descendants(document.root, document_descendants)
  all_img_tags = document_descendants.select { |x| x.type == :img }

  result = all_img_tags.map do |img_tag|
    img_tag.attr['src'][1..-1] if img_tag.attr['src'] !~ URI::DEFAULT_PARSER.make_regexp
  end

  result.compact
end

#get_image_filename_from_markdown(image_file_name, markdown) ⇒ Object

This method returns the image filename given some markdown

Params:

image_file_name

a filename of a image to look for in markdown

markdown

text of a markdown post



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/services/kramdown_service.rb', line 62

def get_image_filename_from_markdown(image_file_name, markdown)
  document = Kramdown::Document.new(markdown)
  document_descendants = []

  get_document_descendants(document.root, document_descendants)
  all_img_tags = document_descendants.select { |x| x.type == :img }
  matching_image_tag = all_img_tags.find { |x| get_filename_for_image_tag(x).tr(' ', '_') == image_file_name }

  return get_filename_for_image_tag(matching_image_tag) if matching_image_tag

  nil
end

#get_preview(text) ⇒ Object

This method takes given markdown and converts it to HTML for the post preview

Params:

text

markdown to convert to html



52
53
54
# File 'lib/services/kramdown_service.rb', line 52

def get_preview(text)
  Kramdown::Document.new(text).to_preview
end