mirror of
https://github.com/thumbsup/thumbsup
synced 2024-11-07 15:20:26 +00:00
refactor(themes): extract the {{relative}} helper to be standalone + add some tests
This commit is contained in:
parent
be0ee25c64
commit
deee049650
@ -81,11 +81,6 @@ class Theme {
|
|||||||
const fullPath = path.join(this.dir, 'helpers', filename)
|
const fullPath = path.join(this.dir, 'helpers', filename)
|
||||||
handlebars.registerHelper(name, require(fullPath))
|
handlebars.registerHelper(name, require(fullPath))
|
||||||
})
|
})
|
||||||
|
|
||||||
// render a relative path
|
|
||||||
handlebars.registerHelper('relative', (target, options) => {
|
|
||||||
return path.relative(path.dirname(options.data.root.album.path), target)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
renderStyles (done) {
|
renderStyles (done) {
|
||||||
|
27
test/themes/helpers/relative.spec.js
Normal file
27
test/themes/helpers/relative.spec.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
const date = require('../../../themes/base/helpers/relative')
|
||||||
|
const handlebars = require('handlebars')
|
||||||
|
const should = require('should/as-function')
|
||||||
|
|
||||||
|
describe('Handlebars helpers: relative', () => {
|
||||||
|
handlebars.registerHelper('relative', date)
|
||||||
|
|
||||||
|
it('returns a path in the same folder', () => {
|
||||||
|
const template = handlebars.compile(`<link rel="stylesheet" href="{{relative 'public/theme.css'}}" />`)
|
||||||
|
const res = template({
|
||||||
|
album: {
|
||||||
|
path: 'index.html'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
should(res).eql('<link rel="stylesheet" href="public/theme.css" />')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns a relative path for albums in nested folders', () => {
|
||||||
|
const template = handlebars.compile(`<link rel="stylesheet" href="{{relative 'public/theme.css'}}" />`)
|
||||||
|
const res = template({
|
||||||
|
album: {
|
||||||
|
path: 'albums/holidays.html'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
should(res).eql('<link rel="stylesheet" href="../public/theme.css" />')
|
||||||
|
})
|
||||||
|
})
|
6
themes/base/helpers/relative.js
Normal file
6
themes/base/helpers/relative.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
const path = require('path')
|
||||||
|
|
||||||
|
module.exports = (target, options) => {
|
||||||
|
const albumPath = options.data.root.album.path
|
||||||
|
return path.relative(path.dirname(albumPath), target)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user