Skip to content

Commit

Permalink
Resolves #75, adds support for dot.notated.pattern.parts
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Nov 14, 2023
1 parent a1d2a2f commit 67f2432
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"test": "c8 mocha"
},
"dependencies": {
"dlv": "^1.1.3",
"regexparam": "^2.0.1",
"slugify": "^1.6.6"
},
Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import { dateFormatter as format } from './date.js'
import slugify from 'slugify'
import * as route from 'regexparam'
import get from 'dlv'

const dupeHandlers = {
error(targetPath, filesObj, filename, opts) {
Expand Down Expand Up @@ -202,12 +203,15 @@ const resolve = (str, directoryIndex = 'index.html') => {
*/
const replace = (pattern, data, options) => {
if (!pattern) return null
const { keys } = route.parse(pattern)
// regexparam has logic that interprets a dot as start of an extension name
// we don't want this here, so we replace it temporarily with a NUL char
const remapped = pattern.replace(/\./g, '\0')
const { keys } = route.parse(remapped)
const ret = {}

for (let i = 0, key; (key = keys[i++]); ) {
const val = data[key]
const isOptional = pattern.match(`${key}\\?`)
const val = get(data, key.replace(/\0/g, '.'))
const isOptional = remapped.match(`${key}\\?`)
if (!val || (Array.isArray(val) && val.length === 0)) {
if (isOptional) {
ret[key] = ''
Expand All @@ -222,7 +226,7 @@ const replace = (pattern, data, options) => {
}
}

const transformed = route.inject(pattern, ret)
const transformed = route.inject(remapped, ret)

// handle absolute paths
if (transformed.startsWith('/')) return transformed.slice(1)
Expand Down
Empty file.
7 changes: 7 additions & 0 deletions test/fixtures/dot-notated-pattern-parts/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: hello
nested:
object:
property:
patternpart: world
---
5 changes: 5 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const fixtures = [
folder: 'permalink-invalid-chars',
options: ':title'
},
{
message: 'should support dot-notated properties',
folder: 'dot-notated-pattern-parts',
options: ':title/:nested.object.property.patternpart'
},
{
message: 'should accept a shorthand string',
folder: 'shorthand',
Expand Down

0 comments on commit 67f2432

Please sign in to comment.