{"id":20603364,"url":"https://github.com/metalsmith/layouts","last_synced_at":"2025-04-05T05:07:57.553Z","repository":{"id":22834653,"uuid":"26181795","full_name":"metalsmith/layouts","owner":"metalsmith","description":"A metalsmith plugin for layouts","archived":false,"fork":false,"pushed_at":"2024-02-15T00:13:48.000Z","size":1952,"stargazers_count":115,"open_issues_count":3,"forks_count":49,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-14T23:00:21.878Z","etag":null,"topics":["layouts","metalsmith","metalsmith-plugin","nodejs","templates"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/metalsmith.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","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":"2014-11-04T18:11:26.000Z","updated_at":"2024-06-18T15:15:54.760Z","dependencies_parsed_at":"2022-08-17T17:00:43.571Z","dependency_job_id":"25c16a1b-1116-4b82-892b-761a738a43dc","html_url":"https://github.com/metalsmith/layouts","commit_stats":{"total_commits":174,"total_committers":21,"mean_commits":8.285714285714286,"dds":"0.45402298850574707","last_synced_commit":"bcc0342d6c6d437599e6b1a6eac43d5ded91c8ba"},"previous_names":["metalsmith/metalsmith-layouts","superwolff/metalsmith-layouts"],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metalsmith%2Flayouts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metalsmith%2Flayouts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metalsmith%2Flayouts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metalsmith%2Flayouts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/metalsmith","download_url":"https://codeload.github.com/metalsmith/layouts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247289428,"owners_count":20914464,"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":["layouts","metalsmith","metalsmith-plugin","nodejs","templates"],"created_at":"2024-11-16T09:17:04.451Z","updated_at":"2025-04-05T05:07:57.528Z","avatar_url":"https://github.com/metalsmith.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @metalsmith/layouts\n\nA metalsmith plugin for layouts\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: MIT][license-badge]][license-url]\n\n## Features\n\n- wraps source files' `contents` field in a layout rendered with a [Jstransformer templating engine](https://github.com/jstransformers/jstransformer)\n- alters file extensions from `transform.inputFormats` to `transform.outputFormat`\n- can be used multiple times with different configs per metalsmith pipeline\n\n## Installation\n\nNPM:\n\n```bash\nnpm install @metalsmith/layouts jstransformer-handlebars\n```\n\nYarn:\n\n```bash\nyarn add @metalsmith/layouts jstransformer-handlebars\n\n```\n\nThis plugin works with [jstransformers](https://github.com/jstransformers/jstransformer) but they should be installed separately. `jstransformer-handlebars` is just an example, you could use any transformer. To render markdown you could install [jstransformer-marked](https://github.com/jstransformers/jstransformer-marked). To render handlebars you would install [jstransformer-handlebars](https://github.com/jstransformers/jstransformer-handlebars). Other popular templating options include: [Nunjucks](https://github.com/jstransformers/jstransformer-nunjucks), [Twig](https://github.com/jstransformers/jstransformer-twig), [Pug](https://github.com/jstransformers/jstransformer-pug), or [EJS](https://github.com/jstransformers/jstransformer-ejs). See also [this map](https://github.com/jstransformers/inputformat-to-jstransformer/blob/master/dictionary.json) to see which extensions map to which jstransformer.\n\n## Usage\n\nPass `@metalsmith/layouts` to `metalsmith.use` :\n\n```js\nimport layouts from '@metalsmith/layouts'\n\n// shorthand\nmetalsmith.use(layouts({ transform: 'nunjucks' }))\n\n// same as shorthand\nmetalsmith.use(\n  layouts({\n    directory: 'layouts' // === path.join(metalsmith.directory(), 'layouts')\n    transform: jsTransformerNunjucks, // resolved\n    extname: '.html',\n    pattern: '**/*.{njk,nunjucks}*',\n    engineOptions: {}\n  })\n)\n```\n\nIn the transformed file, you have access to `{ ...metalsmith.metadata(), ...fileMetadata }`, so that the following build\n\n```js\nmetalsmith\n  .metadata({ title: 'Default title', nodeVersion: process.version })\n  .use(layouts({ transform: 'handlebars' }))\n```\n\nfor a file:\n\n```yml\n---\ntitle: Article title\nlayout: default.hbs\n---\n```\n\nwith layout:\n\n```hbs\n\u003ch1\u003e{{title}}\u003c/h1\u003eNode v{{nodeVersion}}\n```\n\nwould render `\u003ch1\u003eArticle title\u003c/h1\u003eNode v16.20`.\n\n### Options\n\nIn most cases, you will only need to specify the `transform`, `default`, and `engineOptions` option.\n\n- transform (`string|JsTransformer`): **required**. Which transformer to use. The full name of the transformer, e.g. `jstransformer-handlebars`, its shorthand `handlebars`, a relative JS module path starting with `.`, e.g. `./my-transformer.js`, whose default export is a jstransformer or an actual jstransformer: an object with `name`, `inputFormats`,`outputFormat`, and at least one of the render methods `render`, `renderAsync`, `compile` or `compileAsync` described in the [jstransformer API docs](https://github.com/jstransformers/jstransformer#api)\n- [extname](#extension-handling) (`string|false|null`): optional. How to transform a file's extensions: `''|false|null` to remove the last `transform.inputFormat` matching extension, `.\u003cext\u003e` to force an extension rename.\n- [engineOptions](#engineoptions) (`Object\u003cstring, any\u003e`): optional. Pass options to the jstransformer that's rendering the files. The default is `{}`.\n- pattern (`string|string[]`): optional. Override default glob pattern matching `**/*.\u003ctransform.inputFormats\u003e*`. Useful to limit the scope of the transform by path or glob to a subfolder, or to include files not matching `transform.inputFormats`.\n- default (`string`): optional. The default layout to apply to files matched with `pattern`. If none is given, files matched without defined layout will be skipped. Files whose `layout` is set to `false` will also be skipped.\n- directory (`string`): optional. The directory for the layouts (relative to `metalsmith.directory()`, not `metalsmith.source()`!). Defaults to `layouts`.\n\n#### directory\n\nThe directory path is resolved **relative to** `Metalsmith#directory`, not `Metalsmith#source`.\nIf you prefer having the layouts directory _inside_ the Metalsmith source folder, it is advisable to use `Metalsmith#ignore` to avoid loading the layouts twice (once via Metalsmith and once via the JSTransformer):\n\n```js\nimport layouts from '@metalsmith/layouts'\n\nmetalsmith.ignore('layouts').use(\n  layouts({\n    directory: 'src/layouts'\n  })\n)\n```\n\n#### `engineOptions`\n\nUse `engineOptions` to pass options to the jstransformer that's rendering your templates. For example:\n\n```js\nimport layouts from '@metalsmith/layouts'\n\nmetalsmith.use(\n  layouts({\n    engineOptions: {\n      cache: false\n    }\n  })\n)\n```\n\nWould pass `{ \"cache\": false }` to the used jstransformer.\n\n### Extension handling\n\nBy default layouts will apply smart default extension handling based on `transform.inputFormats` and `transform.outputFormat`.\nFor example, any of the source files below processed through `layouts({ transform: 'handlebars' })` will yield `index.html`.\n\n| source             | output           |\n| ------------------ | ---------------- |\n| src/index.hbs      | build/index.html |\n| src/index.hbs.html | build/index.html |\n| src/index.html.hbs | build/index.html |\n\n### Usage with @metalsmith/in-place\n\nIn most cases `@metalsmith/layouts` is intended to be used after `@metalsmith/in-place`.\nYou can easily share `engineOptions` configs between both plugins:\n\n```js\nimport inPlace from '@metalsmith/in-place'\nimport layouts from '@metalsmith/layouts'\n\nconst engineOptions = {}\nmetalsmith // index.hbs.hbs\n  .use(inPlace({ transform: 'handlebars', extname: '', engineOptions })) // -\u003e index.hbs\n  .use(layouts({ transform: 'handlebars', engineOptions })) // -\u003e index.html\n```\n\n@metalsmith/in-place uses a similar mechanism targeting `transform.inputFormats` file extensions by default.\nThe example requires files ending in `.hbs.hbs` extension, but if you don't like this, you can just have a single `.hbs` extension, and change the in-place invocation to `inPlace({ engineOptions, transform, extname: '.hbs' })` for the same result.\n\n### Debug\n\nTo enable debug logs, set the `DEBUG` environment variable to `@metalsmith/layouts`:\n\n```js\nmetalsmith.env('DEBUG', '@metalsmith/layouts*')\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/layouts` to the `plugins` key in your `metalsmith.json` file:\n\n```json\n{\n  \"plugins\": [\n    {\n      \"@metalsmith/layouts\": {\n        \"default\": null,\n        \"directory\": \"layouts\",\n        \"engineOptions\": {}\n      }\n    }\n  ]\n}\n```\n\n## Credits\n\n- [Ismay Wolff](https://github.com/ismay) for the current shape of the layouts plugin\n- [Ian Storm Taylor](https://github.com/ianstormtaylor) for creating [metalsmith-templates](https://github.com/segmentio/metalsmith-templates), on which this plugin was based\n- [Rob Loach](https://github.com/RobLoach) for creating [metalsmith-jstransformer](https://github.com/RobLoach/metalsmith-jstransformer), which inspired our switch to jstransformers\n\n## License\n\n[MIT](LICENSE)\n\n[npm-badge]: https://img.shields.io/npm/v/@metalsmith/layouts.svg\n[npm-url]: https://www.npmjs.com/package/@metalsmith/layouts\n[ci-badge]: https://app.travis-ci.com/metalsmith/layouts.svg?branch=master\n[ci-url]: https://app.travis-ci.com/github/metalsmith/layouts\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/layouts\n[codecov-url]: https://coveralls.io/github/metalsmith/layouts\n[license-badge]: https://img.shields.io/github/license/metalsmith/layouts\n[license-url]: LICENSE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetalsmith%2Flayouts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetalsmith%2Flayouts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetalsmith%2Flayouts/lists"}