Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add markdownlint script to lint docs markup #19855

Merged
merged 10 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .markdownlintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bin
build
node_modules
phpunit
playground
storybook
test
vendor
wordpress
127 changes: 127 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@
"lint-css": "wp-scripts lint-style '**/*.scss'",
"lint-css:fix": "npm run lint-css -- --fix",
"lint-types": "tsc",
"lint-md": "wp-scripts lint-md",
"lint:md-js": "wp-scripts lint-md-js",
mkaz marked this conversation as resolved.
Show resolved Hide resolved
"lint:md-docs": "wp-scripts lint-md-docs",
"package-plugin": "./bin/build-plugin-zip.sh",
"pot-to-php": "./bin/pot-to-php.js",
"publish:check": "lerna updated",
Expand Down
4 changes: 3 additions & 1 deletion packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
### New Features

- Add SVGR support to compile SVG files to React components using the `@svgr/webpack` plugin ([#18243](https://github.com/WordPress/gutenberg/pull/18243)).

- Add `format-js` script to format JavaScript source code, it uses the [`wp-prettier`](https://github.com/Automattic/wp-prettier) – Prettier fork adjusted to WordPress coding style guidelines ([#18048](https://github.com/WordPress/gutenberg/pull/18048)).
- Add `lint-md` script to lint JavaScript source code in markdown files, uses the `eslint-plugin-markdown` plugin ([#19518](https://github.com/WordPress/gutenberg/pull/19518)).
- Add `lint-md-js` script to lint JavaScript source code in markdown files, uses the `eslint-plugin-markdown` plugin ([#19518](https://github.com/WordPress/gutenberg/pull/19518)).
- Add `lint-md-docs` script to lint the markup of markdown files, uses the `markdownlint` module ([#19855](https://github.com/WordPress/gutenberg/pull/19855)).
- Add `packages-update` script to update WordPress packages to the latest version automatically ([#19448](https://github.com/WordPress/gutenberg/pull/19448)).

## 6.1.1 (2020-01-01)
Expand Down
35 changes: 30 additions & 5 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ _Example:_
"format:js": "wp-scripts format-js",
"lint:css": "wp-scripts lint-style",
"lint:js": "wp-scripts lint-js",
"lint:md": "wp-scripts lint-md",
"lint:md:docs": "wp-scripts lint-md-docs",
"lint:md:js": "wp-scripts lint-md-js",
"lint:pkg-json": "wp-scripts lint-pkg-json",
"packages-update": "wp-scripts packages-update",
"start": "wp-scripts start",
Expand Down Expand Up @@ -251,23 +252,47 @@ By default, files located in `build` and `node_modules` folders are ignored.

It uses [npm-package-json-lint](https://www.npmjs.com/package/npm-package-json-lint) with the set of recommended rules defined in [@wordpress/npm-package-json-lint-config](https://www.npmjs.com/package/@wordpress/npm-package-json-lint-config) npm package. You can override default rules with your own as described in [npm-package-json-lint wiki](https://github.com/tclindner/npm-package-json-lint/wiki). Learn more in the [Advanced Usage](#advanced-usage) section.

### `lint-md`
### `lint-md-docs`

Helps enforce standards for JS source code in your markdown files.
Uses markdownlint to lint the markup of markdown files to enforce standards.

_Example:_

```json
{
"scripts": {
"lint:md": "wp-scripts lint-md"
"lint:md:docs": "wp-scripts lint-md-docs"
}
}
```

This is how you execute the script with presented setup:

* `npm run lint:md` - lints markdown files in the entire project’s directories.
* `npm run lint:md:docs` - lints markdown files in the entire project’s directories.

By default, files located in `build` and `node_modules` folders are ignored.

#### Advanced information

It uses [markdownlint](https://github.com/DavidAnson/markdownlint) with the [.markdownlint.json](https://github.com/WordPress/gutenberg/blob/master/packages/scripts/config/.markdownlint.json) configuration. This configuration tunes the linting rules to match WordPress standard, you can override with your own config, see [markdownlint-cli](https://github.com/igorshubovych/markdownlint-cli/) for command-line parameters.

### `lint-md-js`

Uses ESLint to lint the source included in markdown files to enforce standards for JS code.

_Example:_

```json
{
"scripts": {
"lint:md:js": "wp-scripts lint-md-js"
}
}
```

This is how you execute the script with presented setup:

* `npm run lint:md:js` - lints markdown files in the entire project’s directories.

By default, files located in `build` and `node_modules` folders are ignored.

Expand Down
8 changes: 8 additions & 0 deletions packages/scripts/config/.markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"default": true,
"MD003": { "style": "atx" },
"MD007": { "indent": 4 },
"MD013": { "line_length": 9999 },
"no-hard-tabs": false,
"whitespace": false
}
2 changes: 2 additions & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
"jest-puppeteer": "^4.3.0",
"js-yaml": "^3.13.1",
"lodash": "^4.17.15",
"markdownlint": "^0.18.0",
"markdownlint-cli": "^0.21.0",
"minimist": "^1.2.0",
"npm-package-json-lint": "^4.0.3",
"prettier": "npm:wp-prettier@1.19.1",
Expand Down
55 changes: 55 additions & 0 deletions packages/scripts/scripts/lint-md-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* External dependencies
*/
const { sync: spawn } = require( 'cross-spawn' );
const { sync: resolveBin } = require( 'resolve-bin' );

/**
* Internal dependencies
*/
const {
fromConfigRoot,
getArgsFromCLI,
hasArgInCLI,
hasFileArgInCLI,
hasProjectFile,
} = require( '../utils' );

const args = getArgsFromCLI();

const defaultFilesArgs = hasFileArgInCLI() ? [] : [ '**/*.md' ];

// See: https://github.com/igorshubovych/markdownlint-cli#configuration
// Check if specified on command-line or project file for config
const hasLintConfig = hasArgInCLI( '-c' ) || hasArgInCLI( '--config' ) ||
hasProjectFile( '.markdownlint.json' ) ||
hasProjectFile( '.markdownlint.yaml' ) ||
hasProjectFile( '.markdownlint.yml' );

// When a configuration is not provided by the project, use from the default
// provided with the scripts module.
const defaultConfigArgs = ! hasLintConfig ?
[ '--config', fromConfigRoot( '.markdownlint.json' ) ] :
mkaz marked this conversation as resolved.
Show resolved Hide resolved
[];

// See: https://github.com/igorshubovych/markdownlint-cli#ignoring-files
// Check if ignore specified on command-line or project file
const hasIgnoredFiles = hasArgInCLI( '--ignore' ) || hasArgInCLI( '-i' ) ||
hasProjectFile( '.markdownlintignore' );

// Default ignore [ build, node_modules ] directories
// TODO: Once https://github.com/igorshubovych/markdownlint-cli/issues/46 is in
mkaz marked this conversation as resolved.
Show resolved Hide resolved
// we can switch this to specify an ignore file on the command-line. By default,
// markdownlint looks for .markdownlintignore in project direcotry, but how our
// scripts work we store the configs in the scripts/config directory
const defaultIgnoreArgs = ! hasIgnoredFiles ?
[ '--ignore', '**/build/**', '--ignore', '**/node_modules/**' ] :
[];

const result = spawn(
resolveBin( 'markdownlint-cli', { executable: 'markdownlint' } ),
[ ...defaultConfigArgs, ...defaultIgnoreArgs, ...args, ...defaultFilesArgs ],
{ stdio: 'inherit' }
);

process.exit( result.status );