Skip to content

Commit

Permalink
fix: don't mistake dots in folder paths for extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Feb 15, 2022
1 parent 1a1d83f commit 0392d6f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ const debug = require('debug')('@metalsmith/in-place')
const isUtf8 = require('is-utf8')
const getTransformer = require('./get-transformer')

function parseFilepath(filename) {
const isNested = filename.includes(path.sep)
const dirname = isNested ? path.dirname(filename) : ''
const [base, ...extensions] = path.basename(filename).split('.')
return { dirname, base, extensions }
}

/**
* Engine, renders file contents with all available transformers
*/

function render({ filename, files, metalsmith, settings }) {
const [base, ...extensions] = filename.split('.')
const { dirname, base, extensions } = parseFilepath(filename)
const file = files[filename]
const engineOptions = Object.assign({}, settings.engineOptions)
const metadata = metalsmith.metadata()
Expand Down Expand Up @@ -51,7 +58,7 @@ function render({ filename, files, metalsmith, settings }) {
delete files[filename] // eslint-disable-line no-param-reassign

// Update files with the newly rendered file
const newName = [base, ...extensions].join('.')
const newName = [path.join(dirname, base), ...extensions].join('.')
files[newName] = file // eslint-disable-line no-param-reassign
files[newName].contents = Buffer.from(rendered.body) // eslint-disable-line no-param-reassign

Expand All @@ -77,9 +84,10 @@ function render({ filename, files, metalsmith, settings }) {

function validate({ filename, files }) {
debug(`validating ${filename}`)
const { extensions } = parseFilepath(filename)

// Files without an extension cannot be processed
if (!filename.includes('.')) {
if (!extensions.length) {
debug(`validation failed, ${filename} does not have an extension`)
return false
}
Expand All @@ -91,7 +99,7 @@ function validate({ filename, files }) {
}

// Files without an applicable jstransformer are ignored
const extension = filename.split('.').pop()
const extension = extensions[extensions.length - 1]
const transformer = getTransformer(extension)

if (!transformer) {
Expand Down Expand Up @@ -133,7 +141,7 @@ module.exports = (options) =>

const matchedFiles = metalsmith.match(settings.pattern)

// Filter files by validity
// Filter files by validity, pass basename to avoid dots in folder path
const validFiles = matchedFiles.filter((filename) => validate({ filename, files }))

// Let the user know when there are no files to process, usually caused by missing jstransformer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>The title</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>The title</p>
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ describe('@metalsmith/in-place', () => {
strictEqual(plugin().name, camelCased)
})

it('should support filepaths with dots in dirpaths', (done) => {
Metalsmith(fixture('dots-in-folderpath'))
.use(plugin())
.build((err) => {
if (err) done(err)
equal(fixture('dots-in-folderpath/build'), fixture('dots-in-folderpath/expected'))
done()
})
})

it('should process a single file', (done) => {
Metalsmith(fixture('single-file'))
.use(plugin())
Expand Down

0 comments on commit 0392d6f

Please sign in to comment.