Skip to content

Commit 6bbb661

Browse files
Revert "refactor: backslashes on Windows (#5457)"
This reverts commit 80dafe2.
1 parent cc8c520 commit 6bbb661

File tree

8 files changed

+41
-46
lines changed

8 files changed

+41
-46
lines changed

lib/box/index.ts

Lines changed: 7 additions & 8 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-
src.substring(ctx.base_dir.length),
116+
escapeBackslash(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 = base.substring(ctx.base_dir.length);
132+
const relativeBase = escapeBackslash(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,12 +156,11 @@ class Box extends EventEmitter {
156156
});
157157

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

163162
const file = new File({
164-
// source is used for file system path, keep backslashes on Windows
163+
// source is used for filesystem path, keep backslashes on Windows
165164
source: join(base, path),
166165
// path is used for URL path, replace backslashes on Windows
167166
path: escapeBackslash(path),
@@ -195,7 +194,7 @@ class Box extends EventEmitter {
195194
const { base } = this;
196195

197196
function getPath(path) {
198-
return path.substring(base.length);
197+
return escapeBackslash(path.substring(base.length));
199198
}
200199

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

216215
watcher.on('addDir', path => {
217216
let prefix = getPath(path);
218-
if (prefix) prefix += sep;
217+
if (prefix) prefix += '/';
219218

220219
this._readDir(path, prefix);
221220
});
@@ -288,7 +287,7 @@ function readDirWalker(ctx: Hexo, base: string, results: any[], ignore: any, pre
288287
const prefixPath = `${prefix}${path}`;
289288
if (stats) {
290289
if (stats.isDirectory()) {
291-
return readDirWalker(ctx, fullpath, results, ignore, prefixPath + sep);
290+
return readDirWalker(ctx, fullpath, results, ignore, `${prefixPath}/`);
292291
}
293292
if (!isIgnoreMatch(fullpath, ignore)) {
294293
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);
112+
const id = relative(ctx.base_dir, file.source).replace(/\\/g, '/');
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
@@ -234,7 +234,7 @@ function scanAssetDir(ctx: Hexo, post) {
234234
if (err && err.code === 'ENOENT') return [];
235235
throw err;
236236
}).filter(item => !isExcludedFile(item, ctx.config)).map(item => {
237-
const id = join(assetDir, item).substring(baseDirLength);
237+
const id = join(assetDir, item).substring(baseDirLength).replace(/\\/g, '/');
238238
const renderablePath = id.substring(sourceDirLength + 1);
239239
const asset = PostAsset.findById(id);
240240

@@ -268,7 +268,7 @@ function shouldSkipAsset(ctx: Hexo, post, asset) {
268268
function processAsset(ctx: Hexo, file: _File) {
269269
const PostAsset = ctx.model('PostAsset');
270270
const Post = ctx.model('Post');
271-
const id = file.source.substring(ctx.base_dir.length);
271+
const id = file.source.substring(ctx.base_dir.length).replace(/\\/g, '/');
272272
const postAsset = PostAsset.findById(id);
273273

274274
if (file.type === 'delete' || Post.length === 0) {

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);
7+
const id = file.source.substring(this.base_dir.length).replace(/\\/g, '/');
88
const { path } = file.params;
99
const doc = Asset.findById(id);
1010

test/scripts/box/box.ts

Lines changed: 5 additions & 6 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 = join('test/', name);
123+
const cacheId = '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 = join('test/', name);
147+
const cacheId = '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 = join('test/', name);
170+
const cacheId = '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 = join('test/', name);
193+
const cacheId = 'test/' + name;
194194

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

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

217216
const processor = spy();
218217
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 = join('source/', file.path);
83+
const id = '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 = 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.
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.
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 = join('source/', file.path);
125+
const id = '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 = join('source/', file.path);
156+
const id = '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 = join('source/', file.path);
182+
const id = '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 = join('source/', file.path);
200+
const id = 'source/' + file.path;
201201
await process(file);
202202

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

test/scripts/processors/post.ts

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

120120
await process(file);
121-
const id = join('source/', file.path);
121+
const id = '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 = join('source/', file.path);
144+
const id = '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 = join('source/', file.path);
168+
const id = '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 = join('source/', file.path);
203+
const id = '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 = join('source/', file.path);
238+
const id = '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 = join('source/', file.path);
268+
const id = '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 = join('source/', file.path);
293+
const id = '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 = join('source/', file.path);
312+
const id = '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 = join('source/_posts/foo/', filename);
969+
const id = `source/_posts/foo/${filename}`;
970970
const path = join(hexo.base_dir, id);
971971
const contents = filename.replace(/\.\w+$/, '');
972972
return {
@@ -1022,8 +1022,7 @@ describe('post', () => {
10221022
renderable: true
10231023
});
10241024

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

10291028
await Promise.all([
@@ -1069,8 +1068,7 @@ describe('post', () => {
10691068
renderable: true
10701069
});
10711070

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

10761074
await Promise.all([
@@ -1108,8 +1106,7 @@ describe('post', () => {
11081106
renderable: true
11091107
});
11101108

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

11151112
await Promise.all([
@@ -1286,7 +1283,7 @@ describe('post', () => {
12861283
writeFile(assetFile.source, 'test')
12871284
]);
12881285
await process(file);
1289-
const id = join('source/', assetFile.path);
1286+
const id = 'source/' + assetFile.path;
12901287
const post = Post.findOne({ source: file.path });
12911288
PostAsset.findById(id).renderable.should.be.true;
12921289

@@ -1322,7 +1319,7 @@ describe('post', () => {
13221319
writeFile(assetFile.source, 'test')
13231320
]);
13241321
await process(file);
1325-
const id = join('source/', assetFile.path);
1322+
const id = 'source/' + assetFile.path;
13261323
const post = Post.findOne({ source: file.path });
13271324
PostAsset.findById(id).renderable.should.be.false;
13281325

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 = join('themes/test/', file.path);
59+
const id = '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 = join('themes/test/', file.path);
80+
const id = '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 = join('themes/test/', file.path);
107+
const id = '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 = join('themes/test/', file.path);
133+
const id = '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 = join('themes/test/', file.path);
149+
const id = 'themes/test/' + file.path;
150150

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

0 commit comments

Comments
 (0)