diff --git a/cli/options.js b/cli/options.js index f6377e0..292956b 100644 --- a/cli/options.js +++ b/cli/options.js @@ -20,6 +20,7 @@ function options(args) { ], string: [ // options 'encoding', + 'baseurl', ], alias: { images: 'noimages', // legacy support @@ -34,6 +35,7 @@ function options(args) { o: 'videos', m: 'inlinemin', H: 'header', + b: 'baseurl', }, }); diff --git a/docs/usage.txt b/docs/usage.txt index b4f74f5..16f9d88 100644 --- a/docs/usage.txt +++ b/docs/usage.txt @@ -20,8 +20,9 @@ Options: - -H, --header LINE custom header pass to the server (ala cURL headers) - -e, --encoding ENC override encoding detection + -H, --header LINE custom header pass to the server (ala cURL headers) + -e, --encoding ENC override encoding detection + -b, --baseurl BASEURL use baseurl to fetch assets with relative paths Examples: diff --git a/lib/index.js b/lib/index.js index ea62ffb..7a71fff 100644 --- a/lib/index.js +++ b/lib/index.js @@ -58,6 +58,10 @@ function Inliner(source, options, callback) { this.url = options.url; } + if (options.baseurl) { + this.baseurl = options.baseurl; + } + if (options.filename) { this.filename = options.filename; } @@ -375,13 +379,18 @@ function resolve(from, to) { from = this.url; } + // add base url to relative urls + if (this.baseurl && from.indexOf('http') === -1) { + from = this.baseurl; + } + // don't resolve data urls (already inlined) if (to.indexOf('data:') === 0) { return to; } // always strip querystrings from requests off a local file - if (this.isFile) { + if (this.isFile && !this.baseurl) { to = to.replace(/\??#.*$/, ''); } diff --git a/test/fixtures/baseurl.opts.json b/test/fixtures/baseurl.opts.json new file mode 100644 index 0000000..e90ee51 --- /dev/null +++ b/test/fixtures/baseurl.opts.json @@ -0,0 +1,3 @@ +{ + "baseurl": "https://avatars1.githubusercontent.com" +} \ No newline at end of file diff --git a/test/fixtures/baseurl.result.html b/test/fixtures/baseurl.result.html new file mode 100644 index 0000000..5ad27ba --- /dev/null +++ b/test/fixtures/baseurl.result.html @@ -0,0 +1 @@ +