FEATURE: Add githubfolder engine (#440)

FEATURE: Add githubfolder engine (#440)

  • FEATURE: Add an engine for deep linking to folders in Github repos

  • DEV: add favicon

diff --git a/lib/onebox/engine.rb b/lib/onebox/engine.rb
index 1758a92..0678215 100644
--- a/lib/onebox/engine.rb
+++ b/lib/onebox/engine.rb
@@ -152,6 +152,7 @@ require_relative "engine/amazon_onebox"
 require_relative "engine/github_issue_onebox"
 require_relative "engine/github_blob_onebox"
 require_relative "engine/github_commit_onebox"
+require_relative "engine/github_folder_onebox"
 require_relative "engine/github_gist_onebox"
 require_relative "engine/github_pullrequest_onebox"
 require_relative "engine/google_calendar_onebox"
diff --git a/lib/onebox/engine/github_folder_onebox.rb b/lib/onebox/engine/github_folder_onebox.rb
new file mode 100644
index 0000000..fa1f4b7
--- /dev/null
+++ b/lib/onebox/engine/github_folder_onebox.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+module Onebox
+  module Engine
+    class GithubFolderOnebox
+      include Engine
+      include StandardEmbed
+      include LayoutSupport
+
+      matches_regexp Regexp.new(/^https?:\/\/(?:www\.)?(?:(?:\w)+\.)?(github)\.com[\:\d]*(\/\w*){2}\/tree/)
+      always_https
+
+      private
+
+      def data
+        og = get_opengraph
+
+        max_length = 250
+
+        display_path = extract_path(og.url, max_length)
+        display_description = clean_description(og.description, og.title, max_length)
+
+        {
+          link: og.url,
+          path_link: url,
+          image: og.image,
+          title: og.title,
+          path: display_path,
+          description: display_description,
+          favicon: get_favicon
+        }
+      end
+
+      def extract_path(root, max_length)
+        path = url.split('#')[0].split('?')[0]
+        path = path["#{root}/tree/".length..-1]
+        path.length > max_length ? path[-max_length..-1] : path
+      end
+
+      def clean_description(description, title, max_length)
+        return unless description
+
+        desc_end = " - #{title}"
+        if description[-desc_end.length..-1] == desc_end
+          description = description[0...-desc_end.length]
+        end
+
+        Onebox::Helpers.truncate(description, max_length)
+      end
+    end
+  end
+end
diff --git a/spec/fixtures/githubfolder.response b/spec/fixtures/githubfolder.response
new file mode 100644
index 0000000..6e2ae42
--- /dev/null
+++ b/spec/fixtures/githubfolder.response
@@ -0,0 +1,1549 @@
+
+
+
+
+
+
+
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+  <link rel="dns-prefetch" href="https://github.githubassets.com">
+  <link rel="dns-prefetch" href="https://avatars0.githubusercontent.com">
+  <link rel="dns-prefetch" href="https://avatars1.githubusercontent.com">
+  <link rel="dns-prefetch" href="https://avatars2.githubusercontent.com">
+  <link rel="dns-prefetch" href="https://avatars3.githubusercontent.com">
+  <link rel="dns-prefetch" href="https://github-cloud.s3.amazonaws.com">
+  <link rel="dns-prefetch" href="https://user-images.githubusercontent.com/">
+
+
+
+  <link crossorigin="anonymous" media="all" integrity="sha512-UzMqFF3u6q+PIf7vRROKCCcSAIYk0CGPD1MvMAnv0X7Pqxc6MTt+l1mXE6StaVvPg+m/XgXexi1uO8P/zszHgA==" rel="stylesheet" href="https://github.githubassets.com/assets/frameworks-53332a145deeeaaf8f21feef45138a08.css" />
+  <link crossorigin="anonymous" media="all" integrity="sha512-64lrKWJQDNfV0kiKLzdghJjhBJyD7/qXUKAmjXO+y4Xlnaav6upzJkp9hTlKJrul5SsSRIiX0aaCleQNyMdeZw==" rel="stylesheet" href="https://github.githubassets.com/assets/site-eb896b2962500cd7d5d2488a2f376084.css" />
+    <link crossorigin="anonymous" media="all" integrity="sha512-OjLzwi/p8mjjfWtWCrPLbeZem4jzl/IWUvdseQTXqLcLUjAnF7rGsfFxWUfDRURNW4YCSes9IpkKDcgJ7VTUfg==" rel="stylesheet" href="https://github.githubassets.com/assets/github-3a32f3c22fe9f268e37d6b560ab3cb6d.css" />
+
+
+
+
+
+  <script crossorigin="anonymous" defer="defer" integrity="sha512-8K2vvwbW+6H27Nad5ydg8PA2/aMD/LKq+EiK9s0U0hhVZxCI2tWBsYk9beAtisRw2j+Or5k2/F+6dk02nmj/PA==" type="application/javascript" src="https://github.githubassets.com/assets/environment-f0adafbf.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-QS9QfK1IxTvdFnaP4mBeuBisppg+Ow9rrljCm+oBriKByY64ArshdcA79yrpFSHnm2Surz9uRoZTRBB4x3/2Yg==" type="application/javascript" src="https://github.githubassets.com/assets/chunk-frameworks-412f507c.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-hpGQ52clY/n4R9OsQLgOJzvbYuU227k2yXUQXuwh/PWoRqFFofLCKHsaJGVG7fZy5px2fqJFXwdxjZuhMqSgnQ==" type="application/javascript" src="https://github.githubassets.com/assets/chunk-vendor-869190e7.js"></script>
+
+  <script crossorigin="anonymous" defer="defer" integrity="sha512-cceAQRiPoDQtc7zRrKT3NNwvSh6bCtjBslmEJiosWxw+xdU/61I0Dw9uPZz/VSGcEgCsExeMOtRZfQIRmjYYoA==" type="application/javascript" src="https://github.githubassets.com/assets/behaviors-71c78041.js"></script>
+
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-P2YMkqfXJOOfxTTkNHbblY5ks3U+e9w9tiVyK9syrE5+JmlaCg1kUiuT1DfbyJXwaOLaRLT3zam2r+QrxTZ3iw==" type="application/javascript" data-module-id="./chunk-contributions-spider-graph.js" data-src="https://github.githubassets.com/assets/chunk-contributions-spider-graph-3f660c92.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-obMR8mPKx8OvqRe34LgnUcxeJ1qujiA4ND3H6UX13ExMlA/WfHLjEzXRmgGRcRvN/8J1nzc+Z+jgz/PLTFy6zg==" type="application/javascript" data-module-id="./chunk-drag-drop.js" data-src="https://github.githubassets.com/assets/chunk-drag-drop-a1b311f2.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-yGHlU15Ycv/1UkpPTVKgm9bDZRVbDJxx0n94pbIWequIXR3FYSskJerCSCPSzBAeXis2ZLtihPIP/keMFYwkDg==" type="application/javascript" data-module-id="./chunk-jump-to.js" data-src="https://github.githubassets.com/assets/chunk-jump-to-c861e553.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-tcH4xCRuMBAh1PruDaiwGnRIbHlF6bGLhxyCQ16uqok1cV5QFMguVPWJtN9KI0jGQOgN+Pha3+uOUXhXdfK/qw==" type="application/javascript" data-module-id="./chunk-profile-pins-element.js" data-src="https://github.githubassets.com/assets/chunk-profile-pins-element-b5c1f8c4.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-gPx3bYhTjyC83X5u5KlEDJpwGAHt3AC2p5s9iMuAfPTeSj7kHlKMW231C3K3c7+jvlpWpELk8DJsefrYdRzqjA==" type="application/javascript" data-module-id="./chunk-randomColor.js" data-src="https://github.githubassets.com/assets/chunk-randomColor-80fc776d.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-E+H+wAtjiqutBvn2cnXzDIvmasIhYiS7i7JzOfFUwo+Ej8zT54OrJtP//RhwixnypgOpCF4JvqzYy6zOtORDmg==" type="application/javascript" data-module-id="./chunk-runner-groups.js" data-src="https://github.githubassets.com/assets/chunk-runner-groups-13e1fec0.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-qdiCkJPPR4LxwUKftEmJe2v79E8xnTceYqylsWkMsGuARkiKkX9iFNwkvZJ3bDfS5YHSPD3+k+N2/I73tvlL1Q==" type="application/javascript" data-module-id="./chunk-sortable-behavior.js" data-src="https://github.githubassets.com/assets/chunk-sortable-behavior-a9d88290.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-NpURjBPyJ0JT8hWOMbLErYNeb0bTkKfmFX1hl1F8C/q6jckqWObeOzEAcs6TRlj+cqAR6GDohEBxDgkYBlx+QQ==" type="application/javascript" data-module-id="./chunk-tweetsodium.js" data-src="https://github.githubassets.com/assets/chunk-tweetsodium-3695118c.js"></script>
+    <script crossorigin="anonymous" defer="defer" integrity="sha512-e+30BriXsBu+fb/LGEU9gAzSwn25wRYzDDPpr2AA8jZJI9034xzRHYXjA9XhF+7ncBIGJ/uTD11G25/H0XyuZg==" type="application/javascript" data-module-id="./chunk-user-status-submit.js" data-src="https://github.githubassets.com/assets/chunk-user-status-submit-7bedf406.js"></script>
+

[... diff too long, it was truncated ...]

GitHub sha: ff5a2fb8

This commit appears in #440 which was approved by eviltrout. It was merged by jbrw.