Skip to content

Commit dc0a0ef

Browse files
committed
Resolve file-specific permalinks in file.permalink, handle edge cases for root directoryIndex
1 parent 3d975e5 commit dc0a0ef

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,11 @@ function permalinks(options) {
268268
}
269269

270270
const data = files[file]
271-
const hasOwnPermalinkDeclaration = !!data.permalink
271+
const fileSpecificPermalink = data.permalink
272+
const hasOwnPermalinkDeclaration = !!fileSpecificPermalink
272273
const linkset = findLinkset(data, file, metalsmith)
273274
const permalinkTransformContext = { ...normalizedOptions, ...defaultLinkset, ...linkset }
274-
if (hasOwnPermalinkDeclaration) permalinkTransformContext.pattern = data.permalink
275+
if (hasOwnPermalinkDeclaration) permalinkTransformContext.pattern = fileSpecificPermalink
275276

276277
debug('Applying pattern: "%s" to file: "%s"', linkset.pattern, file)
277278

@@ -309,12 +310,12 @@ function permalinks(options) {
309310
// files matched for permalinking that are already at their destination (/index.html) have an empty string permalink ('')
310311
// normalize('') results in '.', which we don't want here
311312
let permalink = ppath.length ? normalize(ppath) : ppath
312-
if (permalink.length && normalizedOptions.trailingSlash) {
313-
permalink = join(permalink, './')
314-
}
315-
316-
// contrary to the 2.x "path" property, the permalink property does not override previously set file metadata
317-
if (!hasOwnPermalinkDeclaration) {
313+
// only rewrite data.permalink when a file-specific permalink contains :pattern placeholders
314+
if (hasOwnPermalinkDeclaration) {
315+
if (permalink !== fileSpecificPermalink) data.permalink = permalink
316+
} else {
317+
// only add trailingSlash when permalink !== ''
318+
if (permalink && normalizedOptions.trailingSlash) permalink = join(permalink, './')
318319
data.permalink = permalink
319320
}
320321

0 commit comments

Comments
 (0)