plugin

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package plugin provides the plugin interface, registry, preset system, and execution engine for the SVG optimization pipeline.

This mirrors SVGO's plugin architecture:

  • Plugins are functions that receive the AST + params and return a Visitor
  • Plugins are registered in a global registry by name
  • Presets group multiple plugins into an ordered execution list
  • invokePlugins iterates plugins sequentially, calling each fn then visiting the AST

Index

Constants

This section is empty.

Variables

View Source
var PresetDefaultPluginNames = []string{
	"removeDoctype",
	"removeXMLProcInst",
	"removeComments",
	"removeDeprecatedAttrs",
	"removeMetadata",
	"removeEditorsNSData",
	"cleanupAttrs",
	"mergeStyles",
	"inlineStyles",
	"minifyStyles",
	"cleanupIds",
	"removeUselessDefs",
	"cleanupNumericValues",
	"convertColors",
	"removeUnknownsAndDefaults",
	"removeNonInheritableGroupAttrs",
	"removeUselessStrokeAndFill",
	"cleanupEnableBackground",
	"removeHiddenElems",
	"removeEmptyText",
	"convertShapeToPath",
	"convertEllipseToCircle",
	"moveElemsAttrsToGroup",
	"moveGroupAttrsToElems",
	"collapseGroups",
	"convertPathData",
	"convertTransform",
	"removeEmptyAttrs",
	"removeEmptyContainers",
	"mergePaths",
	"removeUnusedNS",
	"sortAttrs",
	"sortDefsChildren",
	"removeDesc",
}

PresetDefaultPluginNames lists the 34 plugins in preset-default order. Matches SVGO's plugins/preset-default.js exactly.

Functions

func Has

func Has(name string) bool

Has checks if a plugin is registered.

func InvokePlugins

func InvokePlugins(
	root *svgast.Root,
	info *PluginInfo,
	pluginConfigs []PluginConfig,
	globalOverrides map[string]any,
) error

InvokePlugins resolves plugin configs and invokes them on the AST. This is the main entry point called from the optimize pipeline.

It matches SVGO's invokePlugins flow:

  1. For each plugin config, resolve it (find the builtin fn, merge params)
  2. Call plugin.fn(ast, params, info) to get a Visitor
  3. If visitor is non-nil, call Visit(ast, visitor)

func InvokeResolved

func InvokeResolved(root *svgast.Root, info *PluginInfo, plugins []*ResolvedPlugin)

InvokeResolved invokes already-resolved plugins on the AST. Used by presets that have already resolved their sub-plugins.

func Names

func Names() []string

Names returns all registered plugin names in no particular order.

func Register

func Register(p *Plugin)

Register adds a plugin to the global registry. Panics if a plugin with the same name is already registered.

Types

type Plugin

type Plugin struct {
	// Name is the unique plugin identifier (e.g. "removeComments").
	Name string

	// Description is a human-readable description of what the plugin does.
	Description string

	// Fn is the plugin implementation function.
	Fn PluginFunc

	// IsPreset indicates this is a preset (contains sub-plugins).
	IsPreset bool

	// Plugins holds the sub-plugins for presets.
	// Only used when IsPreset is true.
	Plugins []*Plugin
}

Plugin represents a registered plugin with metadata.

func CreatePreset

func CreatePreset(name string, plugins []*Plugin) *Plugin

CreatePreset creates a preset plugin that groups multiple plugins. Matches SVGO's createPreset() function in plugins.js.

A preset is itself a plugin whose fn calls invokePlugins on its sub-plugins. Preset params support:

  • "overrides": map[string]any — per-plugin param overrides (or false to disable)
  • "floatPrecision": float64 — global float precision for all sub-plugins

func Get

func Get(name string) *Plugin

Get returns a registered plugin by name, or nil if not found.

func RegisterPresetDefault

func RegisterPresetDefault() *Plugin

RegisterPresetDefault creates and registers the preset-default plugin. This should be called after all individual plugins are registered. Returns the created preset plugin.

type PluginConfig

type PluginConfig struct {
	// Name is the plugin name (required).
	Name string

	// Params is plugin-specific parameters (optional).
	Params map[string]any

	// Fn is a custom plugin function (optional).
	// If nil, the builtin plugin is looked up by Name.
	Fn PluginFunc
}

PluginConfig represents a user-specified plugin configuration. This is the input format — it gets resolved against the registry.

type PluginFunc

type PluginFunc func(root *svgast.Root, params map[string]any, info *PluginInfo) *svgast.Visitor

PluginFunc is the function signature for plugin implementations.

It receives:

  • root: the full AST tree
  • params: merged parameters (plugin defaults + global overrides + user overrides)
  • info: contextual information (file path, multipass count)

It returns a Visitor to traverse the AST, or nil to skip this plugin. Matches SVGO's: (ast, params, info) => visitor | null

type PluginInfo

type PluginInfo struct {
	// Path is the file path of the SVG being optimized.
	Path string

	// MultipassCount is the current pass number (0-based) during multipass optimization.
	MultipassCount int
}

PluginInfo provides contextual information to plugin functions. Matches SVGO's info object passed to plugin fn.

type ResolvedPlugin

type ResolvedPlugin struct {
	Name   string
	Params map[string]any
	Fn     PluginFunc
}

ResolvedPlugin is a plugin ready for execution with its merged params. This is the result of resolving a PluginConfig against the registry.

Jump to

Keyboard shortcuts

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