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

This commit is contained in:
Vinoth Kannan 2017-12-21 12:58:57 +05:30
parent 6ecf37c482
commit 129d924c0d
2 changed files with 27 additions and 4 deletions

View File

@ -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;
}
};

View File

@ -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 => {