🐛 Fixed subdirectory handling and deduplication

no issue

Ghost's relative->absolute handling is a little strange when the rootUrl includes a subdirectory. Root-relative paths such as /content/image.jpg are actually treated as subdirectory-relative. This means that it's possible to migrate from a root config to a subdirectory config without migrating data in the database, _however_ that means that the database will now have a mix of path styles (/content/image.png and /subdir/content/image.png). To handle this when all root-relative paths are treated as subdir-relative we have to rely on subdirectory deduplication.

- updates tests to reflect correct subdirectory handling according to the above rules
- fixes missing subdirectories when root urls contain subdirectories but relative paths do not
- fixes subdirectory deduplication when the supplied url/path does not have a trailing slash but matches the root url's subdirectory
This commit is contained in:
Kevin Ansfield 2019-10-02 12:33:34 +01:00
parent 86343f028b
commit 523278b295
2 changed files with 9 additions and 1 deletions

View File

@ -127,5 +127,13 @@ describe('utils: deduplicateSubdirectory()', function () {
deduplicateSubdirectory(path, 'http://example.blog/blog/subdir/') deduplicateSubdirectory(path, 'http://example.blog/blog/subdir/')
.should.eql('/blog/blog/file.png?test=true#testing', 'with root trailing-slash'); .should.eql('/blog/blog/file.png?test=true#testing', 'with root trailing-slash');
}); });
it('deduplicates path with no trailing slash that matches subdir', function () {
deduplicateSubdirectory('/blog/blog', 'http://example.com/blog')
.should.equal('/blog/', 'without root trailing-slash');
deduplicateSubdirectory('/blog/blog', 'http://example.com/blog/')
.should.equal('/blog/', 'with root trailing-slash');
});
}); });
}); });

View File

@ -23,7 +23,7 @@ const deduplicateSubdirectory = function deduplicateSubdirectory(url, rootUrl) {
const subdir = parsedRoot.pathname.replace(/(^\/|\/$)+/g, ''); const subdir = parsedRoot.pathname.replace(/(^\/|\/$)+/g, '');
// we can have subdirs that match TLDs so we need to restrict matches to // we can have subdirs that match TLDs so we need to restrict matches to
// duplicates that start with a / or the beginning of the url // duplicates that start with a / or the beginning of the url
const subdirRegex = new RegExp(`(^|/)${subdir}/${subdir}/`); const subdirRegex = new RegExp(`(^|/)${subdir}/${subdir}(/|$)`);
return url.replace(subdirRegex, `$1${subdir}/`); return url.replace(subdirRegex, `$1${subdir}/`);
}; };