Skip to content

Commit

Permalink
Add autolink (#25)
Browse files Browse the repository at this point in the history
* feat: Add autolink

* test: Add autolink test cases
  • Loading branch information
hugoabonizio authored and veelenga committed Aug 3, 2017
1 parent 2bb3c8a commit 27dbd5a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ shards:
github: Amber-Crystal/amber
version: 0.1.6

autolink:
github: hugoabonizio/autolink.cr
version: 0.1.1

baked_file_system:
github: schovi/baked_file_system
version: 0.9.4
Expand Down
4 changes: 4 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ dependencies:
hashids:
github: splattael/hashids.cr

autolink:
github: hugoabonizio/autolink.cr
version: 0.1.1

development_dependencies:
spec2:
github: waterlink/spec2.cr
Expand Down
23 changes: 23 additions & 0 deletions spec/models/announcement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ describe Announcement do
ann = announcement(description: "<script>console.log('hello')</script>")
expect(ann.content).to eq "<p>&lt;script>console.log('hello')&lt;/script></p>"
end

it "autolinks" do
anns = [
announcement(description: "http://www.myproj.com/"),
announcement(description: "Some new project at http://www.myproj.com/."),
announcement(description: "(link: http://www.myproj.com/)."),
announcement(description: "# Bigger example\nLorem ipsum http://www.myproj.com/."),
]
results = [
"<p><a href=\"http://www.myproj.com/\">http://www.myproj.com/</a></p>",
"<p>Some new project at <a href=\"http://www.myproj.com/\">http://www.myproj.com/</a>.</p>",
"<p>(link: <a href=\"http://www.myproj.com/\">http://www.myproj.com/</a>).</p>",
"<h1>Bigger example</h1>\n\n<p>Lorem ipsum <a href=\"http://www.myproj.com/\">http://www.myproj.com/</a>.</p>",
]
anns.size.times do |i|
expect(anns[i].content).to eq(results[i])
end
end

pending "autolink/markdown edge case" do
content = "<a href=\"hello\">http://example.com</a>"
expect(announcement(description: content).content).to eq content
end
end

describe "#short_path" do
Expand Down
3 changes: 2 additions & 1 deletion src/models/announcement.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "granite_orm/adapter/pg"
require "markdown"
require "autolink"

class Announcement < Granite::ORM
TYPES = {
Expand Down Expand Up @@ -97,6 +98,6 @@ class Announcement < Granite::ORM
end

def content
Markdown.to_html(description.not_nil!)
Autolink.auto_link(Markdown.to_html(description.not_nil!))
end
end

0 comments on commit 27dbd5a

Please sign in to comment.