From fd15562939fa80af22c650eb5291e742e36a9d6c Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 8 Oct 2024 11:13:44 +0900 Subject: [PATCH] FIX: allows to use open attribute with details (#29116) Supporting `open` allows to show a `details` block open by default. Usage: ``` [details open] my visible content [/details] ``` --- .../lib/discourse-markdown/details.js | 6 +++++- .../spec/components/pretty_text_spec.rb | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/plugins/discourse-details/assets/javascripts/lib/discourse-markdown/details.js b/plugins/discourse-details/assets/javascripts/lib/discourse-markdown/details.js index 00bd6b670a5..028b9e9bcdf 100644 --- a/plugins/discourse-details/assets/javascripts/lib/discourse-markdown/details.js +++ b/plugins/discourse-details/assets/javascripts/lib/discourse-markdown/details.js @@ -2,9 +2,13 @@ const rule = { tag: "details", before(state, tagInfo) { const attrs = tagInfo.attrs; - state.push("bbcode_open", "details", 1); + const details = state.push("bbcode_open", "details", 1); state.push("bbcode_open", "summary", 1); + if (attrs.open === "") { + details.attrs = [["open", ""]]; + } + let token = state.push("text", "", 0); token.content = attrs["_default"] || ""; diff --git a/plugins/discourse-details/spec/components/pretty_text_spec.rb b/plugins/discourse-details/spec/components/pretty_text_spec.rb index cf1e87e6011..28b8b67cc1e 100644 --- a/plugins/discourse-details/spec/components/pretty_text_spec.rb +++ b/plugins/discourse-details/spec/components/pretty_text_spec.rb @@ -20,10 +20,25 @@ RSpec.describe PrettyText do HTML end + it "supports open attribute" do + cooked_html = PrettyText.cook <<~MARKDOWN + [details open] + bar + [/details] + MARKDOWN + + expect(cooked_html).to match_html <<~HTML +
+ +

bar

+
+ HTML + end + it "deletes elided content" do cooked_html = PrettyText.cook <<~MARKDOWN Hello World - +
42
MARKDOWN