Skip to content

Commit f53c06c

Browse files
committed
Simplify file discovery code in markdown:check-links task
Since projects often contain many Markdown files and new files may be added or the paths of the existing files changed frequently, the best approach for validating Markdown files is to search the project file tree recursively for all Markdown files, with exclusions configured for the paths of any externally maintained files. The `markdown:check-links` task uses the markdown-link-check tool. This tool does not have a capability for discovering Markdown files so it is necessary to use the `find` command to discover the files, then pass their paths to the markdown-link-check tool. Previously the discovery code used `find` to generate an array of paths, which was iterated over passed individually to markdown-link-check in a `for` loop. The `for` loop is unnecessary because `find` has an `-exec` flag that can be used to execute commands using the discovered paths. Although the syntax and behavior of this flag is unintuitive, these disadvantages that come from its use are outweighed by the benefits of the significant amount of code that can be replaced by it. Since the `-exec`/`-execdir` flags are already in use in the assets and project infrastructure, the maintainer will be forced to work with them regardless.
1 parent 68c0664 commit f53c06c

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

Taskfile.yml

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -241,46 +241,35 @@ tasks:
241241
echo "Please install: https://github.com/tcort/markdown-link-check#readme"
242242
exit 1
243243
fi
244-
# Default behavior of the task on Windows is to exit the task when the first broken link causes a non-zero
245-
# exit status, but it's better to check all links before exiting.
246-
set +o errexit
247-
STATUS=0
248244
# Using -regex instead of -name to avoid Task's behavior of globbing even when quoted on Windows
249245
# The odd method for escaping . in the regex is required for windows compatibility because mvdan.cc/sh gives
250246
# \ characters special treatment on Windows in an attempt to support them as path separators.
251-
for file in $(
252-
find . \
253-
-type d -name '.git' -prune -o \
254-
-type d -name '.licenses' -prune -o \
255-
-type d -name '__pycache__' -prune -o \
256-
-type d -name 'node_modules' -prune -o \
257-
-regex ".*[.]md" -print
258-
); do
259-
markdown-link-check \
260-
--quiet \
261-
--config "./.markdown-link-check.json" \
262-
"$file"
263-
STATUS=$(( $STATUS + $? ))
264-
done
265-
exit $STATUS
266-
else
267-
npx --package=markdown-link-check --call='
268-
STATUS=0
269-
for file in $(
270-
find . \
271-
-type d -name '.git' -prune -o \
272-
-type d -name '.licenses' -prune -o \
273-
-type d -name '__pycache__' -prune -o \
274-
-type d -name 'node_modules' -prune -o \
275-
-regex ".*[.]md" -print
276-
); do
247+
find . \
248+
-type d -name ".git" -prune -o \
249+
-type d -name ".licenses" -prune -o \
250+
-type d -name "__pycache__" -prune -o \
251+
-type d -name "node_modules" -prune -o \
252+
-regex ".*[.]md" \
253+
-exec \
277254
markdown-link-check \
278255
--quiet \
279256
--config "./.markdown-link-check.json" \
280-
"$file"
281-
STATUS=$(( $STATUS + $? ))
282-
done
283-
exit $STATUS
257+
\{\} \
258+
+
259+
else
260+
npx --package=markdown-link-check --call='
261+
find . \
262+
-type d -name ".git" -prune -o \
263+
-type d -name ".licenses" -prune -o \
264+
-type d -name "__pycache__" -prune -o \
265+
-type d -name "node_modules" -prune -o \
266+
-regex ".*[.]md" \
267+
-exec \
268+
markdown-link-check \
269+
--quiet \
270+
--config "./.markdown-link-check.json" \
271+
\{\} \
272+
+
284273
'
285274
fi
286275

0 commit comments

Comments
 (0)