Skip to content

Commit

Permalink
fix(tag): use url_for
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Dec 22, 2023
1 parent ea4f63c commit 857a2ba
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/filter/before_exit/save_database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type Hexo from '../../../hexo';

function saveDatabaseFilter(this: Hexo) {
function saveDatabaseFilter(this: Hexo): Promise<void> {
if (!this.env.init || !this._dbLoaded) return;

return this.database.save().then(() => {
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/tag/asset_img.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export = (ctx: Hexo) => {
for (let i = 0; i < len; i++) {
const asset = PostAsset.findOne({post: this._id, slug: args[i]});
if (asset) {
// img tag will call url_for so no need to call it here
args[i] = encodeURL(new URL(asset.path, ctx.config.url).pathname);
return img(ctx)(args);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/tag/asset_link.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeURL, escapeHTML } from 'hexo-util';
import { url_for, escapeHTML } from 'hexo-util';
import type Hexo from '../../hexo';

/**
Expand Down Expand Up @@ -28,7 +28,7 @@ export = (ctx: Hexo) => {
const attrTitle = escapeHTML(title);
if (escape === 'true') title = attrTitle;

const link = encodeURL(new URL(asset.path, ctx.config.url).pathname);
const link = url_for.call(ctx, asset.path);

return `<a href="${link}" title="${attrTitle}">${title}</a>`;
};
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/tag/asset_path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeURL } from 'hexo-util';
import { url_for } from 'hexo-util';
import type Hexo from '../../hexo';

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

const path = encodeURL(new URL(asset.path, ctx.config.url).pathname);
const path = url_for.call(ctx, asset.path);

return path;
};
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/tag/post_link.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeURL, escapeHTML } from 'hexo-util';
import { url_for, encodeURL, escapeHTML } from 'hexo-util';
import { postFindOneFactory } from './';
import type Hexo from '../../hexo';

Expand Down Expand Up @@ -41,7 +41,7 @@ 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}` : '');
const url = url_for.call(ctx, post.path) + (hash ? `#${hash}` : '');
const link = encodeURL(url);

return `<a href="${link}" title="${attrTitle}">${title}</a>`;
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/tag/post_path.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encodeURL } from 'hexo-util';
import { url_for } from 'hexo-util';
import { postFindOneFactory } from './';
import type Hexo from '../../hexo';

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

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

return link;
};
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('with root path', () => {
hexo.config.root = '/root/';
postLink(['foo']).should.eql('<a href="/root/foo/" title="Hello world">Hello world</a>');
});
});

0 comments on commit 857a2ba

Please sign in to comment.