Browse Source

Added emails autolinking and improved tests coverage

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
d5683371b3
  1. 5
      lib/index.js
  2. 7
      lib/parser_block.js
  3. 26
      lib/rules_core/linkify.js
  4. 11
      test/fixtures/remarkable/linkify.txt
  5. 19
      test/misc.js

5
lib/index.js

@ -65,7 +65,7 @@ Remarkable.prototype.set = function (options) {
Remarkable.prototype.configure = function (presets) { Remarkable.prototype.configure = function (presets) {
var self = this; 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); } if (presets.options) { self.set(presets.options); }
@ -74,9 +74,6 @@ Remarkable.prototype.configure = function (presets) {
if (presets.components[name].rules) { if (presets.components[name].rules) {
self[name].ruler.enable(presets.components[name].rules, true); self[name].ruler.enable(presets.components[name].rules, true);
} }
if (presets.components[name].options) {
self[name].set(presets.components[name].options);
}
}); });
} }
}; };

7
lib/parser_block.js

@ -62,11 +62,10 @@ ParserBlock.prototype.tokenize = function (state, startLine, endLine) {
if (ok) { break; } if (ok) { break; }
} }
/* istanbul ignore if */
if (!ok) { throw new Error('No matching rules found'); } if (!ok) { throw new Error('No matching rules found'); }
/* istanbul ignore if */
if (line === state.line) { if (line === state.line) { throw new Error('None of rules updated state.line'); }
throw new Error('None of rules updated state.line');
}
// set state.tight iff we had an empty line before current tag // set state.tight iff we had an empty line before current tag
// i.e. latest empty line should not count // i.e. latest empty line should not count

26
lib/rules_core/linkify.js

@ -8,7 +8,7 @@
var Autolinker = require('autolinker'); var Autolinker = require('autolinker');
var LINK_SCAN_RE = /www|\:\/\//; var LINK_SCAN_RE = /www|@|\:\/\//;
function isLinkOpen(str) { function isLinkOpen(str) {
@ -25,10 +25,27 @@ function createLinkifier() {
var links = []; var links = [];
var autolinker = new Autolinker({ var autolinker = new Autolinker({
stripPrefix: false, stripPrefix: false,
url: true,
email: true,
twitter: false,
replaceFn: function (autolinker, match) { replaceFn: function (autolinker, match) {
// Only collect matched strings but don't change anything. // Only collect matched strings but don't change anything.
if (match.getType() === 'url') { switch (match.getType()) {
links.push({ text: match.matchedText, url: match.getUrl() }); 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; return false;
} }
@ -104,7 +121,8 @@ module.exports = function linkify(state) {
pos = text.indexOf(links[ln].text); pos = text.indexOf(links[ln].text);
if (pos === -1) { continue; } /* istanbul ignore next */
if (pos === -1) { continue; } // that should never happen
if (pos) { if (pos) {
level = level; level = level;

11
test/fixtures/remarkable/linkify.txt

@ -44,3 +44,14 @@ www.example.org版权所有
. .
<p><a href="http://www.example.org">www.example.org</a>版权所有</p> <p><a href="http://www.example.org">www.example.org</a>版权所有</p>
. .
emails
.
test@example.com
mailto:test@example.com
.
<p><a href="mailto:test@example.com">test@example.com</a></p>
<p><a href="mailto:test@example.com">mailto:test@example.com</a></p>
.

19
test/misc.js

@ -43,14 +43,23 @@ describe('Utils', function () {
describe('API', function () { describe('API', function () {
it('Constructor', function () { it('constructor', function () {
assert.throws(function () { assert.throws(function () {
var md = new Remarkable('bad preset'); var md = new Remarkable('bad preset');
md.render('123'); 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; var succeeded = false;
function plugin(self, opts) { if (opts === 'bar') { succeeded = true; } } function plugin(self, opts) { if (opts === 'bar') { succeeded = true; } }
@ -75,6 +84,12 @@ describe('Misc', function () {
assert.strictEqual(md.render('123\n'), '<p>123</p>\n'); assert.strictEqual(md.render('123\n'), '<p>123</p>\n');
}); });
it('Should quickly exit on empty string', function () {
var md = new Remarkable();
assert.strictEqual(md.render(''), '');
});
}); });

Loading…
Cancel
Save