diff --git a/lib/github/markup/asciidoctor_converter.rb b/lib/github/markup/asciidoctor_converter.rb new file mode 100644 index 00000000..8d6fed0b --- /dev/null +++ b/lib/github/markup/asciidoctor_converter.rb @@ -0,0 +1,29 @@ +require "asciidoctor" +require "asciidoctor/converter/html5" + +module GitHub + module Markup + # HTML5 converter that renders admonitions (NOTE, TIP, IMPORTANT, WARNING, + # CAUTION) with the same markup as Markdown alerts, so they pick up the + # styles GitHub already ships for those. Every other node is converted by + # the built-in Asciidoctor HTML5 converter. + # + # The class is passed to Asciidoctor through the :converter option rather + # than registered for the html5 backend, so other Asciidoctor conversions + # in the same process are not affected. + class AsciidoctorConverter < ::Asciidoctor::Converter::Html5Converter + def convert_admonition(node) + id_attr = node.id ? %( id="#{node.id}") : '' + name = node.attr 'name' + role = node.role ? " #{node.role}" : '' + # Like a Markdown alert with a custom title, the block title replaces the label. + title = node.title? ? node.title : (node.attr 'textlabel') + content = node.content_model == :compound ? node.content : %(

#{node.content}

) + %( +

#{title}

+#{content} +) + end + end + end +end diff --git a/lib/github/markups.rb b/lib/github/markups.rb index 4b2f8e0e..f4c3e9fc 100644 --- a/lib/github/markups.rb +++ b/lib/github/markups.rb @@ -43,8 +43,9 @@ else attributes['outfilesuffix'] = '.adoc' end + require "github/markup/asciidoctor_converter" Asciidoctor::Compliance.unique_id_start_index = 1 - Asciidoctor.convert(content, :safe => :secure, :attributes => attributes) + Asciidoctor.convert(content, :safe => :secure, :attributes => attributes, :converter => GitHub::Markup::AsciidoctorConverter) end GitHub::Markup.command( diff --git a/test/markup_test.rb b/test/markup_test.rb index baa1dec2..ab4f239d 100644 --- a/test/markup_test.rb +++ b/test/markup_test.rb @@ -107,6 +107,30 @@ def test_rendering_by_symbol assert_match result, GitHub::Markup.render_s(GitHub::Markups::MARKUP_ASCIIDOC, markup).split.join end + def test_asciidoc_admonitions_render_as_markdown_alerts + html = GitHub::Markup.render('README.adoc', <<~ADOC) + NOTE: A *simple* note. + + [[tip-id]] + .A _custom_ title + [TIP.custom-role] + ==== + A compound tip. + ==== + ADOC + doc = Nokogiri::HTML.fragment(html) + note, tip = doc.css('div.markdown-alert').to_a + assert_equal 'markdown-alert markdown-alert-note', note['class'] + assert_equal 'Note', note.at_css('> p.markdown-alert-title').text + assert_equal 'A simple note.', note.css('> p')[1].inner_html + assert_equal 'tip-id', tip['id'] + assert_equal 'markdown-alert markdown-alert-tip custom-role', tip['class'] + assert_equal 'A custom title', tip.at_css('> p.markdown-alert-title').inner_html + assert_empty tip.css('div.title') + assert_equal 'A compound tip.', tip.at_css('> div.paragraph > p').text + assert_empty doc.css('.admonitionblock') + end + def test_raises_error_if_command_exits_non_zero GitHub::Markup.command(:doesntmatter, 'test/fixtures/fail.sh', /fail/, ['Java'], 'fail') assert GitHub::Markup.can_render?('README.java', 'stop swallowing errors') diff --git a/test/markups/README.admonitions.adoc b/test/markups/README.admonitions.adoc new file mode 100644 index 00000000..f584648e --- /dev/null +++ b/test/markups/README.admonitions.adoc @@ -0,0 +1,21 @@ += Admonitions + +NOTE: Useful information that users should know, even when *skimming* content. + +TIP: Helpful advice for doing things better or more easily. + +[IMPORTANT] +Key information users need to know to achieve their goal. + +[[warning-id]] +.Before you continue +[WARNING] +==== +Urgent info that needs immediate user attention to avoid problems. + +* Back up your data +* Check your configuration +==== + +[CAUTION.custom-role] +Advises about risks or negative outcomes of certain actions. diff --git a/test/markups/README.admonitions.adoc.html b/test/markups/README.admonitions.adoc.html new file mode 100644 index 00000000..02581d86 --- /dev/null +++ b/test/markups/README.admonitions.adoc.html @@ -0,0 +1,33 @@ +

Admonitions

+
+

Note

+

Useful information that users should know, even when skimming content.

+
+
+

Tip

+

Helpful advice for doing things better or more easily.

+
+
+

Important

+

Key information users need to know to achieve their goal.

+
+
+

Before you continue

+
+

Urgent info that needs immediate user attention to avoid problems.

+
+
+
    +
  • +

    Back up your data

    +
  • +
  • +

    Check your configuration

    +
  • +
+
+
+
+

Caution

+

Advises about risks or negative outcomes of certain actions.

+
\ No newline at end of file diff --git a/test/markups/README.asciidoc.html b/test/markups/README.asciidoc.html index 4e28204e..6c30ba84 100644 --- a/test/markups/README.asciidoc.html +++ b/test/markups/README.asciidoc.html @@ -24,16 +24,8 @@

First Section

Another Section

- - - - - -
-
Note
-
-Here is some source code. -
+

Note

+

Here is some source code.