Merge pull request #4010 from riking/patch-sitelinks

FEATURE: Add /search discovery
This commit is contained in:
Régis Hanol 2016-04-14 10:35:13 +02:00
commit 379bfac36d
9 changed files with 73 additions and 18 deletions

View File

@ -1,8 +1,8 @@
class ManifestJsonController < ApplicationController class MetadataController < ApplicationController
layout false layout false
skip_before_filter :preload_json, :check_xhr, :redirect_to_login_if_required skip_before_filter :preload_json, :check_xhr, :redirect_to_login_if_required
def index def manifest
manifest = { manifest = {
short_name: SiteSetting.title, short_name: SiteSetting.title,
display: 'standalone', display: 'standalone',
@ -14,4 +14,8 @@ class ManifestJsonController < ApplicationController
render json: manifest.to_json render json: manifest.to_json
end end
def opensearch
render file: "#{Rails.root}/app/views/metadata/opensearch.xml"
end
end end

View File

@ -1,8 +1,8 @@
require_dependency 'site_serializer' require_dependency 'site_serializer'
class SiteController < ApplicationController class SiteController < ApplicationController
layout false
skip_before_filter :preload_json skip_before_filter :preload_json, :check_xhr
def site def site
render json: Site.json_for(guardian) render json: Site.json_for(guardian)
@ -23,5 +23,4 @@ class SiteController < ApplicationController
def emoji def emoji
render json: custom_emoji render json: custom_emoji
end end
end end

View File

@ -157,6 +157,20 @@ module ApplicationHelper
result.join("\n") result.join("\n")
end end
def render_sitelinks_search_tag
json = {
'@context': 'http://schema.org',
'@type': 'WebSite',
url: Discourse.base_url,
potentialAction: {
'@type': 'SearchAction',
target: "#{Discourse.base_url}/search?q={search_term_string}",
'query-input': 'required name=search_term_string',
}
}
content_tag(:script, MultiJson.dump(json).html_safe, type: 'application/ld+json'.freeze)
end
def application_logo_url def application_logo_url
@application_logo_url ||= (mobile_view? && SiteSetting.mobile_logo_url) || SiteSetting.logo_url @application_logo_url ||= (mobile_view? && SiteSetting.mobile_logo_url) || SiteSetting.logo_url
end end

View File

@ -12,3 +12,5 @@
<meta name="theme-color" content="#<%= ColorScheme.hex_for_name('header_background') %>"> <meta name="theme-color" content="#<%= ColorScheme.hex_for_name('header_background') %>">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes"> <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes">
<%= canonical_link_tag %> <%= canonical_link_tag %>
<%= render_sitelinks_search_tag %>
<link rel="search" type="application/opensearchdescription+xml" href="<%= Discourse.base_url %>/opensearch.xml">

View File

@ -0,0 +1,12 @@
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName><%= SiteSetting.title %> Search</ShortName>
<Description>Search for posts on <%= SiteSetting.title %></Description>
<Tags>discourse forum</Tags>
<% if SiteSetting.favicon_url =~ /\.ico$/ -%>
<Image height="16" width="16" type="image/vnd.microsoft.icon"><%= UrlHelper.absolute SiteSetting.favicon_url %></Image>
<%- else -%>
<Image type="image/png"><%= UrlHelper.absolute SiteSetting.favicon_url %></Image>
<%- end %>
<Url type="text/html" method="get" template="<%= Discourse.base_url %>/search?q={searchTerms}"/>
<Query role="example" searchTerms="search term"/>
</OpenSearchDescription>

View File

@ -603,7 +603,8 @@ Discourse::Application.routes.draw do
get "favicon/proxied" => "static#favicon", format: false get "favicon/proxied" => "static#favicon", format: false
get "robots.txt" => "robots_txt#index" get "robots.txt" => "robots_txt#index"
get "manifest.json" => "manifest_json#index", as: :manifest get "manifest.json" => "metadata#manifest", as: :manifest
get "opensearch" => "metadata#opensearch", format: :xml
Discourse.filters.each do |filter| Discourse.filters.each do |filter|
root to: "list##{filter}", constraints: HomePageConstraint.new("#{filter}"), :as => "list_#{filter}" root to: "list##{filter}", constraints: HomePageConstraint.new("#{filter}"), :as => "list_#{filter}"

View File

@ -1,12 +0,0 @@
require 'rails_helper'
RSpec.describe ManifestJsonController do
context 'index' do
it 'returns the right output' do
title = 'MyApp'
SiteSetting.title = title
get :index
expect(response.body).to include(title)
end
end
end

View File

@ -0,0 +1,28 @@
require 'rails_helper'
RSpec.describe MetadataController do
describe 'manifest.json' do
it 'returns the right output' do
title = 'MyApp'
SiteSetting.title = title
get :manifest
expect(response.body).to include(title)
expect(response.content_type).to eq('application/json')
end
end
describe 'opensearch.xml' do
it 'returns the right output' do
title = 'MyApp'
favicon_path = '/uploads/something/23432.png'
SiteSetting.title = title
SiteSetting.favicon_url = favicon_path
get :opensearch, format: :xml
expect(response.body).to include(title)
expect(response.body).to include("/search?q={searchTerms}")
expect(response.body).to include('image/png')
expect(response.body).to include(favicon_path)
expect(response.content_type).to eq('application/xml')
end
end
end

View File

@ -78,6 +78,13 @@ Spork.prefork do
SiteSetting.defaults[k] = v SiteSetting.defaults[k] = v
end end
# Monkey patch for NoMethodError: undefined method `cache' for nil:NilClass
# https://github.com/rspec/rspec-rails/issues/1532#issuecomment-174679485
# fixed in Rspec 3.4.1
RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator.class_eval do
alias_method :find_all_anywhere, :find_all
end
require_dependency 'site_settings/local_process_provider' require_dependency 'site_settings/local_process_provider'
SiteSetting.provider = SiteSettings::LocalProcessProvider.new SiteSetting.provider = SiteSettings::LocalProcessProvider.new
end end