fix: relative() helper with --link-prefix

The previous implementation did not work when the link prefix
went back deeper than the current folder allows
e.g. ../../.. when running from /app

Which is expected to work regardless because the link prefix
is assumed to exist relative to the final deployment

Fixes #351
pull/353/merge
Romain 5 months ago
parent 6871f6edd2
commit 4ffba3b276

@ -6,7 +6,8 @@ module.exports = (target, options) => {
return target
}
const albumPath = options.data.root.album.path
const relative = path.relative(path.dirname(albumPath), target)
const backToGalleryRoot = path.relative(path.dirname(albumPath), '.')
const relative = path.join(backToGalleryRoot, target)
const url = relative.replace(/\\/g, '/')
// Escape single/double quotes
return url.replace(/'/g, '%27').replace(/"/g, '%22')

@ -48,6 +48,26 @@ describe('Handlebars helpers: relative', () => {
should(res).eql('<img src="../media/thumbs/img.jpg" />')
})
it('can use a relative link from the root album', () => {
const template = handlebars.compile('<link rel="stylesheet" href="{{relative \'../../photos/img.jpg\'}}" />')
const res = template({
album: {
path: 'index.html'
}
})
should(res).eql('<link rel="stylesheet" href="../../photos/img.jpg" />')
})
it('can use relative link from a nested album', () => {
const template = handlebars.compile('<link rel="stylesheet" href="{{relative \'../../photos/img.jpg\'}}" />')
const res = template({
album: {
path: 'albums/holidays.html'
}
})
should(res).eql('<link rel="stylesheet" href="../../../photos/img.jpg" />')
})
it('does not do anything if the path is an absolute URL', () => {
// This can happen when using --link-prefix
const url = 'http://example.com/photo.jpg'

Loading…
Cancel
Save