-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# slackify-html | ||
|
||
convert html to slack markdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "slackify-html", | ||
"version": "1.0.0", | ||
"description": "convert simple html to slack markdown", | ||
"main": "slackify-html.js", | ||
"scripts": { | ||
"test": "tap tests.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mrq-cz/slackify-html.git" | ||
}, | ||
"keywords": [ | ||
"html", | ||
"parser", | ||
"slack", | ||
"to", | ||
"html", | ||
"markdown" | ||
], | ||
"author": "mrq", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/mrq-cz/slackify-html/issues" | ||
}, | ||
"homepage": "https://github.com/mrq-cz/slackify-html", | ||
"dependencies": { | ||
"html-entities": "^1.1.3", | ||
"htmlparser": "^1.7.7" | ||
}, | ||
"devDependencies": { | ||
"tap": "^1.4.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var htmlparser = require('htmlparser'), | ||
Entities = require('html-entities').AllHtmlEntities; | ||
|
||
entities = new Entities(); | ||
|
||
module.exports = function slackify(html) { | ||
var handler = new htmlparser.DefaultHandler(function (error, dom) { | ||
// error ignored | ||
}); | ||
var parser = new htmlparser.Parser(handler); | ||
parser.parseComplete(html); | ||
var dom = handler.dom; | ||
if (dom) | ||
return entities.decode(walk(dom)); | ||
else | ||
return ''; | ||
} | ||
|
||
function walk(dom) { | ||
var out = ''; | ||
if (dom) | ||
dom.forEach(function (el) { | ||
if ('text' === el.type) { | ||
out += el.data; | ||
} | ||
if ('tag' === el.type) { | ||
switch (el.name) { | ||
case 'a': | ||
out += '<' + el.attribs.href + '|' + walk(el.children) + '>'; | ||
break; | ||
case 'strong': | ||
case 'b': | ||
out += '*' + walk(el.children) + '*'; | ||
break; | ||
case 'i': | ||
case 'em': | ||
out += '_' + walk(el.children) + '_'; | ||
break; | ||
default: | ||
out += walk(el.children); | ||
} | ||
} | ||
}); | ||
return out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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'); | ||
t.end(); | ||
}); | ||
|
||
tap.test('tags', function tags(t) { | ||
t.equals(slackify('test <b>bold</b>'), 'test *bold*'); | ||
t.equals(slackify('test <a href="http://example.com">example link</b>'), 'test <http://example.com|example link>'); | ||
t.end(); | ||
}); | ||
|
||
tap.test('malformed html', function invalid(t) { | ||
t.equals(slackify('test <b>asd'), 'test *asd*'); | ||
t.equals(slackify('<sad>sab tag</sad>'), 'sab tag'); | ||
t.equals(slackify('<sad'), ''); | ||
t.end(); | ||
}); | ||
|
||
tap.test('empty', function empty(t) { | ||
t.equals(slackify(''), ''); | ||
t.end(); | ||
}); | ||
|
||
tap.test('vcheck example', function vcheck(t) { | ||
t.equals(slackify('<b>2.4-SNAPSHOT</b> • revision <a href="https://a-team.csint.cz/stash/projects/webapi/repos/webapi/commits/a245dc97ec7bbe9ed6cb9fc1025dd846fefc7c89">a245dc9</a> • build 2015-09-07 14:06 • wbl 1.3.33 • <a href="http://www.csast.csas.cz/at/webapi/api/v1/version">details »</a>'), | ||
'*2.4-SNAPSHOT* • revision <https://a-team.csint.cz/stash/projects/webapi/repos/webapi/commits/a245dc97ec7bbe9ed6cb9fc1025dd846fefc7c89|a245dc9> • build 2015-09-07 14:06 • wbl 1.3.33 • <http://www.csast.csas.cz/at/webapi/api/v1/version|details »>'); | ||
t.end(); | ||
}); |