The v3.30.0 release of Vale includes a significant, long-requested feature: the ability to lint a page’s markup, and not just its prose.
Vale has always read markup in order to skip it. It finds the prose in a Markdown page, a reStructuredText file, or a Go comment and checks that.
The problem is that a lot of what a style guide cares about isn’t prose at all: whether the headings skip a level, whether a list is indented by two spaces or four, whether a code block states its language. Up to this point, those questions had to go to a second tool.
With v3.30.0, a rule can read a page exactly as it’s written, one block at a time. There’s no new extension point to learn, either — the ones you already know (existence, metric, consistency, and script) do the work, and nearly everything new in this release is a scope.
People have been asking for this since 2018. In 2020, a writer on GitLab’s documentation team asked for a way to match raw markup. Here’s what I said at the time:
I’m of the belief that a well-implemented linter for Markdown (or another such format) shouldn’t rely on regex for its understanding of the format.
That request eventually became scope: raw, which gives a rule the entire file as a single string. This works well enough for simple patterns, but the problem I was worried about is still there. A rule on raw has to figure out for itself where a code block ends and whether or not a # is a heading.
I still think that answer was the right one. The difference now is that a rule doesn’t have to understand the format — Vale’s parser already does, and it can tell a rule where everything is.
In order to get a better idea of how this works, we’re going to look at a common request: a blank line after every code block. Here’s the rule:
yaml
Copy
# styles/House/CodeBlocks.ymlextends: metricmessage: "Put a blank line after this code block."scope: 'raw & doc(pre.fenced)'formula: blank_after < 1 && last == 0
The new part is the scope. raw & doc(...) gives the rule each block that the selector matches, with its markers and indentation, exactly as it appears in the file.
The selector itself is CSS over the page’s structure (the same one you’d use in a doc(...) scope for prose).
The raw half is what makes this a rule about markup. On its own, doc(pre.fenced) is a prose scope: it gives a rule the text inside an element with the markup removed, and a code block has no prose to give. Adding raw asks for the same blocks as they’re written instead:
Pick a selector to see what each scope hands the rule.
pre.fencedh1liCode block
The page
1# Install **Vale**
2
3Run the installer:
4
5```sh
6brew install vale
7```
8Then sync your styles:
9
10* Run the `sync` command.
11* Commit the [config](.vale.ini).
doc(pre.fenced)
The prose inside, markup removed.
Nothing. Code isn’t prose, so there is no text here for a rule to read.
raw & doc(pre.fenced)
Each block as written, and where it sits.
```sh
brew install vale
```
lines 5–7 · blank_before 1 · blank_after 0
One page and three selectors. A doc(...) scope reads an element's prose; raw & doc(...) reads the same element's lines as written, with the blank lines around it counted.
Each block also comes with a set of counts that a metric formula can read: its indentation, the blank lines around it, a heading’s level and the level of the heading before it, an item’s position in its list, and so on. This means that a rule for skipped heading levels is a single formula:
The :heading selector is also new. It matches a heading of any level, so we don’t have to write out h1, h2, h3, h4, h5, h6, and it works with the rest of CSS: doc(:heading:has(code)) is every heading that contains a code span.
Now, let’s say we have a page that jumps from a level-one heading to a level-three one on line 3 and closes a code block on line 7 with text directly under it. When we run Vale, we get both alerts:
console
$ vale --output=line guide.mdguide.md:3:1:House.HeadingLevels:Don't skip a heading level.guide.md:7:1:House.CodeBlocks:Put a blank line after this code block.
Many markup rules are about how a page starts, and front matter makes that harder to say than it should be: is the “first” block the front matter or the heading under it? There are two new scopes to help with this.
The first is :first-block, which is the block a page opens with after any front matter. Here’s a rule that requires a title, whether or not the page has front matter:
yaml
Copy
extends: existencemessage: "Open the page with a title."scope: 'raw & doc(:first-block:not(h1)::first-line)'
You may have noticed that this rule doesn’t have any tokens. When a rule is scoped to a part of the page as written, the scope can be the entire rule — Vale simply reports whatever the scope selects.
The second is raw & frontmatter, which is the front matter itself. It has the same counts as a block, so our code block rule from above only needs a different scope:
yaml
Copy
extends: metricmessage: "Put a blank line after the front matter."scope: 'raw & frontmatter'formula: blank_after < 1 && last == 0
Blocks contain code spans, links, emphasis, and footnotes, and a rule that’s about one of those shouldn’t have to find it first. Each one now has a scope of its own:
Scope
What it selects
raw & code
Each code span
raw & emphasis, raw & strong
Each emphasis, from its opening marker to its closing one
raw & delimiter
A pair of emphasis markers that didn’t make emphasis, * like this *
raw & url
Where each link goes (url.bare is an address written on its own)
raw & label
The [ref] a link refers to and the [label] a definition is written under
raw & info
What a fence says about its code
raw & math
The content between dollar signs
raw & footnote.reference, raw & footnote.definition
Each footnote, where it’s referred to and where it’s defined
Some of these scopes know more than just their location. For example, a link written with a label knows whether or not the page defines that label, and a definition knows whether or not any link uses it. As a result, we can catch a broken reference link without writing a pattern:
yaml
Copy
extends: existencemessage: "No definition on this page has the label this link uses."scope: 'raw & link.full.undefined'
The delimiter scope solves a similar problem. If you put spaces inside of your emphasis markers, you don’t get emphasis — the parser leaves the markers as text, which used to mean there was nothing for a rule to select. Now, the pair is a scope:
yaml
Copy
extends: existencemessage: "Remove the spaces inside these emphasis markers."scope: 'raw & delimiter'
You can also use any of these names to exclude content. For instance, adding & ~code keeps a rule out of code spans and & ~math keeps it out of math.
Most markup rules come down to a choice between equivalent ways of writing the same thing. A consistency rule can now give each of them a name:
yaml
Copy
extends: consistencymessage: "Write emphasis one way on a page: '*' or '_'."nonword: truescope: 'raw & emphasis & ~emphasis.intraword'options: asterisk: '\A\*' underscore: '\A_'
By default, a page keeps whichever one it uses first. If you’d rather require one of them on every page, you can set it in your .vale.ini file:
Markup rules tend to have settings: which punctuation a heading can end with, what level a title should be, which languages a code block can name. While we could copy a rule every time we want a different value, wouldn’t it be nice if we could just set it from our configuration?
This is where params come into play. Any rule can now declare a list of named values and use each one wherever it writes ${name}:
yaml
Copy
# styles/House/HeadingEnds.ymlextends: existencemessage: "Don't end a heading with '%s'."nonword: truescope: 'raw & doc(:heading)'params: marks: '.,:!'tokens: - '[${marks}]$'
To change a value, we use the same bracket syntax that Std introduced:
ini
Copy
House.HeadingEnds[marks] = .!
In a pattern, a value matches exactly what it spells — the . above is a period, not “any character” — and a list matches any one of its items. You can also use a param in a rule’s message or scope.
Some rules need to look at more than one thing at a time (a table’s columns, for example), and a pattern isn’t a good fit for that. A script rule can now read a block’s counts and spans by name. When it’s scoped to the whole page, it’s given every block; when it’s scoped to a table, it’s given the table’s rows: each row’s text, its cells, and the position of each pipe.
A design like this is only useful if real rules actually fit into it. To find out, I wrote all 53 markdownlint rules as a Vale style, which is now available as the Markdown package:
Most of its rules are just a scope and a pattern or formula. They have the same names that markdownlint gives them (HeadingIncrement, UlIndent, NoBareUrls).
I used markdownlint as the reference. To check the style against it, I ran both over five documentation sets with their default settings:
Documentation
Alerts from markdownlint
Missed by the style
Extra from the style
GitLab
94,815
0
0
Node.js
75,102
0
0
Docker
25,894
0
0
vale.sh
2,273
0
0
The Rust Programming Language
1,461
0
0
That’s 199,545 alerts that the style reports on the same lines, with none missed and none extra.
None of this is a reason to switch. If Markdown is all you lint, markdownlint is the right tool, and it’s the reference that every rule in the package is tested against.
The package is for teams that already run Vale and want their markup rules in the same pass.
The ability to lint markup means that your style guide, spelling, terminology, and markup rules can all live in a single .vale.ini file. There’s one set of comments for switching a rule off and one list of alerts in your editor and in CI.
It also means that the rules are yours to change. If your team wants headings no deeper than three levels, or a blank line before every table but not after, you only need a few lines of YAML against the same scopes that the package uses.
The block scopes and their counts already work in the other formats Vale supports, including AsciiDoc and reStructuredText. The scopes inside of a block are currently Markdown-only, and I’ll be working on bringing them to the other formats next.
If you have any questions or run into any problems, feel free to open an issue at the Vale repository.