mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:29:30 +08:00
FIX: remove empty lines but keep whitespace on first line in code blocks
This commit is contained in:
parent
8fcbea0c2c
commit
aa41a9ce70
|
@ -167,6 +167,11 @@ function outdent(t) {
|
|||
return t.replace(/^([ ]{4}|\t)/gm, "");
|
||||
}
|
||||
|
||||
function removeEmptyLines(t) {
|
||||
return t.replace(/^\n+/, "")
|
||||
.replace(/\s+$/, "");
|
||||
}
|
||||
|
||||
function hideBackslashEscapedCharacters(t) {
|
||||
return t.replace(/\\\\/g, "\u1E800")
|
||||
.replace(/\\`/g, "\u1E8001");
|
||||
|
@ -186,14 +191,14 @@ function hoistCodeBlocksAndSpans(text) {
|
|||
// <pre>...</pre> code blocks
|
||||
text = text.replace(/(^\n*|\n\n)<pre>([\s\S]*?)<\/pre>/ig, function(_, before, content) {
|
||||
var hash = md5(content);
|
||||
hoisted[hash] = escape(showBackslashEscapedCharacters(content.trim()));
|
||||
hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
|
||||
return before + "<pre>" + hash + "</pre>";
|
||||
});
|
||||
|
||||
// fenced code blocks (AKA GitHub code blocks)
|
||||
text = text.replace(/(^\n*|\n\n)```([a-z0-9\-]*)\n([\s\S]*?)\n```/g, function(_, before, language, content) {
|
||||
var hash = md5(content);
|
||||
hoisted[hash] = escape(showBackslashEscapedCharacters(content.trim()));
|
||||
hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
|
||||
return before + "```" + language + "\n" + hash + "\n```";
|
||||
});
|
||||
|
||||
|
@ -209,9 +214,7 @@ function hoistCodeBlocksAndSpans(text) {
|
|||
}
|
||||
// we can safely hoist the code block
|
||||
var hash = md5(content);
|
||||
// only remove trailing whitespace
|
||||
content = content.replace(/\s+$/, "");
|
||||
hoisted[hash] = escape(outdent(showBackslashEscapedCharacters(content)));
|
||||
hoisted[hash] = escape(outdent(showBackslashEscapedCharacters(removeEmptyLines(content))));
|
||||
return before + " " + hash + "\n";
|
||||
});
|
||||
|
||||
|
|
|
@ -529,6 +529,6 @@ test("censoring", function() {
|
|||
|
||||
test("code blocks/spans hoisting", function() {
|
||||
cooked("```\n\n some code\n```",
|
||||
"<p><pre><code class=\"lang-auto\">some code</code></pre></p>",
|
||||
"<p><pre><code class=\"lang-auto\"> some code</code></pre></p>",
|
||||
"it works when nesting standard markdown code blocks within a fenced code block");
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user