From d7d2e3691888af0e698eda8e565f72a5b18eb353 Mon Sep 17 00:00:00 2001 From: jbrw Date: Thu, 19 Nov 2020 14:09:55 -0500 Subject: [PATCH 1/2] FEATURE: Add an engine for deep linking to folders in Github repos --- lib/onebox/engine.rb | 1 + lib/onebox/engine/github_folder_onebox.rb | 51 + spec/fixtures/githubfolder.response | 1549 +++++++++++++++++ .../engine/github_folder_onebox_spec.rb | 28 + templates/githubfolder.mustache | 11 + 5 files changed, 1640 insertions(+) create mode 100644 lib/onebox/engine/github_folder_onebox.rb create mode 100644 spec/fixtures/githubfolder.response create mode 100644 spec/lib/onebox/engine/github_folder_onebox_spec.rb create mode 100644 templates/githubfolder.mustache diff --git a/lib/onebox/engine.rb b/lib/onebox/engine.rb index 1758a926..06782150 100644 --- a/lib/onebox/engine.rb +++ b/lib/onebox/engine.rb @@ -152,6 +152,7 @@ def always_https? 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 00000000..3a0d1de4 --- /dev/null +++ b/lib/onebox/engine/github_folder_onebox.rb @@ -0,0 +1,51 @@ +# 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 + } + 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 00000000..6e2ae422 --- /dev/null +++ b/spec/fixtures/githubfolder.response @@ -0,0 +1,1549 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + discourse/spec/fixtures at master · discourse/discourse · GitHub + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Skip to content + + + + + + + + +
+ +
+ + + + + +
+ + + +
+ + + + + + + + + +
+
+
+ + + + + + + + + + + + + + +
+ +
+ +
+

+ + + / + + discourse + + +

+ + +
+ + + +
+ + +
+ + +
+
+ + + +
+ +
+
+ + + master + + + + +
+ + + +
+
+
+ +
+ + +
+
+ discourse/spec/fixtures/ +
+
+ + +
+ +
discourse/spec/fixtures/
+ + + +
+
+

Latest commit

+
+ +
+ +
+
+ + @jbrw +
+
+ +
+ +
+ + +
#11253)
+
+* FEATURE: display error if Oneboxing fails due to HTTP error
+
+- display warning if onebox URL is unresolvable
+- display warning if attributes are missing
+
+* FEATURE: Use new Instagram oEmbed endpoint if access token is configured
+
+Instagram requires an Access Token to access their oEmbed endpoint. The requirements (from https://developers.facebook.com/docs/instagram/oembed/) are as follows:
+
+- a Facebook Developer account, which you can create at developers.facebook.com
+- a registered Facebook app
+- the oEmbed Product added to the app
+- an Access Token
+- The Facebook app must be in Live Mode
+
+The generated Access Token, once added to SiteSetting.facebook_app_access_token, will be passed to onebox. Onebox can then use this token to access the oEmbed endpoint to generate a onebox for Instagram.
+
+* DEV: update user agent string
+
+* DEV: don’t do HEAD requests against news.yahoo.com
+
+* DEV: Bump onebox version from 2.1.5 to 2.1.6
+
+* DEV: Avoid re-reading templates
+
+* DEV: Tweaks to onebox mustache templates
+
+* DEV: simplified error message for missing onebox data
+
+* Apply suggestions from code review
+Co-authored-by: Gerhard Schlager <mail@gerhard-schlager.at>
+
+ 331236d +
+
+
+

Git stats

+ +
+
+
+

Files

+ + + + Permalink + +
+ + + Failed to load latest commit information. + +
+
+
+
Type
+
Name
+
Latest commit message
+
Commit time
+
+
+ +
+
+
+ +
+
+ + +
+ +
+ backups +
+ + + +
+ Aug 21, 2020 +
+ +
+
+
+ + +
+ +
+ csv +
+ + + +
+ Jul 29, 2020 +
+ +
+
+
+ + +
+ +
+ db +
+ + + +
+ Jun 16, 2020 +
+ +
+
+
+ + +
+ +
+ emails +
+ + + +
+ Jul 27, 2020 +
+ +
+
+
+ + +
+ +
+ encodings +
+ + + +
+ Aug 1, 2018 +
+ +
+
+
+ + +
+ +
+ feed +
+ + + +
+ Aug 1, 2018 +
+ +
+
+
+ + +
+ +
+ i18n +
+ + + +
+ Jun 5, 2019 +
+ +
+
+
+ + +
+ +
+ images +
+ + + +
+ Oct 26, 2020 +
+ +
+
+
+ + +
+ +
+ json +
+ + + +
+ Aug 24, 2020 +
+ +
+
+
+ + +
+ +
+ md +
+ + + +
+ Oct 8, 2019 +
+ +
+
+
+ + +
+ +
+ media +
+ + + +
+ Jun 17, 2020 +
+ +
+
+
+ + +
+ +
+ mmdb +
+ + + +
+ Oct 25, 2018 +
+ +
+
+
+ + +
+ +
+ multisite +
+ + + +
+ Aug 8, 2017 +
+ +
+
+
+ + +
+ +
+ onebox +
+ + + +
+ Nov 18, 2020 +
+ +
+
+
+ + +
+ +
+ pdf +
+ + + +
+ Jul 25, 2019 +
+ +
+
+
+ + +
+ +
+ plugins +
+ + + +
+ Nov 11, 2020 +
+ +
+
+
+ + +
+ +
+ scss +
+ + + +
+ Sep 21, 2018 +
+ +
+
+
+ + +
+ + + + + +
+ Aug 15, 2017 +
+ +
+
+
+ + +
+ + + + + +
+ Apr 15, 2020 +
+ +
+
+
+ + +
+ +
+ themes +
+ + + +
+ Oct 14, 2019 +
+ +
+
+
+ + +
+ +
+ woff2 +
+ + + +
+ May 10, 2017 +
+ +
+
+
+ + + + +
+ + + + + +
+
+ +
+
+ +
+ + + + + + +
+ + + You can’t perform that action at this time. +
+ + + + + + + + + + + + diff --git a/spec/lib/onebox/engine/github_folder_onebox_spec.rb b/spec/lib/onebox/engine/github_folder_onebox_spec.rb new file mode 100644 index 00000000..5bdca4b5 --- /dev/null +++ b/spec/lib/onebox/engine/github_folder_onebox_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Onebox::Engine::GithubFolderOnebox do + before(:all) do + @link = "https://github.com/discourse/discourse/tree/master/spec/fixtures" + @uri = "https://github.com/discourse/discourse/tree/master/spec/fixtures" + fake(@uri, response(described_class.onebox_name)) + end + + include_context "engines" + it_behaves_like "an engine" + + describe "#to_html" do + it "includes link to folder with truncated display path" do + expect(html).to include("master/spec/fixtures") + end + + it "includes repository name" do + expect(html).to include("discourse/discourse") + end + + it "includes logo" do + expect(html).to include("") + end + end +end diff --git a/templates/githubfolder.mustache b/templates/githubfolder.mustache new file mode 100644 index 00000000..b2898bbc --- /dev/null +++ b/templates/githubfolder.mustache @@ -0,0 +1,11 @@ +{{#image}}{{/image}} + +

{{title}}

+ +{{#path}} +

{{path}}

+{{/path}} + +{{#description}} +

{{description}}

+{{/description}} From 93d33ec24e8d728dd5cb90dc4cea5e3849c630a1 Mon Sep 17 00:00:00 2001 From: jbrw Date: Thu, 19 Nov 2020 14:46:09 -0500 Subject: [PATCH 2/2] DEV: add favicon --- lib/onebox/engine/github_folder_onebox.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/onebox/engine/github_folder_onebox.rb b/lib/onebox/engine/github_folder_onebox.rb index 3a0d1de4..fa1f4b78 100644 --- a/lib/onebox/engine/github_folder_onebox.rb +++ b/lib/onebox/engine/github_folder_onebox.rb @@ -26,7 +26,8 @@ def data image: og.image, title: og.title, path: display_path, - description: display_description + description: display_description, + favicon: get_favicon } end