forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for URLs relative to context root
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 17462 18164 +4.02% BenchmarkAbsURLSrcset 18842 19632 +4.19% BenchmarkXMLAbsURLSrcset 18643 19313 +3.59% BenchmarkXMLAbsURL 9283 9656 +4.02% benchmark old allocs new allocs delta BenchmarkAbsURL 24 28 +16.67% BenchmarkAbsURLSrcset 29 32 +10.34% BenchmarkXMLAbsURLSrcset 27 30 +11.11% BenchmarkXMLAbsURL 12 14 +16.67% benchmark old bytes new bytes delta BenchmarkAbsURL 3154 3404 +7.93% BenchmarkAbsURLSrcset 2376 2573 +8.29% BenchmarkXMLAbsURLSrcset 2569 2763 +7.55% BenchmarkXMLAbsURL 1888 1998 +5.83% ``` Fixes gohugoio#1104 Fixes gohugoio#622 Fixes gohugoio#937 Fixes #157
- Loading branch information
Showing
10 changed files
with
181 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,11 @@ | ||
package transform | ||
|
||
import ( | ||
"github.com/spf13/viper" | ||
"sync" | ||
) | ||
var ar *absURLReplacer = newAbsURLReplacer() | ||
|
||
// to be used in tests; the live site will get its value from Viper. | ||
var AbsBaseUrl string | ||
|
||
var absURLInit sync.Once | ||
var ar *absURLReplacer | ||
|
||
func AbsURL() (trs []link, err error) { | ||
initAbsURLReplacer() | ||
return absURLFromReplacer(ar) | ||
} | ||
|
||
func absURLFromURL(URL string) (trs []link, err error) { | ||
return absURLFromReplacer(newAbsURLReplacer(URL)) | ||
} | ||
|
||
func absURLFromReplacer(ar *absURLReplacer) (trs []link, err error) { | ||
trs = append(trs, func(ct contentTransformer) { | ||
ar.replaceInHTML(ct) | ||
}) | ||
return | ||
} | ||
|
||
func AbsURLInXML() (trs []link, err error) { | ||
initAbsURLReplacer() | ||
return absURLInXMLFromReplacer(ar) | ||
} | ||
|
||
func absURLInXMLFromURL(URL string) (trs []link, err error) { | ||
return absURLInXMLFromReplacer(newAbsURLReplacer(URL)) | ||
var AbsURL = func(ct contentTransformer) { | ||
ar.replaceInHTML(ct) | ||
} | ||
|
||
func absURLInXMLFromReplacer(ar *absURLReplacer) (trs []link, err error) { | ||
trs = append(trs, func(ct contentTransformer) { | ||
ar.replaceInXML(ct) | ||
}) | ||
return | ||
} | ||
|
||
func initAbsURLReplacer() { | ||
absURLInit.Do(func() { | ||
var url string | ||
|
||
if AbsBaseUrl != "" { | ||
url = AbsBaseUrl | ||
} else { | ||
url = viper.GetString("BaseURL") | ||
} | ||
|
||
ar = newAbsURLReplacer(url) | ||
}) | ||
var AbsURLInXML = func(ct contentTransformer) { | ||
ar.replaceInXML(ct) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.