Commit d0f8668d authored by Dmitriy Zaporozhets (DZ)'s avatar Dmitriy Zaporozhets (DZ)

Merge branch '338273-add-frontmatter-metadata-to-code-blocks' into 'master'

Identify frontmatter code blocks when rendering Markdown

See merge request gitlab-org/gitlab!70717
parents 580ae2c5 c2e5d7b8
...@@ -9,7 +9,7 @@ module Banzai ...@@ -9,7 +9,7 @@ module Banzai
html.sub(Gitlab::FrontMatter::PATTERN) do |_match| html.sub(Gitlab::FrontMatter::PATTERN) do |_match|
lang = $~[:lang].presence || lang_mapping[$~[:delim]] lang = $~[:lang].presence || lang_mapping[$~[:delim]]
["```#{lang}", $~[:front_matter], "```", "\n"].join("\n") ["```#{lang}:frontmatter", $~[:front_matter], "```", "\n"].join("\n")
end end
end end
end end
......
...@@ -28,6 +28,7 @@ module Banzai ...@@ -28,6 +28,7 @@ module Banzai
def highlight_node(node) def highlight_node(node)
css_classes = +'code highlight js-syntax-highlight' css_classes = +'code highlight js-syntax-highlight'
lang, lang_params = parse_lang_params(node.attr('lang')) lang, lang_params = parse_lang_params(node.attr('lang'))
sourcepos = node.parent.attr('data-sourcepos')
retried = false retried = false
if use_rouge?(lang) if use_rouge?(lang)
...@@ -55,7 +56,9 @@ module Banzai ...@@ -55,7 +56,9 @@ module Banzai
retry retry
end end
highlighted = %(<pre class="#{css_classes}" sourcepos_attr = sourcepos ? "data-sourcepos=\"#{sourcepos}\"" : ""
highlighted = %(<pre #{sourcepos_attr} class="#{css_classes}"
lang="#{language}" lang="#{language}"
#{lang_params} #{lang_params}
v-pre="true"><code>#{code}</code></pre>) v-pre="true"><code>#{code}</code></pre>)
......
...@@ -39,7 +39,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do ...@@ -39,7 +39,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do
aggregate_failures do aggregate_failures do
expect(output).not_to include '---' expect(output).not_to include '---'
expect(output).to include "```yaml\nfoo: :foo_symbol\n" expect(output).to include "```yaml:frontmatter\nfoo: :foo_symbol\n"
end end
end end
...@@ -59,7 +59,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do ...@@ -59,7 +59,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do
aggregate_failures do aggregate_failures do
expect(output).not_to include '+++' expect(output).not_to include '+++'
expect(output).to include "```toml\nfoo = :foo_symbol\n" expect(output).to include "```toml:frontmatter\nfoo = :foo_symbol\n"
end end
end end
...@@ -81,7 +81,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do ...@@ -81,7 +81,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do
aggregate_failures do aggregate_failures do
expect(output).not_to include ';;;' expect(output).not_to include ';;;'
expect(output).to include "```json\n{\n \"foo\": \":foo_symbol\",\n" expect(output).to include "```json:frontmatter\n{\n \"foo\": \":foo_symbol\",\n"
end end
end end
...@@ -101,7 +101,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do ...@@ -101,7 +101,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do
aggregate_failures do aggregate_failures do
expect(output).not_to include '---arbitrary' expect(output).not_to include '---arbitrary'
expect(output).to include "```arbitrary\nfoo = :foo_symbol\n" expect(output).to include "```arbitrary:frontmatter\nfoo = :foo_symbol\n"
end end
end end
...@@ -130,7 +130,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do ...@@ -130,7 +130,7 @@ RSpec.describe Banzai::Filter::FrontMatterFilter do
aggregate_failures do aggregate_failures do
expect(output).to eq <<~MD expect(output).to eq <<~MD
```yaml ```yaml:frontmatter
foo: :foo_symbol foo: :foo_symbol
bar: :bar_symbol bar: :bar_symbol
``` ```
......
...@@ -98,6 +98,14 @@ RSpec.describe Banzai::Filter::SyntaxHighlightFilter do ...@@ -98,6 +98,14 @@ RSpec.describe Banzai::Filter::SyntaxHighlightFilter do
end end
end end
context "when sourcepos metadata is available" do
it "includes it in the highlighted code block" do
result = filter('<pre data-sourcepos="1:1-3:3"><code lang="plaintext">This is a test</code></pre>')
expect(result.to_html).to eq('<pre data-sourcepos="1:1-3:3" class="code highlight js-syntax-highlight language-plaintext" lang="plaintext" v-pre="true"><code><span id="LC1" class="line" lang="plaintext">This is a test</span></code></pre>')
end
end
context "when Rouge lexing fails" do context "when Rouge lexing fails" do
before do before do
allow_next_instance_of(Rouge::Lexers::Ruby) do |instance| allow_next_instance_of(Rouge::Lexers::Ruby) do |instance|
......
...@@ -20,7 +20,7 @@ RSpec.describe Banzai::Pipeline::PreProcessPipeline do ...@@ -20,7 +20,7 @@ RSpec.describe Banzai::Pipeline::PreProcessPipeline do
aggregate_failures do aggregate_failures do
expect(result[:output]).not_to include "\xEF\xBB\xBF" expect(result[:output]).not_to include "\xEF\xBB\xBF"
expect(result[:output]).not_to include '---' expect(result[:output]).not_to include '---'
expect(result[:output]).to include "```yaml\nfoo: :foo_symbol\n" expect(result[:output]).to include "```yaml:frontmatter\nfoo: :foo_symbol\n"
expect(result[:output]).to include "> blockquote\n" expect(result[:output]).to include "> blockquote\n"
end end
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