]> cat aescling's git repositories - mastodon.git/commitdiff
Minor optimization
authorThibaut Girka <thib@sitedethib.com>
Fri, 17 May 2019 09:14:09 +0000 (11:14 +0200)
committerThibG <thib@sitedethib.com>
Fri, 17 May 2019 21:51:14 +0000 (23:51 +0200)
app/lib/formatter.rb

index ccebf4353f54db950b2a7d9c69e684e8a91329c2..78a0e9f254d0ab0af30a3013c34b7925a297719b 100644 (file)
@@ -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