Metalsmith and Filenames, What can we do to improve them?

Author: leeolaCreated May 23, 2014Updated Jan 27, 2023
Labelsquestionexplainer

I just wanted to create a metalsmith-core discussion after we talked in https://github.com/segmentio/metalsmith-collections/pull/9. So, with that said..

The Problem

Filename mutations are hard to track, and hard to reference. The most common use case i run into is files linking to themselves, or other files. A file object found in the files object has no clue what it's own filename is. So templates that want to render that file, possibly with links to itself or other similar files, run into issues.

This problem becomes more prevalent with plugins like metalsmith-collections(). A collection is a list of files, and because these files are broken away from the files object, there is a large disconnect between the file data and the filename of that file.

The core of the issue is that an object, as it seems to me, is a poor storage format for a series of files. We can create plugins to store the filename within the file data, but when the key of the files inevitably mutates, there is no longer any validity to file.filename. You can set a spec, which requires updates to both to be changed in tandem, but then you have to wonder why even bother with the key at all?

Solution?

The two most obvious solutions are..

  • Store a filename value in each file's metadata, and have plugins update that when they change the filename. This is suboptimal for multiple reasons (mild incompatability with current plugins, error prone due to sync requirements, etc)
  • Another solution would be to replace the object with an array. While this would cleanly solve the syncing issue above, it also is a massive change to the core design to this library, and severely breaks all plugins.. i think it's a safe assumption that we're stuck with the file Object for now.

Any thoughts? Hopefully there is a sane solution i'm not thinking of.