From deee0496502a353a70997fe638bbc3ed17c9e084 Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 12 Jun 2018 00:37:44 +0200 Subject: [PATCH] refactor(themes): extract the {{relative}} helper to be standalone + add some tests --- src/website/theme.js | 5 ----- test/themes/helpers/relative.spec.js | 27 +++++++++++++++++++++++++++ themes/base/helpers/relative.js | 6 ++++++ 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 test/themes/helpers/relative.spec.js create mode 100644 themes/base/helpers/relative.js diff --git a/src/website/theme.js b/src/website/theme.js index 75f4a92..99e2bde 100644 --- a/src/website/theme.js +++ b/src/website/theme.js @@ -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) { diff --git a/test/themes/helpers/relative.spec.js b/test/themes/helpers/relative.spec.js new file mode 100644 index 0000000..53454a9 --- /dev/null +++ b/test/themes/helpers/relative.spec.js @@ -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(``) + const res = template({ + album: { + path: 'index.html' + } + }) + should(res).eql('') + }) + + it('returns a relative path for albums in nested folders', () => { + const template = handlebars.compile(``) + const res = template({ + album: { + path: 'albums/holidays.html' + } + }) + should(res).eql('') + }) +}) diff --git a/themes/base/helpers/relative.js b/themes/base/helpers/relative.js new file mode 100644 index 0000000..90bab9c --- /dev/null +++ b/themes/base/helpers/relative.js @@ -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) +}