├── index.js ├── README.md ├── .gitignore ├── package.json ├── test ├── html.js └── text.js └── lib ├── html.js └── text.js /index.js: -------------------------------------------------------------------------------- 1 | module.exports.indentText = require("./lib/text").indentText; 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mailpurify 2 | ========== 3 | 4 | Tidy e-mail HTML contents 5 | 6 | Doesn't really work yet. See tests for examples -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | lib-cov 4 | *.seed 5 | *.log 6 | *.csv 7 | *.dat 8 | *.out 9 | *.pid 10 | *.gz 11 | 12 | pids 13 | logs 14 | results 15 | 16 | node_modules 17 | npm-debug.log -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mailpurify", 3 | "version": "0.1.5", 4 | "description": "Convert plaintext e-mail body to HTML", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "nodeunit test" 8 | }, 9 | "repository": "", 10 | "author": "Andris Reinman", 11 | "license": "MIT", 12 | "dependencies": { 13 | }, 14 | "devDependencies": { 15 | "jsdom": "*", 16 | "nodeunit": "*", 17 | "mailparser": "*" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /test/html.js: -------------------------------------------------------------------------------- 1 | var testCase = require('nodeunit').testCase, 2 | indentHTML = require("../lib/html").indentHTML, 3 | MailParser = require("mailparser").Mailparser, 4 | jsdom = require("jsdom"); 5 | 6 | exports["Simple paragraphs"] = function(test){ 7 | var mailHtml = "
test
sdfsd
"; 8 | indentHTML(mailHtml, function(err, html){ 9 | console.log(html) 10 | test.ifError(err) 11 | test.done(); 12 | }); 13 | } -------------------------------------------------------------------------------- /lib/html.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Tidies up mail HTML body into HTML with nested quotes 3 | * 4 | * @param {String} text E-mail html body 5 | * @param {Function} callback Callback function to run when ready 6 | */ 7 | module.exports.indentHTML = function(html, callback){ 8 | html = (html || "").toString().trim(); 9 | 10 | if(!html){ 11 | return callback(null, ""); 12 | } 13 | 14 | html = (html || "").toString().trim(); 15 | 16 | return callback(null, html); 17 | } -------------------------------------------------------------------------------- /test/text.js: -------------------------------------------------------------------------------- 1 | var testCase = require('nodeunit').testCase, 2 | indentText = require("../lib/text").indentText, 3 | MailParser = require("mailparser").Mailparser, 4 | jsdom = require("jsdom"); 5 | 6 | exports["Simple paragraphs"] = function(test){ 7 | var mailText = 'This is line nr 1\r\n'+ 8 | 'This is the same paragraph\n\n'+ 9 | 'This is another paragraph\n\n'+ 10 | 'And yet another paragraph'; 11 | 12 | jsdom.env(indentText(mailText), function(err, window){ 13 | var list; 14 | 15 | test.ifError(err); 16 | test.ok(!!window); 17 | 18 | list = Array.prototype.slice.call(window.document.body.getElementsByTagName("p") || []); 19 | 20 | test.equal(list.length, 3); 21 | test.equal((list[0].innerHTML || "").trim(), 'This is line nr 1" + node.lines.map(function(line){
86 | return line.replace(/&/g,"&").
87 | replace(/^\s+/, function(str){
88 | return new Array(str.length+1).join(" ");
89 | }).
90 | replace(//g,">").
92 | replace(/"/g,""");
93 | }).join("
\n") + "
\n" + 96 | indenter(node.lines.map(function(line){ 97 | return line.substr(line.charAt(0) != ">" && node.startPos || 0); 98 | }).join("\n"), level + 1) + 99 | ""; 100 | } 101 | }).join("\n"); 102 | 103 | return text; 104 | } 105 | 106 | // Modified http://stackoverflow.com/a/37687 107 | function replaceURLWithHTMLLinks(text){ 108 | var exp = /(\b(?:https?|ftp|file):\/\/|mailto:)([-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; 109 | return text.replace(exp, function(full, prefix, link){ 110 | return ''+(prefix.toLowerCase() != "mailto:" ? full : link)+''; 111 | }); 112 | } 113 | --------------------------------------------------------------------------------