A platform for community discussion. Free, open, simple.
FEATURE: adds support for loading existing core asset in pretty text
Joffrey JAFFEUX45f657336ecf
diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rbindex e46cf891fd8..4ec176b8d09 100644--- a/lib/discourse_plugin_registry.rb+++ b/lib/discourse_plugin_registry.rb@@ -82,6 +82,9 @@ class DiscoursePluginRegistry@vendored_pretty_text ||= Set.newend+ def vendored_core_pretty_text+ @vendored_core_pretty_text ||= Set.new+ endenddef register_js(filename, options = {})@@ -136,6 +139,8 @@ class DiscoursePluginRegistryself.admin_javascripts << assetelsif opts == :vendored_pretty_textself.vendored_pretty_text << asset+ elsif opts == :vendored_core_pretty_text+ self.vendored_core_pretty_text << assetelseself.javascripts << assetend@@ -180,6 +185,15 @@ class DiscoursePluginRegistryresult.uniqend+ VENDORED_CORE_PRETTY_TEXT_MAP = {+ "moment.js" => "lib/javascripts/moment.js"+ }+ def self.core_asset_for_name(name)+ asset = VENDORED_CORE_PRETTY_TEXT_MAP[name]+ raise KeyError, "Asset #{name} not found in #{VENDORED_CORE_PRETTY_TEXT_MAP}" unless asset+ asset+ end+def localesself.class.localesend@@ -235,6 +249,7 @@ class DiscoursePluginRegistryasset_globs.clearhtml_builders.clearvendored_pretty_text.clear+ vendored_core_pretty_text.clearseed_path_builders.clearlocales.clearenddiff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex 91953a10747..c2d41e2c3fa 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -338,7 +338,12 @@ class Plugin::Instanceenddef register_asset(file, opts = nil)- full_path = File.dirname(path) << "/assets/" << file+ if opts && opts == :vendored_core_pretty_text+ full_path = DiscoursePluginRegistry.core_asset_for_name(file)+ else+ full_path = File.dirname(path) << "/assets/" << file+ end+assets << [full_path, opts]end@@ -506,6 +511,7 @@ JSdef javascript_includesassets.map do |asset, opts|+ next if opts == :vendored_core_pretty_textnext if opts == :adminnext unless asset =~ DiscoursePluginRegistry::JS_REGEXassetdiff --git a/lib/pretty_text.rb b/lib/pretty_text.rbindex 4e2fc4e81e3..082835eb919 100644--- a/lib/pretty_text.rb+++ b/lib/pretty_text.rb@@ -101,6 +101,10 @@ module PrettyTextendend+ DiscoursePluginRegistry.vendored_core_pretty_text.each do |vpt|+ ctx.eval(File.read(vpt))+ end+DiscoursePluginRegistry.vendored_pretty_text.each do |vpt|ctx.eval(File.read(vpt))enddiff --git a/spec/components/discourse_plugin_registry_spec.rb b/spec/components/discourse_plugin_registry_spec.rbindex b6b00018e65..82ecbbe680b 100644--- a/spec/components/discourse_plugin_registry_spec.rb+++ b/spec/components/discourse_plugin_registry_spec.rb@@ -168,6 +168,13 @@ describe DiscoursePluginRegistry doexpect(registry.admin_javascripts.count).to eq(1)expect(registry.javascripts.count).to eq(0)end++ it "registers vendored_core_pretty_text properly" do+ registry.register_asset("my_lib.js", :vendored_core_pretty_text)++ expect(registry.vendored_core_pretty_text.count).to eq(1)+ expect(registry.javascripts.count).to eq(0)+ endendcontext '#register_seed_data' dodiff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rbindex 82e9cb3a2bd..de6641218ac 100644--- a/spec/components/plugin/instance_spec.rb+++ b/spec/components/plugin/instance_spec.rb@@ -93,6 +93,15 @@ describe Plugin::Instance doexpect(DiscoursePluginRegistry.mobile_stylesheets.count).to eq(0)expect(DiscoursePluginRegistry.stylesheets.count).to eq(2)end++ it "remaps vendored_core_pretty_text asset" do+ plugin = Plugin::Instance.new nil, "/tmp/test.rb"+ plugin.register_asset("moment.js", :vendored_core_pretty_text)++ plugin.send :register_assets!++ expect(DiscoursePluginRegistry.vendored_core_pretty_text.first).to eq("lib/javascripts/moment.js")+ endendcontext "register service worker" do