UX: Use tight list by default in HTML to Markdown conversion

This commit is contained in:
Vinoth Kannan 2017-12-22 21:34:55 +05:30
parent ac1e93e82a
commit 3ae9d91d86
2 changed files with 25 additions and 10 deletions

View File

@ -35,7 +35,7 @@ class Tag {
static blocks() { static blocks() {
return ["address", "article", "aside", "dd", "div", "dl", "dt", "fieldset", "figcaption", "figure", return ["address", "article", "aside", "dd", "div", "dl", "dt", "fieldset", "figcaption", "figure",
"footer", "form", "header", "hgroup", "hr", "main", "nav", "p", "pre", "section", "ul"]; "footer", "form", "header", "hgroup", "hr", "main", "nav", "p", "pre", "section"];
} }
static headings() { static headings() {
@ -51,17 +51,18 @@ class Tag {
} }
static trimmable() { static trimmable() {
return [...Tag.blocks(), ...Tag.headings(), ...Tag.slices(), "li", "td", "th", "br", "hr", "blockquote", "table", "ol", "tr"]; return [...Tag.blocks(), ...Tag.headings(), ...Tag.slices(), "li", "td", "th", "br", "hr", "blockquote", "table", "ol", "tr", "ul"];
} }
static block(name, prefix, suffix) { static block(name, prefix, suffix) {
return class extends Tag { return class extends Tag {
constructor() { constructor() {
super(name, prefix, suffix); super(name, prefix, suffix);
this.gap = "\n\n";
} }
decorate(text) { decorate(text) {
return `\n\n${this.prefix}${text}${this.suffix}\n\n`; return `${this.gap}${this.prefix}${text}${this.suffix}${this.gap}`;
} }
}; };
} }
@ -259,8 +260,23 @@ class Tag {
}; };
} }
static list(name) {
return class extends Tag.block(name) {
decorate(text) {
let smallGap = "";
if (this.element.filterParentNames(["li"]).length) {
this.gap = "";
smallGap = "\n";
}
return smallGap + super.decorate(trimRight(text));
}
};
}
static ol() { static ol() {
return class extends Tag.block("ol") { return class extends Tag.list("ol") {
decorate(text) { decorate(text) {
text = "\n" + text; text = "\n" + text;
const bullet = text.match(/\n\t*\*/)[0]; const bullet = text.match(/\n\t*\*/)[0];
@ -295,7 +311,7 @@ const tags = [
Tag.cell("td"), Tag.cell("th"), Tag.cell("td"), Tag.cell("th"),
Tag.replace("br", "\n"), Tag.replace("hr", "\n---\n"), Tag.replace("head", ""), 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.keep("ins"), Tag.keep("del"), Tag.keep("small"), Tag.keep("big"),
Tag.li(), Tag.link(), Tag.image(), Tag.code(), Tag.blockquote(), Tag.table(), Tag.ol(), Tag.tr(), Tag.li(), Tag.link(), Tag.image(), Tag.code(), Tag.blockquote(), Tag.table(), Tag.tr(), Tag.ol(), Tag.list("ul"),
]; ];
class Element { class Element {

View File

@ -77,14 +77,14 @@ QUnit.test("converts ul list tag", assert => {
Item 2 Item 2
<ul> <ul>
<li>Sub Item 1</li> <li>Sub Item 1</li>
<li>Sub Item 2</li> <li><p>Sub Item 2</p></li>
<ul><li>Sub <i>Sub</i> Item 1</li><li>Sub <b>Sub</b> Item 2</li></ul> <li>Sub Item 3<ul><li>Sub <i>Sub</i> Item 1</li><li>Sub <b>Sub</b> Item 2</li></ul></li>
</ul> </ul>
</li> </li>
<li>Item 3</li> <li>Item 3</li>
</ul> </ul>
`; `;
const markdown = `* Item 1\n* Item 2\n\n * Sub Item 1\n * Sub Item 2\n\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n\n* Item 3`; const markdown = `* Item 1\n* Item 2\n * Sub Item 1\n * Sub Item 2\n\n * Sub Item 3\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n* Item 3`;
assert.equal(toMarkdown(html), markdown); assert.equal(toMarkdown(html), markdown);
}); });
@ -221,12 +221,11 @@ QUnit.test("converts ol list tag", assert => {
<ol start="100"> <ol start="100">
<li>Sub Item 1</li> <li>Sub Item 1</li>
<li>Sub Item 2</li> <li>Sub Item 2</li>
<ul><li>Sub <i>Sub</i> Item 1</li><li>Sub <b>Sub</b> Item 2</li></ul>
</ol> </ol>
</li> </li>
<li>Item 3</li> <li>Item 3</li>
</ol> </ol>
`; `;
const markdown = `Testing\n\n1. Item 1\n2. Item 2\n\n 100. Sub Item 1\n 101. Sub Item 2\n\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n\n3. Item 3`; const markdown = `Testing\n\n1. Item 1\n2. Item 2\n 100. Sub Item 1\n 101. Sub Item 2\n3. Item 3`;
assert.equal(toMarkdown(html), markdown); assert.equal(toMarkdown(html), markdown);
}); });