From ffb816391c3b5ea5286463c57bb6ec5de40769ff Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Mon, 27 Jan 2014 17:21:48 -0500 Subject: [PATCH] Use our own fenced code preprocessor, fixing output mismatches (imported from commit 3d4abb1043349e30adf265342ee7c946a99b3ce2) --- static/js/echo.js | 16 ++++++++++++++-- zerver/fixtures/bugdown-data.json | 12 ++++++------ zerver/tests/frontend/node/echo.js | 5 +++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/static/js/echo.js b/static/js/echo.js index 64d358f159..fd02cbd0fb 100644 --- a/static/js/echo.js +++ b/static/js/echo.js @@ -31,7 +31,8 @@ exports.contains_bugdown = function contains_bugdown(content) { }; exports.apply_markdown = function apply_markdown(content) { - return marked(content).trim(); + // Our python-markdown processor appends two \n\n to input + return marked(content + '\n\n').trim(); }; function resend_message(message, row) { @@ -421,6 +422,10 @@ $(function () { return '
\n'; }; + function preprocess_code_blocks(src) { + return fenced_code.process_fenced_code(src); + } + // Disable ordered lists // We used GFM + tables, so replace the list start regex for that ruleset // We remove the |[\d+]\. that matches the numbering in a numbered list @@ -439,6 +444,12 @@ $(function () { exports.set_realm_filters(page_params.realm_filters); + // Tell our fenced code preprocessor how to insert arbitrary + // HTML into the output. This generated HTML is safe to not escape + fenced_code.set_stash_func(function (html) { + return marked.stashHtml(html, true); + }); + marked.setOptions({ gfm: true, tables: true, @@ -451,7 +462,8 @@ $(function () { emojiHandler: handleEmoji, userMentionHandler: handleUserMentions, realmFilterHandler: handleRealmFilter, - renderer: r + renderer: r, + preprocessors: [preprocess_code_blocks] }); function on_failed_action(action, callback) { diff --git a/zerver/fixtures/bugdown-data.json b/zerver/fixtures/bugdown-data.json index e38f63343b..fb945f272d 100644 --- a/zerver/fixtures/bugdown-data.json +++ b/zerver/fixtures/bugdown-data.json @@ -28,24 +28,24 @@ "name": "hanging_multi_codeblock", "input": "Hamlet said:\n~~~~\ndef speak(self):\n x = 1\n# Comment to make this code block longer to test Trac #1162\n~~~~\n\nThen he mentioned ````y = 4 + x**2```` and\n~~~~\ndef foobar(self):\n return self.baz()", "expected_output": "

Hamlet said:

\n
def speak(self):\n    x = 1\n# Comment to make this code block longer to test Trac #1162\n
\n\n\n

Then he mentioned y = 4 + x**2 and

\n
def foobar(self):\n    return self.baz()\n
", - "bugdown_matches_marked": false + "bugdown_matches_marked": true }, { "name": "fenced_quote", "input": "Hamlet said:\n~~~ quote\nTo be or **not** to be.\n\nThat is the question\n~~~", "expected_output": "

Hamlet said:

\n
\n

To be or not to be.

\n

That is the question

\n
", - "bugdown_matches_marked": false + "bugdown_matches_marked": true }, { "name": "fenced_nested_quote", "input": "Hamlet said:\n~~~ quote\nPolonius said:\n> This above all: to thine ownself be true,\nAnd it must follow, as the night the day,\nThou canst not then be false to any man.\n\nWhat good advice!\n~~~", "expected_output": "

Hamlet said:

\n
\n

Polonius said:

\n
\n

This above all: to thine ownself be true,
\nAnd it must follow, as the night the day,
\nThou canst not then be false to any man.

\n
\n

What good advice!

\n
", - "bugdown_matches_marked": false + "bugdown_matches_marked": true }, { "name": "complexly_nested_quote", - "input": "I heard about this second hand...\n~~~ quote\n\nHe said:\n~~~ quote\nThe customer is complaining.\n\nThey looked at this code:\n``` .py\ndef hello(): print 'hello\n```\nThey would prefer:\n~~~ .rb\ndef hello()\n puts 'hello'\nend\n~~~\n\nPlease advise.\n~~~\n\nShe said:\n~~~ quote\nJust send them this:\n``` .sh\necho \"hello\n\"\n```\n~~~", - "expected_output": "

I heard about this second hand...

\n
\n

He said:

\n
\n

The customer is complaining.

\n

They looked at this code:

\n
def hello(): print 'hello\n
\n\n\n

They would prefer:

\n
def hello()\n  puts 'hello'\nend\n
\n\n\n

Please advise.

\n
\n

She said:

\n
\n

Just send them this:

\n
echo "hello\n"\n
\n\n\n
\n
", + "input": "I heard about this second hand...\n~~~ quote\n\nHe said:\n~~~ quote\nThe customer is complaining.\n\nThey looked at this code:\n``` \ndef hello(): print 'hello\n```\nThey would prefer:\n~~~\ndef hello()\n puts 'hello'\nend\n~~~\n\nPlease advise.\n~~~\n\nShe said:\n~~~ quote\nJust send them this:\n```\necho \"hello\n\"\n```\n~~~", + "expected_output": "

I heard about this second hand...

\n
\n

He said:

\n
\n

The customer is complaining.

\n

They looked at this code:

\n
def hello(): print 'hello\n
\n\n\n

They would prefer:

\n
\n

def hello()
\n puts 'hello'
\nend

\n
\n

Please advise.

\n
She said:\n~~~ quote\nJust send them this:\n```\necho "hello\n"\n```\n
", "bugdown_matches_marked": false }, { @@ -58,7 +58,7 @@ "name": "dangerous_block", "input": "``` one ```\n\n``` two ```\n\n~~~~\nx = 1", "expected_output": "

one

\n

two

\n
x = 1\n
", - "bugdown_matches_marked": false + "bugdown_matches_marked": true }, { "name": "ulist_standard", diff --git a/zerver/tests/frontend/node/echo.js b/zerver/tests/frontend/node/echo.js index 78feeb1611..b365430cc8 100644 --- a/zerver/tests/frontend/node/echo.js +++ b/zerver/tests/frontend/node/echo.js @@ -9,7 +9,8 @@ add_dependencies({ _: 'third/underscore/underscore.js', marked: 'third/marked/lib/marked.js', Dict: 'js/dict.js', - emoji: 'js/emoji.js' + emoji: 'js/emoji.js', + fenced_code: 'js/fenced_code.js' }); var doc = ""; @@ -114,7 +115,7 @@ var bugdown_data = JSON.parse(fs.readFileSync(path.join(__dirname, '../../../fix {input: '1. an\n2. ordered \n3. list', expected: '

1. an

\n

2. ordered

\n

3. list

'}, {input: '\n~~~quote\nquote this for me\n~~~\nthanks\n', - expected: '
\n

quote this for me

\n
\n\n\n

thanks

'}, + expected: '
\n

quote this for me

\n
\n

thanks

'}, {input: 'This is a @**Cordelia Lear** mention', expected: '

This is a @Cordelia Lear mention

'}, {input: 'This is an :poop: message',