-
Notifications
You must be signed in to change notification settings - Fork 627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal for metalsmith.get/set() helpers #385
Comments
Upon rethinking this, the better signature for get/set is an extension of a combo of Example: /** @returns {void} */
// for all files matched set type 'post' to the metadata
metalsmith.set('posts/*.html', 'type', 'post')
/** @returns {Array<*>} */
// for all files matched get the type
metalsmith.get('{posts,services,products}/*.html', 'type') Signatures are:
The result returned is always similar to the Metalsmith.Files object. metalsmith set "posts/*.html" type post
metalsmith get "{posts,services,products}/*.html" type |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context
It is a frequent requirement for plugins to access and manipulate properties on metalsmith metadata or files. Retrieving and setting these properties is not always worry-free. For example, if a plugin wishes to allow users to specify plugin options to be read from a YAML file inside
metalsmith.source()
, let's say@metalsmith/layouts
:it needs to check if the file is found, and do null-safe gets to nested properties
This could be shortened to:
Another need (or inflexibility) is that a lot of plugins "dictate" the key path in the file metadata they read config options from.
For example using
@metalsmith/layouts
you must specify the keylayout:
in metadata. This will be ok for some (if not most) users, but other users may prefer to introduce different namespaces in front-matter (eg. translations, config, seo, content). So if the user would prefer reading the layout value fromconfig.layout
they should have a way to do that, without putting the onus of implementing the mechanism on the plugin developer. Building on the@metalsmith/layouts
example, this would allowSpec
get(context, keypath)
andset(context, keypath, value)
const { get, set } = metalsmith
.files
, so that it can be omitted andget('blog/index.md')
andset('blog/index.md', { contents: Buffer.from('overwritten') })
operate onfiles
.files
, the methods could accept globs and work as shortcuts for resp. (get)metalsmith.match(glob).map(path => files[path])
, always returning an array, and (set)metalsmith.match(glob).forEach(path => { files[path][keypath] = value })
Pros
metalsmith.get(files, 'nested/index.md')
.Signature
The text was updated successfully, but these errors were encountered: