metalsmith / collections

A Metalsmith plugin that groups files together into collections, which it adds to the global metadata.
MIT License
105 stars 66 forks source link

Multiple builds break the plugin #78

Closed rstacruz closed 2 years ago

rstacruz commented 7 years ago

The plugin isn't idempotent, running it twice will screw up the metadata.

app = metalsmith()
  .use(collections({ articles: { ... } })
  .build(done => ...)
  .build(done => ...)

The workaround is to create a middleware that clears up metadata:


app = metalsmith()
  .use((files, ms, done) => {
    ms.metadata({ articles: [] })
    done()
  })
  .use(collections({ articles: { ... } })
  .build(done => ...)
  .build(done => ...)
dwightjack commented 6 years ago

👍 On this.

I've spent some time on this issue. In my case (using collection: in frontmatter, collections gets populated automatically.

My solution is as follows:

app = metalsmith()
  .use((files, ms, done) => {
    const metadata = ms.metadata()
    const { collections } = metadata
    if (collections) {
      Object.keys(collections).forEach((k) => {
        metadata[k] = [];
      });
    }
    done()
  })
  .use(collections())
  .build(done => ...)
  .build(done => ...)
webketje commented 2 years ago

Closing as this is a duplicate of #27