hash tags are linked on the front end, adding markdown.js file

This commit is contained in:
danielgrippi 2012-01-06 16:35:40 -08:00 committed by Dennis Collinson
parent fd6e3bae62
commit 0fb1bf5d4b
4 changed files with 1512 additions and 3 deletions

View file

@ -16,7 +16,9 @@ class Post < ActiveRecord::Base
api_accessible :backbone do |t| api_accessible :backbone do |t|
t.add :id t.add :id
t.add :guid t.add :guid
t.add :text t.add lambda { |post|
post.raw_message
}, :as => :text
t.add :public t.add :public
t.add :created_at t.add :created_at
t.add :comments_count t.add :comments_count

View file

@ -2,9 +2,21 @@
var postContentView = app.views.StreamObject.extend({ var postContentView = app.views.StreamObject.extend({
presenter : function(){ presenter : function(){
return _.extend(this.defaultPresenter(), { return _.extend(this.defaultPresenter(), {
text : markdown.toHTML(this.model.get("text") || "") text : metafyText(this.model.get("text"))
}) })
},
function metafyText(text) {
//we want it to return at least a <p> from markdown
text = text || ""
return hashtagify(markdown.toHTML(text) || text)
}
function hashtagify(text){
return text.replace(/(#([\u0080-\uFFFF|\w|-]+|&lt;3))/g, function(tagText) {
return "<a href='/tags/" + tagText.substring(1) + "' class='tag'>" + tagText + "</a>"
})
}
}
}) })

1467
public/javascripts/vendor/markdown.js vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -38,6 +38,34 @@ describe("app.views.Post", function(){
expect(window.markdown.toHTML).toHaveBeenCalledWith("I have three Belly Buttons") expect(window.markdown.toHTML).toHaveBeenCalledWith("I have three Belly Buttons")
}) })
context("changes hashtags to links", function(){
it("links to a hashtag to the tag page", function(){
this.statusMessage.set({text: "I love #parties"})
var view = new app.views.Post({model : this.statusMessage}).render();
expect(view.$("a:contains('#parties')").attr('href')).toBe('/tags/parties')
})
it("changes all hashtags", function(){
this.statusMessage.set({text: "I love #parties and #rockstars and #unicorns"})
var view = new app.views.Post({model : this.statusMessage}).render();
expect(view.$("a.tag").length).toBe(3)
})
// NOTE THIS DIVERGES FROM GRUBER'S ORIGINAL DIALECT OF MARKDOWN.
// We had to edit markdown.js line 291 - good people would have made a new dialect.
//
// original : var m = block.match( /^(#{1,6})\s*(.*?)\s*#*\s*(?:\n|$)/ );
// \s* changed to \s+
//
it("doesn't create a header tag if the first word is a hashtag", function(){
this.statusMessage.set({text: "#parties, I love"})
var view = new app.views.Post({model : this.statusMessage}).render();
expect(view.$("h1:contains(parties)")).not.toExist();
expect(view.$("a:contains('#parties')")).toExist();
})
})
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);