Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Add multi-source support #11

Merged
merged 1 commit into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ Then include `jekyll-imgix` in the `gems:` section of your `_config.yml` file:
gems: [jekyll/imgix]
```

## Configuration

jekyll-imgix requires a configuration block in your `_config.yml`:

```yaml
imgix:
source: assets.imgix.net # Your imgix source address
secure_url_token: FACEBEEF12 # (optional) The Secure URL Token associated with your source
include_library_param: true # (optional) If `true` all the URLs will include `ixlib` parameter
```

### Multi-source configuration

In addition to the standard configuration flags, the following options can be used to serve images across different sources.

```yaml
imgix:
sources: # imgix source-secure_url_token key-value pairs.
assets.imgix.net: FACEBEEF12
assets2.imgix.net: # Will generate unsigned URLs
default_source: assets.imgix.net # (optional) specify a default source for generating URLs.
```

Note: `sources` and `source` *cannot* be used together.

## Usage

**jekyll-imgix does not do anything unless JEKYLL_ENV is set to production**. For example,
Expand Down Expand Up @@ -53,16 +78,24 @@ Which would result in the following HTML:
<img src="https://assets.imgix.net/images/bear.jpg?w=400&h=300" />
```

### Configuration
### Multi-source usage

jekyll-imgix requires a configuration block in your `_config.yml`:
To use jekyll-imgix in a multi-source setup:

```yaml
imgix:
source: assets.imgix.net # Your imgix source address
secure_url_token: FACEBEEF12 # (optional) The Secure URL Token associated with your source
```html
<img src={{ "/images/bear.jpg" | imgix_url: "assets2.imgix.net", w: 400, h: 300 }} />
<img src={{ "/images/bear.jpg" | imgix_url: w: 400, h: 300 }} /> <!-- will use default_source from config -->
```

Which would generate:

```html
<img src="https://assets2.imgix.net/images/bear.jpg?w=400&h=300" />
<img src="https://assets.imgix.net/images/bear.jpg?w=400&h=300" />
```

In absence of correctly configured `default_source`, `imgix_url` will report `RuntimeError` if it's used without specifying a valid source.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/imgix/jekyll-imgix.
Expand All @@ -72,3 +105,5 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/imgix/

The gem is available as open source under the terms of the [BSD-2-Clause License](http://opensource.org/licenses/BSD-2-Clause).

## Code of Conduct
Users contributing to or participating in the development of this project are subject to the terms of imgix's [Code of Conduct](https://github.com/imgix/code-of-conduct).
100 changes: 82 additions & 18 deletions lib/jekyll/imgix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,88 @@

module Jekyll
module Imgix
def imgix_url(raw, opts={})
return raw unless production?
verify_config_present!
client.path(raw).to_url(opts)
def imgix_url(*args)
case args.size
when 1
path = args[0]
source = nil
options = {}
when 2
if args[0].is_a?(String) && args[1].is_a?(Hash)
path = args[0]
source = nil
options = args[1]
elsif args[0].is_a?(String) && args[1].is_a?(String)
path = args[0]
source = args[1]
options = {}
else
raise RuntimeError.new("path and source must be of type String; options must be of type Hash")
end
when 3
path = args[0]
source = args[1]
options = args[2]
else
raise RuntimeError.new('path missing')
end

return path unless production?

verify_config!
imgix_client(source).path(path).to_url(options)
end

private

def verify_config_present!
unless @context.registers[:site].config['imgix']
DEFAULT_OPTS = {
library_param: "jekyll",
library_version: VERSION,
use_https: true
}.freeze


def verify_config!
config = @context.registers[:site].config['imgix']
unless config
raise StandardError.new("No 'imgix' section present in _config.yml. Please see https://github.com/imgix/jekyll-imgix for configuration instructions")
end
if !(config['source'] || config['sources'])
raise StandardError.new("One of 'source', 'sources' is required")
end
if (config['source'] && config['sources'])
raise StandardError.new("'source' and 'sources' can't be used together")
end
end

def imgix_client(src)
begin
return imgix_clients.fetch(src)
rescue KeyError
raise RuntimeError.new("Unknown source '#{src}'")
end
end

def client
return @client if @client
def imgix_clients
return @imgix_clients if @imgix_clients

opts = default_opts.dup
opts = DEFAULT_OPTS.dup
opts[:secure_url_token] = secure_url_token if secure_url_token
opts[:include_library_param] = include_library_param?
@client = ::Imgix::Client.new(opts)
end

def default_opts
{
host: source,
library_param: "jekyll",
library_version: VERSION,
use_https: true
}
@imgix_clients = {}
sources.map do |source, token|
opts[:host] = source
opts[:secure_url_token] = token
@imgix_clients[source] = ::Imgix::Client.new(opts)
end

begin
@imgix_clients[nil] = @imgix_clients.fetch(default_source || source)
rescue
end

@imgix_clients
end

def production?
Expand All @@ -52,6 +104,18 @@ def source
ix_config.fetch('source', nil)
end

def sources
begin
return ix_config.fetch('sources')
rescue
return { ix_config.fetch('source') => secure_url_token }
end
end

def default_source
ix_config.fetch('default_source', nil)
end

def secure_url_token
ix_config.fetch('secure_url_token', nil)
end
Expand Down
Loading