Skip to content

Commit

Permalink
Fixes #135 and also removes some warnings that logged when they shoul…
Browse files Browse the repository at this point in the history
…dn't
  • Loading branch information
webketje committed Jan 20, 2023
1 parent c7a1436 commit c415e24
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 47 deletions.
86 changes: 40 additions & 46 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const dupeHandlers = {
return target
},
overwrite(targetPath, filesObj, filename, opts) {
return path.join(targetPath, opts.directoryIndex)
return path.join(targetPath || '', opts.directoryIndex)
}
}

Expand Down Expand Up @@ -77,24 +77,13 @@ const dupeHandlers = {

/** @type {Options} */
const defaultOptions = {
pattern: null,
date: 'YYYY/MM/DD',
slug: { lower: true },
relative: true,
indexFile: 'index.html',
trailingSlash: false,
unique: false,
duplicatesFail: false,
linksets: []
}

const defaultLinkset = {
date: 'YYYY/MM/DD',
slug: { lower: true },
relative: true,
isDefault: false
}

/**
* Maps the slugify function to slug to maintain compatibility
*
Expand All @@ -104,7 +93,7 @@ const defaultLinkset = {
* @return {String}
*/
function slugFn(options = defaultOptions.slug) {
return (text) => {
return function defaultSlugFn(text) {
if (typeof options.extend === 'object' && options.extend !== null) {
slugify.extend(options.extend)
}
Expand Down Expand Up @@ -146,15 +135,6 @@ const html = (str) => path.extname(str) === '.html'
*/
const format = (string) => (date) => moment(date).utc().format(string)

const normalizeLinkset = (linkset) => {
linkset = Object.assign({}, defaultLinkset, linkset)
linkset.date = format(linkset.date)
if (typeof linkset.slug !== 'function') {
linkset.slug = slugFn(linkset.slug)
}
return linkset
}

/**
* Normalize an options argument.
*
Expand All @@ -165,10 +145,7 @@ const normalizeOptions = (options) => {
if (typeof options === 'string') {
options = { pattern: options }
}
options = normalizeLinkset(Object.assign({}, defaultOptions, options))
if (Array.isArray(options.linksets)) {
options.linksets = options.linksets.map(normalizeLinkset)
}
options = Object.assign({}, defaultOptions, options)

if (!options.duplicates) {
if (options.duplicatesFail) {
Expand All @@ -184,10 +161,17 @@ const normalizeOptions = (options) => {
options.duplicates = dupeHandlers[options.duplicates]
}

if (options.indexFile && !options.directoryIndex) {
options.directoryIndex = options.indexFile
if (!options.directoryIndex) {
if (options.indexFile) {
options.directoryIndex = options.indexFile
} else {
options.directoryIndex = 'index.html'
}
}

options.slug = typeof options.slug === 'function' ? options.slug : slugFn(options.slug)
options.date = format(options.date)

return options
}

Expand Down Expand Up @@ -316,21 +300,16 @@ const replace = (pattern, data, options) => {
* @returns {import('metalsmith').Plugin}
*/
function permalinks(options) {
options = normalizeOptions(options)
const normalizedOptions = normalizeOptions(options)

const { linksets } = options
let defaultLinkset = linksets.find((ls) => {
return Boolean(ls.isDefault)
})

if (!defaultLinkset) {
defaultLinkset = options
let primaryLinkset = normalizedOptions.linksets.find((ls) => Boolean(ls.isDefault))
if (!primaryLinkset) {
primaryLinkset = normalizedOptions
}

const dupes = {}

const findLinkset = (file) => {
const set = linksets.find((ls) =>
const set = normalizedOptions.linksets.find((ls) =>
Object.keys(ls.match).some((key) => {
if (file[key] === ls.match[key]) {
return true
Expand All @@ -340,14 +319,15 @@ function permalinks(options) {
}
})
)
return set || defaultLinkset

return set ? set : primaryLinkset
}

return function permalinks(files, metalsmith, done) {
const debug = metalsmith.debug('@metalsmith/permalinks')
debug.info('Running with options: %O', options)
debug.info('Running with options: %O', normalizedOptions)

if (options.relative || options.linksets.find((ls) => ls.relative)) {
if (normalizedOptions.relative || normalizedOptions.linksets.find((ls) => ls.relative)) {
debug.warn(
'The relative option is deprecated and its default value will be changed to false before being removed in the next major versions.'
)
Expand All @@ -356,8 +336,8 @@ function permalinks(options) {
)
}

const makeUnique = options.duplicates
const map = new Map(Object.entries(options))
const makeUnique = normalizedOptions.duplicates
const map = new Map(Object.entries(normalizedOptions))
/* istanbul ignore next */
if (map.has('duplicatesFail') || map.has('unique')) {
debug.warn(
Expand All @@ -375,7 +355,21 @@ function permalinks(options) {

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

let ppath = replace(linkset.pattern, data, linkset) || resolve(file, options.directoryIndex)
const opts =
linkset === primaryLinkset
? primaryLinkset
: {
...linkset,
directoryIndex: normalizedOptions.directoryIndex,
slug:
typeof linkset.slug === 'function'
? linkset.slug
: typeof linkset.slug === 'object'
? slugFn(linkset.slug)
: normalizedOptions.slug,
date: typeof linkset.date === 'string' ? format(linkset.date) : normalizedOptions.date
}
let ppath = replace(linkset.pattern, data, opts) || resolve(file, normalizedOptions.directoryIndex)

let fam
switch (linkset.relative) {
Expand All @@ -394,7 +388,7 @@ function permalinks(options) {
ppath = data.permalink
}

const out = makeUnique(path.normalize(ppath), files, file, options)
const out = makeUnique(path.normalize(ppath), files, file, normalizedOptions)
if (out instanceof Error) {
return done(out)
}
Expand All @@ -413,7 +407,7 @@ function permalinks(options) {

// add to permalink data for use in links in templates
let permalink = ppath === '.' ? '' : ppath.replace(/\\/g, '/')
if (options.trailingSlash) {
if (normalizedOptions.trailingSlash) {
permalink = path.posix.join(permalink, './')
}
// contrary to the 2.x "path" property, the permalink property does not override previously set file metadata
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions test/fixtures/slug-custom-function-linksets/src/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: post1
collection: blog
---
18 changes: 17 additions & 1 deletion test/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ const fixtures = [
slug: transliteration.slugify
}
},
{
message: 'should use custom slug function for linksets too',
folder: 'slug-custom-function-linksets',
options: {
relative: false,
slug(str) {
return str + str.length
},
linksets: [
{
match: { collection: 'blog' },
pattern: 'blog/:title'
}
]
}
},
{
message: 'should accept options for slug module',
folder: 'slug-options',
Expand Down Expand Up @@ -247,7 +263,7 @@ describe('@metalsmith/permalinks', () => {
.env('DEBUG', process.env.DEBUG)
.use(permalinks(options))
.build((err) => {
if (err) done(err)
if (err) return done(err)
try {
equal(path.join(basePath, 'expected'), path.join(basePath, 'build'))
done()
Expand Down

0 comments on commit c415e24

Please sign in to comment.