forked from pivotal-cf/bookbinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subdirectory_aware_assets.rb
36 lines (29 loc) · 1.14 KB
/
subdirectory_aware_assets.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class SubdirectoryAwareAssets < ::Middleman::Extension
helpers do
def asset_path(path, prefix="", options={})
url = super(path, prefix, options)
unless global_asset_at? url
current_dir = Pathname('/' + current_resource.destination_path)
url = Pathname(url).relative_path_from(current_dir.dirname).to_s
end
# middleman assumes your assets live at the top level, but they may be in the nested repos instead
# here we start at top level and dive down until we find the real asset
current_page_path_parts = current_resource.destination_path.split('/')
current_page_path_parts.pop
current_page_dir = File.join('source', current_page_path_parts, '')
while (!File.exists? "#{current_page_dir}#{url}") && url.match(/^\.\.\//) do
url = url.gsub(/^\.\.\//, "")
end
url
end
private
def global_asset_at?(path)
path.include?('//') ||
path.start_with?('data:') ||
!current_resource ||
path.index('.css') ||
path.index('.js')
end
end
end
::Middleman::Extensions.register(:subdirectory_aware_assets, SubdirectoryAwareAssets)