Skip to content

Commit

Permalink
Use Ckeditor::Rails::AssetUrlProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
tsechingho committed Nov 16, 2021
1 parent f745a2e commit f8c4bcf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/ckeditor-rails/asset_url_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Ckeditor
module Rails
# Rewrites urls in CSS files with the digested paths
class AssetUrlProcessor
REGEX = /url\(\s*["']?(?!(?:\#|data|http))([^"'\s)]+)\s*["']?\)/

def self.stylesheet_files
@stylesheet_files ||= ::Ckeditor::Rails::Asset.new.stylesheet_files
end

def self.call(input)
return { data: input[:data] } unless stylesheet_files.include?(input[:filename])

context = input[:environment].context_class.new(input)
path_prefix = "#{context.assets_prefix}/ckeditor"
matched_folders = input[:filename].match(/\/ckeditor\/(plugins|skins)\/([\w-]+)\//)

data = input[:data].gsub(REGEX) { |_match|
raw_asset_path = context.asset_path($1)
if raw_asset_path.starts_with?(path_prefix)
"url(#{raw_asset_path})"
elsif matched_folders
"url(#{path_prefix}/#{matched_folders[1]}/#{matched_folders[2]}#{raw_asset_path.gsub('/..', '')})"
else
"url(#{path_prefix}#{raw_asset_path})"
end
}

{ data: data }
end
end
end
end
10 changes: 10 additions & 0 deletions lib/ckeditor-rails/engine.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
require 'ckeditor-rails/asset_url_processor'

module Ckeditor
module Rails
class Engine < ::Rails::Engine
initializer 'ckeditor.assets.precompile', group: :all do |app|
app.config.assets.precompile += Ckeditor::Rails::Asset.new.files
end

# Follow sprockets-rails 3.3.0+ to use postprocessor of Sprockets
# https://github.com/rails/sprockets-rails/blob/v3.3.0/lib/sprockets/railtie.rb#L121
initializer 'ckeditor.asset_url_processor' do |app|
if ::Sprockets.respond_to? :register_postprocessor
::Sprockets.register_postprocessor 'text/css', ::Ckeditor::Rails::AssetUrlProcessor
end
end

rake_tasks do
load "ckeditor-rails/tasks.rake"
end
Expand Down

0 comments on commit f8c4bcf

Please sign in to comment.