From 4da3d36c06d44a4eab09eff1c4d0c26557385388 Mon Sep 17 00:00:00 2001 From: Pete Michel Date: Tue, 29 Mar 2016 11:33:02 -0400 Subject: [PATCH 01/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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/53] 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" } }