From d5683371b3e6e07b6676e2036e7b67caa5e072ee Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Thu, 6 Nov 2014 11:07:31 +0300 Subject: [PATCH] Added emails autolinking and improved tests coverage --- lib/index.js | 5 +---- lib/parser_block.js | 7 +++---- lib/rules_core/linkify.js | 26 ++++++++++++++++++++++---- test/fixtures/remarkable/linkify.txt | 11 +++++++++++ test/misc.js | 19 +++++++++++++++++-- 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/lib/index.js b/lib/index.js index 94801e1..914d725 100644 --- a/lib/index.js +++ b/lib/index.js @@ -65,7 +65,7 @@ Remarkable.prototype.set = function (options) { Remarkable.prototype.configure = function (presets) { var self = this; - if (!presets) { throw new Error('Wrong preset name'); } + if (!presets) { throw new Error('Wrong `remarkable` preset, check name/content'); } if (presets.options) { self.set(presets.options); } @@ -74,9 +74,6 @@ Remarkable.prototype.configure = function (presets) { if (presets.components[name].rules) { self[name].ruler.enable(presets.components[name].rules, true); } - if (presets.components[name].options) { - self[name].set(presets.components[name].options); - } }); } }; diff --git a/lib/parser_block.js b/lib/parser_block.js index 923b358..b8e9882 100644 --- a/lib/parser_block.js +++ b/lib/parser_block.js @@ -62,11 +62,10 @@ ParserBlock.prototype.tokenize = function (state, startLine, endLine) { if (ok) { break; } } + /* istanbul ignore if */ if (!ok) { throw new Error('No matching rules found'); } - - if (line === state.line) { - throw new Error('None of rules updated state.line'); - } + /* istanbul ignore if */ + if (line === state.line) { throw new Error('None of rules updated state.line'); } // set state.tight iff we had an empty line before current tag // i.e. latest empty line should not count diff --git a/lib/rules_core/linkify.js b/lib/rules_core/linkify.js index f865d90..4b31ff5 100644 --- a/lib/rules_core/linkify.js +++ b/lib/rules_core/linkify.js @@ -8,7 +8,7 @@ var Autolinker = require('autolinker'); -var LINK_SCAN_RE = /www|\:\/\//; +var LINK_SCAN_RE = /www|@|\:\/\//; function isLinkOpen(str) { @@ -25,10 +25,27 @@ function createLinkifier() { var links = []; var autolinker = new Autolinker({ stripPrefix: false, + url: true, + email: true, + twitter: false, replaceFn: function (autolinker, match) { // Only collect matched strings but don't change anything. - if (match.getType() === 'url') { - links.push({ text: match.matchedText, url: match.getUrl() }); + switch (match.getType()) { + case 'url': + links.push({ + text: match.matchedText, + url: match.getUrl() + }); + break; + case 'email': + links.push({ + text: match.matchedText, + // normalize email protocol + url: 'mailto:' + match.getEmail().replace(/^mailto:/i, '') + }); + break; + /* istanbul ignore next */ + default: } return false; } @@ -104,7 +121,8 @@ module.exports = function linkify(state) { pos = text.indexOf(links[ln].text); - if (pos === -1) { continue; } + /* istanbul ignore next */ + if (pos === -1) { continue; } // that should never happen if (pos) { level = level; diff --git a/test/fixtures/remarkable/linkify.txt b/test/fixtures/remarkable/linkify.txt index 4856943..ecafa68 100644 --- a/test/fixtures/remarkable/linkify.txt +++ b/test/fixtures/remarkable/linkify.txt @@ -44,3 +44,14 @@ www.example.org版权所有 .

www.example.org版权所有

. + + +emails +. +test@example.com + +mailto:test@example.com +. +

test@example.com

+

mailto:test@example.com

+. diff --git a/test/misc.js b/test/misc.js index 9ca6c20..df78843 100644 --- a/test/misc.js +++ b/test/misc.js @@ -43,14 +43,23 @@ describe('Utils', function () { describe('API', function () { - it('Constructor', function () { + it('constructor', function () { assert.throws(function () { var md = new Remarkable('bad preset'); md.render('123'); }); }); - it('Plugin', function () { + it('configure coverage', function () { + var md = new Remarkable(); + + // conditions coverage + md.configure({}); + + md.render('123'); + }); + + it('plugin', function () { var succeeded = false; function plugin(self, opts) { if (opts === 'bar') { succeeded = true; } } @@ -75,6 +84,12 @@ describe('Misc', function () { assert.strictEqual(md.render('123\n'), '

123

\n'); }); + it('Should quickly exit on empty string', function () { + var md = new Remarkable(); + + assert.strictEqual(md.render(''), ''); + }); + });