mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 06:29:30 +08:00
FEATURE: Add endpoint for individual SVG icons
- includes an optional color parameter
This commit is contained in:
parent
ff331e845a
commit
e618efe966
|
@ -1,9 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class SvgSpriteController < ApplicationController
|
||||
skip_before_action :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :search]
|
||||
skip_before_action :preload_json, :redirect_to_login_if_required, :check_xhr, :verify_authenticity_token, only: [:show, :search, :svg_icon]
|
||||
|
||||
requires_login except: [:show]
|
||||
requires_login except: [:show, :svg_icon]
|
||||
|
||||
def show
|
||||
|
||||
|
@ -49,4 +49,25 @@ class SvgSpriteController < ApplicationController
|
|||
render json: icons.take(200), root: false
|
||||
end
|
||||
end
|
||||
|
||||
def svg_icon
|
||||
no_cookies
|
||||
|
||||
RailsMultisite::ConnectionManagement.with_hostname(params[:hostname]) do
|
||||
params.permit(:color)
|
||||
name = params.require(:name)
|
||||
icon = SvgSprite.search(name)
|
||||
|
||||
if icon.blank?
|
||||
render body: nil, status: 404
|
||||
else
|
||||
doc = Nokogiri.XML(icon)
|
||||
doc.at_xpath("symbol").name = "svg"
|
||||
doc.at_xpath("svg")['xmlns'] = "http://www.w3.org/2000/svg"
|
||||
doc.at_xpath("svg")['fill'] = "##{params[:color]}" if params[:color]
|
||||
|
||||
render plain: doc, disposition: nil, content_type: 'image/svg+xml'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -503,6 +503,7 @@ Discourse::Application.routes.draw do
|
|||
get "svg-sprite/:hostname/svg-:theme_ids-:version.js" => "svg_sprite#show", constraints: { hostname: /[\w\.-]+/, version: /\h{40}/, theme_ids: /([0-9]+(,[0-9]+)*)?/, format: :js }
|
||||
get "svg-sprite/search/:keyword" => "svg_sprite#search", format: false, constraints: { keyword: /[-a-z0-9\s\%]+/ }
|
||||
get "svg-sprite/picker-search" => "svg_sprite#icon_picker_search", defaults: { format: :json }
|
||||
get "svg-sprite/icon(/:color)/:name.svg" => "svg_sprite#svg_icon", constraints: { name: /[-a-z0-9\s\%]+/, format: :svg }
|
||||
|
||||
get "highlight-js/:hostname/:version.js" => "highlight_js#show", constraints: { hostname: /[\w\.-]+/, format: :js }
|
||||
|
||||
|
|
|
@ -93,4 +93,24 @@ describe SvgSpriteController do
|
|||
expect(data[0]["id"]).to eq("fab-500px")
|
||||
end
|
||||
end
|
||||
|
||||
context 'svg_icon' do
|
||||
it "should return 404 when not appending .svg" do
|
||||
get "/svg-sprite/icon/bolt"
|
||||
expect(response.status).to eq(404)
|
||||
end
|
||||
|
||||
it "should return SVG given an icon name" do
|
||||
get "/svg-sprite/icon/bolt.svg"
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to include('bolt')
|
||||
end
|
||||
|
||||
it "should return SVG given an icon name and a color" do
|
||||
get "/svg-sprite/icon/CC0000/fab-github.svg"
|
||||
expect(response.status).to eq(200)
|
||||
expect(response.body).to include('fab-github')
|
||||
expect(response.body).to include('fill="#CC0000"')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user