Skip to main content
go-swarm-icons
On this page

Renderer & SVG Utilities

Documents attribute layering, ARIA injection, and the Goldmark markdown extension.

The IconRenderer type that merges attributes and injects ARIA, plus the Goldmark extension's parser and renderer types.

Overview

IconRenderer merges SVG attributes across five precedence layers and injects ARIA attributes for accessibility. SVG parsing and sanitization are internal to the library: the FromFile and FromString constructors and every provider path sanitize incoming content automatically, with no public parsing functions to call.

For a guide on the five-layer merge and ARIA rules, see Attribute Management. For the sanitization pipeline details, see SVG Sanitization.

Types

IconRenderer

Go
type IconRenderer struct {
// unexported fields
}

Stores global default attributes, per-prefix attributes, and per-prefix suffix attributes. Created via NewIconRenderer.

Functions

NewIconRenderer

Go
func NewIconRenderer(
defaultAttrs map[string]string,
prefixAttrs map[string]map[string]string,
) *IconRenderer

Creates an IconRenderer with the given global default attributes (layer 2) and per-prefix attribute maps (layer 3). Either argument may be nil. Both maps are deep-copied.

Suffix attributes (layer 4) are configured separately via SetSuffixAttributes.

Go
import swarmicons "github.com/frostybee/go-swarm-icons"
r := swarmicons.NewIconRenderer(
map[string]string{"fill": "none", "stroke": "currentColor"},
map[string]map[string]string{
"tabler": {"stroke-width": "1.5"},
},
)

Methods

Render

Go
func (r *IconRenderer) Render(
icon *Icon,
prefix, iconName string,
callerAttrs map[string]string,
) *Icon

Applies the five-layer attribute merge and ARIA injection. Returns a new *Icon with the merged attributes. The original icon is not modified.

Merge order (lowest to highest precedence):

  1. Icon's own attributes.
  2. Global default attributes.
  3. Prefix attributes for prefix.
  4. Suffix attributes matching the end of iconName.
  5. Caller-provided attributes.

class is concatenated across all layers. All other keys: last layer wins. Empty-string values are skipped.

After merging, ARIA attributes are injected:

  • If aria-label, aria-labelledby, or role is present: sets role="img" (unless role is already set).
  • Otherwise: sets aria-hidden="true" and focusable="false" (unless already set).

SetSuffixAttributes

Go
func (r *IconRenderer) SetSuffixAttributes(
prefix, suffix string,
attrs map[string]string,
)

Registers attributes applied to icons under prefix whose name ends with "-{suffix}". For example, suffix "solid" matches icon name "check-solid".

When multiple suffixes match, the longest suffix wins. Pass an empty suffix ("") as a catch-all for icons that match no other suffix.

Go
import swarmicons "github.com/frostybee/go-swarm-icons"
r := swarmicons.NewIconRenderer(nil, nil)
r.SetSuffixAttributes("heroicons", "solid", map[string]string{
"fill": "currentColor",
})
r.SetSuffixAttributes("heroicons", "outline", map[string]string{
"fill": "none",
"stroke": "currentColor",
})
r.SetSuffixAttributes("heroicons", "", map[string]string{
"fill": "currentColor",
})

Goldmark Extension

The goldmark submodule (github.com/frostybee/go-swarm-icons/goldmark, package swarmgoldmark) adds inline icon syntax to Goldmark-processed Markdown.

Types

Extension

Go
type Extension struct {
Manager *swarmicons.IconManager
SilentOnMissing bool
}

Goldmark extender. Manager is required; Extend panics if it is nil. Set SilentOnMissing to true to render an HTML comment instead of returning an error for missing icons.

IconNode

Go
type IconNode struct {
ast.BaseInline
Name string
IconAttrs map[string]string
}

Inline AST node representing an :icon[prefix:name] reference.

Variables

Go
var KindIconNode = ast.NewNodeKind("Icon")

The ast.NodeKind for IconNode. Used to register the node renderer.

Functions

NewParser

Go
func NewParser() parser.InlineParser

Returns a Goldmark InlineParser that recognizes :icon[...] syntax. Trigger character: :.

NewRenderer

Go
func NewRenderer(manager *swarmicons.IconManager, silentOnMissing bool) renderer.NodeRenderer

Returns a Goldmark NodeRenderer that resolves IconNode references through the given IconManager. Transform attributes (size, rotate, flip, opacity, title) are applied via fluent methods; all other attributes are passed through to manager.Get as caller attributes.

Methods

Extension.Extend

Go
func (e *Extension) Extend(m goldmark.Markdown)

Registers the icon inline parser (priority 500) and node renderer (priority 500) with the Goldmark instance. Panics if e.Manager is nil.

IconNode.Kind

Go
func (n *IconNode) Kind() ast.NodeKind

Returns KindIconNode.

IconNode.Dump

Go
func (n *IconNode) Dump(source []byte, level int)

Writes a debug representation of the node.

For a usage guide, see Goldmark Integration.

See also

Edit this page

Last updated: