import toMarkdown from "discourse/lib/to-markdown"; QUnit.module("lib:to-markdown"); QUnit.test("converts styles between normal words", assert => { const html = `Line with styles between words.`; const markdown = `Line with ~~styles~~ ***between*** words.`; assert.equal(toMarkdown(html), markdown); assert.equal(toMarkdown("A bold word"), "A **bold** word"); }); QUnit.test("converts inline nested styles", assert => { let html = `Italicised line with some random bold words.`; let markdown = `*Italicised line with **some random** **bold** words.*`; assert.equal(toMarkdown(html), markdown); html = `Italicised line with some
random
bold words.
`; markdown = `Italicised line with some\nrandom ~~bold~~ words.`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("converts a link", assert => { let html = `Discourse`; let markdown = `[Discourse](https://discourse.org)`; assert.equal(toMarkdown(html), markdown); html = `Disc\n\n\nour\n\nse`; markdown = `[Disc our se](https://discourse.org)`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("put raw URL instead of converting the link", assert => { let url = "https://discourse.org"; const html = () => `${url}`; assert.equal(toMarkdown(html()), url); url = "discourse.org/t/topic-slug/1"; assert.equal(toMarkdown(html()), url); }); QUnit.test("skip empty link", assert => { assert.equal(toMarkdown(``), ""); }); QUnit.test("converts heading tags", assert => { const html = `

Heading 1

Heading 2

\t

Heading 3

Heading 4

Heading 5
Heading 6
`; const markdown = `# Heading 1\n\n## Heading 2\n\n### Heading 3\n\n#### Heading 4\n\n##### Heading 5\n\n###### Heading 6`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("converts ul list tag", assert => { let html = ` `; let markdown = `* Item 1\n* Item 2\n * Sub Item 1\n * Sub Item 2\n * Sub Item 3\n * Sub *Sub* Item 1\n * Sub **Sub** Item 2\n* Item 3`; assert.equal(toMarkdown(html), markdown); html = ` `; markdown = `* Bullets at level 1 * Bullets at level 1 * Bullets at level 2 * Bullets at level 2 * Bullets at level 3 * Bullets at level 2 * Bullets at level 1`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("stripes unwanted inline tags", assert => { const html = `

Lorem ipsum dolor sit amet, consectetur elit.

Ut minim veniam, laboris ut aliquip ex ea commodo.

`; const markdown = `Lorem ipsum dolor sit amet, consectetur ~~elit.~~\n\nUt minim veniam, quis nostrud laboris ut aliquip ex ea commodo.`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("converts table tags", assert => { let html = `
Discourse Avenue
laboris
Heading 1Head 2
Loremipsum
dolor sit amet
`; let markdown = `Discourse Avenue\n\n**laboris**\n\n|Heading 1|Head 2|\n| --- | --- |\n|Lorem|ipsum|\n|**dolor**|*sit amet*|`; assert.equal(toMarkdown(html), markdown); html = `
Heading 1Head 2
Loremipsum
`; markdown = `|Heading 1|Head 2|\n| --- | --- |\n|[![Lorem\\|45x45](http://example.com/image.png)](http://example.com)|ipsum|`; assert.equal(toMarkdown(html), markdown); }); QUnit.test( "replace pipes with spaces if table format not supported", assert => { let html = `
Headi

ng 1
Head 2
Loremipsum
sit amet
`; let markdown = `Headi\n\nng 1 Head 2\nLorem ipsum\n[![](http://dolor.com/image.png)](http://example.com) *sit amet*`; assert.equal(toMarkdown(html), markdown); html = `
Heading 1
Lorem
sit amet
`; markdown = `Heading 1\nLorem\n*sit amet*`; assert.equal(toMarkdown(html), markdown); html = `
Loremsit amet
`; markdown = `Lorem **sit amet**`; assert.equal(toMarkdown(html), markdown); } ); QUnit.test("converts img tag", assert => { const url = "https://example.com/image.png"; let html = ``; assert.equal(toMarkdown(html), `![|100x50](${url})`); html = `
description
`; assert.equal(toMarkdown(html), `![description|50x100](${url})`); html = `description`; assert.equal( toMarkdown(html), `[![description](${url})](http://example.com)` ); html = `description `; assert.equal( toMarkdown(html), `[description ![](${url})](http://example.com)` ); html = `description`; assert.equal(toMarkdown(html), ""); html = `description`; assert.equal(toMarkdown(html), `![description](${url})`); }); QUnit.test("supporting html tags by keeping them", assert => { let html = "Lorem ipsum dolor sit amet, consectetur"; let output = html; assert.equal(toMarkdown(html), output); html = `Lorem ipsum dolor sit amet, consectetur`; assert.equal(toMarkdown(html), output); html = `Lorem ipsum dolor sit.`; output = `[Lorem ipsum dolor sit](http://example.com).`; assert.equal(toMarkdown(html), output); html = `Lorem ipsum dolor sit.`; assert.equal(toMarkdown(html), html); html = `Have you tried clicking the Help Me! button?`; assert.equal(toMarkdown(html), html); html = `Lorem ipsum \n\n\n dolor sit.`; output = `Lorem [ipsum dolor sit.](http://example.com)`; assert.equal(toMarkdown(html), output); }); QUnit.test("converts code tags", assert => { let html = `Lorem ipsum dolor sit amet,
var helloWorld = () => {
  alert('    hello \t\t world    ');
    return;
}
helloWorld();
consectetur.`; let output = `Lorem ipsum dolor sit amet,\n\n\`\`\`\nvar helloWorld = () => {\n alert(' hello \t\t world ');\n return;\n}\nhelloWorld();\n\`\`\`\n\nconsectetur.`; assert.equal(toMarkdown(html), output); html = `Lorem ipsum dolor sit amet, var helloWorld = () => { alert(' hello \t\t world '); return; } helloWorld();consectetur.`; output = `Lorem ipsum dolor sit amet, \`var helloWorld = () => {\n alert(' hello \t\t world ');\n return;\n}\nhelloWorld();\` consectetur.`; assert.equal(toMarkdown(html), output); }); QUnit.test("converts blockquote tag", assert => { let html = "
Lorem ipsum
"; let output = "> Lorem ipsum"; assert.equal(toMarkdown(html), output); html = "
Lorem ipsum

dolor sit amet

"; output = "> Lorem ipsum\n\n> dolor sit amet"; assert.equal(toMarkdown(html), output); html = "
\nLorem ipsum\n

dolor

sit
amet

"; output = "> Lorem ipsum\n> > dolor\n> > > sit\n> > amet"; assert.equal(toMarkdown(html), output); }); QUnit.test("converts ol list tag", assert => { const html = `Testing
  1. Item 1
  2. Item 2
    1. Sub Item 1
    2. Sub Item 2
  3. 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); }); QUnit.test("converts list tag from word", assert => { const html = `Sample

· Item 1

· Item 2

· Item 3

· Item 4

List`; const markdown = `Sample\n\n* **Item 1**\n * *Item 2*\n * Item 3\n* Item 4\n\nList`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("keeps mention/hash class", assert => { const html = `

User mention: @discourse

Group mention: @discourse-group

Category link: #foo

Sub-category link: #foo:bar

`; const markdown = `User mention: @discourse\n\nGroup mention: @discourse-group\n\nCategory link: #foo\n\nSub-category link: #foo:bar`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("keeps emoji and removes click count", assert => { const html = `

A link1 with click count and :boom: emoji.

`; const markdown = `A [link](http://example.com) with click count and :boom: emoji.`; assert.equal(toMarkdown(html), markdown); });