Comparing version 2.1.0 to 2.1.1
@@ -8,10 +8,10 @@ const url = require('url'); | ||
const commandRegExp = /^\/(\w+)\s?/; | ||
const commandRegExp = /(?:^|.*[ ])\/(\w+)\s?(.*)/gim; | ||
const emptyImage = 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='; | ||
const commands = { | ||
giphy: function(markdown, args, callback) { | ||
giphy: function(args, callback) { | ||
// This command requires arguments. | ||
if (!args) { | ||
return callback(null, markdown); | ||
return callback(); | ||
} | ||
@@ -31,7 +31,7 @@ | ||
callback(null, `${markdown}\n${gif.images.original.url}`); | ||
callback(null, `/giphy ${args}\n${gif.images.original.url}`); | ||
}); | ||
}, | ||
shrug: function(markdown, args, callback) { | ||
callback(null, `${markdown}\n\`¯\\_(ツ)_/¯\``); | ||
shrug: function(args, callback) { | ||
callback(null, '¯\\\\_(ツ)\\_/¯'); | ||
} | ||
@@ -139,31 +139,56 @@ }; | ||
// Check for a /command. | ||
var matches = markdown.match(commandRegExp); | ||
// Check for /commands | ||
var commandMatches = markdown.match(commandRegExp); | ||
// If there isn't a /command, return. | ||
if (!matches) { | ||
// If there aren't any /commands, return. | ||
if (!commandMatches || !commandMatches.length) { | ||
return callback(); | ||
} | ||
// The command's name is captured in the first match. | ||
var command = matches[1]; | ||
var commandIndex = 0; | ||
// If the /command isn't supported, return. | ||
if (!commands[command]) { | ||
return callback(); | ||
} | ||
var lines = markdown.split(/\n/gmi).map(l => { | ||
var isCommand = new RegExp(commandRegExp.source, 'gim').test(l); | ||
// Get the command's arguments. | ||
var args = markdown.replace(commandRegExp, '').trim(); | ||
return { | ||
commandIndex: isCommand ? commandIndex++ : -1, | ||
markdown: l | ||
}; | ||
}); | ||
// Execute the /command | ||
commands[command](markdown, args, (err, _markdown) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
async.forEachOf(commandMatches, function(commandMatch, index, callback) { | ||
var tokens = new RegExp(commandRegExp.source, 'gim').exec(commandMatch); | ||
if (_markdown) { | ||
markdown = _markdown; | ||
// The command's name is captured in the first match. | ||
var commandName = tokens[1]; | ||
// If the /command isn't supported, return. | ||
if (!commands[commandName]) { | ||
return callback(); | ||
} | ||
// Get the command's arguments. | ||
var args = tokens[2]; | ||
// Execute the /command | ||
commands[commandName](args, (err, result) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (result) { | ||
var line = lines.find(l => l.commandIndex === index); | ||
var replace = `/${commandName}`; | ||
if (args) { | ||
replace += ` ${args}`; | ||
} | ||
line.markdown = line.markdown.replace(replace, result); | ||
} | ||
callback(); | ||
}); | ||
}, function() { | ||
markdown = lines.map(l => l.markdown).join('\n'); | ||
callback(); | ||
@@ -170,0 +195,0 @@ }); |
@@ -21,3 +21,3 @@ { | ||
}, | ||
"version": "2.1.0" | ||
"version": "2.1.1" | ||
} |
@@ -40,3 +40,4 @@ mehdown | ||
- `/shrug` | ||
- `/giphy [text]` - Post a random GIF | ||
- `/shrug` - ¯\\\_(ツ)\_/¯ | ||
@@ -43,0 +44,0 @@ ## Supports a subset of BBCode tags |
@@ -89,5 +89,3 @@ const assert = require('assert'); | ||
it('/giphy meh', function(done) { | ||
var markdown = '/giphy meh'; | ||
mehdown.render(markdown, function(err, html) { | ||
mehdown.render('/giphy meh', function(err, html) { | ||
assert.notEqual(html, '<p>/giphy meh</p>'); | ||
@@ -98,2 +96,11 @@ assert.notEqual(html.indexOf('http'), -1); | ||
}); | ||
it('lorem ipsum\n/giphy first\nfoo bar\n/giphy second third\n@username /giphy fourth\nhey @username /giphy fifth\nthis is not a command `/giphy sixth`', function(done) { | ||
mehdown.render('lorem ipsum\n/giphy first\nfoo bar\n/giphy second third\n@username /giphy fourth\nhey @username /giphy fifth\nthis is not a command `/giphy sixth`', function(err, html) { | ||
assert.notEqual(html.indexOf('lorem ipsum'), -1); | ||
assert.notEqual(html.indexOf('foo bar'), -1); | ||
assert.equal(html.match(/<img/g).length, 4); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -104,3 +111,3 @@ | ||
mehdown.render('/shrug', function(err, html) { | ||
assert.equal(html, '<p>/shrug<br />\n<code>¯\\_(ツ)_/¯</code></p>'); | ||
assert.equal(html, '<p>¯\\\_(ツ)\_/¯</p>'); | ||
done(); | ||
@@ -107,0 +114,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102447
1573
56