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
type IconRenderer struct { // unexported fields}Stores global default attributes, per-prefix attributes, and per-prefix suffix attributes. Created via NewIconRenderer.
Functions
NewIconRenderer
func NewIconRenderer( defaultAttrs map[string]string, prefixAttrs map[string]map[string]string,) *IconRendererCreates 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.
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
func (r *IconRenderer) Render( icon *Icon, prefix, iconName string, callerAttrs map[string]string,) *IconApplies 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):
- Icon's own attributes.
- Global default attributes.
- Prefix attributes for
prefix. - Suffix attributes matching the end of
iconName. - 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, orroleis present: setsrole="img"(unlessroleis already set). - Otherwise: sets
aria-hidden="true"andfocusable="false"(unless already set).
SetSuffixAttributes
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.
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
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
type IconNode struct { ast.BaseInline Name string IconAttrs map[string]string}Inline AST node representing an :icon[prefix:name] reference.
Variables
var KindIconNode = ast.NewNodeKind("Icon")The ast.NodeKind for IconNode. Used to register the node renderer.
Functions
NewParser
func NewParser() parser.InlineParserReturns a Goldmark InlineParser that recognizes :icon[...] syntax. Trigger character: :.
NewRenderer
func NewRenderer(manager *swarmicons.IconManager, silentOnMissing bool) renderer.NodeRendererReturns 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
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
func (n *IconNode) Kind() ast.NodeKindReturns KindIconNode.
IconNode.Dump
func (n *IconNode) Dump(source []byte, level int)Writes a debug representation of the node.
For a usage guide, see Goldmark Integration.
See also
- Attribute Management: practical guide to the five-layer merge and ARIA injection
- SVG Sanitization: the nine-stage pipeline applied to all incoming SVG content