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 = `
Sub Item 2
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
Lorem ipsum dolor sit amet, consectetur elit.
Ut minim veniam, laboris
Heading 1 | Head 2 |
---|---|
Lorem | ipsum |
dolor | sit amet |
Heading 1 | Head 2 |
---|---|
ipsum |
Headi ng 1 | Head 2 |
---|---|
Lorem | ipsum |
sit amet |
Heading 1 |
---|
Lorem |
sit amet |
Lorem | 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
"; output = "> Lorem ipsum\n\n> dolor sit amet"; assert.equal(toMarkdown(html), output); html = "dolor sit amet
\nLorem ipsum\n"; output = "> Lorem ipsum\n> > dolor\n> > > sit\n> > amet"; assert.equal(toMarkdown(html), output); }); QUnit.test("converts ol list tag", (assert) => { const html = `Testingdolor
sitamet
·
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 emoji.
`; const markdown = `A [link](http://example.com) with click count and :boom: emoji.`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("keeps emoji syntax for custom emoji", (assert) => { const html = ``; const markdown = `:custom_emoji:`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("converts image lightboxes to markdown", (assert) => { let html = ` `; let markdown = `![sherlock3_sig.jpg](https://d11a6trkgmumsb.cloudfront.net/uploads/default/original/1X/8hkjhk7692f6afed3cb99d43ab2abd4e30aa8cba.jpeg)`; assert.equal(toMarkdown(html), markdown); html = ``; assert.equal(toMarkdown(html), markdown); html = ` `; markdown = `![sherlock3_sig.jpg](upload://1frsimI7TOtFJyD2LLyKSHM8JWe)`; assert.equal(toMarkdown(html), markdown); }); QUnit.test("converts quotes to markdown", (assert) => { let html = `
there is a quote below
there is a quote above
`; let markdown = ` there is a quote below [quote="foo, post:1, topic:2"] this is a quote [/quote] there is a quote above `; assert.equal(toMarkdown(html), markdown.trim()); }); QUnit.test("strips base64 image URLs", (assert) => { const html = ''; assert.equal(toMarkdown(html), "[image]"); });