Skip to content

Commit 67ba3e6

Browse files
fix(tag): use url_for
1 parent d7ad401 commit 67ba3e6

File tree

6 files changed

+10
-16
lines changed

6 files changed

+10
-16
lines changed

lib/plugins/tag/asset_img.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export = (ctx: Hexo) => {
1818
for (let i = 0; i < len; i++) {
1919
const asset = PostAsset.findOne({post: this._id, slug: args[i]});
2020
if (asset) {
21+
// img tag will call url_for so no need to call it here
2122
args[i] = encodeURL(new URL(asset.path, ctx.config.url).pathname);
2223
return img(ctx)(args);
2324
}

lib/plugins/tag/asset_link.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { encodeURL, escapeHTML } from 'hexo-util';
1+
import { url_for, escapeHTML } from 'hexo-util';
22
import type Hexo from '../../hexo';
33

44
/**
@@ -28,7 +28,7 @@ export = (ctx: Hexo) => {
2828
const attrTitle = escapeHTML(title);
2929
if (escape === 'true') title = attrTitle;
3030

31-
const link = encodeURL(new URL(asset.path, ctx.config.url).pathname);
31+
const link = url_for.call(ctx, asset.path.replace(/\\/g, '/'));
3232

3333
return `<a href="${link}" title="${attrTitle}">${title}</a>`;
3434
};

lib/plugins/tag/asset_path.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { encodeURL } from 'hexo-util';
1+
import { url_for } from 'hexo-util';
22
import type Hexo from '../../hexo';
33

44
/**
@@ -17,7 +17,7 @@ export = (ctx: Hexo) => {
1717
const asset = PostAsset.findOne({post: this._id, slug});
1818
if (!asset) return;
1919

20-
const path = encodeURL(new URL(asset.path, ctx.config.url).pathname);
20+
const path = url_for.call(ctx, asset.path.replace(/\\/g, '/'));
2121

2222
return path;
2323
};

lib/plugins/tag/post_link.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { encodeURL, escapeHTML } from 'hexo-util';
1+
import { url_for, encodeURL, escapeHTML } from 'hexo-util';
22
import { postFindOneFactory } from './';
33
import type Hexo from '../../hexo';
44

@@ -41,14 +41,7 @@ export = (ctx: Hexo) => {
4141
const attrTitle = escapeHTML(post.title || post.slug);
4242
if (escape === 'true') title = escapeHTML(title);
4343

44-
// guarantee the base url ends with a slash. (case of using a subdirectory in the url of the site)
45-
let baseUrl = ctx.config.url;
46-
if (!baseUrl.endsWith('/')) {
47-
baseUrl += '/';
48-
}
49-
50-
const url = new URL(post.path, baseUrl).pathname + (hash ? `#${hash}` : '');
51-
const link = encodeURL(url);
44+
const link = url_for.call(ctx, post.path + (hash ? `#${hash}` : ''));
5245

5346
return `<a href="${link}" title="${attrTitle}">${title}</a>`;
5447
};

lib/plugins/tag/post_path.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { encodeURL } from 'hexo-util';
1+
import { url_for } from 'hexo-util';
22
import { postFindOneFactory } from './';
33
import type Hexo from '../../hexo';
44

@@ -17,7 +17,7 @@ export = (ctx: Hexo) => {
1717
const post = factory({ slug }) || factory({ title: slug });
1818
if (!post) return;
1919

20-
const link = encodeURL(new URL(post.path, ctx.config.url).pathname);
20+
const link = url_for.call(ctx, post.path);
2121

2222
return link;
2323
};

test/scripts/tags/post_link.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('post_link', () => {
8181
});
8282

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

0 commit comments

Comments
 (0)