FIX: Support for nested bold/italics in MD

This commit is contained in:
Robin Ward
2014-06-09 17:46:17 -04:00
parent 93fbbe79b4
commit c6b92f0ef7
3 changed files with 17 additions and 4 deletions

View File

@ -7,12 +7,14 @@
Discourse.Dialect.inlineBetween({ Discourse.Dialect.inlineBetween({
between: '***', between: '***',
wordBoundary: true, wordBoundary: true,
spaceBoundary: true,
emitter: function(contents) { return ['strong', ['em'].concat(contents)]; } emitter: function(contents) { return ['strong', ['em'].concat(contents)]; }
}); });
Discourse.Dialect.inlineBetween({ Discourse.Dialect.inlineBetween({
between: '___', between: '___',
wordBoundary: true, wordBoundary: true,
spaceBoundary: true,
emitter: function(contents) { return ['strong', ['em'].concat(contents)]; } emitter: function(contents) { return ['strong', ['em'].concat(contents)]; }
}); });
@ -21,6 +23,7 @@ var replaceMarkdown = function(match, tag) {
Discourse.Dialect.inlineBetween({ Discourse.Dialect.inlineBetween({
between: match, between: match,
wordBoundary: true, wordBoundary: true,
spaceBoundary: true,
emitter: function(contents) { return [tag].concat(contents) } emitter: function(contents) { return [tag].concat(contents) }
}); });
}; };

View File

@ -118,7 +118,6 @@ function parseTree(tree, path, insideCounts) {
@returns {Boolean} whether there is an invalid word boundary @returns {Boolean} whether there is an invalid word boundary
**/ **/
function invalidBoundary(args, prev) { function invalidBoundary(args, prev) {
if (!args.wordBoundary && !args.spaceBoundary) { return false; } if (!args.wordBoundary && !args.spaceBoundary) { return false; }
var last = prev[prev.length - 1]; var last = prev[prev.length - 1];
@ -260,14 +259,14 @@ Discourse.Dialect = {
inlineBetween: function(args) { inlineBetween: function(args) {
var start = args.start || args.between, var start = args.start || args.between,
stop = args.stop || args.between, stop = args.stop || args.between,
startLength = start.length; startLength = start.length,
self = this;
this.registerInline(start, function(text, match, prev) { this.registerInline(start, function(text, match, prev) {
if (invalidBoundary(args, prev)) { return; } if (invalidBoundary(args, prev)) { return; }
var endPos = text.indexOf(stop, startLength); var endPos = self.findEndPos(text, stop, args, startLength);
if (endPos === -1) { return; } if (endPos === -1) { return; }
var between = text.slice(startLength, endPos); var between = text.slice(startLength, endPos);
// If rawcontents is set, don't process inline // If rawcontents is set, don't process inline
@ -282,6 +281,16 @@ Discourse.Dialect = {
}); });
}, },
findEndPos: function(text, stop, args, start) {
var endPos = text.indexOf(stop, start);
if (endPos === -1) { return -1; }
var after = text.charAt(endPos + stop.length);
if (after && after.indexOf(stop) === 0) {
return this.findEndPos(text, stop, args, endPos + stop.length + 1);
}
return endPos;
},
/** /**
Registers a block for processing. This is more complicated than using one of Registers a block for processing. This is more complicated than using one of
the other helpers such as `replaceBlock` so consider using them first! the other helpers such as `replaceBlock` so consider using them first!

View File

@ -19,6 +19,7 @@ test("basic cooking", function() {
cooked("__bold__", "<p><strong>bold</strong></p>", "it bolds text."); cooked("__bold__", "<p><strong>bold</strong></p>", "it bolds text.");
cooked("*trout*", "<p><em>trout</em></p>", "it italicizes text."); cooked("*trout*", "<p><em>trout</em></p>", "it italicizes text.");
cooked("_trout_", "<p><em>trout</em></p>", "it italicizes text."); cooked("_trout_", "<p><em>trout</em></p>", "it italicizes text.");
cooked("*this is italic **with some bold** inside*", "<p><em>this is italic <strong>with some bold</strong> inside</em></p>", "it handles nested bold in italics");
cooked("***hello***", "<p><strong><em>hello</em></strong></p>", "it can do bold and italics at once."); cooked("***hello***", "<p><strong><em>hello</em></strong></p>", "it can do bold and italics at once.");
cooked("word_with_underscores", "<p>word_with_underscores</p>", "it doesn't do intraword italics"); cooked("word_with_underscores", "<p>word_with_underscores</p>", "it doesn't do intraword italics");
cooked("common/_special_font_face.html.erb", "<p>common/_special_font_face.html.erb</p>", "it doesn't intraword with a slash"); cooked("common/_special_font_face.html.erb", "<p>common/_special_font_face.html.erb</p>", "it doesn't intraword with a slash");