From a7a7387da14381c09938b5bf935b26f2d0555421 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 13 Dec 2013 15:31:25 -0500 Subject: [PATCH] Automatically convert some quotes to blockquotes --- .../discourse/dialects/autoquote_dialect.js | 23 +++++++++++++++++++ test/javascripts/lib/markdown_test.js | 8 +++++++ 2 files changed, 31 insertions(+) create mode 100644 app/assets/javascripts/discourse/dialects/autoquote_dialect.js diff --git a/app/assets/javascripts/discourse/dialects/autoquote_dialect.js b/app/assets/javascripts/discourse/dialects/autoquote_dialect.js new file mode 100644 index 00000000000..e34bd3c2330 --- /dev/null +++ b/app/assets/javascripts/discourse/dialects/autoquote_dialect.js @@ -0,0 +1,23 @@ +/** + If a line contains a single quote, convert it to a blockquote. For example: + + "My fake plants died because I did not pretend to water them." + + Would be: + +
My fake plants died because I did not pretend to water them.
+ +**/ +Discourse.Dialect.registerInline('"', function(str, match, prev) { + + // Make sure we're on a line boundary + var last = prev[prev.length - 1]; + if (typeof last === "string") { return; } + + if (str.length > 2 && str.charAt(0) === '"' && str.charAt(str.length-1) === '"') { + var inner = str.substr(1, str.length-2); + if (inner.indexOf('"') === -1 && inner.indexOf("\n") === -1) { + return [str.length, ['blockquote', inner]]; + } + } +}); \ No newline at end of file diff --git a/test/javascripts/lib/markdown_test.js b/test/javascripts/lib/markdown_test.js index a5af3ac1745..56348270a8a 100644 --- a/test/javascripts/lib/markdown_test.js +++ b/test/javascripts/lib/markdown_test.js @@ -29,6 +29,14 @@ test("basic cooking", function() { cooked("brussel sproutes are *awful*.", "

brussel sproutes are awful.

", "it doesn't swallow periods."); }); +test("Auto quoting", function() { + cooked('"My fake plants died because I did not pretend to water them."', + "

My fake plants died because I did not pretend to water them.

", + "it converts single line quotes to blockquotes"); + cooked('"hello\nworld"', "

\"hello
world\"

", "It doesn't convert multi line quotes"); + cooked('"hello "evil" trout"', '

"hello "evil" trout"

', "it doesn't format quotes in the middle of a line"); +}); + test("Traditional Line Breaks", function() { var input = "1\n2\n3"; cooked(input, "

1
2
3

", "automatically handles trivial newlines");