-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathlint_site.sh
executable file
·223 lines (182 loc) · 8.18 KB
/
lint_site.sh
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/bin/bash
# Copyright Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
FAILED=0
if [[ "$#" -ne 0 ]]; then
LANGS="$*"
else
LANGS="en zh"
fi
red='\e[0;31m'
clr='\e[0m'
error() {
echo -e "${red}$*${clr}"
}
# This performs spell checking and style checking over markdown files in a content
# directory. It transforms the shortcode sequences we use to annotate code blocks
# into classic markdown ``` code blocks, so that the linters aren't confused
# by the code blocks
check_content() {
DIR=$1
LANG=$2
TMP=$(mktemp -d)
# check for use of ```
if grep -nr -e "\`\`\`" --include "*.md" "${DIR}"; then
error "Ensure markdown content uses {{< text >}} for code blocks rather than \`\`\`. Please see https://istio.io/about/contribute/code-blocks/"
FAILED=1
fi
# make the tmp dir
mkdir -p "${TMP}"
# create a throwaway copy of the content
cp -R "${DIR}" "${TMP}"
cp .spelling "${TMP}"
cp mdl.rb "${TMP}"
# replace the {{< text >}} shortcodes with ```plain
find "${TMP}" -type f -name \*.md -exec sed -E -i "s/\\{\\{< text .*>\}\}/\`\`\`plain/g" {} ";"
# replace the {{< mermaid >}} shortcodes with ```mermaid
find "${TMP}" -type f -name \*.md -exec sed -E -i "s/\\{\\{< mermaid .*>\}\}/\`\`\`mermaid/g" {} ";"
# replace the {{< /text >}} shortcodes with ```
find "${TMP}" -type f -name \*.md -exec sed -E -i "s/\\{\\{< \/text .*>\}\}/\`\`\`/g" {} ";"
# replace the {{< /mermaid >}} shortcodes with ```
find "${TMP}" -type f -name \*.md -exec sed -E -i "s/\\{\\{< \/mermaid .*>\}\}/\`\`\`/g" {} ";"
# elide url="*"
find "${TMP}" -type f -name \*.md -exec sed -E -i "s/url=\".*\"/URL/g" {} ";"
# elide link="*"
find "${TMP}" -type f -name \*.md -exec sed -E -i "s/link=\".*\"/LINK/g" {} ";"
# remove any heading anchors
find "${TMP}" -type f -name \*.md -exec sed -E -i "s/(^#.*\S) *\{#.*\} */\1/g" {} ";"
# switch to the temp dir
pushd "${TMP}" >/dev/null
if ! find . -type f -name '*.md' -print0 | xargs -0 -r mdspell "${LANG}" --ignore-acronyms --ignore-numbers --no-suggestions --report; then
error "To learn how to address spelling errors, please see https://istio.io/about/contribute/build/#test-your-changes"
FAILED=1
fi
if ! mdl --ignore-front-matter --style mdl.rb .; then
FAILED=1
fi
if grep -nrP --include "*.md" -e "\(https://istio.io/(?!v[0-9]\.[0-9]/|archive/)" .; then
error "Ensure markdown content uses relative references to istio.io"
FAILED=1
fi
if grep -nr --include "*.md" -e "(https://preliminary.istio.io" .; then
error "Ensure markdown content doesn't contain references to preliminary.istio.io"
FAILED=1
fi
if grep -nr --include "*.md" -e https://github.com/istio/istio/blob/ .; then
error "Ensure markdown content uses {{< github_blob >}}"
FAILED=1
fi
if grep -nr --include "*.md" -e https://github.com/istio/istio/tree/ .; then
error "Ensure markdown content uses {{< github_tree >}}"
FAILED=1
fi
if grep -nr --include "*.md" -e https://raw.githubusercontent.com/istio/istio/ .; then
error "Ensure markdown content uses {{< github_file >}}"
FAILED=1
fi
# go back whence we came
popd >/dev/null
# cleanup
rm -fr "${TMP}"
}
SKIP_LANGS=( en zh pt-br )
for lang in $LANGS; do
for i in "${!SKIP_LANGS[@]}"; do
if [[ "${SKIP_LANGS[$i]}" = "${lang}" ]]; then
unset 'SKIP_LANGS[${i}]'
fi
done
SKIP_LANGS=( "${SKIP_LANGS[@]}" )
if [[ "$lang" == "en" ]]; then
list=$(find ./content/en/docs -name '*.md' -not -exec grep -q '^test: ' {} \; -print)
if [[ -n $list ]]; then
echo "$list"
error "Ensure every document *.md file includes a test: attribute in its metadata"
FAILED=1
fi
list=$(find ./content/en/docs -name 'index.md' -not -exec grep -q '^owner: ' {} \; -print)
if [[ -n $list ]]; then
echo "$list"
error "Ensure every document index.md file includes an owner: attribute in its metadata"
FAILED=1
fi
check_content "content/$lang" --en-us
while IFS= read -r -d '' f; do
if grep -H -n -e '“' "${f}"; then
# shellcheck disable=SC1111
error "Ensure content only uses standard quotation marks and not “"
FAILED=1
fi
done < <(find ./content/en -type f \( -name '*.html' -o -name '*.md' \) -print0)
elif [[ "$lang" == "zh" ]]; then
# only check English words in Chinese docs
check_content "content/$lang" --en-us
while IFS= read -r -d '' f; do
if grep -H -n -E -e "- (/docs|/about|/blog|/faq|/news)" "${f}"; then
error "Ensure translated content doesn't include aliases for English content"
FAILED=1
fi
if grep -H -n -E -e '"(/docs|/about|/blog|/faq|/news)' "${f}"; then
error "Ensure translated content doesn't include references to English content"
FAILED=1
fi
if grep -H -n -E -e '\((/docs|/about|/blog|/faq|/news)' "${f}"; then
error "Ensure translated content doesn't include references to English content"
FAILED=1
fi
done < <(find ./content/zh -type f \( -name '*.html' -o -name '*.md' \) -print0)
elif [[ "$lang" == "pt-br" ]]; then
# only check English words in Portuguese Brazil docs
check_content "content/$lang" --en-us
fi
done
if [ -d ./public ]; then
if [[ ${#SKIP_LANGS[@]} -ne 0 ]]; then
printf -v find_exclude " -name %s -prune -o" "${SKIP_LANGS[@]}"; read -r -a find_exclude <<< "$find_exclude"
fi
while IFS= read -r -d '' f; do
if grep -H -n -i -e blockquote "${f}"; then
error "Ensure content only uses {{< tip >}}, {{< warning >}}, {{< idea >}}, and {{< quote >}} instead of block quotes"
FAILED=1
fi
#if grep -H -n -e "\"https://github.*#L[0-9]*\"" "${f}"; then
# error "Ensure content doesn't use links to specific lines in GitHub files as those are too brittle"
# FAILED=1
#fi
done < <(find ./public "${find_exclude[@]}" -type f -name '*.html' -print0)
if ! htmlproofer ./public --file-ignore "${ignore_files}" --assume-extension --http-status-ignore "0,429" --check-html --check-external-hash --check-opengraph --checks-to-ignore "LinkCheck"; then
FAILED=1
fi
if [[ ${SKIP_LINK_CHECK:-} != "true" ]]; then
if [[ ${#SKIP_LANGS[@]} -ne 0 ]]; then
printf -v ignore_files "/^.\/public\/%s/," "${SKIP_LANGS[@]}"; ignore_files="${ignore_files%,}"
fi
echo "Running linkinator..."
if [[ ${CHECK_EXTERNAL_LINKS:-} == "true" ]]; then
if ! linkinator public/ -r -s 'github.com localhost:3000 localhost:5601 localhost:8001 localhost:9080 localhost:9081 en.wikipedia.org my-istio-logs-database.io' --silent --concurrency 25; then
FAILED=1
fi
else
#TODO: Remove .../workload-selector/ from ignored links. PRs take a long time to get through istio/api, and a link is broken from there. Once this PR is complete, remove it: https://github.com/istio/api/pull/1405
if ! linkinator public/ -r -s 'github.com localhost:3000 localhost:5601 localhost:8001 localhost:9080 localhost:9081 en.wikipedia.org my-istio-logs-database.io ^((?!localhost).)*$ /docs/reference/config/type/v1beta1/workload-selector/' --silent --concurrency 25; then
FAILED=1
fi
fi
fi
fi
if [[ ${FAILED} -eq 1 ]]; then
error "LINTING FAILED"
exit 1
fi