Skip to content
Snippets Groups Projects
Commit b677210c authored by Jonathan Smith's avatar Jonathan Smith Committed by Fran Garcia-Linares
Browse files

Issue #3469949 by jonathan1055, fjgarlin: Internal code check - find non-active documentation links

parent b424c801
No related branches found
No related tags found
1 merge request!251#3469949 Check for unformatted http links
Pipeline #271442 failed
......@@ -135,6 +135,9 @@ workflow:
# Bash standards.
shellcheck --version
shellcheck scripts/*.sh || EXIT_CODE=$((EXIT_CODE+1))
- |
# MkDocs files.
php scripts/unformatted-links.php || EXIT_CODE=$((EXIT_CODE+1))
- |
echo "Number of checks that failed: $EXIT_CODE"
exit $EXIT_CODE
......
......@@ -30,6 +30,3 @@ There may be times when you want to verify which commits are contained in a part
What commits are in the `default` release? [Commits](https://git.drupalcode.org/project/gitlab_templates/-/commits/default-ref?ref_type=tags)
What commits are in `main` but not yet in a tagged release? [Commits](https://git.drupalcode.org/project/gitlab_templates/-/compare/default-ref...main?from_project_id=96292)
What are the latest tags? [Tags](https://git.drupalcode.org/project/gitlab_templates/-/tags)
......@@ -4,7 +4,7 @@ The [PHPStan](https://phpstan.org/) job runs static code quality checks.
If the project contains a `phpstan.neon` configuration file it will be used. If not, a [default configuration](https://git.drupalcode.org/project/gitlab_templates/-/blob/main/assets/phpstan.neon) is used.
The pipeline variable `_PHPSTAN_LEVEL` can be set to a value from 0-9 to specify how relaxed or severe the code quality checks should be. This can be used to temporarily override the value in your `phpstan.neon` file. If no value is given, either in the variable or the fle, then the default of 0 is used. See https://phpstan.org/user-guide/rule-levels for more information.
The pipeline variable `_PHPSTAN_LEVEL` can be set to a value from 0-9 to specify how relaxed or severe the code quality checks should be. This can be used to temporarily override the value in your `phpstan.neon` file. If no value is given, either in the variable or the fle, then the default of 0 is used. See the [PHPStan rule levels](https://phpstan.org/user-guide/rule-levels) for more information.
Projects can specify [baseline](https://phpstan.org/user-guide/baseline) file(s) of messages to ignore, using the `includes:` keyword in the `phpstan.neon` file:
```
......
......@@ -20,6 +20,7 @@ npx cspell --show-suggestions --show-context --no-progress **
npx eslint --no-error-on-unmatched-pattern --ignore-pattern="docs/**" --ext=.yml .
vendor/bin/phpcs scripts/*.php -s
shellcheck scripts/*.sh
php scripts/unformatted-links.php
# Clean up all files that were copied.
if [[ "$1" == "clean" ]]; then
......
<?php
/**
* @file
* Find unformatted http links in .md files.
*
* Arguments:
* -p --path path Path to search in. Default is 'docs'.
* -v --verbose Show verbose debug output.
*/
// Get the options.
$options = getopt('p:v', ['path:', 'verbose']);
$quiet = !array_key_exists('v', $options) && !array_key_exists('verbose', $options);
$path = $options['p'] ?? $options['path'] ?? './docs';
$quiet ?: print "quiet=$quiet\npath=$path\n";
print "Searching for unformatted http links in .md files, starting in $path\n";
$found = 0;
$code_block = FALSE;
$files = glob($path . '/{*.md,**/*.md}', GLOB_BRACE);
$quiet ?: print ".md files found: " . count($files) . "\n";
foreach ($files as $k => $filename) {
$quiet ?: print "file $k = $filename\n";
$lines = file($filename);
foreach ($lines as $key => $text) {
$quiet ?: print $text;
switch (TRUE) {
// Detect when a code block begins and ends.
case strstr($text, "```");
$code_block = !$code_block;
$quiet ?: print '>>> changed: $code_block=' . ($code_block ? 'true' : 'false') . PHP_EOL;
break;
// If the line does not contain http or we are still in a code block then
// move on to the next line.
case !stristr($text, 'http') || $code_block;
$quiet ?: print ">>> no http or still in code block\n";
break;
// If the http is part of an inline code block then it is OK.
case preg_match('/`.*http.*`/', $text, $matches);
$quiet ?: print '>>> Found inline code with http so ignore. $matches=' . print_r($matches, TRUE);
break;
// Report all remaining 'http' that do not have ( immediately in front.
case preg_match('/[^\(]http/', $text, $matches);
$quiet ?: print '>>> Found http without preceding ( $matches=' . print_r($matches, TRUE) . "\nREPORT THIS\n";
print str_repeat('-', 80) . "\n$filename:" . ($key + 1) . "\n$text";
$found++;
$quiet ?: print "found=$found\n";
break;
}
}
}
$found > 0 ? print str_repeat('-', 80) . "\nTo fix these links use the syntax: [text to show](url)\n" : NULL;
print 'Files searched: ' . count($files) . ". Unformatted links found: $found\n";
$exit_code = $found ? 1 : 0;
$quiet ?: print "Ending with exit_code {$exit_code}" . PHP_EOL;
exit($exit_code);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment