2018-07-23 10:05:22 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-07 12:14:41 +00:00
|
|
|
require 'rouge/plugins/common_mark'
|
2020-11-28 21:09:37 +00:00
|
|
|
require "asciidoctor/extensions/asciidoctor_kroki/extension"
|
2015-08-27 20:09:01 +00:00
|
|
|
|
2019-01-23 11:21:12 +00:00
|
|
|
# Generated HTML is transformed back to GFM by app/assets/javascripts/behaviors/markdown/nodes/code_block.js
|
2015-12-15 14:51:16 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
2015-08-27 20:09:01 +00:00
|
|
|
# HTML Filter to highlight fenced code blocks
|
|
|
|
#
|
|
|
|
class SyntaxHighlightFilter < HTML::Pipeline::Filter
|
2019-03-13 13:57:05 +00:00
|
|
|
include OutputSafety
|
|
|
|
|
2021-11-17 15:10:28 +00:00
|
|
|
LANG_PARAMS_DELIMITER = ':'
|
2019-09-10 08:11:43 +00:00
|
|
|
LANG_PARAMS_ATTR = 'data-lang-params'
|
2019-03-13 13:57:05 +00:00
|
|
|
|
2022-01-13 21:14:07 +00:00
|
|
|
CSS = 'pre:not([data-math-style]):not([data-mermaid-style]):not([data-kroki-style]) > code:only-child'
|
2021-06-01 21:10:06 +00:00
|
|
|
XPATH = Gitlab::Utils::Nokogiri.css_to_xpath(CSS).freeze
|
|
|
|
|
2015-08-27 20:09:01 +00:00
|
|
|
def call
|
2021-06-01 21:10:06 +00:00
|
|
|
doc.xpath(XPATH).each do |node|
|
2015-08-27 20:09:01 +00:00
|
|
|
highlight_node(node)
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
def highlight_node(node)
|
2018-07-23 10:05:22 +00:00
|
|
|
css_classes = +'code highlight js-syntax-highlight'
|
2021-11-17 15:10:28 +00:00
|
|
|
lang, lang_params = parse_lang_params(node)
|
2021-09-23 12:11:29 +00:00
|
|
|
sourcepos = node.parent.attr('data-sourcepos')
|
2018-01-31 21:48:18 +00:00
|
|
|
retried = false
|
2017-11-22 03:12:04 +00:00
|
|
|
|
2018-01-31 21:48:18 +00:00
|
|
|
if use_rouge?(lang)
|
|
|
|
lexer = lexer_for(lang)
|
2017-11-22 03:12:04 +00:00
|
|
|
language = lexer.tag
|
2018-01-31 21:48:18 +00:00
|
|
|
else
|
|
|
|
lexer = Rouge::Lexers::PlainText.new
|
|
|
|
language = lang
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
code = Rouge::Formatters::HTMLGitlab.format(lex(lexer, node.text), tag: language)
|
2021-04-07 18:09:45 +00:00
|
|
|
css_classes << " language-#{language}" if language
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError
|
2018-01-31 21:48:18 +00:00
|
|
|
# Gracefully handle syntax highlighter bugs/errors to ensure users can
|
|
|
|
# still access an issue/comment/etc. First, retry with the plain text
|
|
|
|
# filter. If that fails, then just skip this entirely, but that would
|
|
|
|
# be a pretty bad upstream bug.
|
|
|
|
return if retried
|
2017-11-22 03:12:04 +00:00
|
|
|
|
2018-01-31 21:48:18 +00:00
|
|
|
language = nil
|
|
|
|
lexer = Rouge::Lexers::PlainText.new
|
|
|
|
retried = true
|
2017-11-22 03:12:04 +00:00
|
|
|
|
2018-01-31 21:48:18 +00:00
|
|
|
retry
|
2015-09-10 15:23:10 +00:00
|
|
|
end
|
2015-08-27 20:09:01 +00:00
|
|
|
|
2022-03-31 21:08:16 +00:00
|
|
|
sourcepos_attr = sourcepos ? "data-sourcepos=\"#{escape_once(sourcepos)}\"" : ''
|
2021-09-23 12:11:29 +00:00
|
|
|
|
2021-12-13 15:12:59 +00:00
|
|
|
highlighted = %(<div class="gl-relative markdown-code-block js-markdown-code"><pre #{sourcepos_attr} class="#{css_classes}"
|
2019-03-13 13:57:05 +00:00
|
|
|
lang="#{language}"
|
|
|
|
#{lang_params}
|
2021-12-13 15:12:59 +00:00
|
|
|
v-pre="true"><code>#{code}</code></pre><copy-code></copy-code></div>)
|
2016-07-16 01:22:35 +00:00
|
|
|
|
2016-07-01 09:16:48 +00:00
|
|
|
# Extracted to a method to measure it
|
|
|
|
replace_parent_pre_element(node, highlighted)
|
2015-08-27 20:09:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-11-17 15:10:28 +00:00
|
|
|
def parse_lang_params(node)
|
2022-01-10 21:16:38 +00:00
|
|
|
node = node.parent
|
2021-11-17 15:10:28 +00:00
|
|
|
|
|
|
|
# Commonmarker's FULL_INFO_STRING render option works with the space delimiter.
|
|
|
|
# But the current behavior of GitLab's markdown renderer is different - it grabs everything as the single
|
2022-01-10 21:16:38 +00:00
|
|
|
# line, including language and its options. To keep backward compatibility, we have to parse the old format and
|
2021-11-17 15:10:28 +00:00
|
|
|
# merge with the new one.
|
|
|
|
#
|
|
|
|
# Behaviors before separating language and its parameters:
|
|
|
|
# Old ones:
|
|
|
|
# "```ruby with options```" -> '<pre><code lang="ruby with options">'.
|
|
|
|
# "```ruby:with:options```" -> '<pre><code lang="ruby:with:options">'.
|
|
|
|
#
|
|
|
|
# New ones:
|
|
|
|
# "```ruby with options```" -> '<pre><code lang="ruby" data-meta="with options">'.
|
|
|
|
# "```ruby:with:options```" -> '<pre><code lang="ruby:with:options">'.
|
|
|
|
|
|
|
|
language = node.attr('lang')
|
|
|
|
|
2019-03-13 13:57:05 +00:00
|
|
|
return unless language
|
|
|
|
|
2021-11-17 15:10:28 +00:00
|
|
|
language, language_params = language.split(LANG_PARAMS_DELIMITER, 2)
|
2022-01-10 21:16:38 +00:00
|
|
|
language_params = [node.attr('data-meta'), language_params].compact.join(' ')
|
2021-11-17 15:10:28 +00:00
|
|
|
formatted_language_params = format_language_params(language_params)
|
2019-03-13 13:57:05 +00:00
|
|
|
|
2021-11-17 15:10:28 +00:00
|
|
|
[language, formatted_language_params]
|
2019-03-13 13:57:05 +00:00
|
|
|
end
|
|
|
|
|
2016-08-03 14:52:08 +00:00
|
|
|
# Separate method so it can be instrumented.
|
|
|
|
def lex(lexer, code)
|
|
|
|
lexer.lex(code)
|
|
|
|
end
|
|
|
|
|
|
|
|
def lexer_for(language)
|
|
|
|
(Rouge::Lexer.find(language) || Rouge::Lexers::PlainText).new
|
|
|
|
end
|
|
|
|
|
2022-01-13 21:14:07 +00:00
|
|
|
# Replace the parent `pre` element with the entire highlighted block
|
2016-07-01 09:16:48 +00:00
|
|
|
def replace_parent_pre_element(node, highlighted)
|
|
|
|
node.parent.replace(highlighted)
|
|
|
|
end
|
2017-11-22 03:12:04 +00:00
|
|
|
|
|
|
|
def use_rouge?(language)
|
2020-11-28 21:09:37 +00:00
|
|
|
(%w(math suggestion) + ::AsciidoctorExtensions::Kroki::SUPPORTED_DIAGRAM_NAMES).exclude?(language)
|
2017-11-22 03:12:04 +00:00
|
|
|
end
|
2021-11-17 15:10:28 +00:00
|
|
|
|
|
|
|
def format_language_params(language_params)
|
|
|
|
return if language_params.blank?
|
|
|
|
|
|
|
|
%(#{LANG_PARAMS_ATTR}="#{escape_once(language_params)}")
|
|
|
|
end
|
2015-08-27 20:09:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|