autolink urls in content view

This commit is contained in:
danielgrippi 2012-01-07 20:47:30 -08:00
parent 70737ddc9d
commit ad2e6a414f
3 changed files with 29 additions and 2 deletions

View file

@ -10,9 +10,11 @@ app.views.Content = app.views.StreamObject.extend({
text = text || "" text = text || ""
return mentionify( return mentionify(
hashtagify( hashtagify(
urlify(
markdownify(text) markdownify(text)
) )
) )
)
} }
function markdownify(text){ function markdownify(text){
@ -34,11 +36,18 @@ app.views.Content = app.views.StreamObject.extend({
return person.diaspora_id == diasporaId return person.diaspora_id == diasporaId
}).id }).id
return "<a href='/people/" + personId + "' class='mention'>" + fullName + "</a>" return "<a href='/people/" + personId + "' class='mention'>" + fullName + "</a>"
}) })
return text return text
} }
function urlify(text) {
var urlRegex = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi
return text.replace(urlRegex, function(url) {
var protocol = (url.search(/:\/\//) == -1 ? "http://" : "")
return "<a href='" + protocol + url + "' target=_blank>" + url + "</a>"
})
}
} }
}) })

View file

@ -126,6 +126,23 @@ describe("app.views.Post", function(){
}) })
}) })
context("generates urls from plaintext", function(){
it("works", function(){
links = ["http://google.com",
"https://joindiaspora.com",
"http://www.yahooligans.com",
"obama.com",
"japan.co.jp"]
this.statusMessage.set({text : links.join(" ")})
var view = new app.views.Post({model : this.statusMessage}).render();
_.each(links, function(link) {
expect(view.$("a[href='" + link + "']").text()).toContain(link)
})
})
})
context("user not signed in", function(){ context("user not signed in", function(){
it("does not provide a Feedback view", function(){ it("does not provide a Feedback view", function(){
window.current_user = app.user(null); window.current_user = app.user(null);

View file

@ -42,6 +42,7 @@ src_files:
- public/javascripts/app/models/* - public/javascripts/app/models/*
- public/javascripts/app/collections/* - public/javascripts/app/collections/*
- public/javascripts/app/views/stream_object_view.js - public/javascripts/app/views/stream_object_view.js
- public/javascripts/app/views/content_view.js
- public/javascripts/app/views/* - public/javascripts/app/views/*
- public/javascripts/mobile.js - public/javascripts/mobile.js