Skip to content

Commit c128b00

Browse files
committed
Added support for excluding files from directory listing
1 parent 605b56d commit c128b00

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/index.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,41 @@ const findRelated = async (current, relativePath, stat, extension = '.html') =>
251251
return null;
252252
};
253253

254+
const canBeListed = (excluded, file) => {
255+
const slashed = slasher(file);
256+
let whether = true;
257+
258+
for (let mark = 0; mark < excluded.length; mark++) {
259+
const source = excluded[mark];
260+
261+
if (sourceMatches(source, slashed)) {
262+
whether = false;
263+
break;
264+
}
265+
}
266+
267+
return whether;
268+
};
269+
254270
const renderDirectory = async (current, relativePath, absolutePath, handlers, config) => {
255-
const {directoryListing, trailingSlash} = config;
271+
const {directoryListing, trailingSlash, unlisted = []} = config;
256272
const slashSuffix = typeof trailingSlash === 'boolean' ? (trailingSlash ? '/' : '') : '/';
257273

274+
const excluded = [
275+
'.DS_Store',
276+
'.git',
277+
...unlisted
278+
];
279+
258280
if (!applicable(relativePath, directoryListing, false)) {
259281
return null;
260282
}
261283

262284
let files = await handlers.readdir(absolutePath);
263285

264-
for (const file of files) {
286+
for (let index = 0; index < files.length; index++) {
287+
const file = files[index];
288+
265289
const filePath = path.resolve(absolutePath, file);
266290
const details = path.parse(filePath);
267291
const stats = await handlers.stat(filePath);
@@ -279,7 +303,12 @@ const renderDirectory = async (current, relativePath, absolutePath, handlers, co
279303
}
280304

281305
details.title = details.base;
282-
files[files.indexOf(file)] = details;
306+
307+
if (canBeListed(excluded, file)) {
308+
files[index] = details;
309+
} else {
310+
delete files[index];
311+
}
283312
}
284313

285314
const toRoot = path.relative(current, absolutePath);
@@ -308,7 +337,7 @@ const renderDirectory = async (current, relativePath, absolutePath, handlers, co
308337
}
309338

310339
return 0;
311-
});
340+
}).filter(Boolean);
312341

313342
// Add parent directory to the head of the sorted files array
314343
if (toRoot.length > 0) {

0 commit comments

Comments
 (0)