Skip to content

Commit

Permalink
Resolve file-specific permalinks in file.permalink, handle edge cases…
Browse files Browse the repository at this point in the history
… for root directoryIndex
  • Loading branch information
webketje committed Dec 28, 2024
1 parent 3d975e5 commit dc0a0ef
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,11 @@ function permalinks(options) {
}

const data = files[file]
const hasOwnPermalinkDeclaration = !!data.permalink
const fileSpecificPermalink = data.permalink
const hasOwnPermalinkDeclaration = !!fileSpecificPermalink
const linkset = findLinkset(data, file, metalsmith)
const permalinkTransformContext = { ...normalizedOptions, ...defaultLinkset, ...linkset }
if (hasOwnPermalinkDeclaration) permalinkTransformContext.pattern = data.permalink
if (hasOwnPermalinkDeclaration) permalinkTransformContext.pattern = fileSpecificPermalink

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

Expand Down Expand Up @@ -309,12 +310,12 @@ function permalinks(options) {
// files matched for permalinking that are already at their destination (/index.html) have an empty string permalink ('')
// normalize('') results in '.', which we don't want here
let permalink = ppath.length ? normalize(ppath) : ppath
if (permalink.length && normalizedOptions.trailingSlash) {
permalink = join(permalink, './')
}

// contrary to the 2.x "path" property, the permalink property does not override previously set file metadata
if (!hasOwnPermalinkDeclaration) {
// only rewrite data.permalink when a file-specific permalink contains :pattern placeholders
if (hasOwnPermalinkDeclaration) {
if (permalink !== fileSpecificPermalink) data.permalink = permalink
} else {
// only add trailingSlash when permalink !== ''
if (permalink && normalizedOptions.trailingSlash) permalink = join(permalink, './')
data.permalink = permalink
}

Expand Down

0 comments on commit dc0a0ef

Please sign in to comment.