1- // @ts -nocheck
21// require the whatwg compatible URL library (same behaviour in node and browser)
3- const { URL } = require ( 'url' ) ;
4- const urlJoin = require ( './url-join' ) ;
2+ import { URL } from 'url' ;
3+ import urlJoin from './url-join' ;
54
65// NOTE: Ghost's relative->absolute handling is a little strange when the rootUrl
76// includes a subdirectory. Root-relative paths such as /content/image.jpg are
@@ -12,6 +11,12 @@ const urlJoin = require('./url-join');
1211// this when all root-relative paths are treated as subdir-relative we have to
1312// rely on subdirectory deduplication.
1413
14+ interface RelativeToAbsoluteOptions {
15+ assetsOnly ?: boolean ;
16+ staticImageUrlPrefix ?: string ;
17+ secure ?: boolean ;
18+ }
19+
1520/**
1621 * Convert a root-relative path to an absolute URL based on the supplied root.
1722 * Will _only_ convert root-relative urls (/some/path not some/path)
@@ -22,24 +27,36 @@ const urlJoin = require('./url-join');
2227 * @param {object } options
2328 * @returns {string } The passed in url or an absolute URL using
2429 */
25- const relativeToAbsolute = function relativeToAbsolute ( path , rootUrl , itemPath , _options ) {
30+ const relativeToAbsolute = function relativeToAbsolute (
31+ path : string ,
32+ rootUrl : string ,
33+ itemPath ?: string | RelativeToAbsoluteOptions | null ,
34+ _options ?: RelativeToAbsoluteOptions
35+ ) : string {
2636 // itemPath is optional, if it's an object it may be the options param instead
27- if ( typeof itemPath === 'object' && ! _options ) {
28- _options = itemPath ;
29- itemPath = null ;
37+ let actualItemPath : string | null = null ;
38+ let actualOptions : RelativeToAbsoluteOptions ;
39+
40+ if ( itemPath && typeof itemPath === 'object' && ! _options ) {
41+ actualOptions = itemPath ;
42+ actualItemPath = null ;
43+ } else {
44+ actualOptions = _options || { } ;
45+ actualItemPath = typeof itemPath === 'string' ? itemPath : null ;
3046 }
3147
3248 // itemPath could be sent as a full url in which case, extract the pathname
33- if ( itemPath && itemPath . match ( / ^ h t t p / ) ) {
34- const itemUrl = new URL ( itemPath ) ;
35- itemPath = itemUrl . pathname ;
49+ if ( actualItemPath && actualItemPath . match ( / ^ h t t p / ) ) {
50+ const itemUrl = new URL ( actualItemPath ) ;
51+ actualItemPath = itemUrl . pathname ;
3652 }
3753
38- const defaultOptions = {
54+ const defaultOptions : Required < RelativeToAbsoluteOptions > = {
3955 assetsOnly : false ,
40- staticImageUrlPrefix : 'content/images'
56+ staticImageUrlPrefix : 'content/images' ,
57+ secure : false
4158 } ;
42- const options = Object . assign ( { } , defaultOptions , _options ) ;
59+ const options = Object . assign ( { } , defaultOptions , actualOptions ) ;
4360
4461 // return the path as-is if it's not an asset path and we're only modifying assets
4562 if ( options . assetsOnly ) {
@@ -81,7 +98,7 @@ const relativeToAbsolute = function relativeToAbsolute(path, rootUrl, itemPath,
8198 }
8299
83100 const parsedRootUrl = new URL ( rootUrl ) ;
84- const basePath = path . startsWith ( '/' ) ? '' : itemPath ;
101+ const basePath = path . startsWith ( '/' ) ? '' : ( actualItemPath || '' ) ;
85102 const fullPath = urlJoin ( [ parsedRootUrl . pathname , basePath , path ] , { rootUrl} ) ;
86103 const absoluteUrl = new URL ( fullPath , rootUrl ) ;
87104
@@ -92,4 +109,5 @@ const relativeToAbsolute = function relativeToAbsolute(path, rootUrl, itemPath,
92109 return absoluteUrl . toString ( ) ;
93110} ;
94111
112+ export default relativeToAbsolute ;
95113module . exports = relativeToAbsolute ;
0 commit comments