From 9543c59f3df04b4d63c2d808d49c8352f78988f0 Mon Sep 17 00:00:00 2001 From: Nick Adams Date: Wed, 11 Feb 2026 10:19:11 -0500 Subject: [PATCH] Add details/summary support for collapsible blocks Add support for the HTML5 details tag via Asciidoctor collapsible option on example blocks. The block title becomes the summary text. --- htmlbook-autogen/block_example.html.erb | 11 +++++-- htmlbook/block_example.html.erb | 11 +++++-- spec/htmlbook_spec.rb | 39 +++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/htmlbook-autogen/block_example.html.erb b/htmlbook-autogen/block_example.html.erb index b29b4f0..df4684c 100644 --- a/htmlbook-autogen/block_example.html.erb +++ b/htmlbook-autogen/block_example.html.erb @@ -1,4 +1,9 @@ -<%#encoding:UTF-8%> data-type="example"<%= attr?('role') ? %( class="#{attr 'role'}") : nil %>> +<%#encoding:UTF-8%><% if option? 'collapsible' %><%= attr?('role') ? %( class="#{attr 'role'}") : nil %>> +<%= title %> +
+<%= content %> +
+<% else %> data-type="example"<%= attr?('role') ? %( class="#{attr 'role'}") : nil %>>
<%= title %>
-<%= content -%> +<%= content +%><% end %> diff --git a/htmlbook/block_example.html.erb b/htmlbook/block_example.html.erb index b29b4f0..df4684c 100644 --- a/htmlbook/block_example.html.erb +++ b/htmlbook/block_example.html.erb @@ -1,4 +1,9 @@ -<%#encoding:UTF-8%> data-type="example"<%= attr?('role') ? %( class="#{attr 'role'}") : nil %>> +<%#encoding:UTF-8%><% if option? 'collapsible' %><%= attr?('role') ? %( class="#{attr 'role'}") : nil %>> +<%= title %> +
+<%= content %> +
+<% else %> data-type="example"<%= attr?('role') ? %( class="#{attr 'role'}") : nil %>>
<%= title %>
-<%= content -%> +<%= content +%><% end %> diff --git a/spec/htmlbook_spec.rb b/spec/htmlbook_spec.rb index c3a2327..5e1cdd1 100755 --- a/spec/htmlbook_spec.rb +++ b/spec/htmlbook_spec.rb @@ -203,6 +203,45 @@ expect(html.xpath("//div[@data-type='example'][1]/pre[@data-type='programlisting']/@data-code-language").text).to eq("php") end + # Tests collapsible block (details tag) support + it "should convert collapsible blocks to details/summary elements" do + html = Nokogiri::HTML(convert(" +.Details +[%collapsible] +==== +This would be the show and hide! +==== +")) + expect(html.xpath("//details/summary[@class='title']").text).to eq("Details") + expect(html.xpath("//details/div[@class='content']/p").text).to eq("This would be the show and hide!") + end + + it "should convert collapsible blocks with an id" do + html = Nokogiri::HTML(convert(" +[[my-details]] +.Click to expand +[%collapsible] +==== +Hidden content here. +==== +")) + expect(html.xpath("//details/@id").text).to eq("my-details") + expect(html.xpath("//details/summary[@class='title']").text).to eq("Click to expand") + expect(html.xpath("//details/div[@class='content']/p").text).to eq("Hidden content here.") + end + + it "should convert collapsible blocks with a role attribute" do + html = Nokogiri::HTML(convert(" +.Summary text +[%collapsible,role='special'] +==== +Content with a role. +==== +")) + expect(html.xpath("//details/@class").text).to eq("special") + expect(html.xpath("//details/summary[@class='title']").text).to eq("Summary text") + end + it "should convert line numbering attribute in informal code listing" do html = Nokogiri::HTML(convert(" [source, php, linenums]