如何使用wintersmith静态站点生成器过滤出内容节点?

时间:2013-09-26 15:54:47

标签: wintersmith

在Wintersmith应用程序(node.js静态站点生成器)中,我有几个我想要预写的内容/文章。

我只希望在他们的metadata.date过去关于生成日期时生成它们。

你如何用Wintersmith做到这一点?

4 个答案:

答案 0 :(得分:2)

当然,您可以继续为此目的更改分页器文件 -

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

答案 1 :(得分:2)

你有几个选择。一个简单而又笨拙的方法是使用文件名模板并将文件名设置为draft.html,并在某些htaccess文件中忽略它。

元数据中的

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

另一种选择是创建一个根据您的条件填充树的生成器,查看https://github.com/jnordberg/wintersmith/blob/master/examples/blog/plugins/paginator.coffee作为示例。

或者您可以继承MarkdownPage插件,并使用您自己的自定义添加重新注册它,可能会添加draft属性并选中getView以将其发送到none是的。

class DraftyPage extends MarkdownPage

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

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

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

请参阅: https://github.com/jnordberg/wintersmith/blob/master/src/plugins/page.coffee https://github.com/jnordberg/wintersmith/blob/master/src/plugins/markdown.coffee

答案 2 :(得分:1)

另一个hackish解决方案是让一个子文件夹调用_draft,你可以htaccess保护该文件夹中的所有内容。如果要宣传它,只需将其复制到正确的位置即可。

答案 3 :(得分:0)

不是问题的确切答案,而是解决类似的问题。我希望能够在文章上设置草稿变量。解决方案类似于@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