summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-08-04 10:13:39 +0000
committerRémy Coutable <remy@rymai.me>2016-08-04 10:13:39 +0000
commit6ffafc79460bcfef29d8a1b3a30e8a1104f94d32 (patch)
tree016220528f60ba130bbb2b82e926f70a7ca4710e
parentae83801cd52269f6374e8885a96f531d594c6f49 (diff)
parent038d6febedc03280bd686d929057c02d05f2afd6 (diff)
downloadgitlab-ce-6ffafc79460bcfef29d8a1b3a30e8a1104f94d32.tar.gz
Merge branch 'syntax-highlight-filter-performance' into 'master'
Improve performance of SyntaxHighlightFilter ## What does this MR do? This MR improves the performance of `Banzai::Filter::SyntaxHighlightFilter`. See e9bacc6575d0002c6cab620075dea3dc7f93f100 for more information. ## Are there points in the code the reviewer needs to double check? Styling mostly. ## Why was this MR needed? Syntax highlighting is rather slow. ## What are the relevant issue numbers? #18592 ## Does this MR meet the acceptance criteria? - [x] [CHANGELOG](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CHANGELOG) entry added - [x] ~~[Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md)~~ - [x] ~~API support added~~ - Tests - [x] ~~Added for this feature/bug~~ - [ ] All builds are passing - [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides) - [ ] Branch has no merge conflicts with `master` (if you do - rebase it please) - [x] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits) See merge request !5643
-rw-r--r--CHANGELOG1
-rw-r--r--lib/banzai/filter/syntax_highlight_filter.rb26
2 files changed, 19 insertions, 8 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 98bb05d1dd1..e388c7a3cc6 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -22,6 +22,7 @@ v 8.11.0 (unreleased)
- Limit git rev-list output count to one in forced push check
- Clean up unused routes (Josef Strzibny)
- Add green outline to New Branch button. !5447 (winniehell)
+ - Improve performance of syntax highlighting Markdown code blocks
- Update to gitlab_git 10.4.1 and take advantage of preserved Ref objects
- Remove delay when hitting "Reply..." button on page with a lot of discussions
- Retrieve rendered HTML from cache in one request
diff --git a/lib/banzai/filter/syntax_highlight_filter.rb b/lib/banzai/filter/syntax_highlight_filter.rb
index 91f0159f9a1..fcdb496aed2 100644
--- a/lib/banzai/filter/syntax_highlight_filter.rb
+++ b/lib/banzai/filter/syntax_highlight_filter.rb
@@ -17,15 +17,12 @@ module Banzai
def highlight_node(node)
language = node.attr('class')
- code = node.text
-
+ code = node.text
css_classes = "code highlight"
-
- lexer = Rouge::Lexer.find_fancy(language) || Rouge::Lexers::PlainText
- formatter = Rouge::Formatters::HTML.new
+ lexer = lexer_for(language)
begin
- code = formatter.format(lexer.lex(code))
+ code = format(lex(lexer, code))
css_classes << " js-syntax-highlight #{lexer.tag}"
rescue
@@ -41,14 +38,27 @@ module Banzai
private
+ # Separate method so it can be instrumented.
+ def lex(lexer, code)
+ lexer.lex(code)
+ end
+
+ def format(tokens)
+ rouge_formatter.format(tokens)
+ end
+
+ def lexer_for(language)
+ (Rouge::Lexer.find(language) || Rouge::Lexers::PlainText).new
+ end
+
def replace_parent_pre_element(node, highlighted)
# Replace the parent `pre` element with the entire highlighted block
node.parent.replace(highlighted)
end
# Override Rouge::Plugins::Redcarpet#rouge_formatter
- def rouge_formatter(lexer)
- Rouge::Formatters::HTML.new
+ def rouge_formatter(lexer = nil)
+ @rouge_formatter ||= Rouge::Formatters::HTML.new
end
end
end