Skip to content

Commit bcd0d0b

Browse files
committed
Closes #145: proper resubstitution of \0 chars, fix debug message
1 parent 98fe738 commit bcd0d0b

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

src/index.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,12 @@ const normalizeOptions = (options) => {
184184
* @param {Object} data
185185
*
186186
* @return {Mixed} String or Null
187+
*
188+
* @todo embed regexparam in this pkg: this method contains too many workarounds
189+
* to patch regexparam's lack of customization
187190
*/
188191
const replace = ({ pattern, ...options }, data) => {
189-
// regexparam has logic that interprets a dot as start of an extension name
192+
// regexparam.parse has logic that interprets a dot as start of an extension name
190193
// we don't want this here, so we replace it temporarily with a NUL char
191194
const remapped = pattern.replace(/\./g, '\0')
192195
const { keys } = route.parse(remapped)
@@ -213,8 +216,14 @@ const replace = ({ pattern, ...options }, data) => {
213216
}
214217

215218
let transformed = route.inject(remapped, ret)
219+
220+
// avoid index/index permalink
216221
if (path.basename(transformed) === path.basename(options.directoryIndex, path.extname(options.directoryIndex)))
217222
transformed = path.dirname(transformed)
223+
224+
// restore dots that were not part of a :pattern.key keypath (and have not been restored yet)
225+
transformed = transformed.replace(/\0/g, '.')
226+
218227
// handle absolute paths
219228
if (transformed.startsWith('/')) return transformed.slice(1)
220229
return transformed
@@ -274,7 +283,7 @@ function permalinks(options) {
274283
const permalinkTransformContext = { ...normalizedOptions, ...defaultLinkset, ...linkset }
275284
if (hasOwnPermalinkDeclaration) permalinkTransformContext.pattern = fileSpecificPermalink
276285

277-
debug('Applying pattern: "%s" to file: "%s"', linkset.pattern, file)
286+
debug('Applying pattern: "%s" to file: "%s"', permalinkTransformContext.pattern, file)
278287

279288
let ppath
280289

@@ -310,14 +319,10 @@ function permalinks(options) {
310319
// files matched for permalinking that are already at their destination (/index.html) have an empty string permalink ('')
311320
// normalize('') results in '.', which we don't want here
312321
let permalink = ppath.length ? normalize(ppath.replace(/\\/g, '/')) : ppath
313-
// only rewrite data.permalink when a file-specific permalink contains :pattern placeholders
314-
if (hasOwnPermalinkDeclaration) {
315-
if (permalink !== fileSpecificPermalink) data.permalink = permalink
316-
} else {
317-
// only add trailingSlash when permalink !== ''
318-
if (permalink && normalizedOptions.trailingSlash) permalink = join(permalink, './')
319-
data.permalink = permalink
320-
}
322+
323+
// only add trailingSlash when permalink !== '' (=directoryIndex) & not a fixed permalink
324+
if (permalink && normalizedOptions.trailingSlash && !hasOwnPermalinkDeclaration) permalink = join(permalink, './')
325+
data.permalink = permalink
321326

322327
delete files[file]
323328
files[out] = data
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: You Don't Need an Init System for Node.js in Docker
3+
date: 2021-03-20T19:43:00
4+
permalink: blog/you-don-t-need-an-init-system-for-node.js-in-docker
5+
---
6+
test

test/index.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,9 @@ describe('@metalsmith/permalinks', () => {
256256
// Tests comparing build output against expected files
257257
fixtures.forEach(({ message, options, folder }) => {
258258
const basePath = path.join(fixturesBase, folder)
259-
it(message, (done) => {
260-
Metalsmith(basePath)
261-
.env('DEBUG', process.env.DEBUG)
262-
.use(permalinks(options))
263-
.build((err) => {
264-
if (err) return done(err)
265-
try {
266-
equal(path.join(basePath, 'build'), path.join(basePath, 'expected'))
267-
done()
268-
} catch (err) {
269-
done(err)
270-
}
271-
})
259+
it(message, async () => {
260+
await Metalsmith(basePath).env('DEBUG', process.env.DEBUG).use(permalinks(options)).build()
261+
equal(path.join(basePath, 'build'), path.join(basePath, 'expected'))
272262
})
273263
})
274264

0 commit comments

Comments
 (0)