Skip to content

Commit

Permalink
Merge pull request #9 from hexojs/add-priority-config
Browse files Browse the repository at this point in the history
Add priority configuration option
  • Loading branch information
ertrzyiks committed Apr 5, 2018
2 parents 874194b + e2ff4a5 commit fe39e54
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 61 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
All notable changes to this project will be documented in this file.

## [Unreleased]
### Added
- `priority` configuration

## [1.0.4]
### Changed
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ Put a size name as a key. It will be used as a prefix for the generated file nam
Use width and height keys to configure dimensions. Skip one for auto-scale.
For more information and all possible values for `options` check http://sharp.pixelplumbing.com/en/stable/api-resize/

### Priority

Optionally, you can put `pattern` and `sizes` configuration under `rules` key and use `priority` option to
set the priority of `after_generate` filter. Can be handy, if you want to control the order of filters executed
on your files.

```
priority: 9
rules: ...
```

You can find more information about priority in [Filter](https://hexo.io/api/filter.html) documentation.

### Examples

Single pattern:
Expand Down Expand Up @@ -86,6 +99,23 @@ responsive_images:
width: 900
```

And the example with priority:
```
responsive_images:
priority: 9
rules:
- pattern: content/squares/*.jpg
sizes:
square:
width: 200
height: 200
- pattern: content/**/*.+(png|jpg|jpeg)
sizes:
thumb:
width: 900
```

## View helper

To get the responsive image URL you can just refer to it's prefixed version.
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
var generateResponsiveImages = require('./lib/responsive_images')
var getNewPath = require('./lib/new_path')

var config = hexo.config.responsive_images || {}
var priority = typeof config.priority != 'undefined' ? config.priority : 9

hexo.extend.helper.register('image_version', function (original, options) {
return getNewPath(original, options)
});


hexo.extend.filter.register('after_generate', generateResponsiveImages, 9)
hexo.extend.filter.register('after_generate', generateResponsiveImages, priority)
5 changes: 2 additions & 3 deletions lib/responsive_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ var sharp = require('sharp')

function generateResponsiveImages() {
var hexo = this
var config = hexo.config
var rules = config.responsive_images || []
var config = hexo.config.responsive_images || []
var rules = config.rules ? config.rules : config
if (!isArray(rules)) {
rules = [rules]
}

var route = hexo.route
var routes = route.list()

Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"devDependencies": {
"ava": "^1.0.0-beta.3",
"hexo": "^3.6.0",
"hexo-test-utils": "0.0.1"
"hexo-test-utils": "0.0.3"
}
}
19 changes: 9 additions & 10 deletions specs/image_version_helper_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ import {hasHelper, getHelper} from 'hexo-test-utils/helpers'

const sandbox = getSandbox()

test('returns prefixed version of the filename', t => {
const ctx = sandbox()
test('returns prefixed version of the filename', async t => {
const ctx = await sandbox()

t.plan(4)
return process(ctx).then(() => {
t.is(hasHelper(ctx, 'image_version'), true)
await process(ctx)

const imageVersion = getHelper(ctx, 'image_version')
t.is(hasHelper(ctx, 'image_version'), true)

t.is(imageVersion('my/file.jpg', {prefix: 'thumb'}), 'my/thumb_file.jpg')
t.is(imageVersion('my/file.jpg', {prefix: 'small'}), 'my/small_file.jpg')
t.is(imageVersion('file.jpg', {prefix: 'big'}), 'big_file.jpg')
})
const imageVersion = getHelper(ctx, 'image_version')

t.is(imageVersion('my/file.jpg', {prefix: 'thumb'}), 'my/thumb_file.jpg')
t.is(imageVersion('my/file.jpg', {prefix: 'small'}), 'my/small_file.jpg')
t.is(imageVersion('file.jpg', {prefix: 'big'}), 'big_file.jpg')
})
149 changes: 107 additions & 42 deletions specs/responsive_images_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function getImageDimensions(buffer) {
})
}

test('renders prefixed asset', t => {
const ctx = sandbox('test1')
test('renders prefixed asset', async t => {
const ctx = await sandbox('test1')

mockConfig(ctx, 'responsive_images', {
pattern: '*.png',
Expand All @@ -25,17 +25,15 @@ test('renders prefixed asset', t => {
}
})

t.plan(3)
return process(ctx)
.then(ctx => {
t.is(hasRoute(ctx, 'thumb_image.png'), true)
t.is(hasRoute(ctx, 'small_image.png'), true)
t.is(hasRoute(ctx, 'huge_image.png'), true)
})
await process(ctx)

t.is(hasRoute(ctx, 'thumb_image.png'), true)
t.is(hasRoute(ctx, 'small_image.png'), true)
t.is(hasRoute(ctx, 'huge_image.png'), true)
})

test('renders resized asset', t => {
const ctx = sandbox('test1')
test('renders resized asset', async t => {
const ctx = await sandbox('test1')

mockConfig(ctx, 'responsive_images', {
pattern: '*.png',
Expand All @@ -44,18 +42,15 @@ test('renders resized asset', t => {
}
})

t.plan(2)
return process(ctx)
.then(ctx => contentFor(ctx, 'thumb_image.png'))
.then(buffer => getImageDimensions(buffer))
.then(({width, height}) => {
t.is(width, 100)
t.is(height, 100)
})
await process(ctx)
const buffer = await contentFor(ctx, 'thumb_image.png')
const {width, height} = await getImageDimensions(buffer)
t.is(width, 100)
t.is(height, 100)
})

test('renders resized asset by pattern', t => {
const ctx = sandbox('test2')
test('renders resized asset by pattern', async t => {
const ctx = await sandbox('test2')

mockConfig(ctx, 'responsive_images', {
pattern: '*_1.png',
Expand All @@ -64,18 +59,15 @@ test('renders resized asset by pattern', t => {
}
})

t.plan(4)
return process(ctx)
.then(ctx => {
t.is(hasRoute(ctx, 'image_1.png'), true)
t.is(hasRoute(ctx, 'thumb_image_1.png'), true)
t.is(hasRoute(ctx, 'image_2.png'), true)
t.is(hasRoute(ctx, 'thumb_image_2.png'), false)
})
await process(ctx)
t.is(hasRoute(ctx, 'image_1.png'), true)
t.is(hasRoute(ctx, 'thumb_image_1.png'), true)
t.is(hasRoute(ctx, 'image_2.png'), true)
t.is(hasRoute(ctx, 'thumb_image_2.png'), false)
})

test('renders resized assets using array of rules', t => {
const ctx = sandbox('test2')
test('renders resized assets using array of rules', async t => {
const ctx = await sandbox('test2')

mockConfig(ctx, 'responsive_images', [
{
Expand All @@ -89,17 +81,90 @@ test('renders resized assets using array of rules', t => {
sizes: {
thumb: {width: 100}
}
},
}
])

t.plan(6)
return process(ctx)
.then(ctx => {
t.is(hasRoute(ctx, 'image_1.png'), true)
t.is(hasRoute(ctx, 'thumb_image_1.png'), true)
t.is(hasRoute(ctx, 'super_small_image_1.png'), true)
t.is(hasRoute(ctx, 'image_2.png'), true)
t.is(hasRoute(ctx, 'thumb_image_2.png'), true)
t.is(hasRoute(ctx, 'super_small_image_2.png'), false)
})
await process(ctx)
t.is(hasRoute(ctx, 'image_1.png'), true)
t.is(hasRoute(ctx, 'thumb_image_1.png'), true)
t.is(hasRoute(ctx, 'super_small_image_1.png'), true)
t.is(hasRoute(ctx, 'image_2.png'), true)
t.is(hasRoute(ctx, 'thumb_image_2.png'), true)
t.is(hasRoute(ctx, 'super_small_image_2.png'), false)
})

test('uses the priority from the configuration when it is higher', async t => {
const ctx = await sandbox('test2')

ctx.extend.filter.register('after_generate', () => {
ctx.route.remove('image_2.png')
}, 10)

mockConfig(ctx, 'responsive_images', {
priority: 11,
rules: [
{
pattern: '*.png',
sizes: {
super_small: {width: 10}
}
}
]
})

await process(ctx)

t.is(hasRoute(ctx, 'image_1.png'), true)
t.is(hasRoute(ctx, 'super_small_image_1.png'), true)
t.is(hasRoute(ctx, 'image_2.png'), false)
t.is(hasRoute(ctx, 'super_small_image_2.png'), false)
})

test('uses the priority from the configuration when it is lower', async t => {
const ctx = await sandbox('test2')

ctx.extend.filter.register('after_generate', () => {
ctx.route.remove('image_2.png')
}, 10)

mockConfig(ctx, 'responsive_images', {
priority: 9,
rules: [
{
pattern: '*.png',
sizes: {
super_small: {width: 10}
}
}
]
})

await process(ctx)

t.is(hasRoute(ctx, 'image_1.png'), true)
t.is(hasRoute(ctx, 'super_small_image_1.png'), true)
t.is(hasRoute(ctx, 'image_2.png'), false)
t.is(hasRoute(ctx, 'super_small_image_2.png'), true)
})

test('uses the priority from the configuration when it is default', async t => {
const ctx = await sandbox('test2')

ctx.extend.filter.register('after_generate', () => {
ctx.route.remove('image_2.png')
}, 10)

mockConfig(ctx, 'responsive_images', {
pattern: '*.png',
sizes: {
super_small: {width: 10}
}
})

await process(ctx)

t.is(hasRoute(ctx, 'image_1.png'), true)
t.is(hasRoute(ctx, 'super_small_image_1.png'), true)
t.is(hasRoute(ctx, 'image_2.png'), false)
t.is(hasRoute(ctx, 'super_small_image_2.png'), true)
})

0 comments on commit fe39e54

Please sign in to comment.