Skip to content

Commit 2abc1dd

Browse files
committed
Made directory listing adapt to trailingSlash config property
1 parent fc3366e commit 2abc1dd

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

src/directory.jst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@
5858
border-bottom: 1px dashed transparent;
5959
}
6060
61-
h1 a::after {
62-
content: '/';
63-
}
64-
6561
h1 a:hover {
6662
color: #7d7d7d;
6763
}

src/index.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ const findRelated = async (current, relativePath, stat, extension = '.html') =>
252252

253253
const renderDirectory = async (current, relativePath, absolutePath, handlers, config) => {
254254
const {directoryListing, trailingSlash} = config;
255-
const slashSuffix = trailingSlash === true ? '/' : '';
255+
const slashSuffix = typeof trailingSlash === 'boolean' ? (trailingSlash ? '/' : '') : '/';
256256

257257
if (!applicable(relativePath, directoryListing, false)) {
258258
return null;
@@ -281,8 +281,9 @@ const renderDirectory = async (current, relativePath, absolutePath, handlers, co
281281
files[files.indexOf(file)] = details;
282282
}
283283

284-
const directory = path.join(path.basename(current), relativePath, '/');
285-
const pathParts = directory.split(path.sep);
284+
const toRoot = path.relative(current, absolutePath);
285+
const directory = path.join(path.basename(current), toRoot, slashSuffix);
286+
const pathParts = directory.split(path.sep).filter(Boolean);
286287

287288
// Sort to list directories first, then sort alphabetically
288289
files = files.sort((a, b) => {
@@ -309,8 +310,6 @@ const renderDirectory = async (current, relativePath, absolutePath, handlers, co
309310
});
310311

311312
// Add parent directory to the head of the sorted files array
312-
const toRoot = path.relative(absolutePath, current);
313-
314313
if (toRoot.length > 0) {
315314
const directoryPath = [...pathParts].slice(1);
316315
const relative = path.join('/', ...directoryPath, '..', slashSuffix);
@@ -323,26 +322,23 @@ const renderDirectory = async (current, relativePath, absolutePath, handlers, co
323322
}
324323

325324
const paths = [];
326-
pathParts.pop();
327325

328-
for (const part in pathParts) {
329-
if (!{}.hasOwnProperty.call(pathParts, part)) {
330-
continue;
331-
}
326+
for (let index = 0; index < pathParts.length; index++) {
327+
const parents = [];
328+
const isLast = index === (pathParts.length - 1);
332329

333330
let before = 0;
334-
const parents = [];
335331

336-
while (before <= part) {
332+
while (before <= index) {
337333
parents.push(pathParts[before]);
338334
before++;
339335
}
340336

341337
parents.shift();
342338

343339
paths.push({
344-
name: pathParts[part],
345-
url: parents.join('/')
340+
name: pathParts[index] + (isLast ? slashSuffix : '/'),
341+
url: index === 0 ? '' : parents.join('/') + slashSuffix
346342
});
347343
}
348344

0 commit comments

Comments
 (0)