headels

package
v0.85.0-pre.2 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: BSD-3-Clause Imports: 9 Imported by: 0

README

kit/headels

github.com/vormadev/vorma/kit/headels

headels helps you build HTML <head> elements in code, then:

  • dedupe them with stable rules
  • split into title/meta/rest buckets
  • escape them for safe rendering
  • render a final head HTML block with section markers

Import

import "github.com/vormadev/vorma/kit/headels"

Typical Workflow

1) Build head elements
h := headels.New()
h.Title("Dashboard")
h.Description("Admin dashboard")
h.MetaPropertyContent("og:title", "Dashboard")
h.Link(
	h.Rel("stylesheet"),
	h.Href("/assets/app.css"),
	h.SelfClosing(),
)
2) Prepare for rendering
inst := headels.NewInstance("app")
sorted := inst.ToSortedAndPreEscapedHeadEls(h.Collect())
3) Render final HTML
headHTML, err := inst.Render(sorted)
if err != nil {
	return err
}
_ = headHTML

Render wraps output with comment markers based on your instance attribute:

  • <!-- data-<attr>="meta-start" --> ... <!-- data-<attr>="meta-end" -->
  • <!-- data-<attr>="rest-start" --> ... <!-- data-<attr>="rest-end" -->

Deduping Rules

  • by default, only one <title> and one <meta name="description"> survive
  • for matching unique rules, later elements replace earlier ones
  • other elements dedupe by content hash (later duplicate wins)
  • you can extend unique rules once via InitUniqueRules

Safety Notes

  • normal attributes are escaped
  • Attr.KnownSafe() marks an attribute value as trusted/pre-escaped
  • DangerousInnerHTML injects raw HTML and should only be used for trusted content

Builder Helpers

  • high-level tags: Title, Description, Meta, Link, Script, Style
  • attribute helpers: Name, Content, Property, Rel, Href, Src, Type, Charset, As, CrossOrigin
  • content helpers: TextContent, DangerousInnerHTML, SelfClosing
  • generic form: Add(...) if you need full control

API Coverage

Types
  • type Attr
  • type BooleanAttribute
  • type HeadEls
  • type InnerHTML
  • type Instance
  • type SelfClosing
  • type SortedAndPreEscapedHeadEls
  • type Tag
  • type TextContent
Exported Struct Fields
  • SortedAndPreEscapedHeadEls.Meta []*htmlutil.Element
  • SortedAndPreEscapedHeadEls.Rest []*htmlutil.Element
  • SortedAndPreEscapedHeadEls.Title *htmlutil.Element
Functions
  • func FromRaw(els []*htmlutil.Element) *HeadEls
  • func New() *HeadEls
  • func NewInstance(dataAttribute string) *Instance
Methods
  • func (h *HeadEls) Add(defs ...typeInterface)
  • func (h *HeadEls) AddElements(other *HeadEls)
  • func (h *HeadEls) As(as string) *Attr
  • func (h *HeadEls) Attr(name, value string) *Attr
  • func (h *HeadEls) BoolAttr(name string) BooleanAttribute
  • func (h *HeadEls) Charset(charset string) *Attr
  • func (h *HeadEls) Collect() []*htmlutil.Element
  • func (h *HeadEls) Content(content string) *Attr
  • func (h *HeadEls) CrossOrigin(crossOrigin string) *Attr
  • func (h *HeadEls) DangerousInnerHTML(content string) InnerHTML
  • func (h *HeadEls) Description(description string)
  • func (Attr) Type() htmlutilType
  • func (BooleanAttribute) Type() htmlutilType
  • func (InnerHTML) Type() htmlutilType
  • func (SelfClosing) Type() htmlutilType
  • func (Tag) Type() htmlutilType
  • func (TextContent) Type() htmlutilType
  • func (h *HeadEls) Href(href string) *Attr
  • func (inst *Instance) InitUniqueRules(e *HeadEls)
  • func (a *Attr) KnownSafe() *Attr
  • func (h *HeadEls) Link(defs ...typeInterface)
  • func (h *HeadEls) Meta(defs ...typeInterface)
  • func (h *HeadEls) MetaNameContent(name, content string)
  • func (h *HeadEls) MetaPropertyContent(property, content string)
  • func (h *HeadEls) Name(name string) *Attr
  • func (h *HeadEls) Property(property string) *Attr
  • func (h *HeadEls) Rel(rel string) *Attr
  • func (inst *Instance) Render(input *SortedAndPreEscapedHeadEls) (template.HTML, error)
  • func (h *HeadEls) Script(defs ...typeInterface)
  • func (h *HeadEls) SelfClosing() SelfClosing
  • func (h *HeadEls) Src(src string) *Attr
  • func (h *HeadEls) Style(defs ...typeInterface)
  • func (h *HeadEls) TextContent(content string) TextContent
  • func (h *HeadEls) Title(title string)
  • func (inst *Instance) ToSortedAndPreEscapedHeadEls(els []*htmlutil.Element) *SortedAndPreEscapedHeadEls
  • func (h *HeadEls) Type(type_ string) *Attr

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr

type Attr struct {
	// contains filtered or unexported fields
}

func (*Attr) KnownSafe

func (a *Attr) KnownSafe() *Attr

func (Attr) Type

func (Attr) Type() htmlutilType

type BooleanAttribute

type BooleanAttribute string

func (BooleanAttribute) Type

func (BooleanAttribute) Type() htmlutilType

type HeadEls

type HeadEls struct {
	// contains filtered or unexported fields
}

HeadEls is a collection of HTML head elements. It is safe for concurrent use.

func FromRaw

func FromRaw(els []*htmlutil.Element) *HeadEls

func New

func New() *HeadEls

func (*HeadEls) Add

func (h *HeadEls) Add(defs ...typeInterface)

Add appends a new element to the collection. Panics if no Tag is provided among the definitions.

func (*HeadEls) AddElements added in v0.83.0

func (h *HeadEls) AddElements(other *HeadEls)

func (*HeadEls) AppendElementsInto

func (h *HeadEls) AppendElementsInto(
	dst []*htmlutil.Element,
) []*htmlutil.Element

AppendElementsInto appends all current elements into dst and returns the resulting slice.

func (*HeadEls) As added in v0.83.0

func (h *HeadEls) As(as string) *Attr

func (*HeadEls) Attr

func (h *HeadEls) Attr(name, value string) *Attr

func (*HeadEls) BoolAttr added in v0.83.0

func (h *HeadEls) BoolAttr(name string) BooleanAttribute

func (*HeadEls) Charset added in v0.83.0

func (h *HeadEls) Charset(charset string) *Attr

func (*HeadEls) Collect

func (h *HeadEls) Collect() []*htmlutil.Element

func (*HeadEls) Content

func (h *HeadEls) Content(content string) *Attr

func (*HeadEls) CrossOrigin added in v0.83.0

func (h *HeadEls) CrossOrigin(crossOrigin string) *Attr

func (*HeadEls) DangerousInnerHTML added in v0.83.0

func (h *HeadEls) DangerousInnerHTML(content string) InnerHTML

func (*HeadEls) Description

func (h *HeadEls) Description(description string)

func (*HeadEls) Href

func (h *HeadEls) Href(href string) *Attr

func (*HeadEls) Len

func (h *HeadEls) Len() int

Len returns the current element count.

func (h *HeadEls) Link(defs ...typeInterface)

func (*HeadEls) Meta

func (h *HeadEls) Meta(defs ...typeInterface)

func (*HeadEls) MetaNameContent added in v0.83.0

func (h *HeadEls) MetaNameContent(name, content string)

func (*HeadEls) MetaPropertyContent added in v0.83.0

func (h *HeadEls) MetaPropertyContent(property, content string)

func (*HeadEls) Name

func (h *HeadEls) Name(name string) *Attr

func (*HeadEls) Property

func (h *HeadEls) Property(property string) *Attr

func (*HeadEls) Rel

func (h *HeadEls) Rel(rel string) *Attr

func (*HeadEls) Script added in v0.83.0

func (h *HeadEls) Script(defs ...typeInterface)

func (*HeadEls) SelfClosing added in v0.83.0

func (h *HeadEls) SelfClosing() SelfClosing

func (*HeadEls) Src added in v0.83.0

func (h *HeadEls) Src(src string) *Attr

func (*HeadEls) Style added in v0.83.0

func (h *HeadEls) Style(defs ...typeInterface)

func (*HeadEls) TextContent added in v0.83.0

func (h *HeadEls) TextContent(content string) TextContent

func (*HeadEls) Title

func (h *HeadEls) Title(title string)

func (*HeadEls) Type added in v0.83.0

func (h *HeadEls) Type(type_ string) *Attr

type InnerHTML

type InnerHTML string

func (InnerHTML) Type

func (InnerHTML) Type() htmlutilType

type Instance

type Instance struct {
	// contains filtered or unexported fields
}

func NewInstance

func NewInstance(dataAttribute string) *Instance

func (*Instance) InitUniqueRules

func (inst *Instance) InitUniqueRules(e *HeadEls)

func (*Instance) Render

func (inst *Instance) Render(
	input *SortedAndPreEscapedHeadEls,
) (template.HTML, error)

func (*Instance) ToSortedAndPreEscapedHeadEls

func (inst *Instance) ToSortedAndPreEscapedHeadEls(
	els []*htmlutil.Element,
) *SortedAndPreEscapedHeadEls

type SelfClosing

type SelfClosing bool

func (SelfClosing) Type

func (SelfClosing) Type() htmlutilType

type SortedAndPreEscapedHeadEls

type SortedAndPreEscapedHeadEls struct {
	Title *htmlutil.Element
	Meta  []*htmlutil.Element
	Rest  []*htmlutil.Element
}

type Tag

type Tag string

func (Tag) Type

func (Tag) Type() htmlutilType

type TextContent

type TextContent string

func (TextContent) Type

func (TextContent) Type() htmlutilType

Jump to

Keyboard shortcuts

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