Skip to content

Commit 11fe704

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 `/`.
1 parent fb030ae commit 11fe704

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/govuk_tech_docs.rb

Lines changed: 2 additions & 0 deletions
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
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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? and 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+
18+
end
19+
end
20+
end
21+
22+
::Middleman::Extensions.register(:sass_http_prefix, GovukTechDocs::SassHttpPrefixExtension)

0 commit comments

Comments
 (0)