-
Notifications
You must be signed in to change notification settings - Fork 10
126 lines (103 loc) · 4.76 KB
/
Copy pathmarkdown-lint.yml
File metadata and controls
126 lines (103 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
###########################################################################
# #
# Copyright (c) Microsoft Corporation. All rights reserved. #
# #
# This code is licensed under the MIT License (MIT). #
# #
###########################################################################
name: Markdown Lint
on:
pull_request:
branches:
- main
- "releases/**"
paths:
- "docs/**"
- "README.md"
- "en-US/**"
- "**/markdown-lint.yml"
permissions:
contents: read
jobs:
markdown-check:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
check-latest: true
- name: Install npm dependencies
run: npm install markdown-link-check textlint textlint-rule-spelling dictionary-en textlint-filter-rule-comments --save-dev
- name: Markdown link check
id: link-check
continue-on-error: true
shell: pwsh
run: |
$mlc_error_file = "link-errors.txt"
Remove-Item -Path $mlc_error_file -Force -ErrorAction SilentlyContinue
# Get markdown files to validate the links
# We split the two commands because `-Recurse` check for "README.md" in the entire directory tree,
# which includes node_modules, and we want to exclude that.
$mdFiles = (Get-ChildItem -Path ".\docs\" -Recurse -Filter "*.md") + (Get-ChildItem -Path ".\README.md" -Filter "*.md")
# Run markdown-link-check on all markdown files in the repository
$CONFIG_FILE = ".\build\hacks\link-check-config.json"
$mdFiles | ForEach-Object {
Write-Host "Validating links in $($_.FullName)" -ForegroundColor Cyan
npx markdown-link-check $_.FullName -q -c "$CONFIG_FILE" 2>>$mlc_error_file
}
# Check if the error file exists
if (-not (Test-Path "$mlc_error_file")) {
echo "Markdown link check file not found" >> $env:GITHUB_OUTPUT
exit 0
}
# Check if the error file file contains errors
$hasErrors = Select-String -Path "$mlc_error_file" -Pattern "ERROR: " -Quiet
# No errors found
if (-not $hasErrors) {
echo ":white_check_mark: [Markdown Link Check] All links are valid." >> $env:GITHUB_STEP_SUMMARY
exit 0
}
# Errors found
$errorMessage = "Broken links found."
echo ":exclamation: [Markdown Link Check] $errorMessage" >> $env:GITHUB_STEP_SUMMARY
echo $((Get-Content $mlc_error_file -Raw) -replace "`r", "" -replace "`n", "`n`t") >> $env:GITHUB_STEP_SUMMARY
throw "$errorMessage"
- name: Markdown spell check
id: spell-check
continue-on-error: true
shell: pwsh
run: |
# Run spell check on all markdown files in the repository
$sc_error_file = "sc-errors.txt"
Remove-Item -Path $sc_error_file -Force -ErrorAction SilentlyContinue
# Run textlint on markdown files in the repository
npx textlint "README.md" "docs/**/*.md" >> $sc_error_file
if (-not (Test-Path "$sc_error_file")) {
exit 0
}
# Check if the error file file contains errors. If the file is empty, it means no errors were found.
$isFileEmpty = ([string]::IsNullOrWhiteSpace((Get-Content -Path $sc_error_file)))
# No errors found
if ($isFileEmpty) {
echo ":white_check_mark: [Markdown Spell Check] No spelling errors found." >> $env:GITHUB_STEP_SUMMARY
exit 0
}
# Errors found
$errorMessage = "Spelling errors found."
echo ":exclamation: [Markdown Spell Check] $errorMessage">> $env:GITHUB_STEP_SUMMARY
echo $((Get-Content $sc_error_file -Raw) -replace "`r", "" -replace "`n", "`n`t") >> $env:GITHUB_STEP_SUMMARY
throw "$errorMessage"
- name: Check if validation failed
if: always()
shell: bash
run: |
link_status="${{ steps.link-check.outcome }}"
spell_status="${{ steps.spell-check.outcome }}"
if [[ "$link_status" == "success" && "$spell_status" == "success" ]]; then
echo ":white_check_mark: Markdown validation passed." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
error_message="Markdown validation failed"
echo ":x: $error_message. Please check the output for more information." >> "$GITHUB_STEP_SUMMARY"
exit 1