Skip to content

Commit 98ea2fa

Browse files
stevenjoezhangdimaslanjaka
authored andcommitted
refactor: backslashes on Windows (hexojs#5457)
1 parent ccf2951 commit 98ea2fa

File tree

8 files changed

+46
-41
lines changed

8 files changed

+46
-41
lines changed

lib/box/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Box extends EventEmitter {
113113
const src = join(this.base, path);
114114

115115
return Cache.compareFile(
116-
escapeBackslash(src.substring(ctx.base_dir.length)),
116+
src.substring(ctx.base_dir.length),
117117
() => getHash(src),
118118
() => stat(src)
119119
).then(result => ({
@@ -129,7 +129,7 @@ class Box extends EventEmitter {
129129
if (!stats.isDirectory()) return;
130130

131131
// Check existing files in cache
132-
const relativeBase = escapeBackslash(base.substring(ctx.base_dir.length));
132+
const relativeBase = base.substring(ctx.base_dir.length);
133133
const cacheFiles = Cache.filter(item => item._id.startsWith(relativeBase)).map(item => item._id.substring(relativeBase.length));
134134

135135
// Handle deleted files
@@ -156,11 +156,12 @@ class Box extends EventEmitter {
156156
});
157157

158158
return BlueBirdPromise.reduce(this.processors, (count, processor) => {
159-
const params = processor.pattern.match(path);
159+
// patten supports *nix style path only, replace backslashes on Windows
160+
const params = processor.pattern.match(escapeBackslash(path));
160161
if (!params) return count;
161162

162163
const file = new File({
163-
// source is used for filesystem path, keep backslashes on Windows
164+
// source is used for file system path, keep backslashes on Windows
164165
source: join(base, path),
165166
// path is used for URL path, replace backslashes on Windows
166167
path: escapeBackslash(path),
@@ -194,7 +195,7 @@ class Box extends EventEmitter {
194195
const { base } = this;
195196

196197
function getPath(path) {
197-
return escapeBackslash(path.substring(base.length));
198+
return path.substring(base.length);
198199
}
199200

200201
return this.process().then(() => watch(base, this.options)).then(watcher => {
@@ -214,7 +215,7 @@ class Box extends EventEmitter {
214215

215216
watcher.on('addDir', path => {
216217
let prefix = getPath(path);
217-
if (prefix) prefix += '/';
218+
if (prefix) prefix += sep;
218219

219220
this._readDir(path, prefix);
220221
});
@@ -287,7 +288,7 @@ function readDirWalker(ctx: Hexo, base: string, results: any[], ignore: any, pre
287288
const prefixPath = `${prefix}${path}`;
288289
if (stats) {
289290
if (stats.isDirectory()) {
290-
return readDirWalker(ctx, fullpath, results, ignore, `${prefixPath}/`);
291+
return readDirWalker(ctx, fullpath, results, ignore, prefixPath + sep);
291292
}
292293
if (!isIgnoreMatch(fullpath, ignore)) {
293294
results.push(prefixPath);

lib/plugins/processor/asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function processPage(ctx: Hexo, file: _File) {
109109
}
110110

111111
function processAsset(ctx: Hexo, file: _File) {
112-
const id = relative(ctx.base_dir, file.source).replace(/\\/g, '/');
112+
const id = relative(ctx.base_dir, file.source);
113113
const Asset = ctx.model('Asset');
114114
const doc = Asset.findById(id);
115115

lib/plugins/processor/post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function scanAssetDir(ctx: Hexo, post) {
233233
if (err && err.code === 'ENOENT') return [];
234234
throw err;
235235
}).filter(item => !isExcludedFile(item, ctx.config)).map(item => {
236-
const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/');
236+
const id = join(assetDir, item).substring(baseDirLength);
237237
const renderablePath = id.substring(sourceDirLength + 1);
238238
const asset = PostAsset.findById(id);
239239

@@ -267,7 +267,7 @@ function shouldSkipAsset(ctx: Hexo, post, asset) {
267267
function processAsset(ctx: Hexo, file: _File) {
268268
const PostAsset = ctx.model('PostAsset');
269269
const Post = ctx.model('Post');
270-
const id = file.source.substring(ctx.base_dir.length).replace(/\\/g, '/');
270+
const id = file.source.substring(ctx.base_dir.length);
271271
const doc = PostAsset.findById(id);
272272

273273
if (file.type === 'delete') {

lib/theme/processors/source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { _File } from '../../box';
44

55
function process(file: _File) {
66
const Asset = this.model('Asset');
7-
const id = file.source.substring(this.base_dir.length).replace(/\\/g, '/');
7+
const id = file.source.substring(this.base_dir.length);
88
const { path } = file.params;
99
const doc = Asset.findById(id);
1010

test/scripts/box/box.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe('Box', () => {
120120
const box = newBox('test');
121121
const name = 'a.txt';
122122
const path = join(box.base, name);
123-
const cacheId = 'test/' + name;
123+
const cacheId = join('test/', name);
124124

125125
const processor = spy();
126126
box.addProcessor(processor);
@@ -144,7 +144,7 @@ describe('Box', () => {
144144
const box = newBox('test');
145145
const name = 'a.txt';
146146
const path = join(box.base, name);
147-
const cacheId = 'test/' + name;
147+
const cacheId = join('test/', name);
148148

149149
const processor = spy();
150150
box.addProcessor(processor);
@@ -167,7 +167,7 @@ describe('Box', () => {
167167
const box = newBox('test');
168168
const name = 'a.txt';
169169
const path = join(box.base, name);
170-
const cacheId = 'test/' + name;
170+
const cacheId = join('test/', name);
171171

172172
const processor = spy();
173173
box.addProcessor(processor);
@@ -190,7 +190,7 @@ describe('Box', () => {
190190
const box = newBox('test');
191191
const name = 'a.txt';
192192
const path = join(box.base, name);
193-
const cacheId = 'test/' + name;
193+
const cacheId = join('test/', name);
194194

195195
const processor = spy();
196196
box.addProcessor(processor);
@@ -211,7 +211,8 @@ describe('Box', () => {
211211

212212
it('process() - delete', async () => {
213213
const box = newBox('test');
214-
const cacheId = 'test/a.txt';
214+
// join will replace backslashes on Windows
215+
const cacheId = join('test/', 'a.txt');
215216

216217
const processor = spy();
217218
box.addProcessor(processor);

test/scripts/processors/asset.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('asset', () => {
8080

8181
await writeFile(file.source, 'foo');
8282
await process(file);
83-
const id = 'source/' + file.path;
83+
const id = join('source/', file.path);
8484
const asset = Asset.findById(id);
8585

8686
asset._id.should.eql(id);
@@ -103,7 +103,7 @@ describe('asset', () => {
103103

104104
await writeFile(file.source, 'foo');
105105
await process(file);
106-
const id = '../source/foo.jpg'; // The id should a relative path,because the 'lib/models/assets.js' use asset path by joining base path with "_id" directly.
106+
const id = join('../source/', 'foo.jpg'); // The id should a relative path, because the 'lib/models/assets.js' use asset path by joining base path with "_id" directly.
107107
const asset = Asset.findById(id);
108108
asset._id.should.eql(id);
109109
asset.path.should.eql(file.path);
@@ -122,7 +122,7 @@ describe('asset', () => {
122122
renderable: false
123123
});
124124

125-
const id = 'source/' + file.path;
125+
const id = join('source/', file.path);
126126

127127
await Promise.all([
128128
writeFile(file.source, 'test'),
@@ -153,7 +153,7 @@ describe('asset', () => {
153153
renderable: false
154154
});
155155

156-
const id = 'source/' + file.path;
156+
const id = join('source/', file.path);
157157

158158
await Promise.all([
159159
writeFile(file.source, 'test'),
@@ -179,7 +179,7 @@ describe('asset', () => {
179179
renderable: false
180180
});
181181

182-
const id = 'source/' + file.path;
182+
const id = join('source/', file.path);
183183

184184
await Asset.insert({
185185
_id: id,
@@ -197,7 +197,7 @@ describe('asset', () => {
197197
renderable: false
198198
});
199199

200-
const id = 'source/' + file.path;
200+
const id = join('source/', file.path);
201201
await process(file);
202202

203203
should.not.exist(Asset.findById(id));

test/scripts/processors/post.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('post', () => {
118118
});
119119

120120
await process(file);
121-
const id = 'source/' + file.path;
121+
const id = join('source/', file.path);
122122
should.not.exist(PostAsset.findById(id));
123123
});
124124

@@ -141,7 +141,7 @@ describe('post', () => {
141141
const postId = doc._id;
142142
await process(file);
143143

144-
const id = 'source/' + file.path;
144+
const id = join('source/', file.path);
145145
const asset = PostAsset.findById(id);
146146

147147
asset._id.should.eql(id);
@@ -165,7 +165,7 @@ describe('post', () => {
165165
renderable: false
166166
});
167167

168-
const id = 'source/' + file.path;
168+
const id = join('source/', file.path);
169169

170170
const post = await Post.insert({
171171
source: '_posts/foo.html',
@@ -200,7 +200,7 @@ describe('post', () => {
200200
renderable: false
201201
});
202202

203-
const id = 'source/' + file.path;
203+
const id = join('source/', file.path);
204204

205205
const post = await Post.insert({
206206
source: '_posts/foo.html',
@@ -235,7 +235,7 @@ describe('post', () => {
235235
renderable: false
236236
});
237237

238-
const id = 'source/' + file.path;
238+
const id = join('source/', file.path);
239239

240240
const post = await Post.insert({
241241
source: '_posts/foo.html',
@@ -265,7 +265,7 @@ describe('post', () => {
265265
renderable: false
266266
});
267267

268-
const id = 'source/' + file.path;
268+
const id = join('source/', file.path);
269269

270270
const post = await Post.insert({
271271
source: '_posts/foo.html',
@@ -290,7 +290,7 @@ describe('post', () => {
290290
renderable: false
291291
});
292292

293-
const id = 'source/' + file.path;
293+
const id = join('source/', file.path);
294294

295295
await writeFile(file.source, 'test');
296296
await process(file);
@@ -309,7 +309,7 @@ describe('post', () => {
309309
renderable: false
310310
});
311311

312-
const id = 'source/' + file.path;
312+
const id = join('source/', file.path);
313313

314314
await Promise.all([
315315
writeFile(file.source, 'test'),
@@ -966,7 +966,7 @@ describe('post', () => {
966966
'_fizz.jpg',
967967
'_buzz.jpg'
968968
].map(filename => {
969-
const id = `source/_posts/foo/${filename}`;
969+
const id = join('source/_posts/foo/', filename);
970970
const path = join(hexo.base_dir, id);
971971
const contents = filename.replace(/\.\w+$/, '');
972972
return {
@@ -1022,7 +1022,8 @@ describe('post', () => {
10221022
renderable: true
10231023
});
10241024

1025-
const assetId = 'source/_posts/foo/bar.jpg';
1025+
// join will replace backslashes on Windows
1026+
const assetId = join('source/_posts/foo/', 'bar.jpg');
10261027
const assetPath = join(hexo.base_dir, assetId);
10271028

10281029
await Promise.all([
@@ -1068,7 +1069,8 @@ describe('post', () => {
10681069
renderable: true
10691070
});
10701071

1071-
const assetId = 'source/_posts/foo/bar.jpg';
1072+
// join will replace backslashes on Windows
1073+
const assetId = join('source/_posts/foo/', 'bar.jpg');
10721074
const assetPath = join(hexo.base_dir, assetId);
10731075

10741076
await Promise.all([
@@ -1106,7 +1108,8 @@ describe('post', () => {
11061108
renderable: true
11071109
});
11081110

1109-
const assetId = 'source/_posts/foo/bar.jpg';
1111+
// join will replace backslashes on Windows
1112+
const assetId = join('source/_posts/foo/', 'bar.jpg');
11101113
const assetPath = join(hexo.base_dir, assetId);
11111114

11121115
await Promise.all([
@@ -1283,7 +1286,7 @@ describe('post', () => {
12831286
writeFile(assetFile.source, 'test')
12841287
]);
12851288
await process(file);
1286-
const id = 'source/' + assetFile.path;
1289+
const id = join('source/', assetFile.path);
12871290
const post = Post.findOne({ source: file.path });
12881291
PostAsset.findById(id).renderable.should.be.true;
12891292

@@ -1319,7 +1322,7 @@ describe('post', () => {
13191322
writeFile(assetFile.source, 'test')
13201323
]);
13211324
await process(file);
1322-
const id = 'source/' + assetFile.path;
1325+
const id = join('source/', assetFile.path);
13231326
const post = Post.findOne({ source: file.path });
13241327
PostAsset.findById(id).renderable.should.be.false;
13251328

test/scripts/theme_processors/source.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('source', () => {
5656
type: 'create'
5757
});
5858

59-
const id = 'themes/test/' + file.path;
59+
const id = join('themes/test/', file.path);
6060

6161
await writeFile(file.source, 'test');
6262
await process(file);
@@ -77,7 +77,7 @@ describe('source', () => {
7777
type: 'update'
7878
});
7979

80-
const id = 'themes/test/' + file.path;
80+
const id = join('themes/test/', file.path);
8181

8282
await Promise.all([
8383
writeFile(file.source, 'test'),
@@ -104,7 +104,7 @@ describe('source', () => {
104104
type: 'skip'
105105
});
106106

107-
const id = 'themes/test/' + file.path;
107+
const id = join('themes/test/', file.path);
108108

109109
await Promise.all([
110110
writeFile(file.source, 'test'),
@@ -130,7 +130,7 @@ describe('source', () => {
130130
type: 'delete'
131131
});
132132

133-
const id = 'themes/test/' + file.path;
133+
const id = join('themes/test/', file.path);
134134

135135
await Asset.insert({
136136
_id: id,
@@ -146,7 +146,7 @@ describe('source', () => {
146146
type: 'delete'
147147
});
148148

149-
const id = 'themes/test/' + file.path;
149+
const id = join('themes/test/', file.path);
150150

151151
await process(file);
152152
should.not.exist(Asset.findById(id));

0 commit comments

Comments
 (0)