Repository navigation
Trim blank lines from the rendered <head> - #697
Conversation
Template actions that print nothing (assignments, false ifs) left their line's newline and indent behind, so view-source showed a run of empty lines between the robots meta and <title>. Line-leading actions now trim the whitespace before them and conditional tags carry their own newline. Fixes #649 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
The whitespace-only template change is consistent and correct across every branch (verified by standalone rendering), and it is covered by a new assertion with no behavioral or structural regressions.
No open issues 🎉
What changed in this PR
This PR fixes issue #649, where view-source of a rendered page showed runs of empty lines in the <head> — most visibly between the robots meta and <title>, plus smaller gaps (including inside the JSON-LD). The root cause was that template actions that emit nothing (variable assignments, false ifs) still left behind their line's newline + indentation. The fix applies Go template whitespace trimming ({{-) to line-leading actions and splits one-line conditional tags so the emitted markup carries its own newline, keeping templates/shared/_head.html the authoritative place that assembles every page's head.
Changes:
- Prefix line-leading, no-output actions with
{{-to trim the preceding whitespace, and split inline conditional tags ({{ if X }}<meta>{{ end }}) so the branch body is on its own line. - Make
{{ define "_head" -}}so<!DOCTYPE html>is the first byte of output. - Extend
TestHeadMetadatato assert the rendered head contains no blank lines on home, post, plugin, and search pages.
| File | Description |
|---|---|
| templates/shared/_head.html | Adds {{- trims and splits inline conditionals so empty-output actions no longer leave blank lines; trims the define body so output starts at <!DOCTYPE html>. |
| blog/blog_test.go | Adds a loop in TestHeadMetadata asserting the rendered head has no blank lines across home, post, plugin, and search pages. |
I manually traced the whitespace semantics through the changed branches and also rendered the template standalone across all paths (home, post, plugin, search, admin, login, the full site_is_person Person block with populated and empty optionals, and custom_header_code/plugin_head_html). Every branch produced no blank lines, and the Person JSON-LD remained structurally valid. The new test's regexp dependency is already imported and the head/get/check helpers are in scope.
Review effort: Balanced
Fixes #649.
Actions in
templates/shared/_head.htmlthat print nothing (variable assignments, falseifs) left their line's newline + indent behind, so view-source showed a run of empty lines between the robots meta and<title>(and smaller gaps further down, including inside the JSON-LD).{{-to trim the whitespace before them.{{ if X }}<meta>{{ end }}) are split so the branch carries its own newline.{{ define "_head" -}}so<!DOCTYPE html>is the first byte.TestHeadMetadatanow asserts the rendered head has no blank lines on home, post, plugin and search pages.Note: main currently fails to build tests due to stale
goblog/imports, andTestPostTypeListing/TestPageAndPostTypeAPI_NonAdminpanic oncommaListnot defined — both pre-existing on main and unrelated to this change. I verified locally with the imports rewritten temporarily.🤖 Generated with Claude Code