@@ -9,17 +9,8 @@ module URLFilters
9
9
#
10
10
# Returns the absolute URL as a String.
11
11
def absolute_url ( input )
12
- return if input . nil?
13
-
14
- input = input . url if input . respond_to? ( :url )
15
- return input if Addressable ::URI . parse ( input . to_s ) . absolute?
16
-
17
- site = @context . registers [ :site ]
18
- return relative_url ( input ) if site . config [ "url" ] . nil?
19
-
20
- Addressable ::URI . parse (
21
- site . config [ "url" ] . to_s + relative_url ( input )
22
- ) . normalize . to_s
12
+ cache = ( @context . registers [ :cached_absolute_urls ] ||= { } )
13
+ cache [ input ] ||= compute_absolute_url ( input )
23
14
end
24
15
25
16
# Produces a URL relative to the domain root based on site.baseurl
@@ -29,15 +20,8 @@ def absolute_url(input)
29
20
#
30
21
# Returns a URL relative to the domain root as a String.
31
22
def relative_url ( input )
32
- return if input . nil?
33
-
34
- input = input . url if input . respond_to? ( :url )
35
- return input if Addressable ::URI . parse ( input . to_s ) . absolute?
36
-
37
- parts = [ sanitized_baseurl , input ]
38
- Addressable ::URI . parse (
39
- parts . compact . map { |part | ensure_leading_slash ( part . to_s ) } . join
40
- ) . normalize . to_s
23
+ cache = ( @context . registers [ :cached_relative_urls ] ||= { } )
24
+ cache [ input ] ||= compute_relative_url ( input )
41
25
end
42
26
43
27
# Strips trailing `/index.html` from URLs to create pretty permalinks
@@ -53,6 +37,32 @@ def strip_index(input)
53
37
54
38
private
55
39
40
+ def compute_absolute_url ( input )
41
+ return if input . nil?
42
+
43
+ input = input . url if input . respond_to? ( :url )
44
+ return input if Addressable ::URI . parse ( input . to_s ) . absolute?
45
+
46
+ site = @context . registers [ :site ]
47
+ return relative_url ( input ) if site . config [ "url" ] . nil?
48
+
49
+ Addressable ::URI . parse (
50
+ site . config [ "url" ] . to_s + relative_url ( input )
51
+ ) . normalize . to_s
52
+ end
53
+
54
+ def compute_relative_url ( input )
55
+ return if input . nil?
56
+
57
+ input = input . url if input . respond_to? ( :url )
58
+ return input if Addressable ::URI . parse ( input . to_s ) . absolute?
59
+
60
+ parts = [ sanitized_baseurl , input ]
61
+ Addressable ::URI . parse (
62
+ parts . compact . map { |part | ensure_leading_slash ( part . to_s ) } . join
63
+ ) . normalize . to_s
64
+ end
65
+
56
66
def sanitized_baseurl
57
67
site = @context . registers [ :site ]
58
68
site . config [ "baseurl" ] . to_s . chomp ( "/" )
0 commit comments