@metalsmith/layouts
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -7,2 +7,10 @@ ### Changelog | ||
#### [v2.5.0](https://github.com/metalsmith/layouts/compare/v2.4.0...v2.5.0) | ||
- Resolves #181, use metalsmith.match instead of multimatch [`#181`](https://github.com/metalsmith/layouts/issues/181) | ||
- Remove semicolons, run formatting. Remove devDependencies [`605559a`](https://github.com/metalsmith/layouts/commit/605559a025ac6253fbc2d81a3cb10ff647d053fb) | ||
- feat: add JSdocs, named plugin function [`30411dd`](https://github.com/metalsmith/layouts/commit/30411dd449477d6bf9c30a587c3d9bc71c40f884) | ||
- Dependencies: updates debug to 4.3.4 [`1a33ff2`](https://github.com/metalsmith/layouts/commit/1a33ff29bee676454e93400f6ca1902e710b62ec) | ||
- Drop Node < 12 support [`4cd7c4d`](https://github.com/metalsmith/layouts/commit/4cd7c4d30ffa9f21ed33db5a4cd9eca7b74be5b2) | ||
<!-- auto-changelog-above --> | ||
@@ -9,0 +17,0 @@ |
@@ -1,3 +0,3 @@ | ||
const jstransformer = require('jstransformer'); | ||
const toTransformer = require('inputformat-to-jstransformer'); | ||
const jstransformer = require('jstransformer') | ||
const toTransformer = require('inputformat-to-jstransformer') | ||
@@ -8,15 +8,15 @@ /** | ||
const cache = {}; | ||
const cache = {} | ||
function getTransformer(ext) { | ||
if (ext in cache) { | ||
return cache[ext]; | ||
return cache[ext] | ||
} | ||
const transformer = toTransformer(ext); | ||
cache[ext] = transformer ? jstransformer(transformer) : false; | ||
const transformer = toTransformer(ext) | ||
cache[ext] = transformer ? jstransformer(transformer) : false | ||
return cache[ext]; | ||
return cache[ext] | ||
} | ||
module.exports = getTransformer; | ||
module.exports = getTransformer |
156
lib/index.js
@@ -1,8 +0,17 @@ | ||
const debug = require('debug')('@metalsmith/layouts'); | ||
const match = require('multimatch'); | ||
const path = require('path'); | ||
const isUtf8 = require('is-utf8'); | ||
const getTransformer = require('./get-transformer'); | ||
const debug = require('debug')('@metalsmith/layouts') | ||
const path = require('path') | ||
const isUtf8 = require('is-utf8') | ||
const getTransformer = require('./get-transformer') | ||
/** | ||
* `@metalsmith/layouts` options | ||
* @typedef {Object} Options | ||
* @property {string} [default] A default layout to apply to files, eg `default.njk`. | ||
* @property {string} [pattern] The directory for the layouts. The default is `layouts`. | ||
* @property {string|string[]} [directory] Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is `**` (all). | ||
* @property {Object} [engineOptions] Pass options to [the jstransformer](https://github.com/jstransformers/jstransformer) that's rendering your layouts. The default is `{}`. | ||
* @property {boolean} [suppressNoFilesError] By default `@metalsmith/layouts` will exit with an error if there aren't any files to process. Enabling this option will suppress that error. | ||
*/ | ||
/** | ||
* Resolves layouts, in the following order: | ||
@@ -16,6 +25,6 @@ * 1. Layouts in the frontmatter | ||
if (file.layout || file.layout === false) { | ||
return file.layout; | ||
return file.layout | ||
} | ||
return settings.default; | ||
return settings.default | ||
} | ||
@@ -28,14 +37,14 @@ | ||
function render({ filename, files, metadata, settings, metalsmith }) { | ||
const file = files[filename]; | ||
const layout = getLayout({ file, settings }); | ||
const extension = layout.split('.').pop(); | ||
const file = files[filename] | ||
const layout = getLayout({ file, settings }) | ||
const extension = layout.split('.').pop() | ||
debug(`rendering ${filename} with layout ${layout}`); | ||
debug(`rendering ${filename} with layout ${layout}`) | ||
// Stringify file contents | ||
const contents = file.contents.toString(); | ||
const contents = file.contents.toString() | ||
const transform = getTransformer(extension); | ||
const locals = { ...metadata, ...file, contents }; | ||
const layoutPath = path.join(metalsmith.path(settings.directory), layout); | ||
const transform = getTransformer(extension) | ||
const locals = { ...metadata, ...file, contents } | ||
const layoutPath = path.join(metalsmith.path(settings.directory), layout) | ||
@@ -48,4 +57,4 @@ // Transform the contents | ||
// eslint-disable-next-line no-param-reassign | ||
file.contents = Buffer.from(rendered.body); | ||
debug(`done rendering ${filename}`); | ||
file.contents = Buffer.from(rendered.body) | ||
debug(`done rendering ${filename}`) | ||
}) | ||
@@ -55,5 +64,5 @@ .catch((err) => { | ||
// eslint-disable-next-line no-param-reassign | ||
err.message = `${filename}: ${err.message}`; | ||
throw err; | ||
}); | ||
err.message = `${filename}: ${err.message}` | ||
throw err | ||
}) | ||
} | ||
@@ -66,11 +75,11 @@ | ||
function validate({ filename, files, settings }) { | ||
const file = files[filename]; | ||
const layout = getLayout({ file, settings }); | ||
const file = files[filename] | ||
const layout = getLayout({ file, settings }) | ||
debug(`validating ${filename}`); | ||
debug(`validating ${filename}`) | ||
// Files without a layout cannot be processed | ||
if (!layout) { | ||
debug(`validation failed, ${filename} does not have a layout set`); | ||
return false; | ||
debug(`validation failed, ${filename} does not have a layout set`) | ||
return false | ||
} | ||
@@ -80,4 +89,4 @@ | ||
if (!layout.includes('.')) { | ||
debug(`validation failed, layout for ${filename} does not have an extension`); | ||
return false; | ||
debug(`validation failed, layout for ${filename} does not have an extension`) | ||
return false | ||
} | ||
@@ -87,65 +96,70 @@ | ||
if (!isUtf8(file.contents)) { | ||
debug(`validation failed, ${filename} is not utf-8`); | ||
return false; | ||
debug(`validation failed, ${filename} is not utf-8`) | ||
return false | ||
} | ||
// Files without an applicable jstransformer are ignored | ||
const extension = layout.split('.').pop(); | ||
const transformer = getTransformer(extension); | ||
const extension = layout.split('.').pop() | ||
const transformer = getTransformer(extension) | ||
if (!transformer) { | ||
debug(`validation failed, no jstransformer found for layout for ${filename}`); | ||
debug(`validation failed, no jstransformer found for layout for ${filename}`) | ||
} | ||
return transformer; | ||
return transformer | ||
} | ||
/** | ||
* Plugin, the main plugin used by metalsmith | ||
* A metalsmith plugin for rendering layouts | ||
* @param {Options} options | ||
* @returns {import('metalsmith').Plugin} | ||
*/ | ||
function initLayouts(options) { | ||
return function layouts(files, metalsmith, done) { | ||
const metadata = metalsmith.metadata() | ||
const defaults = { | ||
pattern: '**', | ||
directory: 'layouts', | ||
engineOptions: {}, | ||
suppressNoFilesError: false | ||
} | ||
const settings = { ...defaults, ...options } | ||
module.exports = (options) => (files, metalsmith, done) => { | ||
const metadata = metalsmith.metadata(); | ||
const defaults = { | ||
pattern: '**', | ||
directory: 'layouts', | ||
engineOptions: {}, | ||
suppressNoFilesError: false | ||
}; | ||
const settings = { ...defaults, ...options }; | ||
// Check whether the pattern option is valid | ||
if (!(typeof settings.pattern === 'string' || Array.isArray(settings.pattern))) { | ||
return done( | ||
new Error( | ||
'invalid pattern, the pattern option should be a string or array of strings. See https://www.npmjs.com/package/@metalsmith/layouts#pattern' | ||
// Check whether the pattern option is valid | ||
if (!(typeof settings.pattern === 'string' || Array.isArray(settings.pattern))) { | ||
return done( | ||
new Error( | ||
'invalid pattern, the pattern option should be a string or array of strings. See https://www.npmjs.com/package/@metalsmith/layouts#pattern' | ||
) | ||
) | ||
); | ||
} | ||
} | ||
// Filter files by the pattern | ||
const matchedFiles = match(Object.keys(files), settings.pattern); | ||
// Filter files by the pattern | ||
const matchedFiles = metalsmith.match(settings.pattern) | ||
// Filter files by validity | ||
const validFiles = matchedFiles.filter((filename) => validate({ filename, files, settings })); | ||
// Filter files by validity | ||
const validFiles = matchedFiles.filter((filename) => validate({ filename, files, settings })) | ||
// Let the user know when there are no files to process, unless the check is suppressed | ||
if (validFiles.length === 0) { | ||
const message = | ||
'no files to process. See https://www.npmjs.com/package/@metalsmith/layouts#suppressnofileserror'; | ||
// Let the user know when there are no files to process, unless the check is suppressed | ||
if (validFiles.length === 0) { | ||
const message = | ||
'no files to process. See https://www.npmjs.com/package/@metalsmith/layouts#suppressnofileserror' | ||
if (settings.suppressNoFilesError) { | ||
debug(message); | ||
return done(); | ||
if (settings.suppressNoFilesError) { | ||
debug(message) | ||
return done() | ||
} | ||
return done(new Error(message)) | ||
} | ||
return done(new Error(message)); | ||
// Map all files that should be processed to an array of promises and call done when finished | ||
return Promise.all( | ||
validFiles.map((filename) => render({ filename, files, metadata, settings, metalsmith })) | ||
) | ||
.then(() => done()) | ||
.catch(/* istanbul ignore next */ (error) => done(error)) | ||
} | ||
// Map all files that should be processed to an array of promises and call done when finished | ||
return Promise.all( | ||
validFiles.map((filename) => render({ filename, files, metadata, settings, metalsmith })) | ||
) | ||
.then(() => done()) | ||
.catch(/* istanbul ignore next */ (error) => done(error)); | ||
}; | ||
} | ||
initLayouts({}) | ||
module.exports = initLayouts |
{ | ||
"name": "@metalsmith/layouts", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "A metalsmith plugin for layouts", | ||
@@ -15,2 +15,5 @@ "homepage": "https://github.com/metalsmith/layouts#readme", | ||
"author": "Ismay Wolff (https://www.wolffsoftwareengineering.nl)", | ||
"maintainers": [ | ||
"Kevin Van Lierde (https://webketje.com" | ||
], | ||
"main": "lib/index.js", | ||
@@ -28,37 +31,40 @@ "directories": { | ||
"coverage": "nyc report --reporter=text-lcov > ./coverage.info", | ||
"coveralls": "npm run coverage && cat ./coverage.info | coveralls", | ||
"dev": "nodemon --exec 'npm test'", | ||
"release": "release-it .", | ||
"test": "nyc mocha", | ||
"format": "prettier --write \"**/*.{yml,md,js,json}\"", | ||
"lint": "eslint --cache --fix .", | ||
"release": "release-it .", | ||
"test": "nyc mocha" | ||
"format:check": "prettier --list-different \"**/*.{yml,md,js,json}\"", | ||
"lint": "eslint --fix .", | ||
"lint:check": "eslint --fix-dry-run ." | ||
}, | ||
"dependencies": { | ||
"debug": "^4.3.3", | ||
"debug": "^4.3.4", | ||
"inputformat-to-jstransformer": "^1.4.0", | ||
"is-utf8": "^0.2.1", | ||
"jstransformer": "^1.0.0", | ||
"multimatch": "^4.0.0" | ||
"jstransformer": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"assert-dir-equal": "^1.1.0", | ||
"auto-changelog": "^2.3.0", | ||
"coveralls": "^3.1.1", | ||
"eslint": "^8.5.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"jstransformer-handlebars": "^1.1.0", | ||
"auto-changelog": "^2.4.0", | ||
"eslint": "^8.14.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"jstransformer-handlebars": "^1.2.0", | ||
"jstransformer-qejs": "^0.2.0", | ||
"metalsmith": "^2.3.0", | ||
"mocha": "^7.2.0", | ||
"mocha": "^9.2.2", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.5.1", | ||
"release-it": "^14.11.8", | ||
"rimraf": "^3.0.2" | ||
"prettier": "^2.6.2", | ||
"release-it": "^15.0.0" | ||
}, | ||
"peerDependencies": { | ||
"metalsmith": "^2.3.0" | ||
"metalsmith": "^2.4.1" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"engines": { | ||
"node": ">=12.0.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22069
5
146
- Removedmultimatch@^4.0.0
- Removed@types/minimatch@3.0.5(transitive)
- Removedarray-differ@3.0.0(transitive)
- Removedarray-union@2.1.0(transitive)
- Removedarrify@2.0.1(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedmultimatch@4.0.0(transitive)
Updateddebug@^4.3.4