Skip to content

Commit

Permalink
fix(tag/post_link): support url with subdir (hexojs#5419)
Browse files Browse the repository at this point in the history
  • Loading branch information
leafbird authored Feb 21, 2024
1 parent 6cf6993 commit 7ef26ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/plugins/tag/post_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ export = (ctx: Hexo) => {
const attrTitle = escapeHTML(post.title || post.slug);
if (escape === 'true') title = escapeHTML(title);

const url = new URL(post.path, ctx.config.url).pathname + (hash ? `#${hash}` : '');
// guarantee the base url ends with a slash. (case of using a subdirectory in the url of the site)
let baseUrl = ctx.config.url;
if (!baseUrl.endsWith('/')) {
baseUrl += '/';
}

const url = new URL(post.path, baseUrl).pathname + (hash ? `#${hash}` : '');
const link = encodeURL(url);

return `<a href="${link}" title="${attrTitle}">${title}</a>`;
Expand Down
5 changes: 5 additions & 0 deletions test/scripts/tags/post_link.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ describe('post_link', () => {
it('should keep hash', () => {
postLink(['foo#bar']).should.eql('<a href="/foo/#bar" title="Hello world">Hello world</a>');
});

it('should keep subdir', () => {
hexo.config.url = 'http://example.com/subdir';
postLink(['foo']).should.eql('<a href="/subdir/foo/" title="Hello world">Hello world</a>');
});
});

0 comments on commit 7ef26ad

Please sign in to comment.