From ea3db5f180a079fc9562934553e719c92d72ea23 Mon Sep 17 00:00:00 2001 From: Estevao Soares dos Santos Date: Fri, 22 Dec 2017 09:54:23 +0000 Subject: [PATCH] feat(splitAdjacentBlockquotes): add option to split adjacent blockquote blocks With this option enabled, this: ```md > some text > some other text ``` witll result in: ```html

some text

some other text

``` This is the default behavior of GFM. Closes #477 --- README.md | 2 ++ src/options.js | 5 +++++ src/showdown.js | 3 ++- src/subParsers/makehtml/blockQuotes.js | 13 ++++++++++--- test/features/splitAdjacentBlockquotes/basic.html | 8 ++++++++ test/features/splitAdjacentBlockquotes/basic.md | 7 +++++++ .../multiline-paragraph.html | 6 ++++++ .../multiline-paragraph.md | 5 +++++ test/node/testsuite.features.js | 14 +++++++++++++- 9 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 test/features/splitAdjacentBlockquotes/basic.html create mode 100644 test/features/splitAdjacentBlockquotes/basic.md create mode 100644 test/features/splitAdjacentBlockquotes/multiline-paragraph.html create mode 100644 test/features/splitAdjacentBlockquotes/multiline-paragraph.md diff --git a/README.md b/README.md index 4a3c2ca..5056ed3 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,8 @@ var defaultOptions = showdown.getDefaultOptions(); * **metadata**: (boolean) [default false] Enable support for document metadata (defined at the top of the document between `«««` and `»»»` or between `---` and `---`). (since v.1.8.5) + * **splitAdjacentBlockquotes**: (boolean) [default false] Split adjacent blockquote blocks.(since v.1.8.6) + **NOTE**: Please note that until **version 1.6.0**, all of these options are ***DISABLED*** by default in the cli tool. diff --git a/src/options.js b/src/options.js index 5542553..24899b4 100644 --- a/src/options.js +++ b/src/options.js @@ -160,6 +160,11 @@ function getDefaultOpts (simple) { defaultValue: false, description: 'Enable support for document metadata (defined at the top of the document between `«««` and `»»»` or between `---` and `---`).', type: 'boolean' + }, + splitAdjacentBlockquotes: { + defaultValue: false, + description: 'Split adjacent blockquote blocks', + type: 'boolean' } }; if (simple === false) { diff --git a/src/showdown.js b/src/showdown.js index 18b8268..bc0e508 100644 --- a/src/showdown.js +++ b/src/showdown.js @@ -33,7 +33,8 @@ var showdown = {}, ghCompatibleHeaderId: true, ghMentions: true, backslashEscapesHTMLTags: true, - emoji: true + emoji: true, + splitAdjacentBlockquotes: true }, original: { noHeaderId: true, diff --git a/src/subParsers/makehtml/blockQuotes.js b/src/subParsers/makehtml/blockQuotes.js index 6a77d23..b372401 100644 --- a/src/subParsers/makehtml/blockQuotes.js +++ b/src/subParsers/makehtml/blockQuotes.js @@ -3,12 +3,19 @@ showdown.subParser('makehtml.blockQuotes', function (text, options, globals) { text = globals.converter._dispatch('makehtml.blockQuotes.before', text, options, globals); - text = text.replace(/((^ {0,3}>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) { - var bq = m1; + // add a couple extra lines after the text and endtext mark + text = text + '\n\n'; + var rgx = /(^ {0,3}>[ \t]?.+\n(.+\n)*\n*)+/gm; + + if (options.splitAdjacentBlockquotes) { + rgx = /^ {0,3}>[\s\S]*?(?:\n\n)/gm; + } + + text = text.replace(rgx, function (bq) { // attacklab: hack around Konqueror 3.5.4 bug: // "----------bug".replace(/^-/g,"") == "bug" - bq = bq.replace(/^[ \t]*>[ \t]?/gm, '¨0'); // trim one level of quoting + bq = bq.replace(/^[ \t]*>[ \t]?/gm, ''); // trim one level of quoting // attacklab: clean up hack bq = bq.replace(/¨0/g, ''); diff --git a/test/features/splitAdjacentBlockquotes/basic.html b/test/features/splitAdjacentBlockquotes/basic.html new file mode 100644 index 0000000..a58d974 --- /dev/null +++ b/test/features/splitAdjacentBlockquotes/basic.html @@ -0,0 +1,8 @@ +
+

Block quote 1

+

This is my first block quote.

+
+
+

Block quote 2

+

This is my second block quote.

+
diff --git a/test/features/splitAdjacentBlockquotes/basic.md b/test/features/splitAdjacentBlockquotes/basic.md new file mode 100644 index 0000000..d47e82b --- /dev/null +++ b/test/features/splitAdjacentBlockquotes/basic.md @@ -0,0 +1,7 @@ +> # Block quote 1 +> +> This is my first block quote. + +> # Block quote 2 +> +> This is my second block quote. diff --git a/test/features/splitAdjacentBlockquotes/multiline-paragraph.html b/test/features/splitAdjacentBlockquotes/multiline-paragraph.html new file mode 100644 index 0000000..ce8b0ee --- /dev/null +++ b/test/features/splitAdjacentBlockquotes/multiline-paragraph.html @@ -0,0 +1,6 @@ +
+

This is my second block quote +yeah +everythong is ok.

+
+

This is not a blockquote

diff --git a/test/features/splitAdjacentBlockquotes/multiline-paragraph.md b/test/features/splitAdjacentBlockquotes/multiline-paragraph.md new file mode 100644 index 0000000..427556b --- /dev/null +++ b/test/features/splitAdjacentBlockquotes/multiline-paragraph.md @@ -0,0 +1,5 @@ +> This is my second block quote +yeah +everythong is ok. + +This is not a blockquote diff --git a/test/node/testsuite.features.js b/test/node/testsuite.features.js index bb5d612..f96515f 100644 --- a/test/node/testsuite.features.js +++ b/test/node/testsuite.features.js @@ -16,7 +16,8 @@ var bootstrap = require('../bootstrap.js'), literalMidWordUnderscoresSuite = bootstrap.getTestSuite('test/features/literalMidWordUnderscores/'), literalMidWordAsterisksSuite = bootstrap.getTestSuite('test/features/literalMidWordAsterisks/'), completeHTMLOutputSuite = bootstrap.getTestSuite('test/features/completeHTMLOutput/'), - metadataSuite = bootstrap.getTestSuite('test/features/metadata/'); + metadataSuite = bootstrap.getTestSuite('test/features/metadata/'), + splitAdjacentBlockquotesSuite = bootstrap.getTestSuite('test/features/splitAdjacentBlockquotes/'); describe('makeHtml() features testsuite', function () { 'use strict'; @@ -264,4 +265,15 @@ describe('makeHtml() features testsuite', function () { it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter)); } }); + + /** test metadata option **/ + describe('splitAdjacentBlockquotes option', function () { + var converter, + suite = splitAdjacentBlockquotesSuite; + + for (var i = 0; i < suite.length; ++i) { + converter = new showdown.Converter({splitAdjacentBlockquotes: true}); + it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter)); + } + }); });