Lint frontmatters like VueJS props.
npm i vuepress-frontmatter-lint
In .vuepress/config.js
, add
module.exports = {
// ...
plugins: [
[
require('vuepress-frontmatter-lint'),
{
specs: {
fieldName: {
type: String,
required: true,
allowedValues: ['foo', 'bar', 'baz']
},
anotherField: {
type: Number
}
}
}
]
]
};
Contains the frontmatter linting specifications in the form of a POJO, where keys are field-names and values are objects containing the following attributes.
The type of the field. Must be a valid Javascript type class.
Whether the field is required or not. Must be a Boolean value.
A list of valid values for the field. Must be an array. If this field is missing, any value matching the type will be considered valid.
Whether errors should be dumped to a file (see dumpFile
option) at the end of the linting process. Dumpfile is required by the automatic fix tool (see below).
The name of the file where the errors should be dumped to. Ignored if dumpToFile
is set to false
.
Abort the build if there are linting errors.
An array of excluded file paths. Parsed by micromatch and matched against $page.regularPath
.
An optional function that allows you to post-process the list of errors. This is useful to add fix
fields to individual errors (fix
fields are used by the automatic fix tool).
The function signature is
(errorsByUrl: Array, ctx: Object) => Array;
The arguments are
errorsByUrl
- the object containing the errors. Keys are file URLs and values are lists of errors in the following form
{
error: ['MISSING_KEY'|'EMPTY_KEY'|'INVALID_KEY'|'EMPTY_VALUE'|'INVALID_TYPE'|'INVALID_VALUE'],
key: <key name>,
expected: <expected value or type (applicable to 'INVALID_TYPE' and 'INVALID_VALUE')>,
got: <received value or type (applicable to 'INVALID_TYPE' and 'INVALID_VALUE')>
}
ctx
the Vuepress context object.
The return value must be an object in the same form of errorsByUrl
, as it willsubstitute the previous one.
const { mapValues, assign } = require('lodash');
module.exports = (errorsByUrl, ctx) => {
return mapValues(errorsByUrl, (errors, url) => {
return errors.map(e => {
if (e.error !== 'MISSING_KEY') {
return e;
}
const fix = computeFix(url, e, ctx);
if (fix) {
return assign({ fix }, e);
} else {
return e;
}
});
});
};
function computeFix(url, e, context) {
return {
wrongKey: 'correct value'
};
}
The plugin also provides an automatic fixing tool that reads the dumped errors file. You can run it with the following command
$(npm bin)/frontmatter-fix -e <errorDumpFile>
By default, the fixer prompts for a confirmation before applying any fix.
The path of the file containing the dumped errors.
The root path of the files to lint.
The path to a subset of files to restrict the linting to.
Prevents the fixer to ask prompts.
Guess what.