Git Product home page Git Product logo

vuepress-frontmatter-lint's Introduction

Frontmatter Linter for VuePress

Lint frontmatters like VueJS props.

Lint frontmatters like VueJS props

Getting started

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
          }
        }
      }
    ]
  ]
};

Config

specs

Contains the frontmatter linting specifications in the form of a POJO, where keys are field-names and values are objects containing the following attributes.

type

The type of the field. Must be a valid Javascript type class.

required

Whether the field is required or not. Must be a Boolean value.

allowedValues

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.

dumpToFile ([Boolean], optional, default: false)

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).

dumpFile ([String], optional, default: frontmatter-errors.json)

The name of the file where the errors should be dumped to. Ignored if dumpToFile is set to false.

abortBuild ([Boolean], optional, default: false)

Abort the build if there are linting errors.

exclude

An array of excluded file paths. Parsed by micromatch and matched against $page.regularPath.

postProcessErrors ([Function], optional)

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.

example

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'
  };
}

Automatic fixing

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.

Options

-e --errors <file> (required)

The path of the file containing the dumped errors.

-d --dir <directory> (defaults to cwd)

The root path of the files to lint.

-s --subdir <directory> (optional)

The path to a subset of files to restrict the linting to.

-y --yes (optional, default=false)

Prevents the fixer to ask prompts.

-h, --help

Guess what.

vuepress-frontmatter-lint's People

Contributors

xbill82 avatar

Stargazers

Emmanuel Salomon avatar Florimond Manca avatar

Watchers

James Cloos avatar

vuepress-frontmatter-lint's Issues

Define default frontmatter fields

The VuePress Default Theme has some predefined variables supported in the Frontmatter. It appears that with no configuration, vuepress-frontmatter-lint throws errors for those fields.

The default config for vuepress-frontmatter-lint should support those variables out-of-the-box, and users should be able to extend or override that default config to support their own custom frontmatter fields.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.