From f91426a7faaffc4bcd868e09d78b7af1e60ec1ea Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Tue, 20 Sep 2022 01:50:22 +0200 Subject: [PATCH] FIX: Empty inline BBCodes were broken (#18276) Upgrading to Markdown.it v13 broke empty inline BBCodes. This works around the problem by adding an empty token before a closing token if the previous token was a BBCode token. It also removes the unused `jump` attribute which was removed in Markdown.it v12.3 --- .../pretty-text/engines/discourse-markdown/bbcode-inline.js | 5 ++++- spec/lib/pretty_text_spec.rb | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-inline.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-inline.js index 8e8bbeba31e..27a1a1d4284 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-inline.js +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-inline.js @@ -61,6 +61,10 @@ function tokenizeBBCode(state, silent, ruler) { } else { tagInfo.rule = rule; + if (tagInfo.closing && state.tokens.at(-1)?.meta === "bbcode") { + state.push("text", "", 0); + } + let token = state.push("text", "", 0); token.content = state.src.slice(pos, pos + tagInfo.length); token.meta = "bbcode"; @@ -73,7 +77,6 @@ function tokenizeBBCode(state, silent, ruler) { token: state.tokens.length - 1, level: state.level, end: -1, - jump: 0, }); state.pos = pos + tagInfo.length; diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb index 4974ead3271..face37f892b 100644 --- a/spec/lib/pretty_text_spec.rb +++ b/spec/lib/pretty_text_spec.rb @@ -1714,6 +1714,10 @@ HTML expect(PrettyText.cook("a[i]b[/i]c")).to eq('

abc

') end + it "supports empty inline BBCode" do + expect(PrettyText.cook("a[b][/b]c")).to eq('

ac

') + end + it "can handle bbcode after a newline" do # this is not 100% ideal cause we get an extra p here, but this is pretty rare expect(PrettyText.cook("a\n[code]code[/code]")).to eq("

a

\n
code
")