Support uppercase bbcode too.

This commit is contained in:
Robin Ward 2014-03-03 11:59:57 -05:00
parent 13f82f856f
commit 6143753fef
2 changed files with 29 additions and 37 deletions

View File

@ -4,13 +4,27 @@
@method replaceBBCode @method replaceBBCode
@param {tag} tag the tag we want to match @param {tag} tag the tag we want to match
@param {function} emitter the function that creates JsonML for the tag @param {function} emitter the function that creates JsonML for the tag
@param {Object} hash of options to pass to `inlineBetween`
**/ **/
function replaceBBCode(tag, emitter) { function replaceBBCode(tag, emitter, opts) {
Discourse.Dialect.inlineBetween({ opts = opts || {};
start: "[" + tag + "]", opts = _.merge(opts, { start: "[" + tag + "]", stop: "[/" + tag + "]", emitter: emitter });
stop: "[/" + tag + "]", Discourse.Dialect.inlineBetween(opts);
emitter: emitter
}); tag = tag.toUpperCase();
opts = _.merge(opts, { start: "[" + tag + "]", stop: "[/" + tag + "]", emitter: emitter });
Discourse.Dialect.inlineBetween(opts);
}
/**
Shortcut to call replaceBBCode with `rawContents` as true.
@method replaceBBCode
@param {tag} tag the tag we want to match
@param {function} emitter the function that creates JsonML for the tag
**/
function rawBBCode(tag, emitter) {
replaceBBCode(tag, emitter, { rawContents: true });
} }
/** /**
@ -57,37 +71,14 @@ replaceBBCode('ul', function(contents) { return ['ul'].concat(contents); });
replaceBBCode('ol', function(contents) { return ['ol'].concat(contents); }); replaceBBCode('ol', function(contents) { return ['ol'].concat(contents); });
replaceBBCode('li', function(contents) { return ['li'].concat(contents); }); replaceBBCode('li', function(contents) { return ['li'].concat(contents); });
Discourse.Dialect.inlineBetween({ rawBBCode('img', function(contents) { return ['img', {href: contents}]; });
start: '[img]', rawBBCode('email', function(contents) { return ['a', {href: "mailto:" + contents, 'data-bbcode': true}, contents]; });
stop: '[/img]', rawBBCode('url', function(contents) { return ['a', {href: contents, 'data-bbcode': true}, contents]; });
rawContents: true, rawBBCode('spoiler', function(contents) {
emitter: function(contents) { return ['img', {href: contents}]; } if (/<img/i.test(contents)) {
}); return ['div', { 'class': 'spoiler' }, contents];
} else {
Discourse.Dialect.inlineBetween({ return ['span', { 'class': 'spoiler' }, contents];
start: '[email]',
stop: '[/email]',
rawContents: true,
emitter: function(contents) { return ['a', {href: "mailto:" + contents, 'data-bbcode': true}, contents]; }
});
Discourse.Dialect.inlineBetween({
start: '[url]',
stop: '[/url]',
rawContents: true,
emitter: function(contents) { return ['a', {href: contents, 'data-bbcode': true}, contents]; }
});
Discourse.Dialect.inlineBetween({
start: '[spoiler]',
stop: '[/spoiler]',
rawContents: true,
emitter: function(contents) {
if (/<img/i.test(contents)) {
return ['div', { 'class': 'spoiler' }, contents];
} else {
return ['span', { 'class': 'spoiler' }, contents];
}
} }
}); });

View File

@ -16,6 +16,7 @@ test('basic bbcode', function() {
format("[b]evil [i]trout[/i][/b]", format("[b]evil [i]trout[/i][/b]",
"<span class=\"bbcode-b\">evil <span class=\"bbcode-i\">trout</span></span>", "<span class=\"bbcode-b\">evil <span class=\"bbcode-i\">trout</span></span>",
"allows embedding of tags"); "allows embedding of tags");
format("[EMAIL]eviltrout@mailinator.com[/EMAIL]", "<a href=\"mailto:eviltrout@mailinator.com\">eviltrout@mailinator.com</a>", "supports upper case bbcode");
}); });
test('invalid bbcode', function() { test('invalid bbcode', function() {