How to add plugins in ckeditor on Rails

Adding external plugins to ckeditor on rails

Posted by @krazedkrish on November 20, 2016

I needed to add codesnippet plugin for source code syntax highlighting in ckeditor. This was to edit blog articles, and it was built in rails.

Ok so, I google searched the solution for installing the plugin and even tried reading the source code and understanding how it worked. Luckly, I found the solution in one of the issues pointed by google. I have summarized the solution for you.

Solution:

  1. Create folder app/assets/javascripts/ckeditor/plugins
    mkdir -p app/assets/javascripts/ckeditor/plugins
    
  2. Copy the plugins you want to add, in app/assets/javascripts/ckeditor/plugins
  3. Add the plugin assets in to the asset compilation tree, by adding the following in config/initializers/ckeditor.rb
    #handle custom addons
    assets_root =  Rails.root.join('app','assets','javascripts')
    ckeditor_plugins_root = assets_root.join('ckeditor','plugins')
    %w(openlink sourcedialog).each do |ckeditor_plugin|
      Ckeditor.assets += Dir[ckeditor_plugins_root.join(ckeditor_plugin, '**', '*.js')].map do |x|
        x.sub(assets_root.to_path, '').sub(/^\/+/, '')
      end
    end
    
  4. [Optional] Depending on the plugin you are installing you might need to modify the configs for ckeditor. There is a default config file is added by the ckeditor gem. You can refer to this page for more info.

For reference, you can study the following link: https://github.com/galetahub/ckeditor/issues/450