mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 23:07:28 +08:00
FIX: fenced code blocks not hoisted correctly
also fixes unhoisting logic
This commit is contained in:
@ -189,14 +189,15 @@ function hoistCodeBlocksAndSpans(text) {
|
|||||||
// /!\ the order is important /!\
|
// /!\ the order is important /!\
|
||||||
|
|
||||||
// <pre>...</pre> code blocks
|
// <pre>...</pre> code blocks
|
||||||
text = text.replace(/(^\n*|\n\n)<pre>([\s\S]*?)<\/pre>/ig, function(_, before, content) {
|
text = text.replace(/(\s|^)<pre>([\s\S]*?)<\/pre>/ig, function(_, before, content) {
|
||||||
var hash = md5(content);
|
var hash = md5(content);
|
||||||
hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
|
hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
|
||||||
return before + "<pre>" + hash + "</pre>";
|
return before + "<pre>" + hash + "</pre>";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// fenced code blocks (AKA GitHub code blocks)
|
// 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) {
|
text = text.replace(/(^\n*|\n)```([a-z0-9\-]*)\n([\s\S]*?)\n```/g, function(_, before, language, content) {
|
||||||
var hash = md5(content);
|
var hash = md5(content);
|
||||||
hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
|
hoisted[hash] = escape(showBackslashEscapedCharacters(removeEmptyLines(content)));
|
||||||
return before + "```" + language + "\n" + hash + "\n```";
|
return before + "```" + language + "\n" + hash + "\n```";
|
||||||
@ -277,11 +278,19 @@ Discourse.Dialect = {
|
|||||||
// If we hoisted out anything, put it back
|
// If we hoisted out anything, put it back
|
||||||
var keys = Object.keys(hoisted);
|
var keys = Object.keys(hoisted);
|
||||||
if (keys.length) {
|
if (keys.length) {
|
||||||
keys.forEach(function(key) {
|
var found = true;
|
||||||
|
|
||||||
|
var unhoist = function(key) {
|
||||||
result = result.replace(new RegExp(key, "g"), function() {
|
result = result.replace(new RegExp(key, "g"), function() {
|
||||||
|
found = true;
|
||||||
return hoisted[key];
|
return hoisted[key];
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
|
|
||||||
|
while(found) {
|
||||||
|
found = false;
|
||||||
|
keys.forEach(unhoist);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.trim();
|
return result.trim();
|
||||||
|
@ -322,6 +322,12 @@ describe PrettyText do
|
|||||||
expect(PrettyText.cook("```cpp\ncpp\n```")).to match_html("<p></p><pre><code class='lang-cpp'>cpp</code></pre>")
|
expect(PrettyText.cook("```cpp\ncpp\n```")).to match_html("<p></p><pre><code class='lang-cpp'>cpp</code></pre>")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'indents code correctly' do
|
||||||
|
code = "X\n```\n\n #\n x\n```"
|
||||||
|
cooked = PrettyText.cook(code)
|
||||||
|
expect(cooked).to match_html("<p>X<br></p>\n\n<p></p><pre><code class=\"lang-auto\"> #\n x</code></pre>")
|
||||||
|
end
|
||||||
|
|
||||||
it 'can substitute s3 cdn correctly' do
|
it 'can substitute s3 cdn correctly' do
|
||||||
SiteSetting.enable_s3_uploads = true
|
SiteSetting.enable_s3_uploads = true
|
||||||
SiteSetting.s3_access_key_id = "XXX"
|
SiteSetting.s3_access_key_id = "XXX"
|
||||||
|
Reference in New Issue
Block a user