Skip to content

Commit 8075734

Browse files
committed
Add extension to middleman-sprockets to get http_prefix in Sass
We want to be able to prefix directory paths with the http_prefix in Sass, however normally the `asset-path` helper tries to add a file extension, so this commit adds a small extension that skips the usual logic if the path ends with `/`. The extension works by reaching into the `Sprockets::Envionment` instance in the `middleman-sprockets` extension [1], and replacing the definition of the method `asset_path` [2]. Sprockets turns any calls to `asset-path` in any CSS/Sass it processes into a call to this method, so this is our way to smuggle information into our Sass. [1]: https://github.com/middleman/middleman-sprockets/blob/a32cbe3c8ca129b608bdc852c7ff7ad489f9a087/lib/middleman-sprockets/extension.rb#L28 [2]: https://github.com/middleman/middleman-sprockets/blob/a32cbe3c8ca129b608bdc852c7ff7ad489f9a087/lib/middleman-sprockets/extension.rb#L58
1 parent e539c73 commit 8075734

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/govuk_tech_docs.rb

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require "govuk_tech_docs/unique_identifier_generator"
2323
require "govuk_tech_docs/warning_text_extension"
2424
require "govuk_tech_docs/api_reference/api_reference_extension"
25+
require "govuk_tech_docs/http_prefix_extension"
2526

2627
module GovukTechDocs
2728
# Configure the tech docs template
@@ -65,6 +66,7 @@ def self.configure(context, options = {})
6566
context.activate :unique_identifier
6667
context.activate :warning_text
6768
context.activate :api_reference
69+
context.activate :sass_http_prefix
6870

6971
context.helpers do
7072
include GovukTechDocs::TableOfContents::Helpers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module GovukTechDocs
2+
class SassHttpPrefixExtension < Middleman::Extension
3+
def after_configuration
4+
app.extensions[:sprockets].environment.context_class.class_eval do
5+
# Override the `asset-path()` helper available in Sprockets to
6+
# return a directory rather than a file if the path ends with `/`
7+
alias_method :orig_asset_path, :asset_path
8+
9+
def asset_path path, options = {}
10+
if options.empty? && path.end_with?("/")
11+
File.join(*[app.config[:http_prefix], path].compact)
12+
else
13+
orig_asset_path path, options
14+
end
15+
end
16+
end
17+
end
18+
end
19+
end
20+
21+
::Middleman::Extensions.register(:sass_http_prefix, GovukTechDocs::SassHttpPrefixExtension)

0 commit comments

Comments
 (0)