{"id":20603357,"url":"https://github.com/metalsmith/default-values","last_synced_at":"2025-04-15T02:01:08.873Z","repository":{"id":40571045,"uuid":"50655749","full_name":"metalsmith/default-values","owner":"metalsmith","description":"Metalsmith Plugin for setting default values to file metadata","archived":false,"fork":false,"pushed_at":"2024-03-19T23:29:37.000Z","size":786,"stargazers_count":12,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-04-27T05:04:16.405Z","etag":null,"topics":["default-values","metalsmith","metalsmith-plugin","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/metalsmith.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-01-29T10:28:17.000Z","updated_at":"2024-06-21T03:52:19.495Z","dependencies_parsed_at":"2024-06-21T03:52:09.389Z","dependency_job_id":"a7211c8e-7115-4825-a82e-7b4943e76450","html_url":"https://github.com/metalsmith/default-values","commit_stats":{"total_commits":108,"total_committers":6,"mean_commits":18.0,"dds":0.537037037037037,"last_synced_commit":"6c363de5f777046a03c7432ac425fbf6af089d9f"},"previous_names":["woodyrew/metalsmith-default-values"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metalsmith%2Fdefault-values","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metalsmith%2Fdefault-values/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metalsmith%2Fdefault-values/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metalsmith%2Fdefault-values/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/metalsmith","download_url":"https://codeload.github.com/metalsmith/default-values/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248991546,"owners_count":21194894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["default-values","metalsmith","metalsmith-plugin","nodejs"],"created_at":"2024-11-16T09:17:02.802Z","updated_at":"2025-04-15T02:01:08.815Z","avatar_url":"https://github.com/metalsmith.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @metalsmith/default-values\n\nA Metalsmith plugin for setting default values to file metadata.\n\n[![metalsmith: core plugin][metalsmith-badge]][metalsmith-url]\n[![npm: version][npm-badge]][npm-url]\n[![ci: build][ci-badge]][ci-url]\n[![code coverage][codecov-badge]][codecov-url]\n[![license: LGPL-3.0][license-badge]][license-url]\n\n## Features\n\n- sets default values for metadata keys and file contents on files matched by pattern\n- does not overwrite or transform key values that are already defined, unless `strategy: 'overwrite'`.\n- can set computed defaults based on other file keys or metalsmith metadata\n\n## Installation\n\nNPM:\n\n```bash\nnpm install @metalsmith/default-values\n```\n\nYarn:\n\n```bash\nyarn add @metalsmith/default-values\n```\n\n## Usage:\n\nPass `@metalsmith/default-values` to `metalsmith.use`:\n\n```js\nimport defaultValues from '@metalsmith/default-values'\n\n// single defaults set for all HTML and markdown files\nmetalsmith.use({\n  defaults: {\n    pattern: '**/*.md',\n    title: 'Lorem ipsum'\n  }\n})\n\nmetalsmith.use(\n  defaultValues([\n    {\n      pattern: 'posts/*.md',\n      defaults: {\n        layout: 'post.hbs',\n        date: function (post) {\n          return post.stats.ctime\n        }\n      }\n    },\n    {\n      pattern: 'diary/*.md',\n      defaults: {\n        layout: 'diary.hbs',\n        private: true\n      }\n    },\n    {\n      pattern: ['diary/*.md', 'archive/**/*.md'],\n      defaults: {\n        no_index: true\n      }\n    },\n    {\n      pattern: '**/*.md',\n      defaults: {\n        layout: 'default.hbs'\n      }\n    }\n  ])\n)\n```\n\n### Options\n\n`@metalsmith/default-values` takes an array of defaults sets or a single defaults set. The defaults set has the following properties:\n\n- `pattern` (`string|string[]`): One or more glob patterns to match file paths. Defaults to `'**'` (all).\n- `defaults` (`Object\u003cstring, any\u003e`): An object whose key-value pairs will be added to file metadata. You can also specify a function `callback(file, metadata)` to set dynamic defaults based on existing file or global metadata.\n- `strategy` (`'keep'|'overwrite'`): Strategy to handle setting defaults to keys that are aleady defined.\n\n### Examples\n\n#### Setting defaults at a keypath\n\nYou can set a default at a file's nested keypath:\n\n```js\nmetalsmith.use(\n  defaultValues({\n    pattern: '**/*.md',\n    defaults: {\n      pubdate(file) { return new Date() }\n      'config.scripts.app': '/app.js'\n    }\n  })\n)\n```\n\n#### Setting default objects\n\nIf you assign a default object like this:\n\n```js\nmetalsmith.use(defaultValues({ defaults: { someObject: { id: 'some' } } }))\n```\n\nAll files to which the value was set will refer to the same object (`files['index.html'].someObject === files['other.html'].someObject`). If the object needs to be unique for each file, use a default setter function or specify each property as a keypath:\n\n```js\n// using a function\nmetalsmith.use(\n  defaultValues({\n    defaults: {\n      someObject: () =\u003e ({ id: 'some', other: true })\n    }\n  })\n)\n\n// using keypaths\nmetalsmith.use(\n  defaultValues({\n    defaults: {\n      'someObject.id': 'some',\n      'someObject.other': true\n    }\n  })\n)\n```\n\n#### Setting default contents\n\nYou can set a file's default contents (which is a Node buffer) and any other Buffer properties:\n\n```js\nmetalsmith.use(\n  defaultValues({\n    pattern: '**/*.md',\n    defaults: {\n      strategy: 'overwrite',\n      contents: Buffer.from('TO DO')\n    }\n  })\n)\n```\n\nWhen using a JSON config, a string can be used as default and it will automatically be transformed into a buffer.\n\n#### Setting dynamic defaults\n\nYou can set dynamic defaults based on current file metadata or metalsmith metadata:\n\n```js\nmetalsmith\n  .metadata({\n    build: { timestamp: Date.now() }\n  })\n  .use(\n    defaultValues([\n      {\n        strategy: 'overwrite',\n        defaults: {\n          buildInfo(file, metadata) {\n            return metadata.build\n          },\n          excerpt(file) {\n            return file.contents.toString().slice(0, 200)\n          }\n        }\n      }\n    ])\n  )\n```\n\n#### Combining with other plugins\n\n@metalsmith/default-values works great with other `@metalsmith` plugins. The example below attaches a collection and layout matching the parent directory for all files in the directories `services`,`products`, and `articles`:\n\n```js\nimport slugify from 'slugify'\nconst contentTypes = ['product', 'service', 'article']\n\nmetalsmith\n  .use(\n    defaultValues(\n      contentTypes.map((contentType) =\u003e ({\n        pattern: `${contentType}s/*.md`, // pluralized\n        defaults: {\n          collection: `${contentType}s`, // pluralized\n          bodyClass: contentType,\n          layout: `${contentType}.njk`, // using jstransformer-nunjucks\n          contentLength(file) {\n            if (file.contents) return file.contents.toString().length\n            return 0\n          }\n        }\n      }))\n    )\n  )\n  .use(markdown()) // @metalsmith/markdown\n  .use(collections()) // @metalsmith/collections\n  .use(\n    layouts({\n      // @metalsmith/layouts\n      pattern: '**/*.html'\n    })\n  )\n```\n\n### Debug\n\nTo enable debug logs, set the `DEBUG` environment variable to `@metalsmith/default-values*`:\n\n```js\nmetalsmith.env('DEBUG', '@metalsmith/default-values*')\n```\n\nAlternatively you can set `DEBUG` to `@metalsmith/*` to debug all Metalsmith core plugins.\n\n### CLI usage\n\nTo use this plugin with the Metalsmith CLI, add `@metalsmith/default-values` to the `plugins` key in your `metalsmith.json` file:\n\n```json\n{\n  \"plugins\": [\n    {\n      \"@metalsmith/default-values\": [\n        {\n          \"pattern\": \"diary/*.md\",\n          \"defaults\": {\n            \"layout\": \"diary.hbs\",\n            \"private\": true\n          }\n        }\n      ]\n    }\n  ]\n}\n```\n\n## License\n\n[LGPL-3.0 or later](LICENSE)\n\n[npm-badge]: https://img.shields.io/npm/v/@metalsmith/default-values.svg\n[npm-url]: https://www.npmjs.com/package/@metalsmith/default-values\n[ci-badge]: https://github.com/metalsmith/default-values/actions/workflows/test.yml/badge.svg\n[ci-url]: https://github.com/metalsmith/default-values/actions/workflows/test.yml\n[metalsmith-badge]: https://img.shields.io/badge/metalsmith-core_plugin-green.svg?longCache=true\n[metalsmith-url]: https://metalsmith.io\n[codecov-badge]: https://img.shields.io/coveralls/github/metalsmith/default-values\n[codecov-url]: https://coveralls.io/github/metalsmith/default-values\n[license-badge]: https://img.shields.io/github/license/metalsmith/default-values\n[license-url]: LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetalsmith%2Fdefault-values","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetalsmith%2Fdefault-values","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetalsmith%2Fdefault-values/lists"}