mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: Preserve code blocks when quoting (#9632)
But, produce a non-block quote if a single code line is quoted.
This commit is contained in:
parent
e1d64cf896
commit
e7443ab5da
@ -136,6 +136,7 @@ export function selectedText() {
|
|||||||
for (let r = 0; r < selection.rangeCount; r++) {
|
for (let r = 0; r < selection.rangeCount; r++) {
|
||||||
const range = selection.getRangeAt(r);
|
const range = selection.getRangeAt(r);
|
||||||
const $ancestor = $(range.commonAncestorContainer);
|
const $ancestor = $(range.commonAncestorContainer);
|
||||||
|
const $codeBlockTest = $ancestor.parent("pre");
|
||||||
|
|
||||||
// ensure we never quote text in the post menu area
|
// ensure we never quote text in the post menu area
|
||||||
const $postMenuArea = $ancestor.find(".post-menu-area")[0];
|
const $postMenuArea = $ancestor.find(".post-menu-area")[0];
|
||||||
@ -143,8 +144,21 @@ export function selectedText() {
|
|||||||
range.setEndBefore($postMenuArea);
|
range.setEndBefore($postMenuArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($codeBlockTest.length) {
|
||||||
|
const $code = $("<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>");
|
||||||
|
$pre.append($code);
|
||||||
|
$div.append($pre);
|
||||||
|
} else {
|
||||||
|
$div.append($code);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
$div.append(range.cloneContents());
|
$div.append(range.cloneContents());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return toMarkdown($div.html());
|
return toMarkdown($div.html());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user