通过Javascript直接从数组创建metalsmith集合

通过Javascript直接从数组创建metalsmith集合,javascript,arrays,node.js,static-site,metalsmith,Javascript,Arrays,Node.js,Static Site,Metalsmith,我正在使用Metalsmith的JavaScript API和Metalsmith集合生成一个静态站点。我有一个定制的构建脚本,它组装了一个数组dogs,我想用它来创建一个新的集合 const Metalsmith = require('metalsmith') const collections = require('metalsmith-collections') const layouts = require('metalsmith-layouts'

我正在使用Metalsmith的JavaScript API和Metalsmith集合生成一个静态站点。我有一个定制的构建脚本,它组装了一个数组
dogs
,我想用它来创建一个新的集合

const Metalsmith       = require('metalsmith')
const collections      = require('metalsmith-collections')
const layouts          = require('metalsmith-layouts')

var dogs = [
  { name: 'Rover' },
  { name: 'Dog' },
  { name: 'Daisy' }
]

Metalsmith(__dirname)
  .metadata({})
  .source('./src')
  .destination('./build')
  .clean(true)
  .use(layouts())
  .use(collections({
    dogs: {
      // ?
    }
  })
  .build((error) => {
    if (error) throw error
    console.log('All done!')
  })

没有用于
狗的文件
;这只是我自己创建的一个数组。我如何指示
metalsmith collections
从数组中创建集合?

我以前没有使用过
metalsmith collections
,但查看文档,该工具似乎是用来收集文件集合的,而不是像您在这里尝试的那样获取数据数组

传递给
collections()
的选项对象应该为您想要的每个集合(例如
dogs
)都有一个键,并且这些键中的每一个都应该是一个带有您想要的选项的对象:
pattern
,这是一个全局模式,用于拾取应该进入集合的文件(似乎这可能是唯一必需的选项,而其他选项似乎是可选的),
sortBy
,这是一个字符串,您可以通过它对这些文件进行排序,它似乎是从它们的元数据中提取的,
reverse
,这是一个布尔值,您可以使用它来反转排序,以及
元数据
限制
引用
,以及这些文档中提到的其他一些内容

为了将这一点应用到您的用例中,我可能建议将
dogs/
目录放在与您在此处共享的配置文件相同的位置,然后将
rover.md
dog.md
daisy.md
放在
dogs/
目录中。然后您应该可以这样做:

  // ...
  .use(collections({
    dogs: {
      pattern: 'dogs/*.md'
    }
  }))
然后,
dogs/
目录中的那些标记(
*.md
)文件应该出现在您的
dogs
集合中

const Metalsmith       = require('metalsmith')
const collections      = require('metalsmith-collections')
const layouts          = require('metalsmith-layouts')

var dogs = [
  { name: 'Rover' },
  { name: 'Dog' },
  { name: 'Daisy' }
]

Metalsmith(__dirname)
  .metadata({})
  .source('./src')
  .destination('./build')
  .clean(true)
  .use(layouts())
  .use(collections({
    dogs: {
      // ?
    }
  })
  .build((error) => {
    if (error) throw error
    console.log('All done!')
  })