diff --git a/lib/govuk_tech_docs.rb b/lib/govuk_tech_docs.rb index cd19219f..fd454d52 100644 --- a/lib/govuk_tech_docs.rb +++ b/lib/govuk_tech_docs.rb @@ -22,6 +22,7 @@ require "govuk_tech_docs/unique_identifier_generator" require "govuk_tech_docs/warning_text_extension" require "govuk_tech_docs/api_reference/api_reference_extension" +require "govuk_tech_docs/http_prefix_extension" module GovukTechDocs # Configure the tech docs template @@ -65,6 +66,7 @@ def self.configure(context, options = {}) context.activate :unique_identifier context.activate :warning_text context.activate :api_reference + context.activate :sass_http_prefix context.helpers do include GovukTechDocs::TableOfContents::Helpers diff --git a/lib/govuk_tech_docs/http_prefix_extension.rb b/lib/govuk_tech_docs/http_prefix_extension.rb new file mode 100644 index 00000000..82dc427f --- /dev/null +++ b/lib/govuk_tech_docs/http_prefix_extension.rb @@ -0,0 +1,21 @@ +module GovukTechDocs + class SassHttpPrefixExtension < Middleman::Extension + def after_configuration + app.extensions[:sprockets].environment.context_class.class_eval do + # Override the `asset-path()` helper available in Sprockets to + # return a directory rather than a file if the path ends with `/` + alias_method :orig_asset_path, :asset_path + + def asset_path path, options = {} + if options.empty? && path.end_with?("/") + File.join(*[app.config[:http_prefix], path].compact) + else + orig_asset_path path, options + end + end + end + end + end +end + +::Middleman::Extensions.register(:sass_http_prefix, GovukTechDocs::SassHttpPrefixExtension)