diff --git a/frontend_tests/node_tests/echo.js b/frontend_tests/node_tests/echo.js index e105189eff..a6517a5cbf 100644 --- a/frontend_tests/node_tests/echo.js +++ b/frontend_tests/node_tests/echo.js @@ -38,6 +38,7 @@ add_dependencies({ hashchange: 'js/hashchange', fenced_code: 'js/fenced_code.js', katex: 'node_modules/katex/dist/katex.min.js', + util: 'js/util.js', }); var doc = ""; @@ -184,6 +185,8 @@ var bugdown_data = JSON.parse(fs.readFileSync(path.join(__dirname, '../../zerver {input: '__hello__', expected: '

__hello__

'}, {input: '\n```\nfenced code\n```\n\nand then after\n', expected: '
fenced code\n
\n\n\n

and then after

'}, + {input: '\n```\n fenced code trailing whitespace \n```\n\nand then after\n', + expected: '
    fenced code trailing whitespace\n
\n\n\n

and then after

'}, {input: '* a\n* list \n* here', expected: ''}, {input: 'Some text first\n* a\n* list \n* here\n\nand then after', diff --git a/static/js/fenced_code.js b/static/js/fenced_code.js index 3daac4929c..fbbab9e136 100644 --- a/static/js/fenced_code.js +++ b/static/js/fenced_code.js @@ -127,7 +127,7 @@ exports.process_fenced_code = function (content) { if (line === fence) { this.done(); } else { - lines.push(line); + lines.push(util.rtrim(line)); } }, diff --git a/zerver/fixtures/bugdown-data.json b/zerver/fixtures/bugdown-data.json index 699ed885ca..7709cfd141 100644 --- a/zerver/fixtures/bugdown-data.json +++ b/zerver/fixtures/bugdown-data.json @@ -18,6 +18,12 @@ "expected_output": "

Hamlet once said

\n
def func():\n    x = 1\n\n    y = 2\n\n    z = 3\n
\n\n\n

And all was good.

", "bugdown_matches_marked": true }, + { + "name": "codeblock_trailing_whitespace", + "input": "Hamlet once said\n~~~~\ndef func():\n x = 1\n\n y = 2\t\t\n\n z = 3 \n~~~~\nAnd all was good.", + "expected_output": "

Hamlet once said

\n
def func():\n    x = 1\n\n    y = 2\n\n    z = 3\n
\n\n\n

And all was good.

", + "bugdown_matches_marked": true + }, { "name": "codeblock_backticks", "input": "\n```\nfenced code\n```\n\n```inline code```\n", diff --git a/zerver/lib/bugdown/fenced_code.py b/zerver/lib/bugdown/fenced_code.py index 830ce0dd11..620044bbd5 100644 --- a/zerver/lib/bugdown/fenced_code.py +++ b/zerver/lib/bugdown/fenced_code.py @@ -37,6 +37,21 @@ Include tilde's in a code block and wrap with blank lines: ~~~~ +Removes trailing whitespace from code blocks that cause horizontal scrolling + >>> import markdown + >>> text = ''' + ... A paragraph before a fenced code block: + ... + ... ~~~ + ... Fenced code block \t\t\t\t\t\t\t + ... ~~~ + ... ''' + >>> html = markdown.markdown(text, extensions=['fenced_code']) + >>> print html +

A paragraph before a fenced code block:

+
Fenced code block
+    
+ Language tags: >>> text = ''' @@ -92,7 +107,7 @@ FENCE_RE = re.compile(u""" """, re.VERBOSE) -CODE_WRAP = u'
%s
' +CODE_WRAP = u'
%s\n
' LANG_TAG = u' class="%s"' class FencedCodeExtension(markdown.Extension): @@ -193,7 +208,7 @@ class FencedBlockPreprocessor(markdown.preprocessors.Preprocessor): if line.rstrip() == self.fence: self.done() else: - self.lines.append(line) + self.lines.append(line.rstrip()) def done(self): # type: () -> None