Skip to content

Commit

Permalink
initial concept
Browse files Browse the repository at this point in the history
  • Loading branch information
mrq1911 committed Sep 7, 2015
1 parent 826a1ff commit 6807ee6
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# slackify-html

convert html to slack markdown
34 changes: 34 additions & 0 deletions package.json
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"
}
}
45 changes: 45 additions & 0 deletions slackify-html.js
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;
}
32 changes: 32 additions & 0 deletions tests.js
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> &bull; revision <a href="https://a-team.csint.cz/stash/projects/webapi/repos/webapi/commits/a245dc97ec7bbe9ed6cb9fc1025dd846fefc7c89">a245dc9</a> &bull; build 2015-09-07 14:06 &bull; wbl 1.3.33 &bull; <a href="http://www.csast.csas.cz/at/webapi/api/v1/version">details &raquo;</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();
});

0 comments on commit 6807ee6

Please sign in to comment.