Skip to content

Commit

Permalink
Drops support for Metalsmith < 2.5.0, replaces debug with metalsmith.…
Browse files Browse the repository at this point in the history
…debug
  • Loading branch information
webketje committed Nov 13, 2022
1 parent 6354c95 commit 7e1c0dc
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 65 deletions.
29 changes: 16 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"test": "nyc mocha"
},
"dependencies": {
"debug": "^4.3.4",
"moment": "^2.29.1",
"regexparam": "^2.0.1",
"slugify": "^1.6.5"
Expand All @@ -58,7 +57,7 @@
"eslint-config-recommended": "^4.1.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.5.1",
"metalsmith": "^2.5.0",
"metalsmith": "^2.5.1",
"microbundle": "^0.15.1",
"mocha": "^9.2.2",
"nodemon": "^2.0.19",
Expand All @@ -68,7 +67,7 @@
"transliteration": "^2.3.5"
},
"peerDependencies": {
"metalsmith": "^2.3.0"
"metalsmith": "^2.5.0"
},
"files": [
"lib",
Expand Down
96 changes: 47 additions & 49 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import path from 'path'
import createDebug from 'debug'
import moment from 'moment'
import slugify from 'slugify'
import route from 'regexparam'

const debug = createDebug('@metalsmith/permalinks')
const error = debug.extend('error')

/**
* [Slugify options](https://github.com/simov/slugify#options)
*
Expand Down Expand Up @@ -280,6 +276,9 @@ function permalinks(options) {
}

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

setImmediate(done)

const defaultUniquePath = (targetPath, filesObj, filename, opts) => {
Expand All @@ -290,7 +289,7 @@ function permalinks(options) {
do {
target = path.join(`${targetPath}${postfix}`, indexFile || 'index.html')
if (options.duplicatesFail && filesObj[target]) {
error(`Target: ${target} already has a file assigned`)
debug.error(`Target: ${target} already has a file assigned`)
return done(`Permalinks: Clash with another target file ${target}`)
}

Expand All @@ -302,57 +301,56 @@ function permalinks(options) {

const makeUnique = typeof options.unique === 'function' ? options.unique : defaultUniquePath

Object.keys(files).forEach((file) => {
const data = files[file]
debug('checking file: %s', file)

if (!html(file)) return
if (data.permalink === false) return

const linkset = Object.assign({}, defaultLinkset, findLinkset(data))
debug('applying pattern: %s to file: %s', linkset.pattern, file)

let ppath = replace(linkset.pattern, data, linkset) || resolve(file)

let fam
switch (linkset.relative) {
case true:
fam = family(file, files)
break
case 'folder':
fam = folder(file, files)
break
default:
// nothing
}

// Override the path with `permalink` option
if (Object.prototype.hasOwnProperty.call(data, 'permalink') && data.permalink !== false) {
ppath = data.permalink
}
Object.keys(files)
.filter((file) => html(file) && files[file].permalink !== false)
.forEach((file) => {
const data = files[file]
debug('checking file: %s', file)

const linkset = Object.assign({}, defaultLinkset, findLinkset(data))
debug('applying pattern: %s to file: %s', linkset.pattern, file)

let ppath = replace(linkset.pattern, data, linkset) || resolve(file)

let fam
switch (linkset.relative) {
case true:
fam = family(file, files)
break
case 'folder':
fam = folder(file, files)
break
default:
// nothing
}

const out = makeUnique(ppath, files, file, options)
// Override the path with `permalink` option
if (Object.prototype.hasOwnProperty.call(data, 'permalink') && data.permalink !== false) {
ppath = data.permalink
}

// track duplicates for relative files to maintain references
const moved = {}
if (fam) {
for (const key in fam) {
if (Object.prototype.hasOwnProperty.call(fam, key)) {
const rel = path.posix.join(ppath, key)
dupes[rel] = fam[key]
moved[key] = rel
const out = makeUnique(ppath, files, file, options)

// track duplicates for relative files to maintain references
const moved = {}
if (fam) {
for (const key in fam) {
if (Object.prototype.hasOwnProperty.call(fam, key)) {
const rel = path.posix.join(ppath, key)
dupes[rel] = fam[key]
moved[key] = rel
}
}
}
}

// add to path data for use in links in templates
data.path = ppath === '.' ? '' : ppath.replace(/\\/g, '/')
// add to path data for use in links in templates
data.path = ppath === '.' ? '' : ppath.replace(/\\/g, '/')

relink(data, moved)
relink(data, moved)

delete files[file]
files[out] = data
})
delete files[file]
files[out] = data
})

// add duplicates for relative files after processing to avoid double-dipping
// note: `dupes` will be empty if `options.relative` is false
Expand Down

0 comments on commit 7e1c0dc

Please sign in to comment.