From e7443ab5dac29c2b48924b0545be47e500a375b2 Mon Sep 17 00:00:00 2001 From: Kane York Date: Tue, 5 May 2020 12:12:22 -0700 Subject: [PATCH] FIX: Preserve code blocks when quoting (#9632) But, produce a non-block quote if a single code line is quoted. --- .../javascripts/discourse/app/lib/utilities.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.js index b27ae52a7f7..439b521e38b 100644 --- a/app/assets/javascripts/discourse/app/lib/utilities.js +++ b/app/assets/javascripts/discourse/app/lib/utilities.js @@ -136,6 +136,7 @@ export function selectedText() { for (let r = 0; r < selection.rangeCount; r++) { const range = selection.getRangeAt(r); const $ancestor = $(range.commonAncestorContainer); + const $codeBlockTest = $ancestor.parent("pre"); // ensure we never quote text in the post menu area const $postMenuArea = $ancestor.find(".post-menu-area")[0]; @@ -143,7 +144,20 @@ export function selectedText() { range.setEndBefore($postMenuArea); } - $div.append(range.cloneContents()); + if ($codeBlockTest.length) { + const $code = $(""); + $code.append(range.cloneContents()); + // Even though this was a code block, produce a non-block quote if it's a single line. + if (/\n/.test($code.text())) { + const $pre = $("
");
+        $pre.append($code);
+        $div.append($pre);
+      } else {
+        $div.append($code);
+      }
+    } else {
+      $div.append(range.cloneContents());
+    }
   }
 
   return toMarkdown($div.html());