From 4da3d36c06d44a4eab09eff1c4d0c26557385388 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Tue, 29 Mar 2016 11:33:02 -0400 Subject: [PATCH 01/69] adding support for lists, headers, pre/code, and tables --- slackify-html.js | 142 ++++++++++++++++++++++++++++++++++++++++++++++- tests.js | 51 +++++++++++++++++ 2 files changed, 192 insertions(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index f317a13..e35ac67 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -14,8 +14,122 @@ module.exports = function slackify(html) { return entities.decode(walk(dom)); else return ''; +}; + +function walkList(dom, ordered) { + var out = ''; + if (dom) { + var listItemIndex = 1; + dom.forEach(function (el) { + if ('text' === el.type) { + if (el.data.trim() !== '') { + out += el.data; + } + } + else if ('tag' === el.type) { + switch (el.name) { + case 'li': + out += (ordered ? listItemIndex++ + '. ' : "* ") + walk(el.children) + '\n'; + break; + default: + out += walk(el.children); + } + } + }); + } + return out; +} + +function walkPre(dom) { + var out = ''; + if (dom) { + dom.forEach(function (el) { + if ('text' === el.type) { + out += el.data; + } + else if ('tag' === el.type) { + out += walkPre(el.children); + } + }); + } + return out; +} + +function walkTable(dom) { + var out = ''; + if (dom) { + dom.forEach(function (el) { + if ('tag' === el.type) { + if ('thead' === el.name) { + out += walkTableHead(el.children); + } + else if ('tbody' === el.name) { + out += walkTableBody(el.children); + } + } + }); + } + + return out; } +function walkTableHead(dom) { + var out = ''; + if (dom) { + var headers = []; + dom.forEach(function (el) { + if ('text' === el.type) { + if (el.data.trim() !== '') { + out += el.data; + } + } + else if ('tr' === el.name) { + out += walkTableHead(el.children); + } + else if ('th' === el.name) { + var header = walkTableHead(el.children); + headers.push(header); + out += '| ' + header + ' '; + } + }); + if (headers.length > 0) { + out += ' |\n'; + headers.forEach(function (item) { + out += '| '; + for (i = 0; i < item.length; i++) { + out += '-'; + } + out += ' '; + }); + out += ' |\n'; + } + } + + return out; +} + +function walkTableBody(dom) { + var out = ''; + if (dom) { + dom.forEach(function (el) { + if ('text' === el.type) { + if (el.data.trim() !== '') { + console.log('###' + el.data + '###'); + out += el.data; + } + } + else if ('td' === el.name) { + out += '| ' + walkTableBody(el.children) + ' '; + } + else if ('tr' === el.name) { + out += walkTableBody(el.children) + '|\n'; + } + }); + } + return out; +} + + function walk(dom) { var out = ''; if (dom) @@ -23,7 +137,7 @@ function walk(dom) { if ('text' === el.type) { out += el.data; } - if ('tag' === el.type) { + else if ('tag' === el.type) { switch (el.name) { case 'a': out += '<' + el.attribs.href + '|' + walk(el.children) + '>'; @@ -36,6 +150,32 @@ function walk(dom) { case 'em': out += '_' + walk(el.children) + '_'; break; + case 'p': + out += walk(el.children) + '\n'; + break; + case 'br': + out += walk(el.children) + '\n'; + break; + case 'h1': + case 'h2': + case 'h3': + case 'h4': + out += '*' + walk(el.children) + '*'; + break; + case 'ol': + case 'ul': + out += walkList(el.children, 'ol' === el.name); + break; + case 'code': + out += '`' + walk(el.children) + '`'; + break; + case 'pre': + out += '```\n' + walkPre(el.children) + '```'; + break; + case 'table': + out += walkTable(el.children); + break; + //FIXME IMG default: out += walk(el.children); } diff --git a/tests.js b/tests.js index 708da2b..3fd7e17 100644 --- a/tests.js +++ b/tests.js @@ -30,3 +30,54 @@ tap.test('vcheck example', function vcheck(t) { '*2.4-SNAPSHOT* • revision • build 2015-09-07 14:06 • wbl 1.3.33 • '); t.end(); }); + +tap.test('full example', function vcheck(t) { + var input = `

Security Overview Header

+

We take the security of your data very seriously!

+

In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

+

Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

+

Privacy Policy

+

Remember this list

+
    +
  1. foo
  2. +
  3. bar
  4. +
  5. buz
  6. +
+

and this list too...

+
    +
  • abc
  • +
  • def
  • +
  • xyz
  • +
+

and this

+
blah
+
+

+ + + + + + + + + + + + + + + + + + + + +
Column 1Column 2Column 3
FooBarBaz
abcdefghi
+
`; +var expected = '*Security Overview Header*\n*We take the security of your data very seriously!*\n\n In order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, where, when and how we protect your data.\n\n Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n1. foo\n2. bar\n3. buz\n\nand this list too...\n* abc\n* def\n* xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n![](https://qaup.getguru.com/5240119b-9752-443a-9172-73204f8599eb/94acdc58-32c2-44d3-9843-7a2a5cb3fbf5.bc70afa3-1798-4c87-a2fe-21e3f855e35a.jpeg)\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |'; +var output = slackify(input); + t.equals(output, + expected); + t.end(); +}); From 15bfb3a751822ff85cbf6c02fb4aaaa07da202e5 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Tue, 29 Mar 2016 12:17:52 -0400 Subject: [PATCH 02/69] adding nested lists --- slackify-html.js | 11 +++++++++-- tests.js | 6 +++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index e35ac67..1324fc8 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -16,7 +16,7 @@ module.exports = function slackify(html) { return ''; }; -function walkList(dom, ordered) { +function walkList(dom, ordered, nesting) { var out = ''; if (dom) { var listItemIndex = 1; @@ -29,8 +29,15 @@ function walkList(dom, ordered) { else if ('tag' === el.type) { switch (el.name) { case 'li': + for (i=0; i < nesting * 2; i++) { + out += ' '; + } out += (ordered ? listItemIndex++ + '. ' : "* ") + walk(el.children) + '\n'; break; + case 'ol': + case 'ul': + out += walkList(el.children, 'ol' === el.name, nesting+1); + break; default: out += walk(el.children); } @@ -164,7 +171,7 @@ function walk(dom) { break; case 'ol': case 'ul': - out += walkList(el.children, 'ol' === el.name); + out += walkList(el.children, 'ol' === el.name, 0); break; case 'code': out += '`' + walk(el.children) + '`'; diff --git a/tests.js b/tests.js index 3fd7e17..a62ba32 100644 --- a/tests.js +++ b/tests.js @@ -46,6 +46,10 @@ tap.test('full example', function vcheck(t) {

and this list too...

  • abc
  • +
      +
    • sub 1
    • +
    • sub 2
    • +
  • def
  • xyz
@@ -75,7 +79,7 @@ tap.test('full example', function vcheck(t) { `; -var expected = '*Security Overview Header*\n*We take the security of your data very seriously!*\n\n In order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, where, when and how we protect your data.\n\n Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n1. foo\n2. bar\n3. buz\n\nand this list too...\n* abc\n* def\n* xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n![](https://qaup.getguru.com/5240119b-9752-443a-9172-73204f8599eb/94acdc58-32c2-44d3-9843-7a2a5cb3fbf5.bc70afa3-1798-4c87-a2fe-21e3f855e35a.jpeg)\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |'; +var expected = '*Security Overview Header*\n*We take the security of your data very seriously!*\n\n In order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, where, when and how we protect your data.\n\n Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n1. foo\n2. bar\n3. buz\n\nand this list too...\n* abc\n * sub 1\n * sub 2\n* def\n* xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n![](https://qaup.getguru.com/5240119b-9752-443a-9172-73204f8599eb/94acdc58-32c2-44d3-9843-7a2a5cb3fbf5.bc70afa3-1798-4c87-a2fe-21e3f855e35a.jpeg)\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |'; var output = slackify(input); t.equals(output, expected); From 37b41844dcaa198e5a13bc3bf1a8d8322adb9843 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Tue, 29 Mar 2016 12:58:55 -0400 Subject: [PATCH 03/69] debugging --- slackify-html.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 1324fc8..58c7a93 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -30,7 +30,7 @@ function walkList(dom, ordered, nesting) { switch (el.name) { case 'li': for (i=0; i < nesting * 2; i++) { - out += ' '; + out += '^'; } out += (ordered ? listItemIndex++ + '. ' : "* ") + walk(el.children) + '\n'; break; @@ -188,5 +188,6 @@ function walk(dom) { } } }); + console.log(out); return out; } From fa56c9fb92b66e6491d89d0b3afe9227033825df Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Tue, 29 Mar 2016 13:00:44 -0400 Subject: [PATCH 04/69] debugging --- slackify-html.js | 1 - 1 file changed, 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 58c7a93..7c1a2dd 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -188,6 +188,5 @@ function walk(dom) { } } }); - console.log(out); return out; } From 8c5b8ff4a7bc07769e1f7818c462c36606c61479 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Tue, 29 Mar 2016 13:20:38 -0400 Subject: [PATCH 05/69] Fix nested lists --- slackify-html.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 7c1a2dd..5890347 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -30,16 +30,12 @@ function walkList(dom, ordered, nesting) { switch (el.name) { case 'li': for (i=0; i < nesting * 2; i++) { - out += '^'; + out += ' '; } - out += (ordered ? listItemIndex++ + '. ' : "* ") + walk(el.children) + '\n'; - break; - case 'ol': - case 'ul': - out += walkList(el.children, 'ol' === el.name, nesting+1); + out += (ordered ? listItemIndex++ + '. ' : "* ") + walk(el.children, nesting + 1) + '\n'; break; default: - out += walk(el.children); + out += walk(el.children, nesting + 1); } } }); @@ -121,7 +117,6 @@ function walkTableBody(dom) { dom.forEach(function (el) { if ('text' === el.type) { if (el.data.trim() !== '') { - console.log('###' + el.data + '###'); out += el.data; } } @@ -137,7 +132,10 @@ function walkTableBody(dom) { } -function walk(dom) { +function walk(dom, nesting) { + if (!nesting) { + nesting = 0; + } var out = ''; if (dom) dom.forEach(function (el) { @@ -171,7 +169,7 @@ function walk(dom) { break; case 'ol': case 'ul': - out += walkList(el.children, 'ol' === el.name, 0); + out += walkList(el.children, 'ol' === el.name, nesting); break; case 'code': out += '`' + walk(el.children) + '`'; From 9a7d1f4104ca3c6baa4c2fa879479ad159092cfd Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Tue, 29 Mar 2016 14:02:02 -0400 Subject: [PATCH 06/69] handle images --- slackify-html.js | 23 ++++++++++------------- tests.js | 7 ++++--- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 5890347..8e19c98 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -21,10 +21,8 @@ function walkList(dom, ordered, nesting) { if (dom) { var listItemIndex = 1; dom.forEach(function (el) { - if ('text' === el.type) { - if (el.data.trim() !== '') { - out += el.data; - } + if ('text' === el.type && el.data.trim() !== '') { + out += el.data; } else if ('tag' === el.type) { switch (el.name) { @@ -81,10 +79,8 @@ function walkTableHead(dom) { if (dom) { var headers = []; dom.forEach(function (el) { - if ('text' === el.type) { - if (el.data.trim() !== '') { - out += el.data; - } + if ('text' === el.type && el.data.trim() !== '') { + out += el.data; } else if ('tr' === el.name) { out += walkTableHead(el.children); @@ -115,10 +111,8 @@ function walkTableBody(dom) { var out = ''; if (dom) { dom.forEach(function (el) { - if ('text' === el.type) { - if (el.data.trim() !== '') { - out += el.data; - } + if ('text' === el.type && el.data.trim() !== '') { + out += el.data; } else if ('td' === el.name) { out += '| ' + walkTableBody(el.children) + ' '; @@ -180,7 +174,10 @@ function walk(dom, nesting) { case 'table': out += walkTable(el.children); break; - //FIXME IMG + case 'img': + var alt = el.attribs.alt; + out += ''; + break; default: out += walk(el.children); } diff --git a/tests.js b/tests.js index a62ba32..c3e1d37 100644 --- a/tests.js +++ b/tests.js @@ -34,7 +34,7 @@ tap.test('vcheck example', function vcheck(t) { tap.test('full example', function vcheck(t) { var input = `

Security Overview Header

We take the security of your data very seriously!

-

In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

+

In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

Privacy Policy

Remember this list

@@ -45,11 +45,12 @@ tap.test('full example', function vcheck(t) {

and this list too...

    -
  • abc
  • +
  • abc
    • sub 1
    • sub 2
    +
  • def
  • xyz
@@ -79,7 +80,7 @@ tap.test('full example', function vcheck(t) {
`; -var expected = '*Security Overview Header*\n*We take the security of your data very seriously!*\n\n In order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, where, when and how we protect your data.\n\n Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n1. foo\n2. bar\n3. buz\n\nand this list too...\n* abc\n * sub 1\n * sub 2\n* def\n* xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n![](https://qaup.getguru.com/5240119b-9752-443a-9172-73204f8599eb/94acdc58-32c2-44d3-9843-7a2a5cb3fbf5.bc70afa3-1798-4c87-a2fe-21e3f855e35a.jpeg)\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |'; +var expected = '*Security Overview Header*\n*We take the security of your data very seriously!*\n\n In order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, where, when and how we protect your data.\n\n Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n1. foo\n2. bar\n3. buz\n\nand this list too...\n* _abc_\n * sub 1\n * sub 2\n* def\n* xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n![](https://qaup.getguru.com/5240119b-9752-443a-9172-73204f8599eb/94acdc58-32c2-44d3-9843-7a2a5cb3fbf5.bc70afa3-1798-4c87-a2fe-21e3f855e35a.jpeg)\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |'; var output = slackify(input); t.equals(output, expected); From d4d6c82152814b200bb91fd9d084f395fce1f96e Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Thu, 31 Mar 2016 10:03:49 -0400 Subject: [PATCH 07/69] debugging --- slackify-html.js | 1 + 1 file changed, 1 insertion(+) diff --git a/slackify-html.js b/slackify-html.js index 8e19c98..de2c49e 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -4,6 +4,7 @@ var htmlparser = require('htmlparser'), entities = new Entities(); module.exports = function slackify(html) { + console.log('v1.1'); var handler = new htmlparser.DefaultHandler(function (error, dom) { // error ignored }); From 7673f71a99458494448c633571b8364245cc6394 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Tue, 26 Apr 2016 10:48:38 -0400 Subject: [PATCH 08/69] remove debugging --- slackify-html.js | 1 - 1 file changed, 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index de2c49e..8e19c98 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -4,7 +4,6 @@ var htmlparser = require('htmlparser'), entities = new Entities(); module.exports = function slackify(html) { - console.log('v1.1'); var handler = new htmlparser.DefaultHandler(function (error, dom) { // error ignored }); From c66c6942c01e2454c15d1c06b812a5743b330a06 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Wed, 4 May 2016 15:59:45 -0400 Subject: [PATCH 09/69] attempt to fix bullets --- slackify-html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 8e19c98..1d6723a 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -30,7 +30,7 @@ function walkList(dom, ordered, nesting) { for (i=0; i < nesting * 2; i++) { out += ' '; } - out += (ordered ? listItemIndex++ + '. ' : "* ") + walk(el.children, nesting + 1) + '\n'; + out += (ordered ? listItemIndex++ + '. ' : "• ") + walk(el.children, nesting + 1) + '\n'; break; default: out += walk(el.children, nesting + 1); From bcccb33cb751054e90c8e74035d4e4920dd85ce3 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Mon, 7 Nov 2016 11:22:12 -0500 Subject: [PATCH 10/69] handle anchor tags with no href --- slackify-html.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 1d6723a..1fba5be 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -139,7 +139,12 @@ function walk(dom, nesting) { else if ('tag' === el.type) { switch (el.name) { case 'a': - out += '<' + el.attribs.href + '|' + walk(el.children) + '>'; + if (el.attribs.href) { + out += '<' + el.attribs.href + '|' + walk(el.children) + '>'; + } + else { + out += walk(el.children); + } break; case 'strong': case 'b': From 95cd3e6944e29f313c59d5f68a462f2bb9a67f49 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Mon, 7 Nov 2016 14:43:06 -0500 Subject: [PATCH 11/69] better null checking --- slackify-html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 1fba5be..5b7cf59 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -139,7 +139,7 @@ function walk(dom, nesting) { else if ('tag' === el.type) { switch (el.name) { case 'a': - if (el.attribs.href) { + if (el.attribs && el.attribs.href) { out += '<' + el.attribs.href + '|' + walk(el.children) + '>'; } else { From 03fac401e7898805704e7e9c3b71e73b3e1528e5 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Mon, 27 Nov 2017 12:34:43 -0500 Subject: [PATCH 12/69] better handling for bolds and paragraphs for the new editor --- slackify-html.js | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 5b7cf59..c52f014 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -146,13 +146,41 @@ function walk(dom, nesting) { out += walk(el.children); } break; + case 'h1': + case 'h2': + case 'h3': + case 'h4': case 'strong': case 'b': - out += '*' + walk(el.children) + '*'; + var addSpace = false; + content = walk(el.children); + if (content && content.charAt(content.length - 1) === ' ') { + content = content.substring(content, 0, content.length - 1); + addSpace = true; + } + out += '*' + content + '*'; + if (addSpace) { + out += ' '; + } break; case 'i': case 'em': - out += '_' + walk(el.children) + '_'; + var addSpace = false; + content = walk(el.children); + if (content && content.charAt(content.length - 1) === ' ') { + content = content.substring(content, 0, content.length - 1); + addSpace = true; + } + out += '_' + content + '_'; + if (addSpace) { + out += ' '; + } + break; + case 'div': + out += walk(el.children); + if (el.attribs && el.attribs.class === 'ghq-card-content__paragraph') { + out += '\n'; + } break; case 'p': out += walk(el.children) + '\n'; @@ -160,12 +188,6 @@ function walk(dom, nesting) { case 'br': out += walk(el.children) + '\n'; break; - case 'h1': - case 'h2': - case 'h3': - case 'h4': - out += '*' + walk(el.children) + '*'; - break; case 'ol': case 'ul': out += walkList(el.children, 'ol' === el.name, nesting); From e29caa46200b690eccf65cfbaee5ba5204558ead Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Mon, 27 Nov 2017 12:51:59 -0500 Subject: [PATCH 13/69] better handling for bolds and paragraphs for the new editor --- slackify-html.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index c52f014..c9d3192 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -155,7 +155,7 @@ function walk(dom, nesting) { var addSpace = false; content = walk(el.children); if (content && content.charAt(content.length - 1) === ' ') { - content = content.substring(content, 0, content.length - 1); + content = content.substr(0, content.length - 1); addSpace = true; } out += '*' + content + '*'; @@ -168,7 +168,7 @@ function walk(dom, nesting) { var addSpace = false; content = walk(el.children); if (content && content.charAt(content.length - 1) === ' ') { - content = content.substring(content, 0, content.length - 1); + content = content.substr(0, content.length - 1); addSpace = true; } out += '_' + content + '_'; From 803a5e4699cc07afbe22b16dea6362012122db8c Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Mon, 27 Nov 2017 12:57:35 -0500 Subject: [PATCH 14/69] better handling for bolds and paragraphs for the new editor --- slackify-html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index c9d3192..ef9efc9 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -196,7 +196,7 @@ function walk(dom, nesting) { out += '`' + walk(el.children) + '`'; break; case 'pre': - out += '```\n' + walkPre(el.children) + '```'; + out += '```\n' + walkPre(el.children) + '```\n'; break; case 'table': out += walkTable(el.children); From fb794e62bedc79e33bf43ef1a7278cf5a1f3fb62 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Mon, 27 Nov 2017 13:22:29 -0500 Subject: [PATCH 15/69] handle pre spaces as well --- slackify-html.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index ef9efc9..1f32931 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -152,27 +152,43 @@ function walk(dom, nesting) { case 'h4': case 'strong': case 'b': - var addSpace = false; + var prefixSpace = false; + var suffixSpace = false; content = walk(el.children); + if (content && content.charAt(0) === ' ') { + content = content.substr(1, content.length); + prefixSpace = true; + } if (content && content.charAt(content.length - 1) === ' ') { content = content.substr(0, content.length - 1); - addSpace = true; + suffixSpace = true; + } + if (prefixSpace) { + out += ' '; } out += '*' + content + '*'; - if (addSpace) { + if (suffixSpace) { out += ' '; } break; case 'i': case 'em': - var addSpace = false; + var prefixSpace = false; + var suffixSpace = false; content = walk(el.children); + if (content && content.charAt(0) === ' ') { + content = content.substr(1, content.length); + prefixSpace = true; + } if (content && content.charAt(content.length - 1) === ' ') { content = content.substr(0, content.length - 1); - addSpace = true; + suffixSpace = true; + } + if (prefixSpace) { + out += ' '; } out += '_' + content + '_'; - if (addSpace) { + if (suffixSpace) { out += ' '; } break; From e8748b86ea49fc77d5352aba3bbc711640e99e10 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 28 Nov 2017 14:53:04 -0500 Subject: [PATCH 16/69] debugging --- slackify-html.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/slackify-html.js b/slackify-html.js index 1f32931..acb9d94 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -155,6 +155,7 @@ function walk(dom, nesting) { var prefixSpace = false; var suffixSpace = false; content = walk(el.children); + console.log("content1: " + content); if (content && content.charAt(0) === ' ') { content = content.substr(1, content.length); prefixSpace = true; @@ -163,6 +164,7 @@ function walk(dom, nesting) { content = content.substr(0, content.length - 1); suffixSpace = true; } + console.log("content2: " + content); if (prefixSpace) { out += ' '; } From 44fce1e9416b4d7e2310dec51b8299bb58d17387 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 28 Nov 2017 15:00:17 -0500 Subject: [PATCH 17/69] debugging --- slackify-html.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index acb9d94..b0df26b 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -155,7 +155,7 @@ function walk(dom, nesting) { var prefixSpace = false; var suffixSpace = false; content = walk(el.children); - console.log("content1: " + content); + console.log(el.name + " content1: " + content); if (content && content.charAt(0) === ' ') { content = content.substr(1, content.length); prefixSpace = true; @@ -165,13 +165,15 @@ function walk(dom, nesting) { suffixSpace = true; } console.log("content2: " + content); + var innerOutput = ''; if (prefixSpace) { - out += ' '; + innerOutput += ' '; } - out += '*' + content + '*'; + innerOutput += '*' + content + '*'; if (suffixSpace) { - out += ' '; + innerOutput += ' '; } + out += innerOutput; break; case 'i': case 'em': From 81523b491b4f671a8f0da32c6a9b688d8dda6505 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 28 Nov 2017 15:03:46 -0500 Subject: [PATCH 18/69] debugging --- slackify-html.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index b0df26b..af6de43 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -164,7 +164,7 @@ function walk(dom, nesting) { content = content.substr(0, content.length - 1); suffixSpace = true; } - console.log("content2: " + content); + console.log(el.name + " content2: " + content); var innerOutput = ''; if (prefixSpace) { innerOutput += ' '; @@ -173,6 +173,7 @@ function walk(dom, nesting) { if (suffixSpace) { innerOutput += ' '; } + console.log(el.name + " innerOutput: " + innerOutput); out += innerOutput; break; case 'i': From dc0ce690b656e25a2f53fc341df185c8d992620d Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 28 Nov 2017 15:10:48 -0500 Subject: [PATCH 19/69] debugging --- slackify-html.js | 1 + 1 file changed, 1 insertion(+) diff --git a/slackify-html.js b/slackify-html.js index af6de43..78ceec2 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -155,6 +155,7 @@ function walk(dom, nesting) { var prefixSpace = false; var suffixSpace = false; content = walk(el.children); + content = content.replace(/\n/g, ''); console.log(el.name + " content1: " + content); if (content && content.charAt(0) === ' ') { content = content.substr(1, content.length); From 7534c6cfe0d56abdbef0a95875ceac2660d3bcc8 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 28 Nov 2017 15:15:12 -0500 Subject: [PATCH 20/69] add a newline after headers --- slackify-html.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 78ceec2..7211880 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -156,7 +156,6 @@ function walk(dom, nesting) { var suffixSpace = false; content = walk(el.children); content = content.replace(/\n/g, ''); - console.log(el.name + " content1: " + content); if (content && content.charAt(0) === ' ') { content = content.substr(1, content.length); prefixSpace = true; @@ -165,7 +164,6 @@ function walk(dom, nesting) { content = content.substr(0, content.length - 1); suffixSpace = true; } - console.log(el.name + " content2: " + content); var innerOutput = ''; if (prefixSpace) { innerOutput += ' '; @@ -174,8 +172,15 @@ function walk(dom, nesting) { if (suffixSpace) { innerOutput += ' '; } - console.log(el.name + " innerOutput: " + innerOutput); out += innerOutput; + switch (el.name) { + case 'h1': + case 'h2': + case 'h3': + case 'h4': + out += '\n'; + break; + } break; case 'i': case 'em': From 5456a3f4cc466bd3e85faaf39bed12737bdac621 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 28 Nov 2017 15:43:53 -0500 Subject: [PATCH 21/69] handle newlines in bold/italics --- slackify-html.js | 63 ++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 7211880..6e4adb8 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -152,27 +152,34 @@ function walk(dom, nesting) { case 'h4': case 'strong': case 'b': - var prefixSpace = false; - var suffixSpace = false; content = walk(el.children); - content = content.replace(/\n/g, ''); - if (content && content.charAt(0) === ' ') { - content = content.substr(1, content.length); - prefixSpace = true; - } - if (content && content.charAt(content.length - 1) === ' ') { - content = content.substr(0, content.length - 1); - suffixSpace = true; - } - var innerOutput = ''; - if (prefixSpace) { - innerOutput += ' '; - } - innerOutput += '*' + content + '*'; - if (suffixSpace) { - innerOutput += ' '; + var contentArr = content.split('\n'); + for (content in contentArr) { + if (content.trim() === '') { + out += '\n'; + continue; + } + var prefixSpace = false; + var suffixSpace = false; + if (content && content.charAt(0) === ' ') { + content = content.substr(1, content.length); + prefixSpace = true; + } + if (content && content.charAt(content.length - 1) === ' ') { + content = content.substr(0, content.length - 1); + suffixSpace = true; + } + var innerOutput = ''; + if (prefixSpace) { + innerOutput += ' '; + } + innerOutput += '*' + content + '*'; + if (suffixSpace) { + innerOutput += ' '; + } + out += innerOutput; + out += '\n'; } - out += innerOutput; switch (el.name) { case 'h1': case 'h2': @@ -184,9 +191,15 @@ function walk(dom, nesting) { break; case 'i': case 'em': + content = walk(el.children); + var contentArr = content.split('\n'); + for (content in contentArr) { + if (content.trim() === '') { + out += '\n'; + continue; + } var prefixSpace = false; var suffixSpace = false; - content = walk(el.children); if (content && content.charAt(0) === ' ') { content = content.substr(1, content.length); prefixSpace = true; @@ -195,13 +208,17 @@ function walk(dom, nesting) { content = content.substr(0, content.length - 1); suffixSpace = true; } + var innerOutput = ''; if (prefixSpace) { - out += ' '; + innerOutput += ' '; } - out += '_' + content + '_'; + innerOutput += '_' + content + '_'; if (suffixSpace) { - out += ' '; + innerOutput += ' '; } + out += innerOutput; + out += '\n'; + } break; case 'div': out += walk(el.children); From b1db11a50ba91d6db614e496f014882f7eb9eed8 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 28 Nov 2017 15:54:19 -0500 Subject: [PATCH 22/69] handle newlines in bold/italics --- slackify-html.js | 102 ++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 6e4adb8..270c6b7 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -154,31 +154,32 @@ function walk(dom, nesting) { case 'b': content = walk(el.children); var contentArr = content.split('\n'); - for (content in contentArr) { - if (content.trim() === '') { - out += '\n'; - continue; - } - var prefixSpace = false; - var suffixSpace = false; - if (content && content.charAt(0) === ' ') { - content = content.substr(1, content.length); - prefixSpace = true; - } - if (content && content.charAt(content.length - 1) === ' ') { - content = content.substr(0, content.length - 1); - suffixSpace = true; - } - var innerOutput = ''; - if (prefixSpace) { - innerOutput += ' '; + for (var i=0; i Date: Tue, 28 Nov 2017 15:56:59 -0500 Subject: [PATCH 23/69] handle newlines in bold/italics --- slackify-html.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 270c6b7..6edfd75 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -154,7 +154,7 @@ function walk(dom, nesting) { case 'b': content = walk(el.children); var contentArr = content.split('\n'); - for (var i=0; i Date: Tue, 28 Nov 2017 16:00:07 -0500 Subject: [PATCH 24/69] handle newlines in bold/italics --- slackify-html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 6edfd75..133f2a9 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -211,7 +211,7 @@ function walk(dom, nesting) { if (prefixSpace) { innerOutput += ' '; } - innerOutput += '*' + content + '*'; + innerOutput += '_' + content + '_'; if (suffixSpace) { innerOutput += ' '; } From d502e850642facdeeb5b7ac6ddcc839c8a42003a Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 28 Nov 2017 16:18:14 -0500 Subject: [PATCH 25/69] fix double bolding --- slackify-html.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 133f2a9..b7f31de 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -154,6 +154,7 @@ function walk(dom, nesting) { case 'b': content = walk(el.children); var contentArr = content.split('\n'); + var innerOutput = ''; for (var i=0; i Date: Tue, 28 Nov 2017 16:20:52 -0500 Subject: [PATCH 26/69] fix double bolding --- slackify-html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index b7f31de..2de751f 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -174,7 +174,7 @@ function walk(dom, nesting) { } if (content.charAt(0) === '*' && content.charAt(content.length - 1) === '*') { - innerOutput =+ content; + innerOutput += content; } else { innerOutput += '*' + content + '*'; From c0bbf4ea337186654657ed961dfa3ab9816f81d8 Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Tue, 19 Dec 2017 12:25:21 -0500 Subject: [PATCH 27/69] fix links in table cells --- slackify-html.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 2de751f..0679cab 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -115,7 +115,7 @@ function walkTableBody(dom) { out += el.data; } else if ('td' === el.name) { - out += '| ' + walkTableBody(el.children) + ' '; + out += '| ' + walk(el.children) + ' '; } else if ('tr' === el.name) { out += walkTableBody(el.children) + '|\n'; From 30661a7ee14e411f7dc2f3cc7f35f21459dad43c Mon Sep 17 00:00:00 2001 From: Peter Michel Date: Wed, 14 Feb 2018 21:00:51 -0500 Subject: [PATCH 28/69] fix bug with multiple elements in b/strong/em/etc --- slackify-html.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 0679cab..e44365e 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -168,7 +168,6 @@ function walk(dom, nesting) { content = content.substr(0, content.length - 1); suffixSpace = true; } - var innerOutput = ''; if (prefixSpace) { innerOutput += ' '; } @@ -202,6 +201,7 @@ function walk(dom, nesting) { case 'em': content = walk(el.children); var contentArr = content.split('\n'); + var innerOutput = ''; for (var i=0; i Date: Wed, 14 Feb 2018 21:01:07 -0500 Subject: [PATCH 29/69] add simple tester --- tester.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tester.js diff --git a/tester.js b/tester.js new file mode 100644 index 0000000..cc56a0f --- /dev/null +++ b/tester.js @@ -0,0 +1,8 @@ +var slackify = require('./slackify-html'); +var fs = require('fs'); +fs.readFile('/Users/petermichel/Desktop/delme.html', 'utf8', function (err,data) { + if (err) { + return console.log(err); + } + console.log(slackify(data)); +}); From b7feba3367aebe6571c4f57ac0a746e791a7017c Mon Sep 17 00:00:00 2001 From: Austin Park Date: Mon, 3 Feb 2020 13:40:34 -0500 Subject: [PATCH 30/69] added start param --- slackify-html.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index e44365e..41c2429 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -16,10 +16,10 @@ module.exports = function slackify(html) { return ''; }; -function walkList(dom, ordered, nesting) { +function walkList(dom, ordered, nesting, start) { var out = ''; if (dom) { - var listItemIndex = 1; + var listItemIndex = start ? start : 1; dom.forEach(function (el) { if ('text' === el.type && el.data.trim() !== '') { out += el.data; @@ -243,7 +243,7 @@ function walk(dom, nesting) { break; case 'ol': case 'ul': - out += walkList(el.children, 'ol' === el.name, nesting); + out += walkList(el.children, 'ol' === el.name, nesting, el.attribs.start); break; case 'code': out += '`' + walk(el.children) + '`'; From 32cba6728b1c773b08c9e03764d42702d75a3e06 Mon Sep 17 00:00:00 2001 From: Austin Park Date: Mon, 24 Feb 2020 10:49:19 -0500 Subject: [PATCH 31/69] add null check --- slackify-html.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 41c2429..01d9fb9 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -243,7 +243,8 @@ function walk(dom, nesting) { break; case 'ol': case 'ul': - out += walkList(el.children, 'ol' === el.name, nesting, el.attribs.start); + var startIndex = (el.attribs) ? el.attribs.start : false; + out += walkList(el.children, 'ol' === el.name, nesting, startIndex); break; case 'code': out += '`' + walk(el.children) + '`'; From c9e34ab57d2e425ed7b10803bde8634f9486d1f7 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 27 Apr 2020 14:55:26 -0400 Subject: [PATCH 32/69] fix failing test case --- tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests.js b/tests.js index c3e1d37..05b6b88 100644 --- a/tests.js +++ b/tests.js @@ -80,7 +80,7 @@ tap.test('full example', function vcheck(t) { `; -var expected = '*Security Overview Header*\n*We take the security of your data very seriously!*\n\n In order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, where, when and how we protect your data.\n\n Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n1. foo\n2. bar\n3. buz\n\nand this list too...\n* _abc_\n * sub 1\n * sub 2\n* def\n* xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n![](https://qaup.getguru.com/5240119b-9752-443a-9172-73204f8599eb/94acdc58-32c2-44d3-9843-7a2a5cb3fbf5.bc70afa3-1798-4c87-a2fe-21e3f855e35a.jpeg)\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |'; +var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• def\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; var output = slackify(input); t.equals(output, expected); From 14e0fc2f044552c90c2fc1ba70eec132a2ea038a Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 27 Apr 2020 14:55:58 -0400 Subject: [PATCH 33/69] include setup details in readme --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 674ef70..b6d05a8 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,8 @@ example usage: var slackify = require('slackify-html'); var text = slackify('this link is important'); -// text variable contains 'this is *important*' +// text variable contains 'this is *important*' ``` + +### How to setup repo and test locally +https://app.getguru.com/card/TqGjya8c/slackifyhtml-basic-setup From 3eaf96bb933afb7e53c63ea69d4727b01fbb4267 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 27 Apr 2020 14:56:25 -0400 Subject: [PATCH 34/69] ignore package-lock.json --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 123ae94..69c56eb 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules + +# Package +package-lock.json From 8e4cdf565b8587a88f16e190d373cb6ecc1dbe51 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 27 Apr 2020 18:52:36 -0400 Subject: [PATCH 35/69] fix bold text within h1 header --- slackify-html.js | 21 +++++++++++++++++++++ tests.js | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/slackify-html.js b/slackify-html.js index 01d9fb9..8b158c7 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -125,6 +125,19 @@ function walkTableBody(dom) { return out; } +//checks and fixes h1 output with too many asterisks +function checkH1Output(output) { + asteriskSplit = output.split('*'); + if (asteriskSplit.length > 3) { + replaceOutput = ''; + asteriskSplit.forEach(function(text) { + replaceOutput += text; + }); + output = '*' + replaceOutput + '*'; + } + return output; +} + function walk(dom, nesting) { if (!nesting) { @@ -181,6 +194,14 @@ function walk(dom, nesting) { if (suffixSpace) { innerOutput += ' '; } + switch (el.name) { + case 'h1': + innerOutput = checkH1Output(innerOutput); + break; + case 'h2': + case 'h3': + case 'h4': + } } if (i < contentArr.length - 1) { innerOutput += '\n'; diff --git a/tests.js b/tests.js index c3e1d37..fb6a7db 100644 --- a/tests.js +++ b/tests.js @@ -31,6 +31,11 @@ tap.test('vcheck example', function vcheck(t) { t.end(); }); +tap.test('fix header h1 with bold text', function headers(t) { + t.equals(slackify('

h1 with bold text

'), '*h1 with bold text*\n'); + t.end(); +}); + tap.test('full example', function vcheck(t) { var input = `

Security Overview Header

We take the security of your data very seriously!

From 01c85b01853c4649ca62ee6cdea0de785fc560c1 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 27 Apr 2020 19:00:35 -0400 Subject: [PATCH 36/69] apply fix for bold text to h1, h2, h3 --- slackify-html.js | 13 ++++--------- tests.js | 7 +++++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 8b158c7..fefb4bc 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -125,8 +125,8 @@ function walkTableBody(dom) { return out; } -//checks and fixes h1 output with too many asterisks -function checkH1Output(output) { +//checks and fixes header output with too many asterisks +function checkHeaderOutput(output) { asteriskSplit = output.split('*'); if (asteriskSplit.length > 3) { replaceOutput = ''; @@ -194,13 +194,8 @@ function walk(dom, nesting) { if (suffixSpace) { innerOutput += ' '; } - switch (el.name) { - case 'h1': - innerOutput = checkH1Output(innerOutput); - break; - case 'h2': - case 'h3': - case 'h4': + if (el.name === 'h1' || el.name === 'h2' || el.name === 'h3') { + innerOutput = checkHeaderOutput(innerOutput); } } if (i < contentArr.length - 1) { diff --git a/tests.js b/tests.js index fb6a7db..720029b 100644 --- a/tests.js +++ b/tests.js @@ -31,8 +31,11 @@ tap.test('vcheck example', function vcheck(t) { t.end(); }); -tap.test('fix header h1 with bold text', function headers(t) { - t.equals(slackify('

h1 with bold text

'), '*h1 with bold text*\n'); +tap.test('fix h1, h2, h3 with bold text', function headers(t) { + var input = `

h1 with bold text

h2 with bold text

h3 with bold text

`; + var expected = '*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n'; + var output = slackify(input); + t.equals(output, expected); t.end(); }); From 7f50e798dcf703f7cfd75409116a37584f6ac591 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 27 Apr 2020 19:03:51 -0400 Subject: [PATCH 37/69] rename header function to specific it's for bold text --- slackify-html.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index fefb4bc..7aee657 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -126,7 +126,7 @@ function walkTableBody(dom) { } //checks and fixes header output with too many asterisks -function checkHeaderOutput(output) { +function checkHeaderBoldOutput(output) { asteriskSplit = output.split('*'); if (asteriskSplit.length > 3) { replaceOutput = ''; @@ -195,7 +195,7 @@ function walk(dom, nesting) { innerOutput += ' '; } if (el.name === 'h1' || el.name === 'h2' || el.name === 'h3') { - innerOutput = checkHeaderOutput(innerOutput); + innerOutput = checkHeaderBoldOutput(innerOutput); } } if (i < contentArr.length - 1) { From 33b5c377679ceb2cd8cd015005b58bdba1e532fd Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Tue, 28 Apr 2020 10:05:10 -0400 Subject: [PATCH 38/69] refactor check for header with bold text and include h4 --- slackify-html.js | 21 ++++----------------- tests.js | 6 +++--- 2 files changed, 7 insertions(+), 20 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 7aee657..60498be 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -125,20 +125,6 @@ function walkTableBody(dom) { return out; } -//checks and fixes header output with too many asterisks -function checkHeaderBoldOutput(output) { - asteriskSplit = output.split('*'); - if (asteriskSplit.length > 3) { - replaceOutput = ''; - asteriskSplit.forEach(function(text) { - replaceOutput += text; - }); - output = '*' + replaceOutput + '*'; - } - return output; -} - - function walk(dom, nesting) { if (!nesting) { nesting = 0; @@ -188,15 +174,16 @@ function walk(dom, nesting) { content.charAt(content.length - 1) === '*') { innerOutput += content; } + else if (el.name === 'h1' || el.name === 'h2' || el.name === 'h3' || el.name === 'h4') { + content = content.replace(/\*/g, '') + innerOutput = '*' + content + '*'; + } else { innerOutput += '*' + content + '*'; } if (suffixSpace) { innerOutput += ' '; } - if (el.name === 'h1' || el.name === 'h2' || el.name === 'h3') { - innerOutput = checkHeaderBoldOutput(innerOutput); - } } if (i < contentArr.length - 1) { innerOutput += '\n'; diff --git a/tests.js b/tests.js index 720029b..4de599d 100644 --- a/tests.js +++ b/tests.js @@ -31,9 +31,9 @@ tap.test('vcheck example', function vcheck(t) { t.end(); }); -tap.test('fix h1, h2, h3 with bold text', function headers(t) { - var input = `

h1 with bold text

h2 with bold text

h3 with bold text

`; - var expected = '*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n'; +tap.test('fix h1, h2, h3, h4 with bold text', function headers(t) { + var input = `

h1 with bold text

h2 with bold text

h3 with bold text

h4 with bold text

`; + var expected = '*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n'; var output = slackify(input); t.equals(output, expected); t.end(); From 6689a43a91b976bda726edd72a5fb6001b921b86 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Tue, 28 Apr 2020 11:25:58 -0400 Subject: [PATCH 39/69] move changes to h1, h2, h3, h4 switch cases --- slackify-html.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 60498be..d33df2c 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -146,9 +146,25 @@ function walk(dom, nesting) { } break; case 'h1': + content = walk(el.children); + content = content.replace(/\*/g, ''); + out += '*' + content + '*\n'; + break; case 'h2': + content = walk(el.children); + content = content.replace(/\*/g, '') + out += '*' + content + '*\n'; + break; case 'h3': + content = walk(el.children); + content = content.replace(/\*/g, '') + out += '*' + content + '*\n'; + break; case 'h4': + content = walk(el.children); + content = content.replace(/\*/g, '') + out += '*' + content + '*\n'; + break; case 'strong': case 'b': content = walk(el.children); @@ -174,10 +190,6 @@ function walk(dom, nesting) { content.charAt(content.length - 1) === '*') { innerOutput += content; } - else if (el.name === 'h1' || el.name === 'h2' || el.name === 'h3' || el.name === 'h4') { - content = content.replace(/\*/g, '') - innerOutput = '*' + content + '*'; - } else { innerOutput += '*' + content + '*'; } From 3e4a28afb2342ad391ebda8f8df09d9b2137756e Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Tue, 28 Apr 2020 15:43:17 -0400 Subject: [PATCH 40/69] switch the check back inside of 'b' so whitespacing is maintained --- slackify-html.js | 20 ++++---------------- tests.js | 26 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index d33df2c..70fd6d8 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -146,25 +146,9 @@ function walk(dom, nesting) { } break; case 'h1': - content = walk(el.children); - content = content.replace(/\*/g, ''); - out += '*' + content + '*\n'; - break; case 'h2': - content = walk(el.children); - content = content.replace(/\*/g, '') - out += '*' + content + '*\n'; - break; case 'h3': - content = walk(el.children); - content = content.replace(/\*/g, '') - out += '*' + content + '*\n'; - break; case 'h4': - content = walk(el.children); - content = content.replace(/\*/g, '') - out += '*' + content + '*\n'; - break; case 'strong': case 'b': content = walk(el.children); @@ -190,6 +174,10 @@ function walk(dom, nesting) { content.charAt(content.length - 1) === '*') { innerOutput += content; } + else if (el.name === 'h1' || el.name === 'h2' || el.name === 'h3' || el.name === 'h4') { + content = content.replace(/\*/g, ''); + innerOutput += '*' + content + '*'; + } else { innerOutput += '*' + content + '*'; } diff --git a/tests.js b/tests.js index 7f4d3d9..22c0a81 100644 --- a/tests.js +++ b/tests.js @@ -31,16 +31,26 @@ tap.test('vcheck example', function vcheck(t) { t.end(); }); -tap.test('fix h1, h2, h3, h4 with bold text', function headers(t) { - var input = `

h1 with bold text

h2 with bold text

h3 with bold text

h4 with bold text

`; - var expected = '*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n'; - var output = slackify(input); - t.equals(output, expected); +tap.test('test bold text', function boldtext(t) { + t.equals(slackify('totally bold'), '*totally bold*'); + t.equals(slackify('

totally bold in paragraph

'), '*totally bold in paragraph*\n'); + t.equals(slackify('*already slackified bold*'), '*already slackified bold*'); + t.equals(slackify('

A sentence with bold text in between.

'), 'A sentence with *bold text* in between.\n'); + t.end(); +}); + +tap.test('test bold text with headers', function boldheaders(t) { + t.equals(slackify('

alternating bold header content

'), '*alternating bold header content* \n'); + t.equals(slackify('

too many *asterisks* bold text

'), '*too many asterisks bold text*\n'); + t.equals(slackify('

header3 bold tag continues

outside'), '*header3 bold tag continues* \n outside'); + var allHeadersInput = '

h1 with bold text

h2 with bold text

h3 with bold text

h4 with bold text

'; + var allHeadersExpected = '*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n'; + t.equals(slackify(allHeadersInput), allHeadersExpected); t.end(); }); tap.test('full example', function vcheck(t) { - var input = `

Security Overview Header

+ var input = `

Security Overview Header

We take the security of your data very seriously!

In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

@@ -59,7 +69,7 @@ tap.test('full example', function vcheck(t) {
  • sub 2
  • -
  • def
  • +
  • def
  • xyz
  • and this

    @@ -88,7 +98,7 @@ tap.test('full example', function vcheck(t) {
    `; -var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• def\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; +var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; var output = slackify(input); t.equals(output, expected); From f7bb03fc1800ef5966019db8eecee47a23a56a97 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Tue, 28 Apr 2020 15:47:48 -0400 Subject: [PATCH 41/69] update the test case --- tests.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests.js b/tests.js index 22c0a81..75b3211 100644 --- a/tests.js +++ b/tests.js @@ -43,9 +43,7 @@ tap.test('test bold text with headers', function boldheaders(t) { t.equals(slackify('

    alternating bold header content

    '), '*alternating bold header content* \n'); t.equals(slackify('

    too many *asterisks* bold text

    '), '*too many asterisks bold text*\n'); t.equals(slackify('

    header3 bold tag continues

    outside'), '*header3 bold tag continues* \n outside'); - var allHeadersInput = '

    h1 with bold text

    h2 with bold text

    h3 with bold text

    h4 with bold text

    '; - var allHeadersExpected = '*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n'; - t.equals(slackify(allHeadersInput), allHeadersExpected); + t.equals(slackify('

    h1 with bold text

    h2 with bold text

    h3 with bold text

    h4 with bold text

    '), '*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n'); t.end(); }); From bf87054f18b6dbe53a36e7069a634c8890811736 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Wed, 29 Apr 2020 10:09:42 -0400 Subject: [PATCH 42/69] swap if statements order since the current order can lead to failures --- slackify-html.js | 9 ++++----- tests.js | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 70fd6d8..07901ec 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -170,14 +170,13 @@ function walk(dom, nesting) { if (prefixSpace) { innerOutput += ' '; } - if (content.charAt(0) === '*' && - content.charAt(content.length - 1) === '*') { - innerOutput += content; - } - else if (el.name === 'h1' || el.name === 'h2' || el.name === 'h3' || el.name === 'h4') { + if (el.name === 'h1' || el.name === 'h2' || el.name === 'h3' || el.name === 'h4') { content = content.replace(/\*/g, ''); innerOutput += '*' + content + '*'; } + else if (content.charAt(0) === '*' && content.charAt(content.length - 1) === '*') { + innerOutput += content; + } else { innerOutput += '*' + content + '*'; } diff --git a/tests.js b/tests.js index 75b3211..4b5c73a 100644 --- a/tests.js +++ b/tests.js @@ -35,11 +35,17 @@ tap.test('test bold text', function boldtext(t) { t.equals(slackify('totally bold'), '*totally bold*'); t.equals(slackify('

    totally bold in paragraph

    '), '*totally bold in paragraph*\n'); t.equals(slackify('*already slackified bold*'), '*already slackified bold*'); + t.equals(slackify('*bold inside asterisks*'), '*bold *inside* asterisks*'); + t.equals(slackify('*asterisks *inside* asterisks*'), '*asterisks *inside* asterisks*'); t.equals(slackify('

    A sentence with bold text in between.

    '), 'A sentence with *bold text* in between.\n'); t.end(); }); tap.test('test bold text with headers', function boldheaders(t) { + t.equals(slackify('

    a completely bold title

    '), '*a completely bold title*\n'); + t.equals(slackify('

    a completely bold title

    '), '*a completely bold title*\n'); + t.equals(slackify('

    *asterisk title*

    '), '*asterisk title*\n'); + t.equals(slackify('

    *asterisk title with *bold**

    '), '*asterisk title with bold*\n'); t.equals(slackify('

    alternating bold header content

    '), '*alternating bold header content* \n'); t.equals(slackify('

    too many *asterisks* bold text

    '), '*too many asterisks bold text*\n'); t.equals(slackify('

    header3 bold tag continues

    outside'), '*header3 bold tag continues* \n outside'); From 3bf3f51ba15b09a42f4dfa885fd7983207b6334c Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Thu, 30 Apr 2020 20:24:24 -0400 Subject: [PATCH 43/69] add new line to each line in pre code block --- slackify-html.js | 2 +- tests.js | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 07901ec..18274a9 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -49,7 +49,7 @@ function walkPre(dom) { out += el.data; } else if ('tag' === el.type) { - out += walkPre(el.children); + out += walkPre(el.children) + '\n'; } }); } diff --git a/tests.js b/tests.js index 4b5c73a..ae86d04 100644 --- a/tests.js +++ b/tests.js @@ -53,6 +53,14 @@ tap.test('test bold text with headers', function boldheaders(t) { t.end(); }); +tap.test('test code block', function codeblock(t) { + var input = '
    {  "name": "slackify-html",  "version": "1.0.0",  "description": "convert simple html to slack markdown",  "main": "slackify-html.js",  "scripts": {    "test": "tap tests.js"  }}
    '; + var expected = '```\n{\n "name": "slackify-html",\n "version": "1.0.0",\n "description": "convert simple html to slack markdown",\n "main": "slackify-html.js",\n "scripts": {\n "test": "tap tests.js"\n }\n}\n```\n'; + var output = slackify(input); + t.equals(output,expected); + t.end(); +}); + tap.test('full example', function vcheck(t) { var input = `

    Security Overview Header

    We take the security of your data very seriously!

    @@ -102,7 +110,7 @@ tap.test('full example', function vcheck(t) {
    `; -var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; +var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\n\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; var output = slackify(input); t.equals(output, expected); From 386fdaba085ed93a333195f3b18fe93555008472 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Thu, 30 Apr 2020 20:47:27 -0400 Subject: [PATCH 44/69] add test for a code block with only text --- tests.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests.js b/tests.js index ae86d04..118dd89 100644 --- a/tests.js +++ b/tests.js @@ -61,6 +61,14 @@ tap.test('test code block', function codeblock(t) { t.end(); }); +tap.test('test code block text only', function codeblocktextonly(t) { + var input = '
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    '; + var expected = '```\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n```\n'; + var output = slackify(input); + t.equals(output,expected); + t.end(); +}); + tap.test('full example', function vcheck(t) { var input = `

    Security Overview Header

    We take the security of your data very seriously!

    From acaa519bd567009ff6847f11ada1f5806012d878 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Sun, 3 May 2020 13:41:10 -0400 Subject: [PATCH 45/69] add support for blockquote html tags --- slackify-html.js | 3 +++ tests.js | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/slackify-html.js b/slackify-html.js index 18274a9..d47ad69 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -261,6 +261,9 @@ function walk(dom, nesting) { var alt = el.attribs.alt; out += ''; break; + case 'blockquote': + out += '>' + walk(el.children); + break; default: out += walk(el.children); } diff --git a/tests.js b/tests.js index 118dd89..060cc21 100644 --- a/tests.js +++ b/tests.js @@ -69,6 +69,18 @@ tap.test('test code block text only', function codeblocktextonly(t) { t.end(); }); +tap.test('test blockquote', function blockquote(t) { + t.equals(slackify('
    block quote text
    '), '>block quote text'); + t.equals(slackify('
    block quote bold text
    '), '>block quote *bold* text'); + t.equals(slackify('
    block quote italic text
    '), '>block quote _italic_ text'); + t.equals(slackify('
    block quote underline text
    '), '>block quote underline text'); + t.equals(slackify('
    block quote strikethrough text
    '), '>block quote strikethrough text'); + t.equals(slackify('
    block quote highlight text
    '), '>block quote highlight text'); + t.equals(slackify('
    block quote color text
    '), '>block quote color text'); + t.equals(slackify('
    block quote guru code snippet
    '), '>`block quote guru code snippet`'); + t.end(); +}); + tap.test('full example', function vcheck(t) { var input = `

    Security Overview Header

    We take the security of your data very seriously!

    From c381c91013a57ad64bda6ddf0dfb72d4834abf92 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Sun, 3 May 2020 14:01:33 -0400 Subject: [PATCH 46/69] include tests for links/files in blockquote --- tests.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests.js b/tests.js index 060cc21..2b0d6e3 100644 --- a/tests.js +++ b/tests.js @@ -77,6 +77,8 @@ tap.test('test blockquote', function blockquote(t) { t.equals(slackify('
    block quote strikethrough text
    '), '>block quote strikethrough text'); t.equals(slackify('
    block quote highlight text
    '), '>block quote highlight text'); t.equals(slackify('
    block quote color text
    '), '>block quote color text'); + t.equals(slackify('
    block quote link
    '), '>block quote '); + t.equals(slackify('
    block quote file
    '), '>block quote file'); t.equals(slackify('
    block quote guru code snippet
    '), '>`block quote guru code snippet`'); t.end(); }); From acba013c81a3f47f8715d36816b77fbdc8f7e02a Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 4 May 2020 09:41:39 -0400 Subject: [PATCH 47/69] add new line to end of block quotes --- slackify-html.js | 2 +- tests.js | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index d47ad69..9315040 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -262,7 +262,7 @@ function walk(dom, nesting) { out += ''; break; case 'blockquote': - out += '>' + walk(el.children); + out += '>' + walk(el.children) + '\n'; break; default: out += walk(el.children); diff --git a/tests.js b/tests.js index 2b0d6e3..7102fb9 100644 --- a/tests.js +++ b/tests.js @@ -70,16 +70,16 @@ tap.test('test code block text only', function codeblocktextonly(t) { }); tap.test('test blockquote', function blockquote(t) { - t.equals(slackify('
    block quote text
    '), '>block quote text'); - t.equals(slackify('
    block quote bold text
    '), '>block quote *bold* text'); - t.equals(slackify('
    block quote italic text
    '), '>block quote _italic_ text'); - t.equals(slackify('
    block quote underline text
    '), '>block quote underline text'); - t.equals(slackify('
    block quote strikethrough text
    '), '>block quote strikethrough text'); - t.equals(slackify('
    block quote highlight text
    '), '>block quote highlight text'); - t.equals(slackify('
    block quote color text
    '), '>block quote color text'); - t.equals(slackify('
    block quote link
    '), '>block quote '); - t.equals(slackify('
    block quote file
    '), '>block quote file'); - t.equals(slackify('
    block quote guru code snippet
    '), '>`block quote guru code snippet`'); + t.equals(slackify('
    block quote text
    '), '>block quote text\n'); + t.equals(slackify('
    block quote bold text
    '), '>block quote *bold* text\n'); + t.equals(slackify('
    block quote italic text
    '), '>block quote _italic_ text\n'); + t.equals(slackify('
    block quote underline text
    '), '>block quote underline text\n'); + t.equals(slackify('
    block quote strikethrough text
    '), '>block quote strikethrough text\n'); + t.equals(slackify('
    block quote highlight text
    '), '>block quote highlight text\n'); + t.equals(slackify('
    block quote color text
    '), '>block quote color text\n'); + t.equals(slackify('
    block quote link
    '), '>block quote \n'); + t.equals(slackify('
    block quote file
    '), '>block quote file\n'); + t.equals(slackify('
    block quote guru code snippet
    '), '>`block quote guru code snippet`\n'); t.end(); }); From 0e3ccbf38254df9d2920c0f8e5874e3d0ff6f825 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 4 May 2020 09:59:25 -0400 Subject: [PATCH 48/69] add another new line since multiple block quotes can get turned into one block quote --- slackify-html.js | 2 +- tests.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 9315040..e1cf3d4 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -262,7 +262,7 @@ function walk(dom, nesting) { out += ''; break; case 'blockquote': - out += '>' + walk(el.children) + '\n'; + out += '>' + walk(el.children) + '\n\n'; break; default: out += walk(el.children); diff --git a/tests.js b/tests.js index 7102fb9..711080c 100644 --- a/tests.js +++ b/tests.js @@ -71,15 +71,15 @@ tap.test('test code block text only', function codeblocktextonly(t) { tap.test('test blockquote', function blockquote(t) { t.equals(slackify('
    block quote text
    '), '>block quote text\n'); - t.equals(slackify('
    block quote bold text
    '), '>block quote *bold* text\n'); - t.equals(slackify('
    block quote italic text
    '), '>block quote _italic_ text\n'); - t.equals(slackify('
    block quote underline text
    '), '>block quote underline text\n'); - t.equals(slackify('
    block quote strikethrough text
    '), '>block quote strikethrough text\n'); - t.equals(slackify('
    block quote highlight text
    '), '>block quote highlight text\n'); - t.equals(slackify('
    block quote color text
    '), '>block quote color text\n'); - t.equals(slackify('
    block quote link
    '), '>block quote \n'); - t.equals(slackify('
    block quote file
    '), '>block quote file\n'); - t.equals(slackify('
    block quote guru code snippet
    '), '>`block quote guru code snippet`\n'); + t.equals(slackify('
    block quote bold text
    '), '>block quote *bold* text\n\n'); + t.equals(slackify('
    block quote italic text
    '), '>block quote _italic_ text\n\n'); + t.equals(slackify('
    block quote underline text
    '), '>block quote underline text\n\n'); + t.equals(slackify('
    block quote strikethrough text
    '), '>block quote strikethrough text\n\n'); + t.equals(slackify('
    block quote highlight text
    '), '>block quote highlight text\n\n'); + t.equals(slackify('
    block quote color text
    '), '>block quote color text\n\n'); + t.equals(slackify('
    block quote link
    '), '>block quote \n\n'); + t.equals(slackify('
    block quote file
    '), '>block quote file\n\n'); + t.equals(slackify('
    block quote guru code snippet
    '), '>`block quote guru code snippet`\n\n'); t.end(); }); From 1469d5ca80c1441a598c9e25939eddb90ce6e362 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 4 May 2020 10:04:21 -0400 Subject: [PATCH 49/69] add second new line to test --- tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests.js b/tests.js index 711080c..129567c 100644 --- a/tests.js +++ b/tests.js @@ -70,7 +70,7 @@ tap.test('test code block text only', function codeblocktextonly(t) { }); tap.test('test blockquote', function blockquote(t) { - t.equals(slackify('
    block quote text
    '), '>block quote text\n'); + t.equals(slackify('
    block quote text
    '), '>block quote text\n\n'); t.equals(slackify('
    block quote bold text
    '), '>block quote *bold* text\n\n'); t.equals(slackify('
    block quote italic text
    '), '>block quote _italic_ text\n\n'); t.equals(slackify('
    block quote underline text
    '), '>block quote underline text\n\n'); From df13c8b19c83884b364cfa1d500e99b8db905344 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Wed, 6 May 2020 09:48:29 -0400 Subject: [PATCH 50/69] add a couple more blockquote tests --- tests.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests.js b/tests.js index 129567c..eb390e3 100644 --- a/tests.js +++ b/tests.js @@ -80,6 +80,7 @@ tap.test('test blockquote', function blockquote(t) { t.equals(slackify('
    block quote link
    '), '>block quote \n\n'); t.equals(slackify('
    block quote file
    '), '>block quote file\n\n'); t.equals(slackify('
    block quote guru code snippet
    '), '>`block quote guru code snippet`\n\n'); + t.equals(slackify('
    block quote with trailing newlines\n\n
    '), '>block quote with trailing newlines\n\n\n\n'); t.end(); }); @@ -88,8 +89,7 @@ tap.test('full example', function vcheck(t) {

    We take the security of your data very seriously!

    In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

    Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

    -

    Privacy Policy

    -

    Remember this list

    +

    Privacy Policy

    Here's a test blockquote with bolded information

    Remember this list

    1. foo
    2. bar
    3. @@ -132,7 +132,7 @@ tap.test('full example', function vcheck(t) {
    `; -var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\n\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; +var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n>Here\'s a test blockquote *with bolded* information\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\n\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; var output = slackify(input); t.equals(output, expected); From 45da5db11dde61ce22d3881b1afb2c68f82bf309 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Mon, 11 May 2020 16:13:06 -0400 Subject: [PATCH 51/69] handle embedded new lines and line breaks in blockquote --- slackify-html.js | 14 +++++++++++++- tests.js | 18 ++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index e1cf3d4..5cdf16b 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -262,7 +262,19 @@ function walk(dom, nesting) { out += ''; break; case 'blockquote': - out += '>' + walk(el.children) + '\n\n'; + content = walk(el.children); + var innerOutput = ''; + var contentArr = content.split('\n'); + contentArr.forEach((item) => { + if (el.name === 'br' || el.name === 'p') { + innerOutput += '>' + item; + } + innerOutput += '>' + item + '\n'; + }); + if (innerOutput.endsWith('\n>\n')) { + innerOutput = innerOutput.substr(0, innerOutput.length - 2); + } + out += innerOutput + '\n'; break; default: out += walk(el.children); diff --git a/tests.js b/tests.js index eb390e3..504fc38 100644 --- a/tests.js +++ b/tests.js @@ -69,7 +69,22 @@ tap.test('test code block text only', function codeblocktextonly(t) { t.end(); }); -tap.test('test blockquote', function blockquote(t) { +tap.test('test blockquote with line breaks/new lines', function blockquotenewlines(t) { + t.equals(slackify('
    block quote with
    line
    breaks
    '), '>block quote with \n>line\n>breaks\n\n'); + t.equals(slackify('
    block quote with embedded\n\n newlines
    '), '>block quote with embedded\n>\n> newlines\n\n'); + t.equals(slackify('
    block quote with trailing newlines\n\n
    '), '>block quote with trailing newlines\n>\n\n'); + t.end(); +}); + +tap.test('test blockquote with paragraphs and line breaks/new lines', function blockquoteparagraphs(t) { + t.equals(slackify('

    paragraph in blockquote

    '), '>paragraph in blockquote\n\n'); + t.equals(slackify('

    paragraph
    with
    line break blockquote

    '), '>paragraph\n>with\n>line break blockquote\n\n'); + t.equals(slackify('

    paragraph block quote with embedded\n\n newlines

    '), '>paragraph block quote with embedded\n>\n> newlines\n\n'); + t.equals(slackify('

    paragraph block quote with trailing newlines\n\n

    '), '>paragraph block quote with trailing newlines\n>\n>\n\n'); + t.end(); +}) + +tap.test('test guru blockquote', function blockquote(t) { t.equals(slackify('
    block quote text
    '), '>block quote text\n\n'); t.equals(slackify('
    block quote bold text
    '), '>block quote *bold* text\n\n'); t.equals(slackify('
    block quote italic text
    '), '>block quote _italic_ text\n\n'); @@ -80,7 +95,6 @@ tap.test('test blockquote', function blockquote(t) { t.equals(slackify('
    block quote link
    '), '>block quote \n\n'); t.equals(slackify('
    block quote file
    '), '>block quote file\n\n'); t.equals(slackify('
    block quote guru code snippet
    '), '>`block quote guru code snippet`\n\n'); - t.equals(slackify('
    block quote with trailing newlines\n\n
    '), '>block quote with trailing newlines\n\n\n\n'); t.end(); }); From 0353a5be6380c1e66fd1b84f3293b6da9738ee09 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Tue, 12 May 2020 14:23:02 -0400 Subject: [PATCH 52/69] add statement to else block --- slackify-html.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 5cdf16b..93b8152 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -268,8 +268,9 @@ function walk(dom, nesting) { contentArr.forEach((item) => { if (el.name === 'br' || el.name === 'p') { innerOutput += '>' + item; + } else { + innerOutput += '>' + item + '\n'; } - innerOutput += '>' + item + '\n'; }); if (innerOutput.endsWith('\n>\n')) { innerOutput = innerOutput.substr(0, innerOutput.length - 2); From e2dbdbc36e0a8d0f1d769f6aef820e0f5b80ae62 Mon Sep 17 00:00:00 2001 From: Maggie Lin Date: Thu, 14 May 2020 10:07:14 -0400 Subject: [PATCH 53/69] upgrade tap to fix 13 security vulnerabilties --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7625649..4f13ef1 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,6 @@ "htmlparser": "^1.7.7" }, "devDependencies": { - "tap": "^1.4.0" + "tap": "^14.10.7" } } From 3eb55eb01fa2c8a2ea12e8d7e645651ed2f301bd Mon Sep 17 00:00:00 2001 From: Julian Krispel-Samsel Date: Fri, 8 Jul 2022 13:17:45 +0100 Subject: [PATCH 54/69] accomodate nodes inside table elements, add test case --- slackify-html.js | 4 ++-- tests.js | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 93b8152..4e80550 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -115,10 +115,10 @@ function walkTableBody(dom) { out += el.data; } else if ('td' === el.name) { - out += '| ' + walk(el.children) + ' '; + out += "| " + walk(el.children, 0, true) + " "; } else if ('tr' === el.name) { - out += walkTableBody(el.children) + '|\n'; + out += walkTableBody(el.children).replace(/\n/gi, " ") + "|\n"; } }); } diff --git a/tests.js b/tests.js index 504fc38..c011603 100644 --- a/tests.js +++ b/tests.js @@ -147,8 +147,21 @@ tap.test('full example', function vcheck(t) {
    `; var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n>Here\'s a test blockquote *with bolded* information\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\n\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; -var output = slackify(input); + var output = slackify(input); t.equals(output, expected); t.end(); }); + + + +tap.test("tables with paragraph", function vcheck(t) { + var input = '

    one

    dwqtwo

    three

    dwqdwq

    dwqdwqdwq

    dwqdwqdwqdwq

    ' + var expected = `| one | dwqtwo | three | +| dwqdwq | dwqdwqdwq | dwqdwqdwqdwq | +`; + + var output = slackify(input); + t.equal(output, expected); + t.end(); +}); From 6c5ff5ae00a918b0bae74b88d187d68be661c139 Mon Sep 17 00:00:00 2001 From: Eddy Yeung Date: Mon, 2 Jan 2023 15:36:39 -0500 Subject: [PATCH 55/69] first pass --- package.json | 3 +- slackify-html.js | 247 ++++++++++++++++++++++-------------------- test.js | 276 ++++++++++++++++++++++++++++++++++++++++++++++ tests.js | 277 +++++++++++++++++++++++++++++++++++------------ 4 files changed, 613 insertions(+), 190 deletions(-) create mode 100644 test.js diff --git a/package.json b/package.json index 4f13ef1..400e2cb 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "convert simple html to slack markdown", "main": "slackify-html.js", "scripts": { - "test": "tap tests.js" + "test": "jest test.js" }, "repository": { "type": "git", @@ -29,6 +29,7 @@ "htmlparser": "^1.7.7" }, "devDependencies": { + "jest": "^29.3.1", "tap": "^14.10.7" } } diff --git a/slackify-html.js b/slackify-html.js index 4e80550..c69c1a7 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -1,5 +1,5 @@ -var htmlparser = require('htmlparser'), - Entities = require('html-entities').AllHtmlEntities; +var htmlparser = require("htmlparser"), + Entities = require("html-entities").AllHtmlEntities; entities = new Entities(); @@ -10,30 +10,37 @@ module.exports = function slackify(html) { var parser = new htmlparser.Parser(handler); parser.parseComplete(html); var dom = handler.dom; - if (dom) - return entities.decode(walk(dom)); - else - return ''; + if (dom) return entities.decode(walk(dom)); + else return ""; }; function walkList(dom, ordered, nesting, start) { - var out = ''; + var out = ""; if (dom) { var listItemIndex = start ? start : 1; dom.forEach(function (el) { - if ('text' === el.type && el.data.trim() !== '') { + if ("text" === el.type && el.data.trim() !== "") { out += el.data; - } - else if ('tag' === el.type) { + } else if ("tag" === el.type) { switch (el.name) { - case 'li': - for (i=0; i < nesting * 2; i++) { - out += ' '; + case "li": + // Add indentation based on the nesting level + for (i = 0; i < nesting * 2; i++) { + out += " "; } - out += (ordered ? listItemIndex++ + '. ' : "• ") + walk(el.children, nesting + 1) + '\n'; + // Add the bullet or number, followed by the text content of the li element + out += + (ordered ? listItemIndex++ + ". " : "• ") + + walkList(el.children, ordered, nesting) + + "\n"; + break; + case "p": + // Add the text content of the p element + out += walkList(el.children, ordered, nesting) + "\n"; break; default: - out += walk(el.children, nesting + 1); + // Recursively process the children of the element + out += walkList(el.children, ordered, nesting + 1); } } }); @@ -42,14 +49,13 @@ function walkList(dom, ordered, nesting, start) { } function walkPre(dom) { - var out = ''; + var out = ""; if (dom) { dom.forEach(function (el) { - if ('text' === el.type) { + if ("text" === el.type) { out += el.data; - } - else if ('tag' === el.type) { - out += walkPre(el.children) + '\n'; + } else if ("tag" === el.type) { + out += walkPre(el.children) + "\n"; } }); } @@ -57,14 +63,13 @@ function walkPre(dom) { } function walkTable(dom) { - var out = ''; + var out = ""; if (dom) { dom.forEach(function (el) { - if ('tag' === el.type) { - if ('thead' === el.name) { + if ("tag" === el.type) { + if ("thead" === el.name) { out += walkTableHead(el.children); - } - else if ('tbody' === el.name) { + } else if ("tbody" === el.name) { out += walkTableBody(el.children); } } @@ -75,32 +80,30 @@ function walkTable(dom) { } function walkTableHead(dom) { - var out = ''; + var out = ""; if (dom) { var headers = []; dom.forEach(function (el) { - if ('text' === el.type && el.data.trim() !== '') { + if ("text" === el.type && el.data.trim() !== "") { out += el.data; - } - else if ('tr' === el.name) { + } else if ("tr" === el.name) { out += walkTableHead(el.children); - } - else if ('th' === el.name) { + } else if ("th" === el.name) { var header = walkTableHead(el.children); headers.push(header); - out += '| ' + header + ' '; + out += "| " + header + " "; } }); if (headers.length > 0) { - out += ' |\n'; + out += " |\n"; headers.forEach(function (item) { - out += '| '; + out += "| "; for (i = 0; i < item.length; i++) { - out += '-'; + out += "-"; } - out += ' '; + out += " "; }); - out += ' |\n'; + out += " |\n"; } } @@ -108,16 +111,14 @@ function walkTableHead(dom) { } function walkTableBody(dom) { - var out = ''; + var out = ""; if (dom) { dom.forEach(function (el) { - if ('text' === el.type && el.data.trim() !== '') { + if ("text" === el.type && el.data.trim() !== "") { out += el.data; - } - else if ('td' === el.name) { + } else if ("td" === el.name) { out += "| " + walk(el.children, 0, true) + " "; - } - else if ('tr' === el.name) { + } else if ("tr" === el.name) { out += walkTableBody(el.children).replace(/\n/gi, " ") + "|\n"; } }); @@ -129,153 +130,165 @@ function walk(dom, nesting) { if (!nesting) { nesting = 0; } - var out = ''; + var out = ""; if (dom) dom.forEach(function (el) { - if ('text' === el.type) { + if ("text" === el.type) { out += el.data; - } - else if ('tag' === el.type) { + } else if ("tag" === el.type) { switch (el.name) { - case 'a': + case "a": if (el.attribs && el.attribs.href) { - out += '<' + el.attribs.href + '|' + walk(el.children) + '>'; - } - else { + out += "<" + el.attribs.href + "|" + walk(el.children) + ">"; + } else { out += walk(el.children); } break; - case 'h1': - case 'h2': - case 'h3': - case 'h4': - case 'strong': - case 'b': + case "h1": + case "h2": + case "h3": + case "h4": + case "strong": + case "b": content = walk(el.children); - var contentArr = content.split('\n'); - var innerOutput = ''; - for (var i=0; i'; + out += + ""; break; - case 'blockquote': + case "blockquote": content = walk(el.children); - var innerOutput = ''; - var contentArr = content.split('\n'); + var innerOutput = ""; + var contentArr = content.split("\n"); contentArr.forEach((item) => { - if (el.name === 'br' || el.name === 'p') { - innerOutput += '>' + item; + if (el.name === "br" || el.name === "p") { + innerOutput += ">" + item; } else { - innerOutput += '>' + item + '\n'; + innerOutput += ">" + item + "\n"; } }); - if (innerOutput.endsWith('\n>\n')) { + if (innerOutput.endsWith("\n>\n")) { innerOutput = innerOutput.substr(0, innerOutput.length - 2); } - out += innerOutput + '\n'; + out += innerOutput + "\n"; break; default: out += walk(el.children); diff --git a/test.js b/test.js new file mode 100644 index 0000000..e954441 --- /dev/null +++ b/test.js @@ -0,0 +1,276 @@ +slackify = require("./slackify-html"); + +describe("Slackify HTML", () => { + it("simple", () => { + expect(slackify("test")).toBe("test"); + expect(slackify("test 1")).toBe("test 1"); + }); + + it("tags", () => { + expect(slackify("test bold")).toBe("test *bold*"); + expect(slackify('test example link')).toBe( + "test " + ); + }); + + it("malformed html", () => { + expect(slackify("test asd")).toBe("test *asd*"); + expect(slackify("sab tag")).toBe("sab tag"); + expect(slackify(" { + expect(slackify("")).toBe(""); + }); + + it("vcheck example", () => { + expect( + slackify( + '2.4-SNAPSHOT • revision a245dc9 • build 2015-09-07 14:06 • wbl 1.3.33 • details »' + ) + ).toBe( + "*2.4-SNAPSHOT* • revision • build 2015-09-07 14:06 • wbl 1.3.33 • " + ); + }); + + it("test bold text", () => { + expect(slackify("totally bold")).toBe("*totally bold*"); + expect(slackify("

    totally bold in paragraph

    ")).toBe( + "*totally bold in paragraph*\n" + ); + expect(slackify("*already slackified bold*")).toBe( + "*already slackified bold*" + ); + expect(slackify("*bold inside asterisks*")).toBe( + "*bold *inside* asterisks*" + ); + expect(slackify("*asterisks *inside* asterisks*")).toBe( + "*asterisks *inside* asterisks*" + ); + expect( + slackify("

    A sentence with bold text in between.

    ") + ).toBe("A sentence with *bold text* in between.\n"); + }); + + it("test bold text with headers", () => { + expect(slackify("

    a completely bold title

    ")).toBe( + "*a completely bold title*\n" + ); + expect(slackify("

    a completely bold title

    ")).toBe( + "*a completely bold title*\n" + ); + expect(slackify("

    *asterisk title*

    ")).toBe("*asterisk title*\n"); + expect(slackify("

    *asterisk title with *bold**

    ")).toBe( + "*asterisk title with bold*\n" + ); + expect( + slackify("

    alternating bold header content

    ") + ).toBe("*alternating bold header content* \n"); + expect(slackify("

    too many *asterisks* bold text

    ")).toBe( + "*too many asterisks bold text*\n" + ); + expect( + slackify("

    header3 bold tag continues

    outside") + ).toBe("*header3 bold tag continues* \n outside"); + expect( + slackify( + "

    h1 with bold text

    h2 with bold text

    h3 with bold text

    h4 with bold text

    " + ) + ).toBe( + "*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n" + ); + }); + + it("test code block", () => { + var input = + '
    {  "name": "slackify-html",  "version": "1.0.0",  "description": "convert simple html to slack markdown",  "main": "slackify-html.js",  "scripts": {    "test": "tap tests.js"  }}
    '; + var expected = + '```\n{\n "name": "slackify-html",\n "version": "1.0.0",\n "description": "convert simple html to slack markdown",\n "main": "slackify-html.js",\n "scripts": {\n "test": "tap tests.js"\n }\n}\n```\n'; + var output = slackify(input); + expect(output).toBe(expected); + }); + + it("test code block text only", () => { + var input = + '
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    '; + var expected = + "```\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n```\n"; + var output = slackify(input); + expect(output).toBe(expected); + }); + + it("test blockquote with line breaks/new lines", () => { + expect( + slackify("
    block quote with
    line
    breaks
    ") + ).toBe(">block quote with \n>line\n>breaks\n\n"); + expect( + slackify( + "
    block quote with embedded\n\n newlines
    " + ) + ).toBe(">block quote with embedded\n>\n> newlines\n\n"); + expect( + slackify( + "
    block quote with trailing newlines\n\n
    " + ) + ).toBe(">block quote with trailing newlines\n>\n\n"); + }); + + it("test blockquote with paragraphs and line breaks/new lines", () => { + expect( + slackify("

    paragraph in blockquote

    ") + ).toBe(">paragraph in blockquote\n\n"); + expect( + slackify( + "

    paragraph
    with
    line break blockquote

    " + ) + ).toBe(">paragraph\n>with\n>line break blockquote\n\n"); + expect( + slackify( + "

    paragraph block quote with embedded\n\n newlines

    " + ) + ).toBe(">paragraph block quote with embedded\n>\n> newlines\n\n"); + expect( + slackify( + "

    paragraph block quote with trailing newlines\n\n

    " + ) + ).toBe(">paragraph block quote with trailing newlines\n>\n>\n\n"); + }); + + it("test guru blockquote", () => { + expect( + slackify( + '
    block quote text
    ' + ) + ).toBe(">block quote text\n\n"); + expect( + slackify( + '
    block quote bold text
    ' + ) + ).toBe(">block quote *bold* text\n\n"); + expect( + slackify( + '
    block quote italic text
    ' + ) + ).toBe(">block quote _italic_ text\n\n"); + expect( + slackify( + '
    block quote underline text
    ' + ) + ).toBe(">block quote underline text\n\n"); + expect( + slackify( + '
    block quote strikethrough text
    ' + ) + ).toBe(">block quote strikethrough text\n\n"); + expect( + slackify( + '
    block quote highlight text
    ' + ) + ).toBe(">block quote highlight text\n\n"); + expect( + slackify( + '
    block quote color text
    ' + ) + ).toBe(">block quote color text\n\n"); + expect( + slackify( + '
    block quote link
    ' + ) + ).toBe(">block quote \n\n"); + expect( + slackify( + '
    block quote file
    ' + ) + ).toBe(">block quote file\n\n"); + expect( + slackify( + '
    block quote guru code snippet
    ' + ) + ).toBe(">`block quote guru code snippet`\n\n"); + }); + + it("full example", () => { + var input = `

    Security Overview Header

    We take the security of your data very seriously!

    In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

    Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

    Privacy Policy

    Here's a test blockquote with bolded information

    Remember this list

    1. foo

    2. bar

    3. buz

    and this list too..

    • abc

      • sub1

      • sub2

    • def

    • xyz

    and this

    blah
    Column 1Column 2Column 3
    FooBarBaz
    abcdefghi
    `; + var expected = + "*Security Overview Header*\n*We take the security of your data very seriously!*\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n>Here's a test blockquote *with bolded* information\n\nRemember this list\n1. foo\n\n2. bar\n\n3. buz\n\nand this list too..\n• abc\n • sub1\n\n • sub2\n\n\n• def\n\n• xyz\n\n`and this`\n```\nblah\n```\n| Column 1 | Column 2 | Column 3 |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n"; + var output = slackify(input); + expect(output).toBe(expected); + }); + + it("tables with paragraph", () => { + var input = + '

    one

    dwqtwo

    three

    dwqdwq

    dwqdwqdwq

    dwqdwqdwqdwq

    '; + var expected = `| one | dwqtwo | three | +| dwqdwq | dwqdwqdwq | dwqdwqdwqdwq |\n`; + + var output = slackify(input); + expect(output).toBe(expected); + }); + + describe("UL and OL", () => { + describe("Old editor HTML", () => { + it("should handle non-nested
      tags appropriately", () => { + const input = + '
      • 1

      • 2

      • 3

      • 4

      '; + + const expected = `• 1\n\n• 2\n\n• 3\n\n• 4\n\n`; + expect(slackify(input)).toBe(expected); + }); + it("should handle nested
        tags appropriately", () => { + const input = + '
        • 1st level

          • 2nd level

            • 3rd level

              • 4th level

            • 3rd level back

          • 2nd level back

          • 2nd level back again

        • 1st level back

        '; + + const expected = `• 1st level\n • 2nd level\n • 3rd level\n • 4th level\n\n\n • 3rd level back\n\n\n • 2nd level back\n\n • 2nd level back again\n\n\n• 1st level back\n\n`; + expect(slackify(input)).toBe(expected); + }); + it("should handle non-nested
          tags appropriately", () => { + const input = + '
          1. test 1
          2. test 2
          3. test 3

          '; + const expected = "1. test 1\n2. test 2\n3. test 3\n\n"; + expect(slackify(input)).toBe(expected); + }); + it("should handle nested
            tags appropriately", () => { + const input = + '
            1. hi

              1. hello

                1. TEST

                2. test1

              2. hellpo 1

            2. basd

            '; + const expected = ""; + + // expect(slackify(input)).toBe(expected); + }); + }); + + describe("New editor HTML", () => { + it("should handle non-nested
              tags appropriately", () => { + const input = + '
              • 1 bullet
              • 2 bullet
              • 3 bullet
              • 4 bullet

              '; + const expected = "• 1 bullet\n• 2 bullet\n• 3 bullet\n• 4 bullet\n\n"; + + expect(slackify(input)).toBe(expected); + }); + it("should handle nested
                tags appropriately", () => { + const input = + '
                • 1st level
                  • 2nd level
                    • 3rd level
                      • 4th level
                    • 3rd level back
                  • 2nd level back
                  • 2nd level back again
                • 1st level back

                '; + + const expected = `• 1st level\n • 2nd level\n • 3rd level\n • 4th level\n • 3rd level back\n • 2nd level back\n • 2nd level back again\n• 1st level back\n\n`; + + expect(slackify(input)).toBe(expected); + }); + it("should handle non-nested
                  tags appropriately", () => { + const input = + '
                  1. 1st item
                  2. 2nd item
                  3. 3rd item
                  4. 4th item

                  '; + + const expected = + "1. 1st item\n2. 2nd item\n3. 3rd item\n4. 4th item\n\n"; + + expect(slackify(input)).toBe(expected); + }); + it("should handle nested
                    tags appropriately", () => { + const input = + '
                    1. 1st item
                      1. item 1a
                        1. item 1ai
                          1. item 1ai1
                        2. item 1aii
                        3. item 1aiii
                      2. item 1b
                    2. 2nd item

                    '; + const expected = + "1. item1a\n 1. item1ai\n • 3rd level\n • 4th level\n • 3rd level back\n • 2nd level back\n • 2nd level back again\n• 1st level back\n\n"; + + // expect(slackify(input)).toBe(expected); + }); + }); + }); +}); diff --git a/tests.js b/tests.js index c011603..8d60b26 100644 --- a/tests.js +++ b/tests.js @@ -1,104 +1,237 @@ -var tap = require('tap'), - slackify = require('./slackify-html'); +var tap = require("tap"), + slackify = require("./slackify-html"); -tap.test('simple', function simple(t) { - t.equals(slackify('test'), 'test'); - t.equals(slackify('test 1'), 'test 1'); +tap.test("simple", function simple(t) { + t.equal(slackify("test"), "test"); + t.equal(slackify("test 1"), "test 1"); t.end(); }); -tap.test('tags', function tags(t) { - t.equals(slackify('test bold'), 'test *bold*'); - t.equals(slackify('test example link'), 'test '); +tap.test("tags", function tags(t) { + t.equal(slackify("test bold"), "test *bold*"); + t.equal( + slackify('test example link
                    '), + "test " + ); t.end(); }); -tap.test('malformed html', function invalid(t) { - t.equals(slackify('test asd'), 'test *asd*'); - t.equals(slackify('sab tag'), 'sab tag'); - t.equals(slackify('asd"), "test *asd*"); + t.equal(slackify("sab tag"), "sab tag"); + t.equal(slackify("2.4-SNAPSHOT • revision a245dc9 • build 2015-09-07 14:06 • wbl 1.3.33 • details »'), - '*2.4-SNAPSHOT* • revision • build 2015-09-07 14:06 • wbl 1.3.33 • '); +tap.test("vcheck example", function vcheck(t) { + t.equal( + slackify( + '2.4-SNAPSHOT • revision a245dc9 • build 2015-09-07 14:06 • wbl 1.3.33 • details »' + ), + "*2.4-SNAPSHOT* • revision • build 2015-09-07 14:06 • wbl 1.3.33 • " + ); t.end(); }); -tap.test('test bold text', function boldtext(t) { - t.equals(slackify('totally bold'), '*totally bold*'); - t.equals(slackify('

                    totally bold in paragraph

                    '), '*totally bold in paragraph*\n'); - t.equals(slackify('*already slackified bold*'), '*already slackified bold*'); - t.equals(slackify('*bold inside asterisks*'), '*bold *inside* asterisks*'); - t.equals(slackify('*asterisks *inside* asterisks*'), '*asterisks *inside* asterisks*'); - t.equals(slackify('

                    A sentence with bold text in between.

                    '), 'A sentence with *bold text* in between.\n'); +tap.test("test bold text", function boldtext(t) { + t.equal(slackify("totally bold"), "*totally bold*"); + t.equal( + slackify("

                    totally bold in paragraph

                    "), + "*totally bold in paragraph*\n" + ); + t.equal(slackify("*already slackified bold*"), "*already slackified bold*"); + t.equal( + slackify("*bold inside asterisks*"), + "*bold *inside* asterisks*" + ); + t.equal( + slackify("*asterisks *inside* asterisks*"), + "*asterisks *inside* asterisks*" + ); + t.equal( + slackify("

                    A sentence with bold text in between.

                    "), + "A sentence with *bold text* in between.\n" + ); t.end(); }); -tap.test('test bold text with headers', function boldheaders(t) { - t.equals(slackify('

                    a completely bold title

                    '), '*a completely bold title*\n'); - t.equals(slackify('

                    a completely bold title

                    '), '*a completely bold title*\n'); - t.equals(slackify('

                    *asterisk title*

                    '), '*asterisk title*\n'); - t.equals(slackify('

                    *asterisk title with *bold**

                    '), '*asterisk title with bold*\n'); - t.equals(slackify('

                    alternating bold header content

                    '), '*alternating bold header content* \n'); - t.equals(slackify('

                    too many *asterisks* bold text

                    '), '*too many asterisks bold text*\n'); - t.equals(slackify('

                    header3 bold tag continues

                    outside'), '*header3 bold tag continues* \n outside'); - t.equals(slackify('

                    h1 with bold text

                    h2 with bold text

                    h3 with bold text

                    h4 with bold text

                    '), '*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n'); +tap.test("test bold text with headers", function boldheaders(t) { + t.equal( + slackify("

                    a completely bold title

                    "), + "*a completely bold title*\n" + ); + t.equal( + slackify("

                    a completely bold title

                    "), + "*a completely bold title*\n" + ); + t.equal(slackify("

                    *asterisk title*

                    "), "*asterisk title*\n"); + t.equal( + slackify("

                    *asterisk title with *bold**

                    "), + "*asterisk title with bold*\n" + ); + t.equal( + slackify("

                    alternating bold header content

                    "), + "*alternating bold header content* \n" + ); + t.equal( + slackify("

                    too many *asterisks* bold text

                    "), + "*too many asterisks bold text*\n" + ); + t.equal( + slackify("

                    header3 bold tag continues

                    outside"), + "*header3 bold tag continues* \n outside" + ); + t.equal( + slackify( + "

                    h1 with bold text

                    h2 with bold text

                    h3 with bold text

                    h4 with bold text

                    " + ), + "*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n" + ); t.end(); }); -tap.test('test code block', function codeblock(t) { - var input = '
                    {  "name": "slackify-html",  "version": "1.0.0",  "description": "convert simple html to slack markdown",  "main": "slackify-html.js",  "scripts": {    "test": "tap tests.js"  }}
                    '; - var expected = '```\n{\n "name": "slackify-html",\n "version": "1.0.0",\n "description": "convert simple html to slack markdown",\n "main": "slackify-html.js",\n "scripts": {\n "test": "tap tests.js"\n }\n}\n```\n'; +tap.test("test code block", function codeblock(t) { + var input = + '
                    {  "name": "slackify-html",  "version": "1.0.0",  "description": "convert simple html to slack markdown",  "main": "slackify-html.js",  "scripts": {    "test": "tap tests.js"  }}
                    '; + var expected = + '```\n{\n "name": "slackify-html",\n "version": "1.0.0",\n "description": "convert simple html to slack markdown",\n "main": "slackify-html.js",\n "scripts": {\n "test": "tap tests.js"\n }\n}\n```\n'; var output = slackify(input); - t.equals(output,expected); + t.equal(output, expected); t.end(); }); -tap.test('test code block text only', function codeblocktextonly(t) { - var input = '
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                    '; - var expected = '```\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n```\n'; +tap.test("test code block text only", function codeblocktextonly(t) { + var input = + '
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                    '; + var expected = + "```\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n```\n"; var output = slackify(input); - t.equals(output,expected); + t.equal(output, expected); t.end(); }); -tap.test('test blockquote with line breaks/new lines', function blockquotenewlines(t) { - t.equals(slackify('
                    block quote with
                    line
                    breaks
                    '), '>block quote with \n>line\n>breaks\n\n'); - t.equals(slackify('
                    block quote with embedded\n\n newlines
                    '), '>block quote with embedded\n>\n> newlines\n\n'); - t.equals(slackify('
                    block quote with trailing newlines\n\n
                    '), '>block quote with trailing newlines\n>\n\n'); - t.end(); -}); +tap.test( + "test blockquote with line breaks/new lines", + function blockquotenewlines(t) { + t.equal( + slackify("
                    block quote with
                    line
                    breaks
                    "), + ">block quote with \n>line\n>breaks\n\n" + ); + t.equal( + slackify( + "
                    block quote with embedded\n\n newlines
                    " + ), + ">block quote with embedded\n>\n> newlines\n\n" + ); + t.equal( + slackify( + "
                    block quote with trailing newlines\n\n
                    " + ), + ">block quote with trailing newlines\n>\n\n" + ); + t.end(); + } +); -tap.test('test blockquote with paragraphs and line breaks/new lines', function blockquoteparagraphs(t) { - t.equals(slackify('

                    paragraph in blockquote

                    '), '>paragraph in blockquote\n\n'); - t.equals(slackify('

                    paragraph
                    with
                    line break blockquote

                    '), '>paragraph\n>with\n>line break blockquote\n\n'); - t.equals(slackify('

                    paragraph block quote with embedded\n\n newlines

                    '), '>paragraph block quote with embedded\n>\n> newlines\n\n'); - t.equals(slackify('

                    paragraph block quote with trailing newlines\n\n

                    '), '>paragraph block quote with trailing newlines\n>\n>\n\n'); - t.end(); -}) +tap.test( + "test blockquote with paragraphs and line breaks/new lines", + function blockquoteparagraphs(t) { + t.equal( + slackify("

                    paragraph in blockquote

                    "), + ">paragraph in blockquote\n\n" + ); + t.equal( + slackify( + "

                    paragraph
                    with
                    line break blockquote

                    " + ), + ">paragraph\n>with\n>line break blockquote\n\n" + ); + t.equal( + slackify( + "

                    paragraph block quote with embedded\n\n newlines

                    " + ), + ">paragraph block quote with embedded\n>\n> newlines\n\n" + ); + t.equal( + slackify( + "

                    paragraph block quote with trailing newlines\n\n

                    " + ), + ">paragraph block quote with trailing newlines\n>\n>\n\n" + ); + t.end(); + } +); -tap.test('test guru blockquote', function blockquote(t) { - t.equals(slackify('
                    block quote text
                    '), '>block quote text\n\n'); - t.equals(slackify('
                    block quote bold text
                    '), '>block quote *bold* text\n\n'); - t.equals(slackify('
                    block quote italic text
                    '), '>block quote _italic_ text\n\n'); - t.equals(slackify('
                    block quote underline text
                    '), '>block quote underline text\n\n'); - t.equals(slackify('
                    block quote strikethrough text
                    '), '>block quote strikethrough text\n\n'); - t.equals(slackify('
                    block quote highlight text
                    '), '>block quote highlight text\n\n'); - t.equals(slackify('
                    block quote color text
                    '), '>block quote color text\n\n'); - t.equals(slackify('
                    block quote link
                    '), '>block quote \n\n'); - t.equals(slackify('
                    block quote file
                    '), '>block quote file\n\n'); - t.equals(slackify('
                    block quote guru code snippet
                    '), '>`block quote guru code snippet`\n\n'); +tap.test("test guru blockquote", function blockquote(t) { + t.equal( + slackify( + '
                    block quote text
                    ' + ), + ">block quote text\n\n" + ); + t.equal( + slackify( + '
                    block quote bold text
                    ' + ), + ">block quote *bold* text\n\n" + ); + t.equal( + slackify( + '
                    block quote italic text
                    ' + ), + ">block quote _italic_ text\n\n" + ); + t.equal( + slackify( + '
                    block quote underline text
                    ' + ), + ">block quote underline text\n\n" + ); + t.equal( + slackify( + '
                    block quote strikethrough text
                    ' + ), + ">block quote strikethrough text\n\n" + ); + t.equal( + slackify( + '
                    block quote highlight text
                    ' + ), + ">block quote highlight text\n\n" + ); + t.equal( + slackify( + '
                    block quote color text
                    ' + ), + ">block quote color text\n\n" + ); + t.equal( + slackify( + '
                    block quote link
                    ' + ), + ">block quote \n\n" + ); + t.equal( + slackify( + '
                    block quote file
                    ' + ), + ">block quote file\n\n" + ); + t.equal( + slackify( + '
                    block quote guru code snippet
                    ' + ), + ">`block quote guru code snippet`\n\n" + ); t.end(); }); -tap.test('full example', function vcheck(t) { +tap.test("full example", function vcheck(t) { var input = `

                    Security Overview Header

                    We take the security of your data very seriously!

                    In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

                    @@ -146,17 +279,16 @@ tap.test('full example', function vcheck(t) {
                    `; -var expected = '*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n>Here\'s a test blockquote *with bolded* information\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n\`and this\`\n\n\`\`\`\nblah\n\n\n\`\`\`\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n'; + var expected = + "*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n>Here's a test blockquote *with bolded* information\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n`and this`\n\n```\nblah\n\n\n```\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n"; var output = slackify(input); - t.equals(output, - expected); + t.equal(output, expected); t.end(); }); - - tap.test("tables with paragraph", function vcheck(t) { - var input = '

                    one

                    dwqtwo

                    three

                    dwqdwq

                    dwqdwqdwq

                    dwqdwqdwqdwq

                    ' + var input = + '

                    one

                    dwqtwo

                    three

                    dwqdwq

                    dwqdwqdwq

                    dwqdwqdwqdwq

                    '; var expected = `| one | dwqtwo | three | | dwqdwq | dwqdwqdwq | dwqdwqdwqdwq | `; @@ -165,3 +297,4 @@ tap.test("tables with paragraph", function vcheck(t) { t.equal(output, expected); t.end(); }); +a; From f801368a1d66294bd5cfdffa0b4c41500df5fe23 Mon Sep 17 00:00:00 2001 From: Eddy Yeung Date: Tue, 3 Jan 2023 10:58:50 -0500 Subject: [PATCH 56/69] test branch --- .DS_Store | Bin 0 -> 6148 bytes slackify-html.js | 4 +++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Date: Tue, 3 Jan 2023 10:59:39 -0500 Subject: [PATCH 57/69] remove m --- slackify-html.js | 1 - 1 file changed, 1 deletion(-) diff --git a/slackify-html.js b/slackify-html.js index 68ccd14..ab1b384 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -14,7 +14,6 @@ module.exports = function slackify(html) { if (dom) return entities.decode(walk(dom)); else return ""; }; -m; function walkList(dom, ordered, nesting, start) { var out = ""; From 50d5cd1651ad68dc189a72649c07cd993852c234 Mon Sep 17 00:00:00 2001 From: Eddy Yeung Date: Thu, 5 Jan 2023 09:55:18 -0500 Subject: [PATCH 58/69] revert tests cleanup files --- slackify-html.js | 3 +- tester.js | 8 -- tests.js | 300 ----------------------------------------------- 3 files changed, 1 insertion(+), 310 deletions(-) delete mode 100644 tester.js delete mode 100644 tests.js diff --git a/slackify-html.js b/slackify-html.js index ab1b384..c69c1a7 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -10,7 +10,6 @@ module.exports = function slackify(html) { var parser = new htmlparser.Parser(handler); parser.parseComplete(html); var dom = handler.dom; - console.log("I am in the my changes"); if (dom) return entities.decode(walk(dom)); else return ""; }; @@ -26,7 +25,7 @@ function walkList(dom, ordered, nesting, start) { switch (el.name) { case "li": // Add indentation based on the nesting level - for (i = 0; i < nesting * 5; i++) { + for (i = 0; i < nesting * 2; i++) { out += " "; } // Add the bullet or number, followed by the text content of the li element diff --git a/tester.js b/tester.js deleted file mode 100644 index cc56a0f..0000000 --- a/tester.js +++ /dev/null @@ -1,8 +0,0 @@ -var slackify = require('./slackify-html'); -var fs = require('fs'); -fs.readFile('/Users/petermichel/Desktop/delme.html', 'utf8', function (err,data) { - if (err) { - return console.log(err); - } - console.log(slackify(data)); -}); diff --git a/tests.js b/tests.js deleted file mode 100644 index 8d60b26..0000000 --- a/tests.js +++ /dev/null @@ -1,300 +0,0 @@ -var tap = require("tap"), - slackify = require("./slackify-html"); - -tap.test("simple", function simple(t) { - t.equal(slackify("test"), "test"); - t.equal(slackify("test 1"), "test 1"); - t.end(); -}); - -tap.test("tags", function tags(t) { - t.equal(slackify("test bold"), "test *bold*"); - t.equal( - slackify('test example link'), - "test " - ); - t.end(); -}); - -tap.test("malformed html", function invalid(t) { - t.equal(slackify("test asd"), "test *asd*"); - t.equal(slackify("sab tag"), "sab tag"); - t.equal(slackify("2.4-SNAPSHOT • revision a245dc9 • build 2015-09-07 14:06 • wbl 1.3.33 • details »' - ), - "*2.4-SNAPSHOT* • revision • build 2015-09-07 14:06 • wbl 1.3.33 • " - ); - t.end(); -}); - -tap.test("test bold text", function boldtext(t) { - t.equal(slackify("totally bold"), "*totally bold*"); - t.equal( - slackify("

                    totally bold in paragraph

                    "), - "*totally bold in paragraph*\n" - ); - t.equal(slackify("*already slackified bold*"), "*already slackified bold*"); - t.equal( - slackify("*bold inside asterisks*"), - "*bold *inside* asterisks*" - ); - t.equal( - slackify("*asterisks *inside* asterisks*"), - "*asterisks *inside* asterisks*" - ); - t.equal( - slackify("

                    A sentence with bold text in between.

                    "), - "A sentence with *bold text* in between.\n" - ); - t.end(); -}); - -tap.test("test bold text with headers", function boldheaders(t) { - t.equal( - slackify("

                    a completely bold title

                    "), - "*a completely bold title*\n" - ); - t.equal( - slackify("

                    a completely bold title

                    "), - "*a completely bold title*\n" - ); - t.equal(slackify("

                    *asterisk title*

                    "), "*asterisk title*\n"); - t.equal( - slackify("

                    *asterisk title with *bold**

                    "), - "*asterisk title with bold*\n" - ); - t.equal( - slackify("

                    alternating bold header content

                    "), - "*alternating bold header content* \n" - ); - t.equal( - slackify("

                    too many *asterisks* bold text

                    "), - "*too many asterisks bold text*\n" - ); - t.equal( - slackify("

                    header3 bold tag continues

                    outside"), - "*header3 bold tag continues* \n outside" - ); - t.equal( - slackify( - "

                    h1 with bold text

                    h2 with bold text

                    h3 with bold text

                    h4 with bold text

                    " - ), - "*h1 with bold text*\n*h2 with bold text*\n*h3 with bold text*\n*h4 with bold text*\n" - ); - t.end(); -}); - -tap.test("test code block", function codeblock(t) { - var input = - '
                    {  "name": "slackify-html",  "version": "1.0.0",  "description": "convert simple html to slack markdown",  "main": "slackify-html.js",  "scripts": {    "test": "tap tests.js"  }}
                    '; - var expected = - '```\n{\n "name": "slackify-html",\n "version": "1.0.0",\n "description": "convert simple html to slack markdown",\n "main": "slackify-html.js",\n "scripts": {\n "test": "tap tests.js"\n }\n}\n```\n'; - var output = slackify(input); - t.equal(output, expected); - t.end(); -}); - -tap.test("test code block text only", function codeblocktextonly(t) { - var input = - '
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                    '; - var expected = - "```\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n```\n"; - var output = slackify(input); - t.equal(output, expected); - t.end(); -}); - -tap.test( - "test blockquote with line breaks/new lines", - function blockquotenewlines(t) { - t.equal( - slackify("
                    block quote with
                    line
                    breaks
                    "), - ">block quote with \n>line\n>breaks\n\n" - ); - t.equal( - slackify( - "
                    block quote with embedded\n\n newlines
                    " - ), - ">block quote with embedded\n>\n> newlines\n\n" - ); - t.equal( - slackify( - "
                    block quote with trailing newlines\n\n
                    " - ), - ">block quote with trailing newlines\n>\n\n" - ); - t.end(); - } -); - -tap.test( - "test blockquote with paragraphs and line breaks/new lines", - function blockquoteparagraphs(t) { - t.equal( - slackify("

                    paragraph in blockquote

                    "), - ">paragraph in blockquote\n\n" - ); - t.equal( - slackify( - "

                    paragraph
                    with
                    line break blockquote

                    " - ), - ">paragraph\n>with\n>line break blockquote\n\n" - ); - t.equal( - slackify( - "

                    paragraph block quote with embedded\n\n newlines

                    " - ), - ">paragraph block quote with embedded\n>\n> newlines\n\n" - ); - t.equal( - slackify( - "

                    paragraph block quote with trailing newlines\n\n

                    " - ), - ">paragraph block quote with trailing newlines\n>\n>\n\n" - ); - t.end(); - } -); - -tap.test("test guru blockquote", function blockquote(t) { - t.equal( - slackify( - '
                    block quote text
                    ' - ), - ">block quote text\n\n" - ); - t.equal( - slackify( - '
                    block quote bold text
                    ' - ), - ">block quote *bold* text\n\n" - ); - t.equal( - slackify( - '
                    block quote italic text
                    ' - ), - ">block quote _italic_ text\n\n" - ); - t.equal( - slackify( - '
                    block quote underline text
                    ' - ), - ">block quote underline text\n\n" - ); - t.equal( - slackify( - '
                    block quote strikethrough text
                    ' - ), - ">block quote strikethrough text\n\n" - ); - t.equal( - slackify( - '
                    block quote highlight text
                    ' - ), - ">block quote highlight text\n\n" - ); - t.equal( - slackify( - '
                    block quote color text
                    ' - ), - ">block quote color text\n\n" - ); - t.equal( - slackify( - '
                    block quote link
                    ' - ), - ">block quote \n\n" - ); - t.equal( - slackify( - '
                    block quote file
                    ' - ), - ">block quote file\n\n" - ); - t.equal( - slackify( - '
                    block quote guru code snippet
                    ' - ), - ">`block quote guru code snippet`\n\n" - ); - t.end(); -}); - -tap.test("full example", function vcheck(t) { - var input = `

                    Security Overview Header

                    -

                    We take the security of your data very seriously!

                    -

                    In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

                    -

                    Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

                    -

                    Privacy Policy

                    Here's a test blockquote with bolded information

                    Remember this list

                    -
                      -
                    1. foo
                    2. -
                    3. bar
                    4. -
                    5. buz
                    6. -
                    -

                    and this list too...

                    -
                      -
                    • abc -
                        -
                      • sub 1
                      • -
                      • sub 2
                      • -
                      -
                    • -
                    • def
                    • -
                    • xyz
                    • -
                    -

                    and this

                    -
                    blah
                    -
                    -

                    - - - - - - - - - - - - - - - - - - - - -
                    Column 1Column 2Column 3
                    FooBarBaz
                    abcdefghi
                    -
                    `; - var expected = - "*Security Overview Header*\n\n*We take the security of your data very seriously!*\n\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\n\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n\n>Here's a test blockquote *with bolded* information\n\nRemember this list\n\n1. foo\n2. bar\n3. buz\n\nand this list too...\n\n• _abc_\n • sub 1\n • sub 2\n\n\n• *def*\n• xyz\n\n`and this`\n\n```\nblah\n\n\n```\n\n\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n\n"; - var output = slackify(input); - t.equal(output, expected); - t.end(); -}); - -tap.test("tables with paragraph", function vcheck(t) { - var input = - '

                    one

                    dwqtwo

                    three

                    dwqdwq

                    dwqdwqdwq

                    dwqdwqdwqdwq

                    '; - var expected = `| one | dwqtwo | three | -| dwqdwq | dwqdwqdwq | dwqdwqdwqdwq | -`; - - var output = slackify(input); - t.equal(output, expected); - t.end(); -}); -a; From 2951d4f2ae12fe5fbccbf358df7f06841f4a428c Mon Sep 17 00:00:00 2001 From: Eddy Yeung Date: Mon, 9 Jan 2023 08:19:53 -0500 Subject: [PATCH 59/69] remove tap --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 400e2cb..267d12b 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "htmlparser": "^1.7.7" }, "devDependencies": { - "jest": "^29.3.1", - "tap": "^14.10.7" + "jest": "^29.3.1" } } From a22a939a7f96d57338053dafa36f81e4b2154160 Mon Sep 17 00:00:00 2001 From: Eddy Yeung Date: Mon, 23 Jan 2023 13:11:05 -0500 Subject: [PATCH 60/69] ignore ds store --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 69c56eb..11f8aed 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ node_modules # Package package-lock.json + +# logs +.DS_Store From e498bf866615d529a2a93da2d54c716dbd6b6214 Mon Sep 17 00:00:00 2001 From: Eddy Yeung Date: Fri, 3 Feb 2023 15:19:19 -0500 Subject: [PATCH 61/69] fix --- slackify-html.js | 93 ++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 22 ++++++++++-- 2 files changed, 113 insertions(+), 2 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index c69c1a7..f090f5b 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -38,6 +38,96 @@ function walkList(dom, ordered, nesting, start) { // Add the text content of the p element out += walkList(el.children, ordered, nesting) + "\n"; break; + case "a": + if (el.attribs && el.attribs.href) { + out += "<" + el.attribs.href + "|" + walkList(el.children) + "> "; + } else { + out += walkList(el.children); + } + break; + case "code": + out += "`" + walkList(el.children) + "` "; + break; + case "del": + out += "~" + walkList(el.children) + "~ "; + break; + case "strong": + content = walkList(el.children); + var contentArr = content.split("\n"); + var innerOutput = ""; + for (var i = 0; i < contentArr.length; i++) { + content = contentArr[i]; + if (content.trim() !== "") { + var prefixSpace = false; + var suffixSpace = false; + if (content && content.charAt(0) === " ") { + content = content.substr(1, content.length); + prefixSpace = true; + } + if (content && content.charAt(content.length - 1) === " ") { + content = content.substr(0, content.length - 1); + suffixSpace = true; + } + if (prefixSpace) { + innerOutput += " "; + } + if ( + el.name === "h1" || + el.name === "h2" || + el.name === "h3" || + el.name === "h4" + ) { + content = content.replace(/\*/g, ""); + innerOutput += "*" + content + "*"; + } else if ( + content.charAt(0) === "*" && + content.charAt(content.length - 1) === "*" + ) { + innerOutput += content; + } else { + innerOutput += "*" + content + "*"; + } + if (suffixSpace) { + innerOutput += " "; + } + } + if (i < contentArr.length - 1) { + innerOutput += "\n"; + } + } + out += innerOutput + " "; + break; + case "em": + content = walkList(el.children); + var contentArr = content.split("\n"); + var innerOutput = ""; + for (var i = 0; i < contentArr.length; i++) { + content = contentArr[i]; + if (content.trim() !== "") { + var prefixSpace = false; + var suffixSpace = false; + if (content && content.charAt(0) === " ") { + content = content.substr(1, content.length); + prefixSpace = true; + } + if (content && content.charAt(content.length - 1) === " ") { + content = content.substr(0, content.length - 1); + suffixSpace = true; + } + if (prefixSpace) { + innerOutput += " "; + } + innerOutput += "_" + content + "_"; + if (suffixSpace) { + innerOutput += " "; + } + out += innerOutput + " "; + } + if (i < contentArr.length - 1) { + out += "\n"; + } + } + break; default: // Recursively process the children of the element out += walkList(el.children, ordered, nesting + 1); @@ -290,6 +380,9 @@ function walk(dom, nesting) { } out += innerOutput + "\n"; break; + case "del": + out += "~" + walkList(el.children) + "~"; + break; default: out += walk(el.children); } diff --git a/test.js b/test.js index e954441..1dfdb79 100644 --- a/test.js +++ b/test.js @@ -161,7 +161,7 @@ describe("Slackify HTML", () => { slackify( '
                    block quote strikethrough text
                    ' ) - ).toBe(">block quote strikethrough text\n\n"); + ).toBe(">block quote ~strikethrough~ text\n\n"); expect( slackify( '
                    block quote highlight text
                    ' @@ -192,7 +192,7 @@ describe("Slackify HTML", () => { it("full example", () => { var input = `

                    Security Overview Header

                    We take the security of your data very seriously!

                    In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

                    Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

                    Privacy Policy

                    Here's a test blockquote with bolded information

                    Remember this list

                    1. foo

                    2. bar

                    3. buz

                    and this list too..

                    • abc

                      • sub1

                      • sub2

                    • def

                    • xyz

                    and this

                    blah
                    Column 1Column 2Column 3
                    FooBarBaz
                    abcdefghi
                    `; var expected = - "*Security Overview Header*\n*We take the security of your data very seriously!*\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n>Here's a test blockquote *with bolded* information\n\nRemember this list\n1. foo\n\n2. bar\n\n3. buz\n\nand this list too..\n• abc\n • sub1\n\n • sub2\n\n\n• def\n\n• xyz\n\n`and this`\n```\nblah\n```\n| Column 1 | Column 2 | Column 3 |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n"; + "*Security Overview Header*\n*We take the security of your data very seriously!*\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n>Here's a test blockquote *with bolded* information\n\nRemember this list\n1. foo\n\n2. bar\n\n3. buz\n\nand this list too..\n• _abc_ \n • sub1\n\n • sub2\n\n\n• *def* \n\n• xyz\n\n`and this`\n```\nblah\n```\n| Column 1 | Column 2 | Column 3 |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n"; var output = slackify(input); expect(output).toBe(expected); }); @@ -207,6 +207,15 @@ describe("Slackify HTML", () => { expect(output).toBe(expected); }); + it("handles strikethroughs", () => { + var input = + 'strikethrough'; + var expected = "~strikethrough~"; + + var output = slackify(input); + expect(output).toBe(expected); + }); + describe("UL and OL", () => { describe("Old editor HTML", () => { it("should handle non-nested
                      tags appropriately", () => { @@ -263,6 +272,15 @@ describe("Slackify HTML", () => { expect(slackify(input)).toBe(expected); }); + it("should handle various different types of markdown formatting within the list content", () => { + const input = + '
                      • regular codeblock bold italicized hyperlink strikethrough
                      • strikethrough
                      • ITALICIZED TEXT
                      • BOLDED TEXT

                      '; + + const expected = + "• regular `codeblock` *bold* _italicized_ ~strikethrough~ \n• ~strikethrough~ \n• _ITALICIZED TEXT_ \n• *BOLDED TEXT* \n• *_`EVERYTHING`_ * \n• \n\n"; + + expect(slackify(input)).toBe(expected); + }); it("should handle nested
                        tags appropriately", () => { const input = '
                        1. 1st item
                          1. item 1a
                            1. item 1ai
                              1. item 1ai1
                            2. item 1aii
                            3. item 1aiii
                          2. item 1b
                        2. 2nd item

                        '; From 958df4cb425bd1bda1bd7c997683e971fefaa398 Mon Sep 17 00:00:00 2001 From: Eddy Yeung Date: Mon, 6 Feb 2023 08:46:46 -0500 Subject: [PATCH 62/69] remove header conditional --- slackify-html.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index f090f5b..1a74c60 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -72,14 +72,6 @@ function walkList(dom, ordered, nesting, start) { innerOutput += " "; } if ( - el.name === "h1" || - el.name === "h2" || - el.name === "h3" || - el.name === "h4" - ) { - content = content.replace(/\*/g, ""); - innerOutput += "*" + content + "*"; - } else if ( content.charAt(0) === "*" && content.charAt(content.length - 1) === "*" ) { From 2e36218416485f1f0e17f36d53234bac8955a7af Mon Sep 17 00:00:00 2001 From: abevargas Date: Thu, 27 Apr 2023 13:09:49 -0400 Subject: [PATCH 63/69] Remove unused tests --- test.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/test.js b/test.js index 1dfdb79..19a7966 100644 --- a/test.js +++ b/test.js @@ -238,13 +238,6 @@ describe("Slackify HTML", () => { const expected = "1. test 1\n2. test 2\n3. test 3\n\n"; expect(slackify(input)).toBe(expected); }); - it("should handle nested
                          tags appropriately", () => { - const input = - '
                          1. hi

                            1. hello

                              1. TEST

                              2. test1

                            2. hellpo 1

                          2. basd

                          '; - const expected = ""; - - // expect(slackify(input)).toBe(expected); - }); }); describe("New editor HTML", () => { @@ -281,14 +274,6 @@ describe("Slackify HTML", () => { expect(slackify(input)).toBe(expected); }); - it("should handle nested
                            tags appropriately", () => { - const input = - '
                            1. 1st item
                              1. item 1a
                                1. item 1ai
                                  1. item 1ai1
                                2. item 1aii
                                3. item 1aiii
                              2. item 1b
                            2. 2nd item

                            '; - const expected = - "1. item1a\n 1. item1ai\n • 3rd level\n • 4th level\n • 3rd level back\n • 2nd level back\n • 2nd level back again\n• 1st level back\n\n"; - - // expect(slackify(input)).toBe(expected); - }); }); }); }); From bcdb934559d0881dee8fc4379a4b9c48dfcc2a2c Mon Sep 17 00:00:00 2001 From: abevargas Date: Thu, 27 Apr 2023 13:10:25 -0400 Subject: [PATCH 64/69] Update dependency & update package version to reflect tag --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 267d12b..707fe5c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slackify-html", - "version": "1.0.0", + "version": "1.3.4", "description": "convert simple html to slack markdown", "main": "slackify-html.js", "scripts": { @@ -25,7 +25,7 @@ }, "homepage": "https://github.com/mrq-cz/slackify-html", "dependencies": { - "html-entities": "^1.1.3", + "html-entities": "^2.3.3", "htmlparser": "^1.7.7" }, "devDependencies": { From 23d240743dcbe5c0c0a41bd3b3e3b02debb23736 Mon Sep 17 00:00:00 2001 From: abevargas Date: Thu, 27 Apr 2023 13:13:24 -0400 Subject: [PATCH 65/69] Strip hidden tags in link element text to avoid line breaks --- slackify-html.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 1a74c60..95b2226 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -1,7 +1,5 @@ -var htmlparser = require("htmlparser"), - Entities = require("html-entities").AllHtmlEntities; - -entities = new Entities(); +const htmlparser = require("htmlparser"), + entities = require("html-entities"); module.exports = function slackify(html) { var handler = new htmlparser.DefaultHandler(function (error, dom) { @@ -14,6 +12,20 @@ module.exports = function slackify(html) { else return ""; }; +function walkLink(dom) { + let out=''; + + if (dom) { + dom.forEach(function (el) { + if (el.type === 'text') + out = el.data; + else if (el.type === 'tag' && el.children) + out = walkLink(el.children); + }); + } + return out; +} + function walkList(dom, ordered, nesting, start) { var out = ""; if (dom) { @@ -221,7 +233,7 @@ function walk(dom, nesting) { switch (el.name) { case "a": if (el.attribs && el.attribs.href) { - out += "<" + el.attribs.href + "|" + walk(el.children) + ">"; + out += "<" + el.attribs.href + "|" + walkLink(el.children) + ">"; } else { out += walk(el.children); } From 65921948facdc9336d3647845d4dcc76a9326048 Mon Sep 17 00:00:00 2001 From: abevargas Date: Thu, 27 Apr 2023 13:14:44 -0400 Subject: [PATCH 66/69] Add tests with html that was previously failing due to hidden
                            tag in
                            element text value --- test.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 19a7966..83f741f 100644 --- a/test.js +++ b/test.js @@ -210,9 +210,27 @@ describe("Slackify HTML", () => { it("handles strikethroughs", () => { var input = 'strikethrough'; - var expected = "~strikethrough~"; + const expected = "~strikethrough~"; - var output = slackify(input); + const output = slackify(input); + expect(output).toBe(expected); + }); + + it("handles bold & link", () => { + const input = + 'Work Intake (Request Forms)

                            https://wrike.wistia.com/medias/icpvdlvxa5'; + const expected = "*Work Intake (Request Forms)*\n\n"; + + const output = slackify(input); + expect(output).toBe(expected); + }); + + it("handle links with hidden br & strip", () => { + const input = + 'Work Intake (ad-hoc)
                            https://wrike.wistia.com/medias/q97lz2gze7
                            '; + const expected = "*Work Intake (ad-hoc)*"; + + const output = slackify(input); expect(output).toBe(expected); }); From 273da6d2c91d623745f44583e2690f126fcb9a68 Mon Sep 17 00:00:00 2001 From: abevargas Date: Thu, 27 Apr 2023 13:15:05 -0400 Subject: [PATCH 67/69] Cleanup --- slackify-html.js | 106 +++++++++++++++++++++++++++-------------------- test.js | 26 ++++++------ 2 files changed, 73 insertions(+), 59 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 95b2226..6f7542f 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -2,12 +2,12 @@ const htmlparser = require("htmlparser"), entities = require("html-entities"); module.exports = function slackify(html) { - var handler = new htmlparser.DefaultHandler(function (error, dom) { + const handler = new htmlparser.DefaultHandler(function (error, dom) { // error ignored }); - var parser = new htmlparser.Parser(handler); + const parser = new htmlparser.Parser(handler); parser.parseComplete(html); - var dom = handler.dom; + const dom = handler.dom; if (dom) return entities.decode(walk(dom)); else return ""; }; @@ -27,13 +27,20 @@ function walkLink(dom) { } function walkList(dom, ordered, nesting, start) { - var out = ""; + let out = ""; if (dom) { - var listItemIndex = start ? start : 1; + let listItemIndex = start ? start : 1; dom.forEach(function (el) { + let suffixSpace; + let prefixSpace; if ("text" === el.type && el.data.trim() !== "") { out += el.data; } else if ("tag" === el.type) { + let i; + let content; + let contentArr; + let innerOutput; + switch (el.name) { case "li": // Add indentation based on the nesting level @@ -65,19 +72,19 @@ function walkList(dom, ordered, nesting, start) { break; case "strong": content = walkList(el.children); - var contentArr = content.split("\n"); - var innerOutput = ""; - for (var i = 0; i < contentArr.length; i++) { + contentArr = content.split("\n"); + innerOutput = ""; + for (i = 0; i < contentArr.length; i++) { content = contentArr[i]; if (content.trim() !== "") { - var prefixSpace = false; - var suffixSpace = false; + prefixSpace = false; + suffixSpace = false; if (content && content.charAt(0) === " ") { - content = content.substr(1, content.length); + content = content.substring(1, content.length); prefixSpace = true; } if (content && content.charAt(content.length - 1) === " ") { - content = content.substr(0, content.length - 1); + content = content.substring(0, content.length - 1); suffixSpace = true; } if (prefixSpace) { @@ -103,19 +110,19 @@ function walkList(dom, ordered, nesting, start) { break; case "em": content = walkList(el.children); - var contentArr = content.split("\n"); - var innerOutput = ""; - for (var i = 0; i < contentArr.length; i++) { + contentArr = content.split("\n"); + innerOutput = ""; + for (i = 0; i < contentArr.length; i++) { content = contentArr[i]; if (content.trim() !== "") { - var prefixSpace = false; - var suffixSpace = false; + prefixSpace = false; + suffixSpace = false; if (content && content.charAt(0) === " ") { - content = content.substr(1, content.length); + content = content.substring(1, content.length); prefixSpace = true; } if (content && content.charAt(content.length - 1) === " ") { - content = content.substr(0, content.length - 1); + content = content.substring(0, content.length - 1); suffixSpace = true; } if (prefixSpace) { @@ -143,7 +150,7 @@ function walkList(dom, ordered, nesting, start) { } function walkPre(dom) { - var out = ""; + let out = ""; if (dom) { dom.forEach(function (el) { if ("text" === el.type) { @@ -157,7 +164,7 @@ function walkPre(dom) { } function walkTable(dom) { - var out = ""; + let out = ""; if (dom) { dom.forEach(function (el) { if ("tag" === el.type) { @@ -174,16 +181,16 @@ function walkTable(dom) { } function walkTableHead(dom) { - var out = ""; + let out = ""; if (dom) { - var headers = []; + const headers = []; dom.forEach(function (el) { if ("text" === el.type && el.data.trim() !== "") { out += el.data; } else if ("tr" === el.name) { out += walkTableHead(el.children); } else if ("th" === el.name) { - var header = walkTableHead(el.children); + const header = walkTableHead(el.children); headers.push(header); out += "| " + header + " "; } @@ -192,7 +199,7 @@ function walkTableHead(dom) { out += " |\n"; headers.forEach(function (item) { out += "| "; - for (i = 0; i < item.length; i++) { + for (let i = 0; i < item.length; i++) { out += "-"; } out += " "; @@ -205,7 +212,7 @@ function walkTableHead(dom) { } function walkTableBody(dom) { - var out = ""; + let out = ""; if (dom) { dom.forEach(function (el) { if ("text" === el.type && el.data.trim() !== "") { @@ -224,12 +231,19 @@ function walk(dom, nesting) { if (!nesting) { nesting = 0; } - var out = ""; + let out = ""; if (dom) dom.forEach(function (el) { + let suffixSpace; + let prefixSpace; if ("text" === el.type) { out += el.data; } else if ("tag" === el.type) { + let i; + let content; + let contentArr; + let innerOutput; + switch (el.name) { case "a": if (el.attribs && el.attribs.href) { @@ -245,19 +259,19 @@ function walk(dom, nesting) { case "strong": case "b": content = walk(el.children); - var contentArr = content.split("\n"); - var innerOutput = ""; - for (var i = 0; i < contentArr.length; i++) { + contentArr = content.split("\n"); + innerOutput = ""; + for (i = 0; i < contentArr.length; i++) { content = contentArr[i]; if (content.trim() !== "") { - var prefixSpace = false; - var suffixSpace = false; + prefixSpace = false; + suffixSpace = false; if (content && content.charAt(0) === " ") { - content = content.substr(1, content.length); + content = content.substring(1, content.length); prefixSpace = true; } if (content && content.charAt(content.length - 1) === " ") { - content = content.substr(0, content.length - 1); + content = content.substring(0, content.length - 1); suffixSpace = true; } if (prefixSpace) { @@ -301,19 +315,19 @@ function walk(dom, nesting) { case "i": case "em": content = walk(el.children); - var contentArr = content.split("\n"); - var innerOutput = ""; - for (var i = 0; i < contentArr.length; i++) { + contentArr = content.split("\n"); + innerOutput = ""; + for (i = 0; i < contentArr.length; i++) { content = contentArr[i]; if (content.trim() !== "") { - var prefixSpace = false; - var suffixSpace = false; + prefixSpace = false; + suffixSpace = false; if (content && content.charAt(0) === " ") { - content = content.substr(1, content.length); + content = content.substring(1, content.length); prefixSpace = true; } if (content && content.charAt(content.length - 1) === " ") { - content = content.substr(0, content.length - 1); + content = content.substring(0, content.length - 1); suffixSpace = true; } if (prefixSpace) { @@ -347,7 +361,7 @@ function walk(dom, nesting) { break; case "ol": case "ul": - var startIndex = el.attribs ? el.attribs.start : false; + const startIndex = el.attribs ? el.attribs.start : false; out += walkList(el.children, "ol" === el.name, nesting, startIndex); break; case "code": @@ -360,7 +374,7 @@ function walk(dom, nesting) { out += walkTable(el.children); break; case "img": - var alt = el.attribs.alt; + const alt = el.attribs.alt; out += " { if (el.name === "br" || el.name === "p") { innerOutput += ">" + item; @@ -380,7 +394,7 @@ function walk(dom, nesting) { } }); if (innerOutput.endsWith("\n>\n")) { - innerOutput = innerOutput.substr(0, innerOutput.length - 2); + innerOutput = innerOutput.substring(0, innerOutput.length - 2); } out += innerOutput + "\n"; break; diff --git a/test.js b/test.js index 83f741f..0f78bed 100644 --- a/test.js +++ b/test.js @@ -82,20 +82,20 @@ describe("Slackify HTML", () => { }); it("test code block", () => { - var input = + const input = '
                            {  "name": "slackify-html",  "version": "1.0.0",  "description": "convert simple html to slack markdown",  "main": "slackify-html.js",  "scripts": {    "test": "tap tests.js"  }}
                            '; - var expected = + const expected = '```\n{\n "name": "slackify-html",\n "version": "1.0.0",\n "description": "convert simple html to slack markdown",\n "main": "slackify-html.js",\n "scripts": {\n "test": "tap tests.js"\n }\n}\n```\n'; - var output = slackify(input); + const output = slackify(input); expect(output).toBe(expected); }); it("test code block text only", () => { - var input = + const input = '
                            Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                            '; - var expected = + const expected = "```\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n```\n"; - var output = slackify(input); + const output = slackify(input); expect(output).toBe(expected); }); @@ -190,25 +190,25 @@ describe("Slackify HTML", () => { }); it("full example", () => { - var input = `

                            Security Overview Header

                            We take the security of your data very seriously!

                            In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

                            Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

                            Privacy Policy

                            Here's a test blockquote with bolded information

                            Remember this list

                            1. foo

                            2. bar

                            3. buz

                            and this list too..

                            • abc

                              • sub1

                              • sub2

                            • def

                            • xyz

                            and this

                            blah
                            Column 1Column 2Column 3
                            FooBarBaz
                            abcdefghi
                            `; - var expected = + const input = `

                            Security Overview Header

                            We take the security of your data very seriously!

                            In order to instill the necessary confidence, we wanted to provide full transparency on why, who, where, when and how we protect your data.

                            Given the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.

                            Privacy Policy

                            Here's a test blockquote with bolded information

                            Remember this list

                            1. foo

                            2. bar

                            3. buz

                            and this list too..

                            • abc

                              • sub1

                              • sub2

                            • def

                            • xyz

                            and this

                            blah
                            Column 1Column 2Column 3
                            FooBarBaz
                            abcdefghi
                            `; + const expected = "*Security Overview Header*\n*We take the security of your data very seriously!*\nIn order to instill the necessary confidence, we wanted to provide full transparency on _why_, _who_, _where_, _when_ and _how_ we protect your data.\nGiven the sensitive nature of your content and need to maintain your privacy being a priority for us, we wanted to share the practices and policies we have put into place.\n\n>Here's a test blockquote *with bolded* information\n\nRemember this list\n1. foo\n\n2. bar\n\n3. buz\n\nand this list too..\n• _abc_ \n • sub1\n\n • sub2\n\n\n• *def* \n\n• xyz\n\n`and this`\n```\nblah\n```\n| Column 1 | Column 2 | Column 3 |\n| Foo | Bar | Baz |\n| abc | def | ghi |\n"; - var output = slackify(input); + const output = slackify(input); expect(output).toBe(expected); }); it("tables with paragraph", () => { - var input = + const input = '

                            one

                            dwqtwo

                            three

                            dwqdwq

                            dwqdwqdwq

                            dwqdwqdwqdwq

                            '; - var expected = `| one | dwqtwo | three | + const expected = `| one | dwqtwo | three | | dwqdwq | dwqdwqdwq | dwqdwqdwqdwq |\n`; - var output = slackify(input); + const output = slackify(input); expect(output).toBe(expected); }); it("handles strikethroughs", () => { - var input = + const input = 'strikethrough'; const expected = "~strikethrough~"; From 36e9f9e3a9fc4f26a421e337876ff05692007e23 Mon Sep 17 00:00:00 2001 From: abevargas Date: Thu, 27 Apr 2023 13:30:20 -0400 Subject: [PATCH 68/69] Handle a case where there are potentially other tags surrounding text so we can strip those tags and keep the text --- slackify-html.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slackify-html.js b/slackify-html.js index 6f7542f..9e35b1a 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -18,9 +18,9 @@ function walkLink(dom) { if (dom) { dom.forEach(function (el) { if (el.type === 'text') - out = el.data; + out += el.data; else if (el.type === 'tag' && el.children) - out = walkLink(el.children); + out += walkLink(el.children); }); } return out; From ba6355ff39f3845edc25f7cda86071fde8535c8e Mon Sep 17 00:00:00 2001 From: Julian Krispel-Samsel Date: Fri, 12 Jan 2024 13:19:44 +0000 Subject: [PATCH 69/69] update slackify to include collapsibles and add tests --- slackify-html.js | 5 +++++ test.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/slackify-html.js b/slackify-html.js index 9e35b1a..9bc44b9 100644 --- a/slackify-html.js +++ b/slackify-html.js @@ -245,6 +245,11 @@ function walk(dom, nesting) { let innerOutput; switch (el.name) { + case "summary": + out += `\n*${walk(el.children).trim()}*\n` + break; + + case "a": if (el.attribs && el.attribs.href) { out += "<" + el.attribs.href + "|" + walkLink(el.children) + ">"; diff --git a/test.js b/test.js index 0f78bed..12b04c8 100644 --- a/test.js +++ b/test.js @@ -293,5 +293,19 @@ describe("Slackify HTML", () => { expect(slackify(input)).toBe(expected); }); }); + + it('should handle collapsible elements', () => { + + const input = `

                            Collapsible Element with nested one

                            Below is nested

                            Summary

                            Content

                            ` + + const expected = ` +*Collapsible Element with nested one* +Below is nested + +*Summary* +Content +` + expect(slackify(input)).toBe(expected); + }) }); });