Skip to content

A platform for community discussion. Free, open, simple.

Dont support single row or column tables in HTML to Markdown conversion

Vinoth Kannan129d924c0d2d
diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6
index 8f81536f226..1d2f2cbe250 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 4ebb94ec9ae..0a33d642b91 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 = `<table>
+ let html = `<table>
<thead> <tr><th>Headi\n\nng 1</th><th>Head 2</th></tr> </thead>
<tbody>
<tr><td>Lorem</td><td>ipsum</td></tr>
@@ -117,6 +117,18 @@ QUnit.test("returns empty string if table format not supported", assert => {
</table>
`;
assert.equal(toMarkdown(html), "");
+
+ html = `<table>
+ <thead> <tr><th>Heading 1</th></tr> </thead>
+ <tbody>
+ <tr><td>Lorem</td></tr>
+ <tr><td><i>sit amet</i></td></tr></tbody>
+</table>
+ `;
+ assert.equal(toMarkdown(html), "");
+
+ html = `<table><tr><td>Lorem</td><td><i>sit amet</i></td></tr></table>`;
+ assert.equal(toMarkdown(html), "");
});
QUnit.test("converts img tag", assert => {