From cb9c603b6b7e318d2d84ccc80ef60c2b88649e02 Mon Sep 17 00:00:00 2001
From: kerryliu
Date: Mon, 31 Aug 2015 10:01:36 -0700
Subject: [PATCH] workaround for code block being parsed before quote block.
---
app/assets/javascripts/discourse/dialects/code_dialect.js | 1 +
app/assets/javascripts/discourse/dialects/dialect.js | 6 ++++++
test/javascripts/lib/bbcode-test.js.es6 | 7 +++++++
3 files changed, 14 insertions(+)
diff --git a/app/assets/javascripts/discourse/dialects/code_dialect.js b/app/assets/javascripts/discourse/dialects/code_dialect.js
index e7322a4abeb..2594cd8316d 100644
--- a/app/assets/javascripts/discourse/dialects/code_dialect.js
+++ b/app/assets/javascripts/discourse/dialects/code_dialect.js
@@ -33,6 +33,7 @@ function codeFlattenBlocks(blocks) {
Discourse.Dialect.replaceBlock({
start: /^`{3}([^\n\[\]]+)?\n?([\s\S]*)?/gm,
stop: /^```$/gm,
+ withoutLeading: /\[quote/gm, //if leading text contains a quote this should not match
emitter: function(blockContents, matches) {
var klass = Discourse.SiteSettings.default_code_lang;
diff --git a/app/assets/javascripts/discourse/dialects/dialect.js b/app/assets/javascripts/discourse/dialects/dialect.js
index e80e5e96710..1614c1c0f19 100644
--- a/app/assets/javascripts/discourse/dialects/dialect.js
+++ b/app/assets/javascripts/discourse/dialects/dialect.js
@@ -501,6 +501,12 @@ Discourse.Dialect = {
var pos = args.start.lastIndex - match[0].length,
leading = block.slice(0, pos),
trailing = match[2] ? match[2].replace(/^\n*/, "") : "";
+
+ if(args.withoutLeading && args.withoutLeading.test(leading)) {
+ //The other leading block should be processed first! eg a code block wrapped around a code block.
+ return;
+ }
+
// just give up if there's no stop tag in this or any next block
args.stop.lastIndex = block.length - trailing.length;
if (!args.stop.exec(block) && lastChance()) { return; }
diff --git a/test/javascripts/lib/bbcode-test.js.es6 b/test/javascripts/lib/bbcode-test.js.es6
index 575ad7e7558..1c1b4436d87 100644
--- a/test/javascripts/lib/bbcode-test.js.es6
+++ b/test/javascripts/lib/bbcode-test.js.es6
@@ -160,6 +160,13 @@ test("quote formatting", function() {
"",
"handles mismatched nested quote tags");
+
+ formatQ("[quote=\"Alice, post:1, topic:1\"]\n```javascript\nvar foo ='foo';\nvar bar = 'bar';\n```\n[/quote]",
+ "
",
+ "quotes can have code blocks without leading newline");
+ formatQ("[quote=\"Alice, post:1, topic:1\"]\n\n```javascript\nvar foo ='foo';\nvar bar = 'bar';\n```\n[/quote]",
+ "",
+ "quotes can have code blocks with leading newline");
});
test("quotes with trailing formatting", function() {