2
0
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:
Romain 2018-06-12 00:37:44 +02:00
parent be0ee25c64
commit deee049650
3 changed files with 33 additions and 5 deletions

View File

@ -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) {

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

View 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)
}