class Services::KramdownService
This class contains operations related to the kramdown engine
Constants
- DEFAULT_HERO
Public Instance Methods
get_all_image_paths(markdown)
click to toggle source
This method returns an array of all image paths given some markdown
Params:
markdown
-
text of a markdown post
# 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)
click to toggle source
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
# 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)
click to toggle source
This method takes given markdown and converts it to HTML for the post preview
Params:
text
-
markdown to convert to html
# File lib/services/kramdown_service.rb, line 52 def get_preview(text) Kramdown::Document.new(text).to_preview end
Private Instance Methods
get_document_descendants(current_element, result)
click to toggle source
# File lib/services/kramdown_service.rb, line 96 def get_document_descendants(current_element, result) current_element.children.each do |element| result << element get_document_descendants(element, result) end end
get_filename_for_image_tag(image_el)
click to toggle source
# File lib/services/kramdown_service.rb, line 103 def get_filename_for_image_tag(image_el) File.basename(image_el.attr['src']) end