## Summary axe reports `empty-table-header` on 116 generated CLI reference pages and several hand-written pages: a header cell with no text is announced by screen readers as an unnamed column. This PR gives every table header cell in `docs/` text, and adds a lint check so empty header cells can't come back. ## How to review this PR - `docs/reference/cli/`: 116 files, these all generate identically, so I would only spot check 2-3 of them and skip the rest - `docs/*` outside of `docs/reference/cli/`: 6 files, normal review of these - `scripts`: 5 files, code review of the Bash and Go files to make sure we're properly inserting headers into each table ## What changed **Generator.** `scripts/clidocgen` wrote an empty header row (`| | |`) over each option's metadata table (Type, Environment, YAML, Default). It now writes `| Property | Value |`. Subcommand tables already had `Name | Purpose` and are unchanged. Regenerated with `make docs/reference/cli/index.md`: 116 files under `docs/reference/cli/` change. The header rows change, and the body rows of each option table are re-padded to the wider first column; the content is otherwise identical. Every option on those 116 pages now shows a visible `Property | Value` header row above Type, Environment, YAML, and Default, so this is a visual change on the docs site as well as a screen reader one. The CLI's own `--help` output doesn't use this template, so it's unchanged. **Audit logs.** `docs/admin/security/audit-logs.md` already has `Field | Tracked` headers on `main` (the generator writes them), so there was nothing to change there; the live-site finding predates that. **Hand-written tables.** - `docs/admin/networking/port-forwarding.md`: the CORS matrix had three empty spanning corner cells. The corner now reads `From user` and `From workspace` (spanning the three row-header columns, beside the Alice/Bob and Workspace rows) and `To user`, `To workspace`, `To app` (over the three row-header columns). The grid is still 3 header rows by 7 columns. The From cells have `scope="row"` and the other header-row cells `scope="col"`, so the From labels don't become column headers for the body's label columns; the body's user, workspace, and app labels are `<th scope="row">`; and a `<caption>` says columns are the origin and rows the destination. - `groups-roles.md`, `airgap.md`, `scale-testing.md`, `workspace-tags.md`: these four corner cells are already filled by #30873. The new check would fail CI without them, so this PR carries the same four lines, byte-identical to #30873 (`Permission`, `Component`, `Scenario`, `Input`), so whichever PR merges second rebases without conflict. If #30873 merges first, these hunks drop out. **New check.** `scripts/docshtmlcheck` (`make lint/docs-html`, part of `make lint`) now also reports `empty-table-header`. I put it there because that checker already parses every Markdown file under `docs/`, generated pages included, with goldmark and tokenizes raw HTML with `golang.org/x/net/html`, and it already runs in CI. A markdownlint custom rule would have needed a new JS rule and HTML parsing there. - Markdown pipe tables: the header row, via goldmark's GFM table extension. - Raw HTML tables: every `<th>`, tracked across HTML blocks, with entities decoded and whitespace trimmed, so `<th></th>`, `<th> </th>`, and `<th><br></th>` are all reported. Markdown between the HTML blocks of an open `<th>` counts as its text. Tables in code blocks are ignored. - Output: `docs/x.md:29: empty-table-header: header cell 1 has no text (table's first header "Auditor")`, plus a summary footer. - The tokenizer loop moved out of `scanNode` into a shared `tokenizeSegments` helper so both checks use it; `scanNode`'s behavior is unchanged. - The style guide's checks table gets one row; the README for the checker describes the rule. ## Testing - Tests first: `TestCheckTables` (now 22 cases) and `TestRunEmptyTableHeader` failed against a stub, then pass. The existing `docshtmlcheck` tests pass unchanged. - `go run ./scripts/docshtmlcheck` on `main` reports 1,561 empty header cells (116 CLI pages, port-forwarding, and the four #30873 tables); on this branch it reports none. - `go test ./scripts/docshtmlcheck/ ./scripts/clidocgen/ ./scripts/docgenenv/`, golangci-lint on the changed packages, `make lint/docs-html lint/docs-manifest lint/docs-gen-filter lint/emdash`, `pnpm run check-docs`, and Vale on the changed hand-written pages all pass. - `make gen` couldn't run to completion locally (no `protoc-gen-go`); CI's gen check covers it. The CLI pages were regenerated with the repo's own target. Review feedback applied incfd29a2160(entities decoded in Markdown text, image alt text and autolinks count as text),709a58f603(per-kind counts, finding order, CLI generator note),8e61ff3afa(CORS matrix scopes and caption), andb807bcbbc7and2871f89bdf(style guide rule section, `make lint/docs-html` in the checks table, coverage counts). Linear: DOCS-1308 --- 🤖 Built with AI assistance.
267 lines
8.2 KiB
Go
267 lines
8.2 KiB
Go
package main
|
|
|
|
import (
|
|
"cmp"
|
|
_ "embed"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"text/template"
|
|
|
|
"github.com/acarl005/stripansi"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/buildinfo"
|
|
"github.com/coder/coder/v2/scripts/atomicwrite"
|
|
"github.com/coder/coder/v2/scripts/docgenenv"
|
|
"github.com/coder/flog"
|
|
"github.com/coder/serpent"
|
|
)
|
|
|
|
//go:embed command.tpl
|
|
var commandTemplateRaw string
|
|
|
|
var commandTemplate *template.Template
|
|
|
|
func init() {
|
|
commandTemplate = template.Must(
|
|
template.New("command.tpl").Funcs(template.FuncMap{
|
|
"visibleSubcommands": visibleSubcommands,
|
|
"visibleOptions": func(cmd *serpent.Command) []serpent.Option {
|
|
var visible []serpent.Option
|
|
for _, opt := range cmd.Options {
|
|
if opt.Hidden {
|
|
continue
|
|
}
|
|
// Skip YAML-only options that have no CLI flag; documenting them
|
|
// as if they were flags is misleading in the CLI reference.
|
|
if opt.Flag == "" && opt.FlagShorthand == "" {
|
|
continue
|
|
}
|
|
visible = append(visible, opt)
|
|
}
|
|
return visible
|
|
},
|
|
"newLinesToBr": func(s string) string {
|
|
return strings.ReplaceAll(s, "\n", "<br/>")
|
|
},
|
|
"wrapCode": func(s string) string {
|
|
return fmt.Sprintf("<code>%s</code>", s)
|
|
},
|
|
"subcommandLink": subcommandLink,
|
|
// frontMatter renders the page's YAML front matter through the
|
|
// shared docgenenv emitter, so the CLI and API generators cannot
|
|
// drift on field set, ordering, or escaping. The CLI index mirrors
|
|
// the "Command Line" manifest route (main populates cliIndexRoute
|
|
// before the template runs); every other page uses the command's
|
|
// own name and short description.
|
|
"frontMatter": func(cmd *serpent.Command) string {
|
|
if cmd.FullName() == "coder" {
|
|
return docgenenv.FrontMatter(cliIndexRoute)
|
|
}
|
|
return docgenenv.FrontMatter(cliCommandRoute(cmd))
|
|
},
|
|
// generatedContentBanner emits the shared body banner that marks
|
|
// the whole page as generated, sourced from one constant so the CLI
|
|
// and API generators cannot drift on its wording.
|
|
"generatedContentBanner": func() string {
|
|
return docgenenv.GeneratedContentBanner
|
|
},
|
|
// tableHeader opens each option's metadata table (Type,
|
|
// Environment, YAML, Default). The header cells need text: an
|
|
// empty one is announced by screen readers as an unnamed column.
|
|
"tableHeader": func() string {
|
|
return `| Property | Value |
|
|
| --- | --- |`
|
|
},
|
|
"typeHelper": func(opt *serpent.Option) string {
|
|
switch v := opt.Value.(type) {
|
|
case *serpent.Enum:
|
|
return strings.Join(v.Choices, "\\|")
|
|
case *serpent.EnumArray:
|
|
return fmt.Sprintf("[%s]", strings.Join(v.Choices, "\\|"))
|
|
default:
|
|
return v.Type()
|
|
}
|
|
},
|
|
},
|
|
).Parse(strings.TrimSpace(commandTemplateRaw)),
|
|
)
|
|
}
|
|
|
|
// visibleSubcommands returns cmd's direct subcommands that are not hidden.
|
|
// Hidden commands (and their subtrees) get no reference page.
|
|
func visibleSubcommands(cmd *serpent.Command) []*serpent.Command {
|
|
var visible []*serpent.Command
|
|
for _, sub := range cmd.Children {
|
|
if sub.Hidden {
|
|
continue
|
|
}
|
|
visible = append(visible, sub)
|
|
}
|
|
return visible
|
|
}
|
|
|
|
// inlineCode wraps s in backticks. Docs renderers show a title that is
|
|
// entirely wrapped in one pair of backticks as inline code.
|
|
func inlineCode(s string) string {
|
|
return "`" + s + "`"
|
|
}
|
|
|
|
// docsDescription returns cmd's Short as a page description. CLI help
|
|
// leaves Short unpunctuated, but docs descriptions are sentences and the
|
|
// manifest check requires them to end with a period.
|
|
func docsDescription(cmd *serpent.Command) string {
|
|
d := strings.TrimSpace(cmd.Short)
|
|
if d == "" || strings.HasSuffix(d, ".") {
|
|
return d
|
|
}
|
|
return d + "."
|
|
}
|
|
|
|
// cliCommandRoute maps a serpent command to the docgenenv.Route whose per-page
|
|
// metadata the CLI generator mirrors into that command's page front matter.
|
|
// The page title is the full invocation (for example "`coder users list`"), so
|
|
// the page reads as a command even though its sidebar entry only shows the
|
|
// last verb (see cliManifestRoute).
|
|
func cliCommandRoute(cmd *serpent.Command) docgenenv.Route {
|
|
return docgenenv.Route{
|
|
Title: inlineCode(cmd.FullName()),
|
|
Description: docsDescription(cmd),
|
|
}
|
|
}
|
|
|
|
// cliManifestRoute builds the manifest nav entry for cmd and, recursively, its
|
|
// visible subcommands, so the sidebar nests the same way the command tree
|
|
// does. Each entry is titled with only its own verb (for example "`list`"
|
|
// under "`users`") because its ancestors already appear above it in the nav.
|
|
func cliManifestRoute(cmd *serpent.Command) docgenenv.Route {
|
|
return docgenenv.Route{
|
|
Title: inlineCode(cmd.Name()),
|
|
Description: docsDescription(cmd),
|
|
Path: path.Join("reference/cli", fmtDocFilename(cmd)),
|
|
Children: cliManifestChildren(cmd),
|
|
}
|
|
}
|
|
|
|
// cliManifestChildren returns the manifest entries for cmd's visible
|
|
// subcommands, sorted by name so the output is deterministic.
|
|
func cliManifestChildren(cmd *serpent.Command) []docgenenv.Route {
|
|
subs := visibleSubcommands(cmd)
|
|
slices.SortFunc(subs, func(a, b *serpent.Command) int {
|
|
return cmp.Compare(a.Name(), b.Name())
|
|
})
|
|
var children []docgenenv.Route
|
|
for _, sub := range subs {
|
|
children = append(children, cliManifestRoute(sub))
|
|
}
|
|
return children
|
|
}
|
|
|
|
// cliIndexRouteFrom returns the CLI index page's route: a copy of the "Command
|
|
// Line" manifest route with its nav children dropped. Copying the whole route
|
|
// instead of enumerating fields means the index front matter mirrors every
|
|
// current and future per-page field (including curated icon_path and state)
|
|
// automatically, so it can't drift from the shared docgenenv.FrontMatter
|
|
// emitter the way a hand-written field list would.
|
|
func cliIndexRouteFrom(cmdLine docgenenv.Route) docgenenv.Route {
|
|
cmdLine.Children = nil
|
|
return cmdLine
|
|
}
|
|
|
|
// fmtDocFilename returns the path of cmd's reference page relative to the CLI
|
|
// reference directory. Pages nest by command path so the URL mirrors the
|
|
// invocation: "coder users list" is users/list.md, and a command with visible
|
|
// subcommands is the index.md of its own directory (users/index.md) so its
|
|
// subcommands' pages sit beneath it.
|
|
func fmtDocFilename(cmd *serpent.Command) string {
|
|
var parts []string
|
|
for c := cmd; c.Parent != nil; c = c.Parent {
|
|
parts = append([]string{c.Name()}, parts...)
|
|
}
|
|
if len(parts) == 0 || len(visibleSubcommands(cmd)) > 0 {
|
|
return path.Join(append(parts, "index.md")...)
|
|
}
|
|
return path.Join(parts...) + ".md"
|
|
}
|
|
|
|
// subcommandLink returns the link from a parent command's page to sub's page.
|
|
// A parent page is always the index.md of the directory its subcommands live
|
|
// in, so the link is relative to that directory.
|
|
func subcommandLink(sub *serpent.Command) string {
|
|
parentDir := path.Dir(fmtDocFilename(sub.Parent))
|
|
rel := fmtDocFilename(sub)
|
|
if parentDir != "." {
|
|
rel = strings.TrimPrefix(rel, parentDir+"/")
|
|
}
|
|
return "./" + rel
|
|
}
|
|
|
|
func writeCommand(w io.Writer, cmd *serpent.Command) error {
|
|
var b strings.Builder
|
|
err := commandTemplate.Execute(&b, cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
content := stripansi.Strip(b.String())
|
|
|
|
// Remove the version and its right space, since during this script running
|
|
// there is no build info available
|
|
content = strings.ReplaceAll(content, buildinfo.Version()+" ", "")
|
|
|
|
// Remove references to the current working directory
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
content = strings.ReplaceAll(content, cwd, ".")
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
content = strings.ReplaceAll(content, homedir, "~")
|
|
|
|
_, err = w.Write([]byte(content))
|
|
return err
|
|
}
|
|
|
|
func genTree(dir string, cmd *serpent.Command, wroteLog map[string]*serpent.Command) error {
|
|
if cmd.Hidden {
|
|
return nil
|
|
}
|
|
|
|
pagePath := filepath.Join(dir, filepath.FromSlash(fmtDocFilename(cmd)))
|
|
if err := os.MkdirAll(filepath.Dir(pagePath), 0o755); err != nil {
|
|
return xerrors.Errorf("create directory for %q: %w", pagePath, err)
|
|
}
|
|
|
|
var buf strings.Builder
|
|
err := writeCommand(&buf, cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = atomicwrite.File(pagePath, []byte(buf.String()))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
flog.Successf(
|
|
"wrote\t%s",
|
|
pagePath,
|
|
)
|
|
wroteLog[pagePath] = cmd
|
|
for _, sub := range cmd.Children {
|
|
err = genTree(dir, sub, wroteLog)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|