FEATURE: Keep supported HTML tags in HTML to Markdown conversion

This commit is contained in:
Vinoth Kannan 2017-12-18 12:48:17 +05:30
parent fd67508497
commit 0588edbc96
2 changed files with 30 additions and 1 deletions

View File

@ -86,6 +86,14 @@ class Tag {
};
}
static keep(name) {
return class extends Tag {
constructor() {
super(name, `<${name}>`, `</${name}>`);
}
};
}
static replace(name, text) {
return class extends Tag {
constructor() {
@ -184,9 +192,10 @@ const tags = [
...Tag.emphases().map((e) => Tag.emphasis(e[0], e[1])),
Tag.cell("td"), Tag.cell("th"),
Tag.replace("br", "\n"), Tag.replace("hr", "\n---\n"), Tag.replace("head", ""),
Tag.keep("ins"), Tag.keep("del"), Tag.keep("small"), Tag.keep("big"),
Tag.li(), Tag.link(), Tag.image(),
// TO-DO CREATE: code, tbody, ins, del, blockquote, small, large
// TO-DO CREATE: code, tbody, blockquote
// UPDATE: ol, pre, thead, th, td
];

View File

@ -128,3 +128,23 @@ QUnit.test("converts img tag", assert => {
html = `<a><img src="${url}" alt="description" /></a>`;
assert.equal(toMarkdown(html), `![description](${url})`);
});
QUnit.test("suppring html tags by keeping them", assert => {
let html = "Lorem <del>ipsum dolor</del> sit <big>amet, <ins>consectetur</ins></big>";
let output = html;
assert.equal(toMarkdown(html), output);
html = `Lorem <del style="font-weight: bold">ipsum dolor</del> sit <big>amet, <ins onclick="alert('hello')">consectetur</ins></big>`;
assert.equal(toMarkdown(html), output);
html = `<a href="http://example.com" onload="">Lorem <del style="font-weight: bold">ipsum dolor</del> sit</a>.`;
output = `[Lorem <del>ipsum dolor</del> sit](http://example.com).`;
assert.equal(toMarkdown(html), output);
html = `Lorem <del>ipsum \n\n dolor</del> sit.`;
assert.equal(toMarkdown(html), html);
html = `Lorem <a href="http://example.com"><del>ipsum \n\n\n dolor</del> sit.</a>`;
output = `Lorem [<del>ipsum \n dolor</del> sit.](http://example.com)`;
assert.equal(toMarkdown(html), output);
});