Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
4da3d36
adding support for lists, headers, pre/code, and tables
petemichel77 Mar 29, 2016
15bfb3a
adding nested lists
petemichel77 Mar 29, 2016
37b4184
debugging
petemichel77 Mar 29, 2016
fa56c9f
debugging
petemichel77 Mar 29, 2016
8c5b8ff
Fix nested lists
petemichel77 Mar 29, 2016
9a7d1f4
handle images
petemichel77 Mar 29, 2016
d4d6c82
debugging
petemichel77 Mar 31, 2016
7673f71
remove debugging
petemichel77 Apr 26, 2016
c66c694
attempt to fix bullets
petemichel77 May 4, 2016
bcccb33
handle anchor tags with no href
petemichel77 Nov 7, 2016
95cd3e6
better null checking
petemichel77 Nov 7, 2016
03fac40
better handling for bolds and paragraphs for the new editor
petemichel77 Nov 27, 2017
e29caa4
better handling for bolds and paragraphs for the new editor
petemichel77 Nov 27, 2017
803a5e4
better handling for bolds and paragraphs for the new editor
petemichel77 Nov 27, 2017
fb794e6
handle pre spaces as well
petemichel77 Nov 27, 2017
e8748b8
debugging
petemichel77 Nov 28, 2017
44fce1e
debugging
petemichel77 Nov 28, 2017
81523b4
debugging
petemichel77 Nov 28, 2017
dc0ce69
debugging
petemichel77 Nov 28, 2017
7534c6c
add a newline after headers
petemichel77 Nov 28, 2017
5456a3f
handle newlines in bold/italics
petemichel77 Nov 28, 2017
b1db11a
handle newlines in bold/italics
petemichel77 Nov 28, 2017
0c36c03
handle newlines in bold/italics
petemichel77 Nov 28, 2017
328102f
handle newlines in bold/italics
petemichel77 Nov 28, 2017
d502e85
fix double bolding
petemichel77 Nov 28, 2017
31c6278
fix double bolding
petemichel77 Nov 28, 2017
c0bbf4e
fix links in table cells
petemichel77 Dec 19, 2017
30661a7
fix bug with multiple elements in b/strong/em/etc
petemichel77 Feb 15, 2018
9d65c56
add simple tester
petemichel77 Feb 15, 2018
b7feba3
added start param
austinparkk Feb 3, 2020
32cba67
add null check
austinparkk Feb 24, 2020
e7e85ee
Merge pull request #2 from guruhq/feature/ch24020/addNullCheckForLists
austinparkk Feb 24, 2020
c9e34ab
fix failing test case
maggie-lin Apr 27, 2020
14e0fc2
include setup details in readme
maggie-lin Apr 27, 2020
3eaf96b
ignore package-lock.json
maggie-lin Apr 27, 2020
8e4cdf5
fix bold text within h1 header
maggie-lin Apr 27, 2020
01c85b0
apply fix for bold text to h1, h2, h3
maggie-lin Apr 27, 2020
7f50e79
rename header function to specific it's for bold text
maggie-lin Apr 27, 2020
33b5c37
refactor check for header with bold text and include h4
maggie-lin Apr 28, 2020
6689a43
move changes to h1, h2, h3, h4 switch cases
maggie-lin Apr 28, 2020
1f91fcb
Merge branch 'bugfix/mlin/ch23417/fix-current-failing-tests' into ful…
maggie-lin Apr 28, 2020
0a63294
Merge branch 'fullhtml' of github.com:guruhq/slackify-html into bugfi…
maggie-lin Apr 28, 2020
3e4a28a
switch the check back inside of 'b' so whitespacing is maintained
maggie-lin Apr 28, 2020
f7bb03f
update the test case
maggie-lin Apr 28, 2020
bf87054
swap if statements order since the current order can lead to failures
maggie-lin Apr 29, 2020
2b2669e
Merge branch 'bugfix/mlin/ch23417/slack-formatting-wonky-when-user-cl…
maggie-lin Apr 29, 2020
3bf3f51
add new line to each line in pre code block
maggie-lin May 1, 2020
386fdab
add test for a code block with only text
maggie-lin May 1, 2020
99ab70f
Merge branch 'bugfix/mlin/ch20128/markdown-code-formatting-is-whacky-…
maggie-lin May 1, 2020
acaa519
add support for blockquote html tags
maggie-lin May 3, 2020
c381c91
include tests for links/files in blockquote
maggie-lin May 3, 2020
acba013
add new line to end of block quotes
maggie-lin May 4, 2020
0e3ccbf
add another new line since multiple block quotes can get turned into …
maggie-lin May 4, 2020
1469d5c
add second new line to test
maggie-lin May 4, 2020
df13c8b
add a couple more blockquote tests
maggie-lin May 6, 2020
45da5db
handle embedded new lines and line breaks in blockquote
maggie-lin May 11, 2020
0353a5b
add statement to else block
maggie-lin May 12, 2020
66eb055
Merge branch 'feature/mlin/ch28142/support-block-quotes-formatting-fo…
maggie-lin May 13, 2020
e2dbdbc
upgrade tap to fix 13 security vulnerabilties
maggie-lin May 14, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ example usage:
var slackify = require('slackify-html');

var text = slackify('this <a href="http://github.com">link</a> is <b>important</b>');
// text variable contains 'this <http://github.com|link> is *important*'
// text variable contains 'this <http://github.com|link> is *important*'
```

### How to setup repo and test locally
https://app.getguru.com/card/TqGjya8c/slackifyhtml-basic-setup
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"htmlparser": "^1.7.7"
},
"devDependencies": {
"tap": "^1.4.0"
"tap": "^14.10.7"
}
}
251 changes: 246 additions & 5 deletions slackify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,268 @@ module.exports = function slackify(html) {
return entities.decode(walk(dom));
else
return '';
};

function walkList(dom, ordered, nesting, start) {
var out = '';
if (dom) {
var listItemIndex = start ? start : 1;
dom.forEach(function (el) {
if ('text' === el.type && el.data.trim() !== '') {
out += el.data;
}
else if ('tag' === el.type) {
switch (el.name) {
case 'li':
for (i=0; i < nesting * 2; i++) {
out += ' ';
}
out += (ordered ? listItemIndex++ + '. ' : "• ") + walk(el.children, nesting + 1) + '\n';
break;
default:
out += walk(el.children, nesting + 1);
}
}
});
}
return out;
}

function walk(dom) {
function walkPre(dom) {
var out = '';
if (dom)
if (dom) {
dom.forEach(function (el) {
if ('text' === el.type) {
out += el.data;
}
else if ('tag' === el.type) {
out += walkPre(el.children) + '\n';
}
});
}
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 && 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 && el.data.trim() !== '') {
out += el.data;
}
else if ('td' === el.name) {
out += '| ' + walk(el.children) + ' ';
}
else if ('tr' === el.name) {
out += walkTableBody(el.children) + '|\n';
}
});
}
return out;
}

function walk(dom, nesting) {
if (!nesting) {
nesting = 0;
}
var out = '';
if (dom)
dom.forEach(function (el) {
if ('text' === el.type) {
out += el.data;
}
else if ('tag' === el.type) {
switch (el.name) {
case 'a':
out += '<' + el.attribs.href + '|' + walk(el.children) + '>';
if (el.attribs && el.attribs.href) {
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':
out += '*' + walk(el.children) + '*';
content = walk(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;

switch (el.name) {
case 'h1':
case 'h2':
case 'h3':
case 'h4':
out += '\n';
break;
}
break;
case 'i':
case 'em':
out += '_' + walk(el.children) + '_';
content = walk(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;
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';
break;
case 'br':
out += walk(el.children) + '\n';
break;
case 'ol':
case 'ul':
var startIndex = (el.attribs) ? el.attribs.start : false;
out += walkList(el.children, 'ol' === el.name, nesting, startIndex);
break;
case 'code':
out += '`' + walk(el.children) + '`';
break;
case 'pre':
out += '```\n' + walkPre(el.children) + '```\n';
break;
case 'table':
out += walkTable(el.children);
break;
case 'img':
var alt = el.attribs.alt;
out += '<Inline Image' + (alt !== '' ? '('+alt+')' : '') + ': ' + el.attribs.src + '>';
break;
case 'blockquote':
content = walk(el.children);
var innerOutput = '';
var contentArr = content.split('\n');
contentArr.forEach((item) => {
if (el.name === 'br' || el.name === 'p') {
innerOutput += '>' + item;
} else {
innerOutput += '>' + item + '\n';
}
});
if (innerOutput.endsWith('\n>\n')) {
innerOutput = innerOutput.substr(0, innerOutput.length - 2);
}
out += innerOutput + '\n';
break;
default:
out += walk(el.children);
Expand Down
8 changes: 8 additions & 0 deletions tester.js
Original file line number Diff line number Diff line change
@@ -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));
});
Loading