about summary refs log tree commit diff
path: root/app/lib/formatter.rb
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2019-05-17 11:14:09 +0200
committerThibG <thib@sitedethib.com>2019-05-17 23:51:14 +0200
commit9ca21e93cc1506d0a3c0cfe450636933c3d2388a (patch)
treef7ee000b7d49156fe98eefe709a62ccbcbe0ed3a /app/lib/formatter.rb
parent56245a2a7204bb63840e633e923bfd1679c793d9 (diff)
Minor optimization
Diffstat (limited to 'app/lib/formatter.rb')
-rw-r--r--app/lib/formatter.rb58
1 files changed, 31 insertions, 27 deletions
diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb
index ccebf4353..78a0e9f25 100644
--- a/app/lib/formatter.rb
+++ b/app/lib/formatter.rb
@@ -70,33 +70,7 @@ class Formatter
   end
 
   def format_markdown(html)
-    extensions = {
-      autolink: true,
-      no_intra_emphasis: true,
-      fenced_code_blocks: true,
-      disable_indented_code_blocks: true,
-      strikethrough: true,
-      lax_spacing: true,
-      space_after_headers: true,
-      superscript: true,
-      underline: true,
-      highlight: true,
-      footnotes: false,
-    }
-
-    renderer = HTMLRenderer.new({
-      filter_html: false,
-      escape_html: false,
-      no_images: true,
-      no_styles: true,
-      safe_links_only: true,
-      hard_wrap: true,
-      link_attributes: { target: '_blank', rel: 'nofollow noopener' },
-    })
-
-    markdown = Redcarpet::Markdown.new(renderer, extensions)
-
-    html = reformat(markdown.render(html))
+    html = reformat(markdown_formatter.render(html))
     html.delete("\r").delete("\n")
   end
 
@@ -160,6 +134,36 @@ class Formatter
 
   private
 
+  def markdown_formatter
+    return @markdown_formatter if defined?(@markdown_formatter)
+
+    extensions = {
+      autolink: true,
+      no_intra_emphasis: true,
+      fenced_code_blocks: true,
+      disable_indented_code_blocks: true,
+      strikethrough: true,
+      lax_spacing: true,
+      space_after_headers: true,
+      superscript: true,
+      underline: true,
+      highlight: true,
+      footnotes: false,
+    }
+
+    renderer = HTMLRenderer.new({
+      filter_html: false,
+      escape_html: false,
+      no_images: true,
+      no_styles: true,
+      safe_links_only: true,
+      hard_wrap: true,
+      link_attributes: { target: '_blank', rel: 'nofollow noopener' },
+    })
+
+    @markdown_formatter = Redcarpet::Markdown.new(renderer, extensions)
+  end
+
   def html_entities
     @html_entities ||= HTMLEntities.new
   end