3

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 ?

Jerome WAGNER
  • 21,986
  • 8
  • 62
  • 77

4 Answers4

2

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
tusharmath
  • 10,622
  • 12
  • 56
  • 83
  • +1, I accepted the other answer for the different options, but thanks for the example code, it explains what Johan is talking about regarding paginator.coffee – Jerome WAGNER Sep 26 '13 at 17:13
2

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

Johan Nordberg
  • 356
  • 4
  • 8
1

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.

rapsli
  • 749
  • 1
  • 8
  • 23
0

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
MDCore
  • 17,583
  • 8
  • 43
  • 48