Skip to content

Commit ccf2951

Browse files
committed
import from upstream/master
1 parent fc02edc commit ccf2951

File tree

9 files changed

+26
-29
lines changed

9 files changed

+26
-29
lines changed

lib/box/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,10 @@ class Box extends EventEmitter {
160160
if (!params) return count;
161161

162162
const file = new File({
163+
// source is used for filesystem path, keep backslashes on Windows
163164
source: join(base, path),
164-
path,
165+
// path is used for URL path, replace backslashes on Windows
166+
path: escapeBackslash(path),
165167
params,
166168
type
167169
});

lib/models/post_asset.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import warehouse from 'warehouse';
2-
import { join, dirname } from 'path';
2+
import { join, posix } from 'path';
33
import type Hexo from '../hexo';
44

55
export = (ctx: Hexo) => {
66
const PostAsset = new warehouse.Schema({
7-
_id: { type: String, required: true },
8-
slug: { type: String, required: true },
9-
modified: { type: Boolean, default: true },
10-
post: { type: warehouse.Schema.Types.CUID, ref: 'Post' },
11-
renderable: { type: Boolean, default: true }
7+
_id: {type: String, required: true},
8+
slug: {type: String, required: true},
9+
modified: {type: Boolean, default: true},
10+
post: {type: warehouse.Schema.Types.CUID, ref: 'Post'},
11+
renderable: {type: Boolean, default: true}
1212
});
1313

1414
PostAsset.virtual('path').get(function() {
@@ -18,8 +18,9 @@ export = (ctx: Hexo) => {
1818

1919
// PostAsset.path is file path relative to `public_dir`
2020
// no need to urlescape, #1562
21-
// strip extensions better on permalink, #2134
22-
return join(dirname(post.path), post.slug, this.slug);
21+
// strip /\.html?$/ extensions on permalink, #2134
22+
// Use path.posix.join to avoid path.join introducing unwanted backslashes on Windows.
23+
return posix.join(post.path.replace(/\.html?$/, ''), this.slug);
2324
});
2425

2526
PostAsset.virtual('source').get(function() {

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);
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);
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, 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/models/post_asset.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'path';
1+
import { join, posix } from 'path';
22
import Hexo from '../../../lib/hexo';
33
import defaults from '../../../lib/hexo/default_config';
44

@@ -54,7 +54,7 @@ describe('PostAsset', () => {
5454
slug: 'foo.jpg',
5555
post: post._id
5656
});
57-
data.path.should.eql(join(post.path, data.slug));
57+
data.path.should.eql(posix.join(post.path, data.slug));
5858

5959
PostAsset.removeById(data._id);
6060
});
@@ -66,7 +66,7 @@ describe('PostAsset', () => {
6666
slug: 'foo.htm',
6767
post: post._id
6868
});
69-
data.path.should.eql(join(post.path, data.slug));
69+
data.path.should.eql(posix.join(post.path, data.slug));
7070

7171
PostAsset.removeById(data._id);
7272
});
@@ -78,7 +78,7 @@ describe('PostAsset', () => {
7878
slug: 'foo.htm',
7979
post: post._id
8080
});
81-
data.path.should.eql(join(post.path, data.slug));
81+
data.path.should.eql(posix.join(post.path, data.slug));
8282

8383
PostAsset.removeById(data._id);
8484
});

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)