This release introduces support for custom output formats powered by Go's text/template
package (see #260).
You can now pass the path to a template file through the existing --output
option:
$ vale --output='path/to/my/template.tmpl` test.md
Your template files have access to all of the sprig
template functions, in addition to a few Vale-specific functions:
-
red
,blue
,yellow
, &underline
, which perform the text decoration used in Vale's default output style. -
newTable
,addRow
, andrenderTable
, which are utilities for consistent alignment.
Templates are passed a slice of Files
of the form
type ProcessedFile struct {
Alerts []core.Alert
Path string
}
Here's how you could re-implement Vale's default output style with a template:
{{- /* Keep track of our various counts */ -}}
{{- $e := 0 -}}
{{- $w := 0 -}}
{{- $s := 0 -}}
{{- $f := 0 -}}
{{- /* Range over the linted files */ -}}
{{- range .Files}}
{{$table := newTable true}}
{{- $f = add1 $f -}}
{{- .Path | underline | indent 1 -}}
{{- /* Range over the file's alerts */ -}}
{{- range .Alerts -}}
{{- $error := "" -}}
{{- if eq .Severity "error" -}}
{{- $error = .Severity | red -}}
{{- $e = add1 $e -}}
{{- else if eq .Severity "warning" -}}
{{- $error = .Severity | yellow -}}
{{- $w = add1 $w -}}
{{- else -}}
{{- $error = .Severity | blue -}}
{{- $s = add1 $s -}}
{{- end}}
{{- $loc := printf "%d:%d" .Line (index .Span 0) -}}
{{- $row := list $loc $error .Message .Check | toStrings -}}
{{- $table = addRow $table $row -}}
{{end -}}
{{- $table = renderTable $table -}}
{{end}}
{{- $e}} {{"errors" | red}}, {{$w}} {{"warnings" | yellow}} and {{$s}} {{"suggestions" | blue}} in {{$f}} {{$f | int | plural "file" "files"}}.
See the documentation for more information.
Changelog
864264a fix: ensure templates are only passed files with alerts