lib/onebox/engine/github_blob_onebox.rb in onebox-1.2.9 vs lib/onebox/engine/github_blob_onebox.rb in onebox-1.3.0

- old
+ new

@@ -2,19 +2,15 @@ module Engine class GithubBlobOnebox include Engine include LayoutSupport - matches do - http - maybe("www") - domain("github") - tld("com") - anything - with("/blob/") - end + MAX_LINES = 20 + MAX_CHARS = 5000 + matches_regexp(/^https?:\/\/(www\.)?github\.com.*\/blob\//) + private def raw return @raw if @raw m = @url.match(/github\.com\/(?<user>[^\/]+)\/(?<repo>[^\/]+)\/blob\/(?<sha1>[^\/]+)\/(?<file>[^#]+)(#(L(?<from>[^-]*)(-L(?<to>.*))?))?/mi) @@ -30,19 +26,28 @@ end if to > from contents = contents.split("\n")[from..to].join("\n") end end - if contents.length > 5000 - contents = contents[0..5000] + if contents.length > MAX_CHARS + contents = contents[0..MAX_CHARS] @truncated = true end + + split = contents.split("\n") + if split.length > MAX_LINES + contents = split[0..MAX_LINES].join("\n") + @truncated = true + end @raw = contents end end def data - @data ||= {title: link, link: link, content: raw, truncated: @truncated} + @data ||= {title: link.sub(/^https?\:\/\//, ''), + link: link, + content: raw, + truncated: @truncated} end end end end