Question

In a Wintersmith application (node.js static site generator) I have several contents/articles that I want to pre-write.

I only want them to be generated when their metadata.date is in the past regarding to the date of generation.

How can you do this with Wintersmith ?

Was it helpful?

Solution

You have a couple of options. A simple yet hackish one would be to use filename templating and set the filename to something like draft.html and ignore it in some htaccess file.

in metadata: filename: "{{ (page.date>Date.now()) ? 'draft' : 'index' }}.html"

Another option would be to create a generator that populates a tree based on your conditions, check https://github.com/jnordberg/wintersmith/blob/master/examples/blog/plugins/paginator.coffee for a example.

Or you can subclass the MarkdownPage plugin and re-register it with your own custom additions, perhaps adding a draft property and a check in getView to send it to none if draft is true.

class DraftyPage extends MarkdownPage

  isDraft: -> @date > Date.now()

  getView: ->
    return 'none' if @isDraft()
    return super()

env.registerContentPlugin 'pages', '**/*.*(markdown|mkd|md)', DraftyPage

See: https://github.com/jnordberg/wintersmith/blob/master/src/plugins/page.coffee https://github.com/jnordberg/wintersmith/blob/master/src/plugins/markdown.coffee

OTHER TIPS

Sure, you can go ahead and change the paginator file for this purpose -

getArticles = (contents) ->
    # helper that returns a list of articles found in *contents*
    # note that each article is assumed to have its own directory in the articles directory
    articles = contents[options.articles]._.directories.map (item) -> item.index
    #Add the following lines of code 
    articles = articles.filter (article) -> article.metadata.date < new Date
    articles.sort (a, b) -> b.date - a.date
    return articles

another hackish solution would be to have a subfolder call _draft, you could htaccess protect everything in that folder. When you want to promote it, just copy it into the correct location.

Not an exact answer to the question but solving a similar problem. I wanted to be able to set a draft variable on articles. The solution is similar to @Tushar:

getArticles = (contents) ->
    # helper that returns a list of articles found in *contents*
    # note that each article is assumed to have its own directory in the articles directory
    articles = contents[options.articles]._.directories.map (item) -> item.index
    # Filter draft articles
    articles = articles.filter (article) -> typeof article.metadata.draft == 'undefined' or article.metadata.draft == false
    articles.sort (a, b) -> b.date - a.date
    return articles
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top