Skip to content
Snippets Groups Projects
Commit 798d3289 authored by Douwe Maan's avatar Douwe Maan
Browse files

Autolink the raw text and “transfer” the found links to the highlighted text

parent 86c54979
No related branches found
No related tags found
No related merge requests found
Pipeline #
Loading
@@ -22,7 +22,7 @@ module Gitlab
Loading
@@ -22,7 +22,7 @@ module Gitlab
   
def highlight(text, continue: true, plain: false) def highlight(text, continue: true, plain: false)
highlighted_text = highlight_text(text, continue: continue, plain: plain) highlighted_text = highlight_text(text, continue: continue, plain: plain)
autolink_strings(highlighted_text) autolink_strings(text, highlighted_text)
end end
   
def lexer def lexer
Loading
@@ -61,9 +61,42 @@ module Gitlab
Loading
@@ -61,9 +61,42 @@ module Gitlab
highlight_plain(text) highlight_plain(text)
end end
   
def autolink_strings(highlighted_text) def autolink_strings(text, highlighted_text)
raw_lines = text.lines
# TODO: Don't run pre-processing pipeline, because this may break the highlighting # TODO: Don't run pre-processing pipeline, because this may break the highlighting
Banzai.render(highlighted_text, pipeline: :autolink, autolink_emails: true).html_safe linked_text = Banzai.render(
ERB::Util.html_escape(text),
pipeline: :autolink,
autolink_emails: true
).html_safe
linked_lines = linked_text.lines
highlighted_lines = highlighted_text.lines
highlighted_lines.map!.with_index do |rich_line, i|
matches = []
linked_lines[i].scan(/(?<start><a[^>]+>)(?<content>[^<]+)(?<end><\/a>)/) { matches << Regexp.last_match }
next rich_line if matches.empty?
raw_line = raw_lines[i]
marked_line = rich_line.html_safe
matches.each do |match|
marker = StringRegexMarker.new(raw_line, marked_line)
regex = /#{Regexp.escape(match[:content])}/
marked_line = marker.mark(regex) do |text, left:, right:|
"#{match[:start]}#{text}#{match[:end]}"
end
end
marked_line
end
highlighted_lines.join.html_safe
end end
end end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment