Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
DEV: allows plugins to enable markdown features (#878)
Browse files Browse the repository at this point in the history
Usage in plugin.rb:

```ruby
discourse_chat.enable_markdown_feature('discourse-video')
```
  • Loading branch information
jjaffeux committed May 23, 2022
1 parent b850591 commit 181fe28
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/core_ext/plugin_instance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

DiscoursePluginRegistry.define_register(:chat_markdown_features, Set)

class Plugin::Instance
def discourse_chat
DiscourseChatPluginApiExtensions
end

module DiscourseChatPluginApiExtensions
def self.enable_markdown_feature(name)
DiscoursePluginRegistry.chat_markdown_features << name
end
end
end
2 changes: 1 addition & 1 deletion app/models/chat_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def self.uncooked
def self.cook(message, opts = {})
cooked = PrettyText.cook(
message,
features_override: MARKDOWN_FEATURES,
features_override: MARKDOWN_FEATURES + DiscoursePluginRegistry.chat_markdown_features.to_a,
markdown_it_rules: MARKDOWN_IT_RULES,
force_quote_link: true
)
Expand Down
1 change: 1 addition & 0 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

# Site setting validators must be loaded before initialize
require_relative "lib/validators/chat_default_channel_validator.rb"
require_relative "app/core_ext/plugin_instance.rb"

after_initialize do
module ::DiscourseChat
Expand Down
29 changes: 29 additions & 0 deletions spec/integration/plugin_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'rails_helper'

describe 'Plugin API for discourse_chat' do
before do
SiteSetting.chat_enabled = true
end

let(:metadata) do
metadata = Plugin::Metadata.new
metadata.name = 'test'
metadata
end

let(:plugin_instance) do
plugin = Plugin::Instance.new(nil, "/tmp/test.rb")
plugin.metadata = metadata
plugin
end

context 'discourse_chat.enable_markdown_feature' do
it 'stores the markdown feature' do
plugin_instance.discourse_chat.enable_markdown_feature(:foo)

expect(DiscoursePluginRegistry.chat_markdown_features.include?(:foo)).to be_truthy
end
end
end

0 comments on commit 181fe28

Please sign in to comment.