What makes metalsmith so simple and powerful is the simplicity of the files object. The files object is the current state of the files being processed by metalsmith. At any time the files object can be output to the destination dir using only the object key for the file path and the file object's contents
property for the file contents.
There are cases where a key for the current file's path on each file object is nessisary. This has become essential for the metalsmith-navigation plugin I created.
The lifecycle of a metalsmith plugin should be roughly like this:
-
When inspecting file objects:
- Do so as if this was their initial and only state (how the current state was achieved should be irrelevant).
- The files object keys (the file path that will be output for that file) should be the only place this file's path is maintained.
-
When executing
- Modify metalsmith.metadata() and files objects how ever you like.
-
When done execution:
- Properties assigned to file objects or metalsmith.metadata() by the plugin:
- Not Intended to be used by other plugins:
- Should be removed.
- Modified files object keys (file paths) and
contents
property of file objects should be returned to a usable state.
- Intended to be used by other plugins:
- Should be documented and maintained consistently for backward compatibility.
- Should have namespaced and or configurable property names to avoid collision with other plugins.
- Not Intended to be used by other plugins:
- Data intended to reference specific files should not reference them by file path which can change. Reference files by their object.
- Doing this durring execution is fine, other plugins should not be expected to rely on data structured this way.
- The state of the files object should be such that it is ready to be output immediately without futher action or modification.
- Properties assigned to file objects or metalsmith.metadata() by the plugin:
-
Ideally if a plugin has behavior that breaks these rules it should be designed such that the behavior is opt in via configuration.
This readme is a cleaned up version of this comment metalsmith/metalsmith#58 (comment)