Skip to content

Formatting a date with a nested key #2

@jhyland87

Description

@jhyland87

Hello,

I have two dates that I'm trying to format, they're being pulled from a model (called timeline), which is structured like so:

[
	{
		"duration":{
			"begin": "2014-10-01",
			"end": "2015-02-25"
		}
	}
	// Repeat..
]

So, as you can see, the dates are nested inside the duration object. I use Handlebars templating to display the dates:

{{#each model.timeline}}
	<span>{{duration.begin}}</span>
	<span>{{duration.end}}</span>
{{/each}}

The problem I'm having, is the date-formatter doesn't seem to accept a key value of an object item. I've tried the following:

const dateFormatter = require('metalsmith-date-formatter')
const metalsmith = Metalsmith(__dirname)

metalsmith
    // .. cut..
    .use(dateFormatter({
        dates: [{
            key: 'duration.begin',
            format: 'MM YYYY'
        },{
            key: 'begin',
            format: 'MM YYYY'
        },{
            key: 'duration.end',
            format: 'MM YYYY'
        },{
            key: 'end',
            format: 'MM YYYY'
        }]
    }))
    // .. cut..
    .build(function(err, files) {
        if (err) throw err

        console.log('Build Completed')
    })

None of which work. If I move both the duration.begin and duration.end to the top level, and set the key value(s) to begin and end, then it works fine. But I need them to be values inside an object..

Is there a way around this? Am I just doing something wrong? I looked through the unit tests in the repo and I didn't see anything that would help out.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions