Fix LaTeX formatting for AsciiDoc wiki
This commit is contained in:
parent
0b946a7bc6
commit
3ca9328443
8 changed files with 127 additions and 7 deletions
13
lib/banzai/filter/ascii_doc_post_processing_filter.rb
Normal file
13
lib/banzai/filter/ascii_doc_post_processing_filter.rb
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
module Banzai
|
||||||
|
module Filter
|
||||||
|
class AsciiDocPostProcessingFilter < HTML::Pipeline::Filter
|
||||||
|
def call
|
||||||
|
doc.search('[data-math-style]').each do |node|
|
||||||
|
node.set_attribute('class', 'code math js-render-math')
|
||||||
|
end
|
||||||
|
|
||||||
|
doc
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -31,6 +31,10 @@ module Banzai
|
||||||
# Allow span elements
|
# Allow span elements
|
||||||
whitelist[:elements].push('span')
|
whitelist[:elements].push('span')
|
||||||
|
|
||||||
|
# Allow data-math-style attribute in order to support LaTeX formatting
|
||||||
|
whitelist[:attributes]['code'] = %w(data-math-style)
|
||||||
|
whitelist[:attributes]['pre'] = %w(data-math-style)
|
||||||
|
|
||||||
# Allow html5 details/summary elements
|
# Allow html5 details/summary elements
|
||||||
whitelist[:elements].push('details')
|
whitelist[:elements].push('details')
|
||||||
whitelist[:elements].push('summary')
|
whitelist[:elements].push('summary')
|
||||||
|
|
14
lib/banzai/pipeline/ascii_doc_pipeline.rb
Normal file
14
lib/banzai/pipeline/ascii_doc_pipeline.rb
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
module Banzai
|
||||||
|
module Pipeline
|
||||||
|
class AsciiDocPipeline < BasePipeline
|
||||||
|
def self.filters
|
||||||
|
FilterArray[
|
||||||
|
Filter::SanitizationFilter,
|
||||||
|
Filter::ExternalLinkFilter,
|
||||||
|
Filter::PlantumlFilter,
|
||||||
|
Filter::AsciiDocPostProcessingFilter
|
||||||
|
]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -20,21 +20,20 @@ module Gitlab
|
||||||
backend: :gitlab_html5,
|
backend: :gitlab_html5,
|
||||||
attributes: DEFAULT_ADOC_ATTRS }
|
attributes: DEFAULT_ADOC_ATTRS }
|
||||||
|
|
||||||
context[:pipeline] = :markup
|
context[:pipeline] = :ascii_doc
|
||||||
|
|
||||||
plantuml_setup
|
plantuml_setup
|
||||||
|
|
||||||
html = ::Asciidoctor.convert(input, asciidoc_opts)
|
html = ::Asciidoctor.convert(input, asciidoc_opts)
|
||||||
html = Banzai.render(html, context)
|
html = Banzai.render(html, context)
|
||||||
|
|
||||||
html.html_safe
|
html.html_safe
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.plantuml_setup
|
def self.plantuml_setup
|
||||||
Asciidoctor::PlantUml.configure do |conf|
|
Asciidoctor::PlantUml.configure do |conf|
|
||||||
conf.url = ApplicationSetting.current.plantuml_url
|
conf.url = current_application_settings.plantuml_url
|
||||||
conf.svg_enable = ApplicationSetting.current.plantuml_enabled
|
conf.svg_enable = current_application_settings.plantuml_enabled
|
||||||
conf.png_enable = ApplicationSetting.current.plantuml_enabled
|
conf.png_enable = current_application_settings.plantuml_enabled
|
||||||
conf.txt_enable = false
|
conf.txt_enable = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -47,13 +46,13 @@ module Gitlab
|
||||||
def stem(node)
|
def stem(node)
|
||||||
return super unless node.style.to_sym == :latexmath
|
return super unless node.style.to_sym == :latexmath
|
||||||
|
|
||||||
%(<pre#{id_attribute(node)} class="code math js-render-math #{node.role}" data-math-style="display"><code>#{node.content}</code></pre>)
|
%(<pre#{id_attribute(node)} data-math-style="display"><code>#{node.content}</code></pre>)
|
||||||
end
|
end
|
||||||
|
|
||||||
def inline_quoted(node)
|
def inline_quoted(node)
|
||||||
return super unless node.type.to_sym == :latexmath
|
return super unless node.type.to_sym == :latexmath
|
||||||
|
|
||||||
%(<code#{id_attribute(node)} class="code math js-render-math #{node.role}" data-math-style="inline">#{node.text}</code>)
|
%(<code#{id_attribute(node)} data-math-style="inline">#{node.text}</code>)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -28,6 +28,40 @@ feature 'Projects > Wiki > User creates wiki page', js: true, feature: true do
|
||||||
expect(page).to have_content("Last edited by #{user.name}")
|
expect(page).to have_content("Last edited by #{user.name}")
|
||||||
expect(page).to have_content('My awesome wiki!')
|
expect(page).to have_content('My awesome wiki!')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario 'creates ASCII wiki with LaTeX blocks' do
|
||||||
|
stub_application_setting(plantuml_url: 'http://localhost', plantuml_enabled: true)
|
||||||
|
|
||||||
|
ascii_content = <<~MD
|
||||||
|
:stem: latexmath
|
||||||
|
|
||||||
|
[stem]
|
||||||
|
++++
|
||||||
|
\sqrt{4} = 2
|
||||||
|
++++
|
||||||
|
|
||||||
|
another part
|
||||||
|
|
||||||
|
[latexmath]
|
||||||
|
++++
|
||||||
|
\beta_x \gamma
|
||||||
|
++++
|
||||||
|
|
||||||
|
stem:[2+2] is 4
|
||||||
|
MD
|
||||||
|
|
||||||
|
find('#wiki_format option[value=asciidoc]').select_option
|
||||||
|
fill_in :wiki_content, with: ascii_content
|
||||||
|
|
||||||
|
page.within '.wiki-form' do
|
||||||
|
click_button 'Create page'
|
||||||
|
end
|
||||||
|
|
||||||
|
page.within '.wiki' do
|
||||||
|
expect(page).to have_selector('.katex', count: 3)
|
||||||
|
expect(page).to have_content('2+2 is 4')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when wiki is not empty' do
|
context 'when wiki is not empty' do
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe Banzai::Filter::AsciiDocPostProcessingFilter, lib: true do
|
||||||
|
include FilterSpecHelper
|
||||||
|
|
||||||
|
it "adds class for elements with data-math-style" do
|
||||||
|
result = filter('<pre data-math-style="inline">some code</pre><div data-math>and</div>').to_html
|
||||||
|
expect(result).to eq('<pre data-math-style="inline" class="code math js-render-math">some code</pre><div data-math>and</div>')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "keeps content when no data-math-style found" do
|
||||||
|
result = filter('<pre>some code</pre><div data-math>and</div>').to_html
|
||||||
|
expect(result).to eq('<pre>some code</pre><div data-math>and</div>')
|
||||||
|
end
|
||||||
|
end
|
|
@ -97,6 +97,22 @@ describe Banzai::Filter::SanitizationFilter, lib: true do
|
||||||
expect(filter(act).to_html).to eq exp
|
expect(filter(act).to_html).to eq exp
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'allows `data-math-style` attribute on `code` and `pre` elements' do
|
||||||
|
html = <<-HTML
|
||||||
|
<pre class="code" data-math-style="inline">something</pre>
|
||||||
|
<code class="code" data-math-style="inline">something</code>
|
||||||
|
<div class="code" data-math-style="inline">something</div>
|
||||||
|
HTML
|
||||||
|
|
||||||
|
output = <<-HTML
|
||||||
|
<pre data-math-style="inline">something</pre>
|
||||||
|
<code data-math-style="inline">something</code>
|
||||||
|
<div>something</div>
|
||||||
|
HTML
|
||||||
|
|
||||||
|
expect(filter(html).to_html).to eq(output)
|
||||||
|
end
|
||||||
|
|
||||||
it 'removes `rel` attribute from `a` elements' do
|
it 'removes `rel` attribute from `a` elements' do
|
||||||
act = %q{<a href="#" rel="nofollow">Link</a>}
|
act = %q{<a href="#" rel="nofollow">Link</a>}
|
||||||
exp = %q{<a href="#">Link</a>}
|
exp = %q{<a href="#">Link</a>}
|
||||||
|
|
|
@ -70,6 +70,31 @@ module Gitlab
|
||||||
expect(output).to include('rel="nofollow noreferrer noopener"')
|
expect(output).to include('rel="nofollow noreferrer noopener"')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'LaTex code' do
|
||||||
|
it 'adds class js-render-math to the output' do
|
||||||
|
input = <<~MD
|
||||||
|
:stem: latexmath
|
||||||
|
|
||||||
|
[stem]
|
||||||
|
++++
|
||||||
|
\sqrt{4} = 2
|
||||||
|
++++
|
||||||
|
|
||||||
|
another part
|
||||||
|
|
||||||
|
[latexmath]
|
||||||
|
++++
|
||||||
|
\beta_x \gamma
|
||||||
|
++++
|
||||||
|
|
||||||
|
stem:[2+2] is 4
|
||||||
|
MD
|
||||||
|
|
||||||
|
expect(render(input, context)).to include('<pre data-math-style="display" class="code math js-render-math"><code>eta_x gamma</code></pre>')
|
||||||
|
expect(render(input, context)).to include('<p><code data-math-style="inline" class="code math js-render-math">2+2</code> is 4</p>')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(*args)
|
def render(*args)
|
||||||
|
|
Loading…
Reference in a new issue