@@ -5,17 +5,11 @@ const encodeURL = require('./encode_url');
55const relative_url = require ( './relative_url' ) ;
66const prettyUrls = require ( './pretty_urls' ) ;
77
8- function urlForHelper ( path = '/' , options ) {
9- const pathRegex = / ^ ( # | \/ \/ | h t t p ( s ) ? : ) / ;
10- if ( pathRegex . test ( path ) ) return path ;
8+ const Cache = require ( './cache' ) ;
9+ const cache = new Cache ( ) ;
1110
11+ function urlForHelper ( path = '/' , options ) {
1212 const { config } = this ;
13- const { root } = config ;
14- const sitehost = parse ( config . url ) . hostname || config . url ;
15- const data = new URL ( path , `http://${ sitehost } ` ) ;
16-
17- // Exit if input is an external link or a data url
18- if ( data . hostname !== sitehost || data . origin === 'null' ) return path ;
1913
2014 options = Object . assign ( {
2115 relative : config . relative_link
@@ -26,17 +20,34 @@ function urlForHelper(path = '/', options) {
2620 return relative_url ( this . path , path ) ;
2721 }
2822
29- // Prepend root path
30- path = encodeURL ( ( root + path ) . replace ( / \/ { 2 , } / g, '/' ) ) ;
31-
23+ const { root } = config ;
3224 const prettyUrlsOptions = Object . assign ( {
3325 trailing_index : true ,
3426 trailing_html : true
3527 } , config . pretty_urls ) ;
3628
37- path = prettyUrls ( path , prettyUrlsOptions ) ;
29+ // cacheId is designed to works across different hexo.config & options
30+ return cache . apply ( `${ config . url } -${ root } -${ prettyUrlsOptions . trailing_index } -${ prettyUrlsOptions . trailing_html } -${ path } ` , ( ) => {
31+ const pathRegex = / ^ ( # | \/ \/ | h t t p ( s ) ? : ) / ;
32+ if ( pathRegex . test ( path ) ) {
33+ return path ;
34+ }
35+
36+ const sitehost = parse ( config . url ) . hostname || config . url ;
37+ const data = new URL ( path , `http://${ sitehost } ` ) ;
38+
39+ // Exit if input is an external link or a data url
40+ if ( data . hostname !== sitehost || data . origin === 'null' ) {
41+ return path ;
42+ }
43+
44+ // Prepend root path
45+ path = encodeURL ( ( root + path ) . replace ( / \/ { 2 , } / g, '/' ) ) ;
46+
47+ path = prettyUrls ( path , prettyUrlsOptions ) ;
3848
39- return path ;
49+ return path ;
50+ } ) ;
4051}
4152
4253module . exports = urlForHelper ;
0 commit comments