Add admonition block for HTML5-modified

This commit is contained in:
Gabriel Arazas 2023-11-24 11:23:06 +08:00
parent 74c48a345f
commit 153e110489
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
2 changed files with 72 additions and 0 deletions

View File

@ -30,6 +30,42 @@ module Asciidoctor::Foodogsquared::Converters
html.to_html
end
def convert_admonition(node)
html = Nokogiri::XML::DocumentFragment.parse '<aside></aside>'
aside = html.first_element_child
aside['data-admonition-type'] = node.attr 'name'
add_common_attributes node, aside
if node.document.attr? 'icons', 'image'
html.document.create_element 'svg' do |svg|
html.document.create_element 'use' do |use|
use['href'] = "#{node.image_uri "#{node.attr 'name'}.svg", 'iconsdir'}##{node.attr 'name'}"
use['alt'] = node.attr 'textlabel'
svg.add_child use
end
aside.add_child svg
end
else
html.document.create_element 'div' do |div|
div.add_class 'admonition-label'
div.content = node.attr 'textlabel'
aside.add_child div
end
end
if node.title?
html.document.create_element 'strong', class: 'title' do |strong|
strong.add_class 'title'
strong.content = node.captioned_title
aside.add_child strong
end
end
aside.inner_html += node.content
html.to_html(indent: 2)
end
def add_common_attributes(node, html)
html['id'] = node.id if node.id

View File

@ -29,4 +29,40 @@ describe Asciidoctor::Foodogsquared::Converters::HTML5Modified do
actual = (Asciidoctor.convert input).chomp
(expect actual).to eq expected.chomp
end
it 'should have a more semantic version of the admonitions' do
input = <<~INPUT
[WARNING]
====
Hello there
====
INPUT
expected = <<~HTML
<aside data-admonition-type="warning"><div class="admonition-label">Warning</div>
<p>Hello there</p></aside>
HTML
actual = (Asciidoctor.convert input).chomp
(expect actual).to eq expected.chomp
end
it 'should have a more semantic version of the admonitions even with typical Asciidoc elements' do
input = <<~INPUT
[#warning-id.big.reversed]
.A warning
[WARNING]
====
Hello there
====
INPUT
expected = <<~HTML
<aside data-admonition-type="warning" id="warning-id" class="big reversed"><div class="admonition-label">Warning</div>
<strong class="title">A warning</strong><p>Hello there</p></aside>
HTML
actual = (Asciidoctor.convert input).chomp
(expect actual).to eq expected.chomp
end
end