Commit 997d76fe authored by Peter Leitzen's avatar Peter Leitzen

Improve render performance of large wiki pages

Compile often used regexps only once outside of the loop.

This commit improves the performance of large wiki pages with many, many references.
parent d32d8bae
...@@ -100,6 +100,11 @@ module Banzai ...@@ -100,6 +100,11 @@ module Banzai
ref_pattern = object_class.reference_pattern ref_pattern = object_class.reference_pattern
link_pattern = object_class.link_reference_pattern link_pattern = object_class.link_reference_pattern
# Compile often used regexps only once outside of the loop
ref_pattern_anchor = /\A#{ref_pattern}\z/
link_pattern_start = /\A#{link_pattern}/
link_pattern_anchor = /\A#{link_pattern}\z/
nodes.each do |node| nodes.each do |node|
if text_node?(node) && ref_pattern if text_node?(node) && ref_pattern
replace_text_when_pattern_matches(node, ref_pattern) do |content| replace_text_when_pattern_matches(node, ref_pattern) do |content|
...@@ -108,7 +113,7 @@ module Banzai ...@@ -108,7 +113,7 @@ module Banzai
elsif element_node?(node) elsif element_node?(node)
yield_valid_link(node) do |link, inner_html| yield_valid_link(node) do |link, inner_html|
if ref_pattern && link =~ /\A#{ref_pattern}\z/ if ref_pattern && link =~ ref_pattern_anchor
replace_link_node_with_href(node, link) do replace_link_node_with_href(node, link) do
object_link_filter(link, ref_pattern, link_content: inner_html) object_link_filter(link, ref_pattern, link_content: inner_html)
end end
...@@ -118,7 +123,7 @@ module Banzai ...@@ -118,7 +123,7 @@ module Banzai
next unless link_pattern next unless link_pattern
if link == inner_html && inner_html =~ /\A#{link_pattern}/ if link == inner_html && inner_html =~ link_pattern_start
replace_link_node_with_text(node, link) do replace_link_node_with_text(node, link) do
object_link_filter(inner_html, link_pattern, link_reference: true) object_link_filter(inner_html, link_pattern, link_reference: true)
end end
...@@ -126,7 +131,7 @@ module Banzai ...@@ -126,7 +131,7 @@ module Banzai
next next
end end
if link =~ /\A#{link_pattern}\z/ if link =~ link_pattern_anchor
replace_link_node_with_href(node, link) do replace_link_node_with_href(node, link) do
object_link_filter(link, link_pattern, link_content: inner_html, link_reference: true) object_link_filter(link, link_pattern, link_content: inner_html, link_reference: true)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment