diff --git a/packages/app-cli/tests/html_to_md/table_with_code_3.html b/packages/app-cli/tests/html_to_md/table_with_code_3.html new file mode 100644 index 000000000..0ca92723b --- /dev/null +++ b/packages/app-cli/tests/html_to_md/table_with_code_3.html @@ -0,0 +1,31 @@ +
+ + + + + + + + + + + + + + + +
CodeDescription
+
const test = "hello";
+
+// Another line
+console.log('Test...');
+
+// Blank lines
+
+
+
+// Should not break things.
+
abcda
+
const test = "hello";
+
abcd
+
\ No newline at end of file diff --git a/packages/app-cli/tests/html_to_md/table_with_code_3.md b/packages/app-cli/tests/html_to_md/table_with_code_3.md new file mode 100644 index 000000000..b62d11465 --- /dev/null +++ b/packages/app-cli/tests/html_to_md/table_with_code_3.md @@ -0,0 +1,10 @@ +
CodeDescription
const test = "hello";
+
+// Another line
+console.log('Test...');
+
+// Blank lines
+
+
+
+// Should not break things.
abcda
const test = "hello";
abcd
\ No newline at end of file diff --git a/packages/turndown/src/turndown.js b/packages/turndown/src/turndown.js index ae5c7a9f5..74c822cd6 100644 --- a/packages/turndown/src/turndown.js +++ b/packages/turndown/src/turndown.js @@ -44,7 +44,21 @@ export default function TurndownService (options) { return node.isBlock ? '\n\n' : '' }, keepReplacement: function (content, node) { - return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML + // In markdown, multiple blank lines end an HTML block. We thus + // include an HTML comment to make otherwise blank lines not blank. + const mutliBlankLineRegex = /\n([ \t\r]*)\n/g; + + // We run the replacement multiple times to handle multiple blank + // lines in a row. + // + // For example, "Foo\n\n\nBar" becomes "Foo\n\n\nBar" after the + // first replacement. + let html = node.outerHTML; + while (html.match(mutliBlankLineRegex)) { + html = html.replace(mutliBlankLineRegex, '\n$1\n'); + } + + return node.isBlock ? '\n\n' + html + '\n\n' : html }, defaultReplacement: function (content, node) { return node.isBlock ? '\n\n' + content + '\n\n' : content