doc/development/documentation/testing/vale.md
Vale is a grammar, style, and word usage linter for the
English language. Vale's configuration is stored in the .vale.ini file located
in the root directory of projects. For example, the .vale.ini
of the gitlab project.
Vale supports creating custom rules that extend any of
several types of checks, which we store in the documentation directory of projects. For example,
the doc/.vale directory of the gitlab project.
This configuration is also used in build pipelines, where error-level rules are enforced.
You can use Vale:
Install vale using either:
mise. For example:
mise use -g vale
A package manager:
brew, run: brew install vale.Using linters in your editor is more convenient than having to run the commands from the command line.
To configure Vale in your editor, install one of the following as appropriate:
Visual Studio Code ChrisChinchilla.vale-vscode extension.
You can configure the plugin to display only a subset of alerts.
Sublime Text SublimeLinter-vale package. To have Vale
suggestions appears as blue instead of red (which is how errors appear), add vale configuration to your
SublimeLinter configuration:
"vale": {
"styles": [{
"mark_style": "outline",
"scope": "region.bluish",
"types": ["suggestion"]
}]
}
LSP for Sublime Text package LSP-vale-ls.
Vim ALE plugin.
Emacs Flycheck extension. A minimal configuration for Flycheck to work with Vale could look like:
(flycheck-define-checker vale
"A checker for prose"
:command ("vale" "--output" "line" "--no-wrap"
source)
:standard-input nil
:error-patterns
((error line-start (file-name) ":" line ":" column ":" (id (one-or-more (not (any ":")))) ":" (message) line-end))
:modes (markdown-mode org-mode text-mode)
:next-checkers ((t . markdown-markdownlint-cli))
)
(add-to-list 'flycheck-checkers 'vale)
In this setup the markdownlint checker is set as a "next" checker from the defined vale checker.
Enabling this custom Vale checker provides error linting from both Vale and markdownlint.
Vale returns three types of results:
The result types have these attributes:
| Result type | Displays in CI/CD job output | Displays in MR diff | Causes CI/CD jobs to fail | Vale rule link |
|---|---|---|---|---|
error | {{< icon name="check-circle" >}} Yes | {{< icon name="check-circle" >}} Yes | {{< icon name="check-circle" >}} Yes | Error-level Vale rules |
warning | {{< icon name="dotted-circle" >}} No | {{< icon name="check-circle" >}} Yes | {{< icon name="dotted-circle" >}} No | Warning-level Vale rules |
suggestion | {{< icon name="dotted-circle" >}} No | {{< icon name="dotted-circle" >}} No | {{< icon name="dotted-circle" >}} No | Suggestion-level Vale rules |
It's tempting to add a Vale rule for every style guide rule. However, we should be mindful of the effort to create and enforce a Vale rule, and the noise it creates.
In general, follow these guidelines:
If you add an error-level Vale rule, you must fix the existing occurrences of the issue in the documentation before you can add the rule.
If there are too many issues to fix in a single merge request, add the rule at a
warning level. Then, fix the existing issues in follow-up merge requests.
When the issues are fixed, promote the rule to an error.
If you add a warning-level or suggestion-level rule, consider:
How many more warnings or suggestions it creates in the Vale output. If the number of additional warnings is significant, the rule might be too broad.
How often an author might ignore it because it's acceptable in the context. If the rule is too subjective, it cannot be adequately enforced and creates unnecessary additional warnings.
Whether it's appropriate to display in the merge request diff in the GitLab UI. If the rule is difficult to implement directly in the merge request (for example, it requires page refactoring), set it to suggestion-level so it displays in local editors only.
New Vale rules belong in one of two categories (known in Vale as styles). These
rules are stored separately in specific styles directories specified in a project's .vale.ini file. For example,
.vale.ini for the gitlab project.
Where to add your new rules depends on the type of rule you're proposing:
gitlab_base: base rules that are applicable to any GitLab documentation.gitlab_docs: rules that are only applicable to documentation that is published to https://docs.gitlab.com.Most new rules belong in gitlab_base.
You can set Visual Studio Code to display only a subset of Vale alerts when viewing files:
To display only a subset of Vale alerts when running Vale from the command line, use
the --minAlertLevel flag, which accepts error, warning, or suggestion. Combine it with --config
to point to the configuration file in the project, if needed:
vale --config .vale.ini --minAlertLevel error doc/**/*.md
Omit the flag to display all alerts, including suggestion level alerts.
To test only a single rule when running Vale from the command line, modify this
command, replacing OutdatedVersions with the name of the rule:
vale --no-wrap --filter='.Name=="gitlab_base.OutdatedVersions"' doc/**/*.md
You can disable a specific Vale linting rule or all Vale linting rules for any portion of a document:
<!-- vale gitlab_<type>.rulename = NO --> tag before the text, and a
<!-- vale gitlab_<type>.rulename = YES --> tag after the text, replacing rulename with the filename of a test in the
directory of one of the GitLab styles.<!-- vale off --> tag before the text, and a
<!-- vale on --> tag after the text.Whenever possible, exclude only the problematic rule and lines.
For more information on Vale scoping rules, see Vale's documentation.
raw scopeIn general, you cannot disable Vale rules with the
raw scope.
However, if a change causes Vale to fail because of a false positive, you can sometimes circumvent the by tweaking the formatting around the change. For example, sometimes you can add some extra spaces to the start of the line with the false positive. Be sure to test that the page continues to render fine, and add an HTML comment describing the reason for the special formatting.
For instance (taken from a real example):
<!--
The following codeblock uses extra spaces to avoid the Vale ReferenceLinks test.
Do not remove the two-space nesting.
-->
- [Use a reference-style link that's normally prohibited][1]
[1]: https://example.com/
For more information, see this Vale issue.
By default, the Vale check in Lefthook only shows error-level issues. The default branches have no Vale errors, so any errors listed here are introduced by the commit to the branch.
To also see the Vale warnings, set a local environment variable: VALE_WARNINGS=true.
Enable Vale warnings on commit or push to improve the documentation suite by:
These warnings:
To enable Vale warnings with Lefthook:
Automatically, add VALE_WARNINGS=true to your shell configuration.
Manually, prepend VALE_WARNINGS=true to invocations of lefthook. For example:
VALE_WARNINGS=true bundle exec lefthook run pre-commit
You can also configure your editor to show Vale warnings.
When Vale flags a valid word as a spelling mistake, you can fix it following these guidelines:
| Flagged word | Guideline |
|---|---|
| jargon | Rewrite the sentence to avoid it. |
| correctly-capitalized name of a product or service | Add the word to the Vale spelling exceptions list. |
| name of a person | Remove the name if it's not needed, or add the Vale exception code inline. |
| a command, variable, code, or similar | Put it in backticks or a code block. For example: The git clone command can be used with the CI_COMMIT_BRANCH variable. -> The `git clone` command can be used with the `CI_COMMIT_BRANCH` variable. |
| UI text from GitLab | Verify it correctly matches the UI, then: If it does not match the UI, update it. If it matches the UI, but the UI seems incorrect, create an issue to see if the UI needs to be fixed. If it matches the UI and seems correct, add it to the Vale spelling exceptions list. |
| UI text from a third-party product | Rewrite the sentence to avoid it, or add the Vale exception code in-line. |
The Uppercase.yml
test checks for incorrect usage of words in all capitals. For example, avoid usage
like This is NOT important.
If the word must be in all capitals, follow these guidelines:
| Flagged word | Guideline |
|---|---|
| Acronym (likely known by the average visitor to that page) | Add the acronym to the list of words and acronyms in Uppercase.yml. |
| Acronym (likely not known by the average visitor to that page) | The first time the acronym is used, write it out fully followed by the acronym in parentheses. In later uses, use just the acronym by itself. For example: This feature uses the File Transfer Protocol (FTP). FTP is.... |
| Correctly capitalized name of a product or service | Add the name to the list of words and acronyms in Uppercase.yml. |
| Command, variable, code, or similar | Put it in backticks or a code block. For example: Use `FALSE` as the variable value. |
| UI text from a third-party product | Rewrite the sentence to avoid it, or add the vale exception code in-line. |
In ReadingLevel.yml,
we have implemented
the Flesch-Kincaid grade level test
to determine the readability of our documentation.
As a general guideline, the lower the score, the more readable the documentation.
For example, a page that scores 12 before a set of changes, and 9 after, indicates an iterative improvement to readability. The score is not an exact science, but is meant to help indicate the
general complexity level of the page.
The readability score is calculated based on the number of words per sentence, and the number of syllables per word. For more information, see the Vale documentation.
To export all (or filtered) Vale results to a file, modify this command:
# Returns results of types suggestion, warning, and error
find . -name '*.md' | sort | xargs vale --minAlertLevel suggestion --output line > ../../results.txt
# Returns only warnings and errors
find . -name '*.md' | sort | xargs vale --minAlertLevel warning --output line > ../../results.txt
# Returns only errors
find . -name '*.md' | sort | xargs vale --minAlertLevel error --output line > ../../results.txt
These results can be used to generate documentation-related issues for Hackathons.
Vale 3.0 and later supports using two locations for rules. This change enables you to create and use your own custom rules alongside the rules included in a project.
To create and use custom rules locally on macOS:
Create a local file in the Application Support folder for Vale:
touch ~/Library/Application\ Support/vale/.vale.ini
Add these lines to the .vale.ini file you just created:
[*.md]
BasedOnStyles = local
If the folder ~/Library/Application Support/vale/styles/local does not exist,
create it:
mkdir ~/Library/Application\ Support/vale/styles/local
Add your desired rules to ~/Library/Application Support/vale/styles/local.
Rules in your local style directory are prefixed with local instead of gitlab
in Vale results, like this:
$ vale --minAlertLevel warning doc/ci/yaml/index.md
doc/ci/yaml/index.md
...[snip]...
3876:17 warning Instead of future tense 'will gitlab.FutureTense
be', use present tense.
3897:26 error Remove 'documentation' local.new-rule
✖ 1 error, 5 warnings and 0 suggestions in 1 file.