From a0cd54750cca2866dfa978858c21c1a88a09899b Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 26 Apr 2018 15:18:22 +1000 Subject: [PATCH] FIX: inline [code] not handled properly The text a [code]test[/code] Would eat up the `test` text cause translation from inline to block for replace rule was not properly handled --- .../engines/discourse-markdown/bbcode-block.js.es6 | 9 ++++++++- spec/components/pretty_text_spec.rb | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-block.js.es6 b/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-block.js.es6 index ce9d468d486..b35d716189e 100644 --- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-block.js.es6 +++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/bbcode-block.js.es6 @@ -242,7 +242,14 @@ function applyBBCode(state, startLine, endLine, silent, md) { state.lineMax = nextLine; if (rule.replace) { - let content = state.src.slice(state.bMarks[startLine+1], state.eMarks[nextLine-1]); + let content; + + if (startLine === nextLine) { + content = state.src.slice(start + info.length, closeTag.start); + } else { + content = state.src.slice(state.bMarks[startLine+1], state.eMarks[nextLine-1]); + } + if (!rule.replace.call(this, state, info, content)) { return false; } diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 702b4582d76..07cb22915b3 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -980,6 +980,14 @@ HTML expect(PrettyText.cook("a[i]b[/i]c")).to eq('

abc

') 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
") + + # this is fine + expect(PrettyText.cook("a\na[code]code[/code]")).to eq("

a
\nacode

") + end + it "can onebox local topics" do op = Fabricate(:post) reply = Fabricate(:post, topic_id: op.topic_id)