如何使用温特史密斯静态站点生成器过滤掉内容节点



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

我只希望当它们的元数据.date是关于生成日期的过去时生成它们。

你怎么能和温特史密斯一起做到这一点?

你有几个选择。一个简单但黑客的方法是使用文件名模板并将文件名设置为类似 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.coffeehttps://github.com/jnordberg/wintersmith/blob/master/src/plugins/markdown.coffee

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

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

另一个黑客解决方案是_draft有一个子文件夹调用,你可以htaccess保护该文件夹中的所有内容。当您想推广它时,只需将其复制到正确的位置即可。

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

最新更新