我正在使用Metalsmith的JavaScript应用编程接口和metalsmith-collections生成一个静态站点。我有一个定制的构建脚本,它组装了一个数组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!')
})这里没有dogs的文件;它只是一个我自己创建的数组。如何指示metalsmith-collections从数组创建集合?
发布于 2019-03-07 03:24:18
我以前没有使用过metalsmith-collections,但是看一下文档here,它看起来像是用来收集文件集合的,而不是像您在这里试图做的那样获取一组数据。
您传递给collections()的options对象应该为您想要的每个集合都有一个键(例如dogs),并且每个键都应该是一个具有您想要的选项的对象:pattern,它是一个全局模式,用于挑选应该放入集合中的文件(似乎这可能是唯一必需的选项-其他选项似乎是可选的),sortBy,它是一个字符串,您可以根据它对这些文件进行排序,它似乎是从它们的元数据中拉出的,reverse是一个布尔值,您可以使用它来颠倒排序,还有metadata,limit,refer,以及那些文档中提到的其他一些人。
要将其应用于您的用例,我可能建议在与您在此处共享的配置文件相同的位置创建一个dogs/目录,然后将rover.md、dog.md和daisy.md放入dogs/目录中。那么你应该准备好做这样的事情:
// ...
.use(collections({
dogs: {
pattern: 'dogs/*.md'
}
}))那么dogs/目录中的那些Markdown (*.md)文件应该存在于您的dogs集合中。
https://stackoverflow.com/questions/55030390
复制相似问题