Implement a workflow that lints changed markdown files. (#39707)

* Implement a workflow that lints changed markdown files.

* Supposed to pipe the contents of files not file names.

* Fix typos in the command.

* Test changing a markdown file.

* Use a more reliable action to determine changed files.

* Adjust glob.

* Try a faster setup approach with wp-scripts installed globally

* std in does not work with wp scripts, run markdownlint directly, improve output for multiple file failures

* Try simplify the script.

* Try basic colorizing to help read output.

* separate output with a line, remove ignore because we use stdin for this.

* Play around with separation of output.

* Try use ccze to colorize output more simply.

* Remove attempt at colorizing.

* Copy config from Gutenberg to match, try coloring again.

* Do not fail after first failure found.

* Try capture errors.

* Capture in var and capture error.

* Now we capture, add back colors.

* Minor formatting adjustments.

* More minor formatting improvements.

* Add a second file to test multiple lints.

* Revert changes to md files to show a run with no lint issues.

* Test fixing a markdown file to have a lint pass with changes.

* Revert testing changes to markdown file.

* Revert lock changes we dont have dep changes in this PR.
This commit is contained in:
Sam Seay 2023-08-15 09:13:01 +08:00 committed by GitHub
parent 837c2d9961
commit 728c0db95f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 0 deletions

64
.github/workflows/pr-lint-markdown.yml vendored Normal file
View File

@ -0,0 +1,64 @@
name: MarkdownLint on PR
on:
pull_request:
paths-ignore:
- '**/changelog/**'
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v37
with:
files: |
**/*.md
- name: Setup PNPM
uses: pnpm/action-setup@c3b53f6a16e57305370b4ae5a540c2077a1d50dd
with:
version: '8.6.7'
- name: Setup Node
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c
with:
node-version-file: .nvmrc
cache: pnpm
registry-url: 'https://registry.npmjs.org'
- name: Install prerequisites
run: |
pnpm i -g markdownlint-cli
- name: Lint changed markdown files
run: |
RED="\e[1;31m"
GREEN="\e[1;32m"
NC="\e[0m"
set -e
rc=0
changed_files="${{ steps.changed-files.outputs.all_changed_files }}"
if [ -n "$changed_files" ]; then
lint_results=""
for file in $changed_files; do
lint_result=$( { cat "$file" | markdownlint --stdin ; } 2>&1 ) || rc="$?"
if [ $rc -ne 0 ]; then
lint_results="$lint_results\n>>>Linting failed for file: $file <<<\n$lint_result\n--------"
fi
done
if [ $rc -ne 0 ]; then
echo -e "${RED}Linting failed for one or more files${NC}"
echo -e "$lint_results"
exit 1
else
echo -e "${GREEN}Linting successful for all files.${NC}"
fi
else
echo "No markdown files changed."
fi

8
.markdownlint.json Normal file
View File

@ -0,0 +1,8 @@
{
"default": true,
"MD003": { "style": "atx" },
"MD007": { "indent": 4 },
"MD013": { "line_length": 9999 },
"no-hard-tabs": false,
"whitespace": false
}