Skip to content

Commit

Permalink
BREAKING: Resolves #19, throw an error when the permalink resolves wi…
Browse files Browse the repository at this point in the history
…th invalid filepath chars
  • Loading branch information
webketje committed Oct 2, 2023
1 parent d936fb8 commit 058c61e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ function permalinks(options) {
}
let ppath = replace(linkset.pattern, data, opts) || resolve(file, normalizedOptions.directoryIndex)

// invalid on Windows, but best practice not to use them anyway
const invalidFilepathChars = /\||:|<|>|\*|\?|"/
if (invalidFilepathChars.test(ppath)) {
const msg = `Filepath "${file}" contains invalid filepath characters (one of :|<>"*?) after resolving as linkset pattern "${linkset.pattern}"`
debug.error(msg)
done(new Error(msg))
}

// Override the path with `permalink` option
if (Object.prototype.hasOwnProperty.call(data, 'permalink') && data.permalink !== false) {
ppath = data.permalink
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions test/fixtures/invalid-filename-chars/src/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: ":::|||"
---
16 changes: 16 additions & 0 deletions test/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ describe('@metalsmith/permalinks', () => {
})
})

// this test was probably added to address an oddity in regexparam library path handling related to having a leading slash
it('should use the resolve path for false values (not root)', (done) => {
Metalsmith(path.join(fixturesBase, 'falsy'))
.use(permalinks(':falsy/:title'))
Expand All @@ -272,6 +273,21 @@ describe('@metalsmith/permalinks', () => {
})
})

// better to error the build and make users aware of an anti-pattern,
// than silently strip/replace the invalid chars and let them follow bad practice
it('should error when encountering invalid filepath characters after permalink pattern resolution', done => {
Metalsmith(path.join(fixturesBase, 'invalid-filename-chars'))
.use(permalinks(':title'))
.build((err) => {
try {
assert.strictEqual(err.message, 'Filepath "post.html" contains invalid filepath characters (one of :|<>"*?) after resolving as linkset pattern ":title"')
done()
} catch (err) {
done(err)
}
})
})

it('should allow an alternative directoryIndex', (done) => {
const basepath = path.join(fixturesBase, 'custom-indexfile')
Metalsmith(basepath)
Expand Down

0 comments on commit 058c61e

Please sign in to comment.