templatecheck

package
v0.55.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package templatecheck statically analyses Hugo templates for front-matter rendering invariants. The single rule today: every reference to `.Params.summary` must either be a presence predicate or flow through `.RenderString` so backticks in the value render as `<code>` rather than ship as literal characters.

Implementation: text/template/parse with parse.SkipFuncCheck — so Hugo's funcs (`dict`, `partial`, `printf`, …) parse without being declared. The walker descends into every tree the parser produced, not just `tree.Root`, because Hugo layouts wrap their content in `{{ define "main" }}...{{ end }}` blocks whose bodies live in separate trees.

See docs/development/website-config.md for the safe/forbidden shape enumeration this package enforces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Violation

type Violation struct {
	Path string
	Line int
	Why  string
}

Violation records one misuse of `.Params.summary` in a Hugo template, located by file path and line within that file.

func Scan

func Scan(path, content string) ([]Violation, error)

Scan parses one Hugo template (`content`) and returns every `.Params.summary` reference that violates the rule documented on the package. `path` is used only to populate Violation.Path for diagnostics; the function does not touch the filesystem.

Safe shapes:

  • `if` / `else if` predicate, including compound forms (`if and ...`, `if or ...`) and subfield access (`if .Params.summary.HTML`).
  • Argument to a `.RenderString` call — positional, piped, or nested inside a sub-pipeline whose output flows into `.RenderString`. Qualified receivers like `$.RenderString` and `.Page.RenderString` are recognised.

Forbidden shapes:

  • `with` / `else with .Params.summary` — the body rebinds the dot and emits the value raw.
  • `range .Params.summary` — iterates the string rune-by-rune and emits each code point as an integer.
  • `template`/`block` invocations passing the summary as the sub-template's dot.
  • Variable assignment binding the summary to a name, including the `if $s := .Params.summary` form.
  • Any value-emitting action whose pipe references the summary without reaching a `.RenderString` call.

Field-name comparisons are case-insensitive to match Hugo's case-insensitive Params map.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL