fix markdown linking of images, also improve tests grammatically
+ 1 js syntax error
This commit is contained in:
parent
6494b14fb2
commit
8d512c22ac
3 changed files with 19 additions and 8 deletions
|
|
@ -20,12 +20,13 @@
|
||||||
// regex copied from: http://daringfireball.net/2010/07/improved_regex_for_matching_urls (slightly modified)
|
// regex copied from: http://daringfireball.net/2010/07/improved_regex_for_matching_urls (slightly modified)
|
||||||
var urlRegex = /(^|\s)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
|
var urlRegex = /(^|\s)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
|
||||||
text = text.replace(urlRegex, function(wholematch, space, url) {
|
text = text.replace(urlRegex, function(wholematch, space, url) {
|
||||||
|
if( url.match(/^[^\w]/) ) return wholematch; // evil witchcraft, noop
|
||||||
return space+"<"+url+">";
|
return space+"<"+url+">";
|
||||||
});
|
});
|
||||||
|
|
||||||
// process links
|
// process links
|
||||||
// regex copied from: https://code.google.com/p/pagedown/source/browse/Markdown.Converter.js#1198 (and slightly expanded)
|
// regex copied from: https://code.google.com/p/pagedown/source/browse/Markdown.Converter.js#1198 (and slightly expanded)
|
||||||
var linkRegex = /(\[.*\]:\s)?(<|\()((https?|ftp):\/\/[^\/'">\s][^'">\s]+)(>|\))/gi;
|
var linkRegex = /(\[.*\]:\s)?(<|\()((https?|ftp):\/\/[^\/'">\s][^'">\s]+?)(>|\))/gi;
|
||||||
text = text.replace(linkRegex, function() {
|
text = text.replace(linkRegex, function() {
|
||||||
var unicodeUrl = arguments[3];
|
var unicodeUrl = arguments[3];
|
||||||
var addr = parse_url(unicodeUrl);
|
var addr = parse_url(unicodeUrl);
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ describe("app.helpers.textFormatter", function(){
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
context("non-ascii urls", function() {
|
context("non-ascii url", function() {
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
this.evilUrls = [
|
this.evilUrls = [
|
||||||
"http://www.bürgerentscheid-krankenhäuser.de", // example from issue #2665
|
"http://www.bürgerentscheid-krankenhäuser.de", // example from issue #2665
|
||||||
|
|
@ -64,14 +64,14 @@ describe("app.helpers.textFormatter", function(){
|
||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
it("correctly encode to punycode", function() {
|
it("correctly encodes to punycode", function() {
|
||||||
_.each(this.evilUrls, function(url, num) {
|
_.each(this.evilUrls, function(url, num) {
|
||||||
var text = this.formatter.markdownify( "<" + url + ">" );
|
var text = this.formatter.markdownify( "<" + url + ">" );
|
||||||
expect(text).toContain(this.asciiUrls[num]);
|
expect(text).toContain(this.asciiUrls[num]);
|
||||||
}, this);
|
}, this);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("don't break link texts", function() {
|
it("doesn't break link texts", function() {
|
||||||
var linkText = "check out this awesome link!";
|
var linkText = "check out this awesome link!";
|
||||||
var text = this.formatter.markdownify( "["+linkText+"]("+this.evilUrls[0]+")" );
|
var text = this.formatter.markdownify( "["+linkText+"]("+this.evilUrls[0]+")" );
|
||||||
|
|
||||||
|
|
@ -79,7 +79,7 @@ describe("app.helpers.textFormatter", function(){
|
||||||
expect(text).toContain(linkText);
|
expect(text).toContain(linkText);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("don't break reference style links", function() {
|
it("doesn't break reference style links", function() {
|
||||||
var postContent = "blabla blab [my special link][1] bla blabla\n\n[1]: "+this.evilUrls[0]+" and an optional title)";
|
var postContent = "blabla blab [my special link][1] bla blabla\n\n[1]: "+this.evilUrls[0]+" and an optional title)";
|
||||||
var text = this.formatter.markdownify(postContent);
|
var text = this.formatter.markdownify(postContent);
|
||||||
|
|
||||||
|
|
@ -89,12 +89,22 @@ describe("app.helpers.textFormatter", function(){
|
||||||
|
|
||||||
it("can be used as img src", function() {
|
it("can be used as img src", function() {
|
||||||
var postContent = "";
|
var postContent = "";
|
||||||
var niceImg = '"'+ this.asciiUrls[1] +'"'; // the "" are from src=""
|
var niceImg = 'src="'+ this.asciiUrls[1] +'"'; // the "" are from src=""
|
||||||
var text = this.formatter.markdownify(postContent);
|
var text = this.formatter.markdownify(postContent);
|
||||||
|
|
||||||
expect(text).toContain(niceImg);
|
expect(text).toContain(niceImg);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("doesn't break linked images", function() {
|
||||||
|
var postContent = "I am linking an image here []("+this.evilUrls[3]+")";
|
||||||
|
var text = this.formatter.markdownify(postContent);
|
||||||
|
var linked_image = 'src="'+this.asciiUrls[1]+'"';
|
||||||
|
var image_link = 'href="'+this.asciiUrls[3]+'"';
|
||||||
|
|
||||||
|
expect(text).toContain(linked_image);
|
||||||
|
expect(text).toContain(image_link);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ describe("bookmarklet", function() {
|
||||||
_.defer(function() {
|
_.defer(function() {
|
||||||
expect($("#publisher #status_message_fake_text").val() == "").toBeFalsy();
|
expect($("#publisher #status_message_fake_text").val() == "").toBeFalsy();
|
||||||
expect($("#publisher #status_message_text").val() == "").toBeFalsy();
|
expect($("#publisher #status_message_text").val() == "").toBeFalsy();
|
||||||
};
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue