From 47cbfb14989bcf828c66b211827cb9355214e71a Mon Sep 17 00:00:00 2001 From: Penar Musaraj <pmusaraj@gmail.com> Date: Sat, 12 Jan 2019 09:59:12 -0500 Subject: [PATCH] FEATURE: plugin support for custom icons Plugins can now add their own SVG sprites in "plugin-name/svg-icons/". Example: save the following as "plugin-name/svg-icons/plugin-icons.svg" ``` <?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="testplugin-compress" viewBox="0 0 1792 1792"> <path d="M896 960v448q0 26-19 45t-45 19-45-19l-144-144-332 332q-10 10-23 10t-23-10l-114-114q-10-10-10-23t10-23l332-332-144-144q-19-19-19-45t19-45 45-19h448q26 0 45 19t19 45zm755-672q0 13-10 23l-332 332 144 144q19 19 19 45t-19 45-45 19h-448q-26 0-45-19t-19-45v-448q0-26 19-45t45-19 45 19l144 144 332-332q10-10 23-10t23 10l114 114q10 10 10 23z"></path> </symbol> </svg> ``` and then register the icon by adding this in plugin.rb ``` register_svg_icon "testplugin-compress" ``` --- lib/svg_sprite/svg_sprite.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/svg_sprite/svg_sprite.rb b/lib/svg_sprite/svg_sprite.rb index 8608a09ed6b..f4a3335c8d6 100644 --- a/lib/svg_sprite/svg_sprite.rb +++ b/lib/svg_sprite/svg_sprite.rb @@ -185,6 +185,9 @@ module SvgSprite FA_ICON_MAP = { 'far fa-' => 'far-', 'fab fa-' => 'fab-', 'fas fa-' => '', 'fa-' => '' } + SVG_SPRITE_PATHS = Dir.glob(["#{Rails.root}/vendor/assets/svg-icons/**/*.svg", + "#{Rails.root}/plugins/*/svg-icons/*.svg"]) + def self.svg_sprite_cache @svg_sprite_cache ||= DistributedCache.new('svg_sprite') end @@ -229,7 +232,7 @@ Discourse SVG subset of #{fa_license} <svg xmlns='http://www.w3.org/2000/svg' style='display: none;'> """.dup - Dir["#{Rails.root}/vendor/assets/svg-icons/**/*.svg"].each do |fname| + SVG_SPRITE_PATHS.each do |fname| svg_file = Nokogiri::XML(File.open(fname)) do |config| config.options = Nokogiri::XML::ParseOptions::NOBLANKS end @@ -253,7 +256,7 @@ Discourse SVG subset of #{fa_license} def self.search(searched_icon) searched_icon = process(searched_icon.dup) - Dir["#{Rails.root}/vendor/assets/svg-icons/**/*.svg"].each do |fname| + SVG_SPRITE_PATHS.each do |fname| svg_file = Nokogiri::XML(File.open(fname)) svg_filename = "#{File.basename(fname, ".svg")}"