diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 index 8f81536f22..1d2f2cbe25 100644 --- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 +++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 @@ -234,9 +234,20 @@ class Tag { static table() { return class extends Tag.block("table") { decorate(text) { - text = super.decorate(text); - const splitterRow = text.split("|\n")[0].match(/\|/g).map(() => "| --- ").join("") + "|\n"; - text = text.replace("|\n", "|\n" + splitterRow).replace(/\|\n{2,}\|/g, "|\n|"); + text = super.decorate(text).replace(/\|\n{2,}\|/g, "|\n|"); + const rows = text.trim().split("\n"); + const pipes = rows[0].match(/\|/g); + const isValid = rows.length > 1 && + pipes.length > 2 && + rows.reduce((a, c) => a && c.match(/\|/g).length <= pipes.length); + + if (!isValid) { + throw "Unsupported table format for Markdown conversion"; + } + + const splitterRow = pipes.slice(1).map(() => "| --- ").join("") + "|\n"; + text = text.replace("|\n", "|\n" + splitterRow); + return text; } }; diff --git a/test/javascripts/lib/to-markdown-test.js.es6 b/test/javascripts/lib/to-markdown-test.js.es6 index 4ebb94ec9a..0a33d642b9 100644 --- a/test/javascripts/lib/to-markdown-test.js.es6 +++ b/test/javascripts/lib/to-markdown-test.js.es6 @@ -109,7 +109,7 @@ QUnit.test("converts table tags", assert => { }); QUnit.test("returns empty string if table format not supported", assert => { - const html = ` + let html = `
@@ -117,6 +117,18 @@ QUnit.test("returns empty string if table format not supported", assert => {
Headi\n\nng 1Head 2
Loremipsum
`; assert.equal(toMarkdown(html), ""); + + html = ` + + + + +
Heading 1
Lorem
sit amet
+ `; + assert.equal(toMarkdown(html), ""); + + html = `
Loremsit amet
`; + assert.equal(toMarkdown(html), ""); }); QUnit.test("converts img tag", assert => {