app/helpers/wcc/contentful/app/section_helper.rb in wcc-contentful-app-1.0.5 vs app/helpers/wcc/contentful/app/section_helper.rb in wcc-contentful-app-1.0.6

- old
+ new

@@ -1,8 +1,9 @@ # frozen_string_literal: true require 'redcarpet' +require '../wcc-contentful-app/lib/wcc/contentful/app/markdown_renderer' module WCC::Contentful::App::SectionHelper extend self def render_section(section, index) @@ -33,88 +34,14 @@ title = section.try(:bookmark_title) || section.try(:title) CGI.escape(title.gsub(/\W+/, '-')) if title.present? end def markdown(text) - raise ArgumentError, 'markdown method requires text' unless text + html_to_render = WCC::Contentful::App::MarkdownRenderer.new.markdown(text) - markdown_links = links_within_markdown(text) - links_with_classes, raw_classes = gather_links_with_classes_data(markdown_links) - - options = { - filter_html: true, - hard_wrap: true, - link_attributes: { target: '_blank' }, - space_after_headers: true, - fenced_code_blocks: true, - links_with_classes: links_with_classes - } - - extensions = { - autolink: true, - superscript: true, - disable_indented_code_blocks: true, - tables: true - } - - renderer = ::WCC::Contentful::App::CustomMarkdownRender.new(options) - markdown = ::Redcarpet::Markdown.new(renderer, extensions) - html_to_render = markdown.render(remove_markdown_href_class_syntax(raw_classes, text)) - content_tag(:div, CGI.unescapeHTML(html_to_render).html_safe, class: 'formatted-content') - end - - def links_within_markdown(text) - text.scan(/(\[(.*?)\]\((.*?)\)(\{\:.*?\})?)/) - end - - def gather_links_with_classes_data(markdown_links) - links_with_classes_arr = [] - raw_classes_arr = [] - markdown_links.each do |markdown_link_arr| - next unless markdown_link_arr.last.present? - - raw_class = markdown_link_arr[3] - url, title = url_and_title(markdown_link_arr[2]) - content = markdown_link_arr[1] - classes = capture_individual_classes(raw_class) - link_class = combine_individual_classes_to_one_string(classes) - - raw_classes_arr << raw_class - links_with_classes_arr << [url, title, content, link_class] - end - - [links_with_classes_arr, raw_classes_arr] - end - - def remove_markdown_href_class_syntax(raw_classes, text) - text_without_markdown_class_syntax = text.dup - raw_classes.each { |klass| text_without_markdown_class_syntax.slice!(klass) } - text_without_markdown_class_syntax - end - - def url_and_title(markdown_link_and_title) - match = - markdown_link_and_title.scan( - /(\s|^)(https?:\/\/\S*|^\/\S*\/*\S*|^#\S*|mailto:\S*)(?=\s|$)|(\".*?\")/ - ) - url = match[0][1] - title = match[1] ? match[1][2] : nil - [url, title] - end - - def capture_individual_classes(classes) - classes.scan(/\.[^\.\}\s]*/) - end - - def combine_individual_classes_to_one_string(classes) - class_string = '' - classes.each do |klass| - class_string += klass.tr('.', '') + ' ' - end - class_string end def safe_line_break(text, options = {}) return unless text.present?