From 4ffba3b2761f3a22e2bb1424e3242da7551f6a96 Mon Sep 17 00:00:00 2001 From: Romain Date: Mon, 18 Dec 2023 22:18:23 +0000 Subject: [PATCH] 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 --- src/website/theme-base/helpers/relative.js | 3 ++- test/themes/helpers/relative.spec.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/website/theme-base/helpers/relative.js b/src/website/theme-base/helpers/relative.js index 584c185..b865b7f 100644 --- a/src/website/theme-base/helpers/relative.js +++ b/src/website/theme-base/helpers/relative.js @@ -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') diff --git a/test/themes/helpers/relative.spec.js b/test/themes/helpers/relative.spec.js index 814ea82..6722f06 100644 --- a/test/themes/helpers/relative.spec.js +++ b/test/themes/helpers/relative.spec.js @@ -48,6 +48,26 @@ describe('Handlebars helpers: relative', () => { should(res).eql('') }) + it('can use a relative link from the root album', () => { + const template = handlebars.compile('') + const res = template({ + album: { + path: 'index.html' + } + }) + should(res).eql('') + }) + + it('can use relative link from a nested album', () => { + const template = handlebars.compile('') + const res = template({ + album: { + path: 'albums/holidays.html' + } + }) + should(res).eql('') + }) + 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'