refactor(themes): extract the {{relative}} helper to be standalone + add some tests

pull/108/head
Romain 6 years ago
parent be0ee25c64
commit deee049650

@ -81,11 +81,6 @@ class Theme {
const fullPath = path.join(this.dir, 'helpers', filename)
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) {

@ -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" />')
})
})

@ -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…
Cancel
Save