diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7d3341fc560..c867145f20d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -434,6 +434,10 @@ class ApplicationController < ActionController::Base data.merge! DiscoursePluginRegistry.custom_html end + DiscoursePluginRegistry.html_builders.each do |name, blk| + data[name] = blk.call + end + MultiJson.dump(data) end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e534aa50da7..0b37380f2cf 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -316,6 +316,11 @@ module ApplicationHelper end end + def build_plugin_html(name) + return "" unless allow_plugins? + DiscoursePluginRegistry.build_html(name) || "" + end + def theme_lookup(name) lookup = Theme.lookup_field(theme_key, mobile_view? ? :mobile : :desktop, name) lookup.html_safe if lookup diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index 1eee3fca072..99aaaf88a93 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -13,7 +13,6 @@ class DiscoursePluginRegistry attr_writer :handlebars attr_writer :serialized_current_user_fields attr_writer :seed_data - attr_accessor :custom_html def plugins @@ -60,6 +59,10 @@ class DiscoursePluginRegistry def seed_data @seed_data ||= HashWithIndifferentAccess.new({}) end + + def html_builders + @html_builders ||= {} + end end def register_js(filename, options={}) @@ -127,6 +130,14 @@ class DiscoursePluginRegistry self.seed_data[key] = value end + def self.register_html_builder(name, &block) + html_builders[name] = block + end + + def self.build_html(name) + html_builders[name]&.call + end + def javascripts self.class.javascripts end @@ -169,6 +180,7 @@ class DiscoursePluginRegistry sass_variables.clear serialized_current_user_fields asset_globs.clear + html_builders.clear end def self.setup(plugin_class) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index e6b8c047565..9fbeb8ef830 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -238,6 +238,10 @@ class Plugin::Instance DiscoursePluginRegistry.custom_html.merge!(hash) end + def register_html_builder(name, &block) + DiscoursePluginRegistry.register_html_builder(name, &block) + end + def register_asset(file, opts=nil) full_path = File.dirname(path) << "/assets/" << file assets << [full_path, opts] diff --git a/spec/components/discourse_plugin_registry_spec.rb b/spec/components/discourse_plugin_registry_spec.rb index 26cb894d465..b2809390127 100644 --- a/spec/components/discourse_plugin_registry_spec.rb +++ b/spec/components/discourse_plugin_registry_spec.rb @@ -44,6 +44,15 @@ describe DiscoursePluginRegistry do end end + context '.register_html_builder' do + it "can register and build html" do + DiscoursePluginRegistry.register_html_builder(:my_html) { "my html" } + expect(DiscoursePluginRegistry.build_html(:my_html)).to eq('my html') + DiscoursePluginRegistry.reset! + expect(DiscoursePluginRegistry.build_html(:my_html)).to be_blank + end + end + context '.register_css' do before do registry_instance.register_css('hello.css')