Skip to content

Commit 612c0cc

Browse files
committed
FIX: Autolinking within a list
1 parent 513f941 commit 612c0cc

3 files changed

Lines changed: 37 additions & 43 deletions

File tree

app/assets/javascripts/discourse/dialects/autolink_dialect.js

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,32 @@ Discourse.Dialect.on("register", function(event) {
1414
Parses out links from HTML.
1515
1616
@method autoLink
17-
@param {Markdown.Block} block the block to examine
18-
@param {Array} next the next blocks in the sequence
19-
@return {Array} the JsonML containing the markup or undefined if nothing changed.
17+
@param {String} text the text match
18+
@param {Array} match the match found
19+
@param {Array} prev the previous jsonML
20+
@return {Array} an array containing how many chars we've replaced and the jsonML content for it.
2021
@namespace Discourse.Dialect
2122
**/
22-
dialect.block['autolink'] = function autoLink(block, next) {
23-
var pattern = /(^|\s)((?:https?:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.])(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»\s]))/gm,
24-
result,
25-
remaining = block,
26-
m;
23+
dialect.inline['http'] = dialect.inline['www'] = function autoLink(text, match, prev) {
2724

28-
var pushIt = function(p) { result.push(p) };
25+
// We only care about links on boundaries
26+
if (prev && (prev.length > 0)) {
27+
var last = prev[prev.length - 1];
28+
if (typeof last === "string" && (!last.match(/\s$/))) { return; }
29+
}
2930

30-
while (m = pattern.exec(remaining)) {
31-
result = result || ['p'];
31+
var pattern = /(^|\s)((?:https?:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.])(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\([^\s()<>]+\)|[^`!()\[\]{};:'".,<>?«»\s]))/gm,
32+
m = pattern.exec(text);
3233

34+
if (m) {
3335
var url = m[2],
34-
urlIndex = remaining.indexOf(url),
35-
before = remaining.slice(0, urlIndex);
36-
37-
if (before.match(/\[\d+\]/)) { return; }
38-
39-
pattern.lastIndex = 0;
40-
remaining = remaining.slice(urlIndex + url.length);
36+
displayUrl = m[2];
4137

42-
if (before) {
43-
this.processInline(before).forEach(pushIt);
44-
}
45-
46-
var displayUrl = url;
4738
if (url.match(/^www/)) { url = "http://" + url; }
48-
result.push(['a', {href: url}, displayUrl]);
49-
50-
if (remaining && remaining.match(/\n/)) {
51-
next.unshift(MD.mk_block(remaining));
52-
remaining = [];
53-
}
39+
return [m[0].length, ['a', {href: url}, displayUrl]];
5440
}
5541

56-
if (result) {
57-
if (remaining.length) {
58-
this.processInline(remaining).forEach(pushIt);
59-
}
60-
return [result];
61-
}
6242
};
6343

44+
6445
});

app/assets/javascripts/discourse/dialects/mention_dialect.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,17 @@ Discourse.Dialect.on("register", function(event) {
1111
var dialect = event.dialect,
1212
MD = event.MD;
1313

14-
dialect.inline['@'] = function(text, match, prev) {
15-
var args = Array.prototype.slice.call(arguments);
14+
/**
15+
Parses out @username mentions.
16+
17+
@method parseMentions
18+
@param {String} text the text match
19+
@param {Array} match the match found
20+
@param {Array} prev the previous jsonML
21+
@return {Array} an array containing how many chars we've replaced and the jsonML content for it.
22+
@namespace Discourse.Dialect
23+
**/
24+
dialect.inline['@'] = function parseMentions(text, match, prev) {
1625

1726
// We only care about mentions on word boundaries
1827
if (prev && (prev.length > 0)) {
@@ -25,8 +34,7 @@ Discourse.Dialect.on("register", function(event) {
2534

2635
if (m) {
2736
var username = m[1],
28-
mentionLookup = dialect.options.mentionLookup || Discourse.Mention.lookupCache,
29-
index = prev.indexOf(username);
37+
mentionLookup = dialect.options.mentionLookup || Discourse.Mention.lookupCache;
3038

3139
if (mentionLookup(username.substr(1))) {
3240
return [username.length, ['a', {'class': 'mention', href: Discourse.getURL("/users/") + username.substr(1).toLowerCase()}, username]];

test/javascripts/components/markdown_test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ test("Line Breaks", function() {
3838

3939
test("Links", function() {
4040

41+
cooked("EvilTrout: http://eviltrout.com",
42+
'<p>EvilTrout: <a href="http://eviltrout.com">http://eviltrout.com</a></p>',
43+
"autolinks a URL");
44+
4145
cooked("Youtube: http://www.youtube.com/watch?v=1MrpeBRkM5A",
4246
'<p>Youtube: <a href="http://www.youtube.com/watch?v=1MrpeBRkM5A">http://www.youtube.com/watch?v=1MrpeBRkM5A</a></p>',
4347
"allows links to contain query params");
@@ -58,10 +62,6 @@ test("Links", function() {
5862
'<p>Atwood: <a href="http://www.codinghorror.com">http://www.codinghorror.com</a></p>',
5963
"autolinks a URL with http://www");
6064

61-
cooked("EvilTrout: http://eviltrout.com",
62-
'<p>EvilTrout: <a href="http://eviltrout.com">http://eviltrout.com</a></p>',
63-
"autolinks a URL");
64-
6565
cooked("EvilTrout: http://eviltrout.com hello",
6666
'<p>EvilTrout: <a href="http://eviltrout.com">http://eviltrout.com</a> hello</p>',
6767
"autolinks with trailing text");
@@ -78,6 +78,11 @@ test("Links", function() {
7878
"<p>Here's a tweet:<br><a href=\"https://twitter.com/evil_trout/status/345954894420787200\" class=\"onebox\">https://twitter.com/evil_trout/status/345954894420787200</a></p>",
7979
"It doesn't strip the new line.");
8080

81+
cooked("1. View @eviltrout's profile here: http://meta.discourse.org/users/eviltrout/activity\nnext line.",
82+
"<ol><li>View <span class=\"mention\">@eviltrout</span>'s profile here: <a href=\"http://meta.discourse.org/users/eviltrout/activity\">http://meta.discourse.org/users/eviltrout/activity</a><br>next line.</li></ol>",
83+
"allows autolinking within a list without inserting a paragraph.");
84+
85+
8186
cooked("[3]: http://eviltrout.com", "", "It doesn't autolink markdown link references");
8287

8388
cooked("http://discourse.org and http://discourse.org/another_url and http://www.imdb.com/name/nm2225369",

0 commit comments

Comments
 (0)