Documentation
¶
Index ¶
- Variables
- func BuiltInDefinitions() []interfaces.ShortcodeDefinition
- func NewNoOpService() interfaces.ShortcodeService
- func NoOpMetrics() interfaces.ShortcodeMetrics
- func RegisterBuiltIns(registry interfaces.ShortcodeRegistry, names []string) error
- type DefinitionValidator
- type NoOpSanitizer
- type Registry
- type Renderer
- type RendererOption
- type Sanitizer
- type Service
- func (s *Service) Process(ctx context.Context, content string, opts interfaces.ShortcodeProcessOptions) (string, error)
- func (s *Service) Registry() interfaces.ShortcodeRegistry
- func (s *Service) Render(ctx interfaces.ShortcodeContext, shortcode string, params map[string]any, ...) (template.HTML, error)
- type ServiceOption
- func WithDefaultCache(cache interfaces.CacheProvider) ServiceOption
- func WithDefaultSanitizer(sanitizer interfaces.ShortcodeSanitizer) ServiceOption
- func WithLogger(logger interfaces.Logger) ServiceOption
- func WithMetrics(metrics interfaces.ShortcodeMetrics) ServiceOption
- func WithParser(parser interfaces.ShortcodeParser) ServiceOption
- func WithWordPressPreprocessor(pre *parserpkg.WordPressPreprocessor) ServiceOption
- func WithWordPressSyntax(enabled bool) ServiceOption
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDuplicateDefinition indicates an attempt to register a shortcode name twice. ErrDuplicateDefinition = errors.New("shortcode: duplicate definition") // ErrInvalidDefinition occurs when a definition fails schema validation. ErrInvalidDefinition = errors.New("shortcode: invalid definition") )
var ( // ErrUnknownParameter indicates the request supplied an unexpected parameter. ErrUnknownParameter = errors.New("shortcode: unknown parameter") // ErrMissingParameter indicates a required parameter was not provided. ErrMissingParameter = errors.New("shortcode: missing required parameter") // ErrParameterType indicates a parameter could not be coerced to the requested type. ErrParameterType = errors.New("shortcode: parameter type mismatch") )
Functions ¶
func BuiltInDefinitions ¶
func BuiltInDefinitions() []interfaces.ShortcodeDefinition
BuiltInDefinitions returns the core shortcode catalogue shipped with go-cms.
func NewNoOpService ¶
func NewNoOpService() interfaces.ShortcodeService
NewNoOpService returns a shortcode service that leaves content untouched.
func NoOpMetrics ¶
func NoOpMetrics() interfaces.ShortcodeMetrics
NoOpMetrics returns a metrics recorder that drops every observation.
func RegisterBuiltIns ¶
func RegisterBuiltIns(registry interfaces.ShortcodeRegistry, names []string) error
RegisterBuiltIns registers the built-in shortcode definitions on the provided registry. When names is empty, every built-in shortcode is registered.
Types ¶
type DefinitionValidator ¶
type DefinitionValidator interface {
ValidateDefinition(def interfaces.ShortcodeDefinition) error
}
DefinitionValidator abstracts definition validation so callers can customise behaviour in tests.
type NoOpSanitizer ¶
type NoOpSanitizer struct{}
NoOpSanitizer bypasses all sanitisation checks and returns input verbatim.
func (NoOpSanitizer) ValidateAttributes ¶
func (NoOpSanitizer) ValidateAttributes(map[string]any) error
func (NoOpSanitizer) ValidateURL ¶
func (NoOpSanitizer) ValidateURL(string) error
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is the thread-safe in-memory implementation of interfaces.ShortcodeRegistry.
func NewRegistry ¶
func NewRegistry(validator DefinitionValidator) *Registry
NewRegistry constructs a registry using the supplied validator.
func (*Registry) Get ¶
func (r *Registry) Get(name string) (interfaces.ShortcodeDefinition, bool)
Get returns the stored definition.
func (*Registry) List ¶
func (r *Registry) List() []interfaces.ShortcodeDefinition
List returns all registered definitions in name order.
func (*Registry) Register ¶
func (r *Registry) Register(def interfaces.ShortcodeDefinition) error
Register stores a definition if it passes validation and the name is not taken.
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer executes shortcode definitions and produces sanitised HTML output.
func NewRenderer ¶
func NewRenderer(registry interfaces.ShortcodeRegistry, validator *Validator, opts ...RendererOption) *Renderer
NewRenderer constructs a renderer using the provided registry and validator.
func (*Renderer) Render ¶
func (r *Renderer) Render(ctx interfaces.ShortcodeContext, shortcode string, params map[string]any, inner string) (template.HTML, error)
Render executes the shortcode and returns sanitised HTML.
func (*Renderer) RenderAsync ¶
func (r *Renderer) RenderAsync(ctx interfaces.ShortcodeContext, shortcode string, params map[string]any, inner string) (<-chan template.HTML, <-chan error)
RenderAsync executes Render in a separate goroutine.
type RendererOption ¶
type RendererOption func(*Renderer)
RendererOption configures the renderer instance.
func WithRendererCache ¶
func WithRendererCache(cache interfaces.CacheProvider) RendererOption
WithRendererCache supplies a cache provider used when definitions specify a CacheTTL.
func WithRendererMetrics ¶
func WithRendererMetrics(metrics interfaces.ShortcodeMetrics) RendererOption
WithRendererMetrics attaches a metrics recorder for cache events.
func WithRendererSanitizer ¶
func WithRendererSanitizer(s interfaces.ShortcodeSanitizer) RendererOption
WithRendererSanitizer overrides the default sanitizer.
type Sanitizer ¶
type Sanitizer struct {
// contains filtered or unexported fields
}
Sanitizer is a conservative implementation that rejects inline script tags and enforces URL schemes.
func NewSanitizer ¶
func NewSanitizer() *Sanitizer
NewSanitizer returns a sanitizer allowing http/https URLs.
func (*Sanitizer) Sanitize ¶
Sanitize rejects obvious script injections while preserving safe markup.
func (*Sanitizer) ValidateAttributes ¶
ValidateAttributes rejects inline event handlers like onload/onerror.
func (*Sanitizer) ValidateURL ¶
ValidateURL ensures the URL has an allowed scheme.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service orchestrates shortcode parsing and rendering for arbitrary content.
func NewService ¶
func NewService(registry interfaces.ShortcodeRegistry, renderer interfaces.ShortcodeRenderer, opts ...ServiceOption) *Service
NewService constructs a shortcode service using the supplied registry and renderer.
func (*Service) Process ¶
func (s *Service) Process(ctx context.Context, content string, opts interfaces.ShortcodeProcessOptions) (string, error)
Process renders any shortcodes found within the content string, returning the resulting HTML.
func (*Service) Registry ¶
func (s *Service) Registry() interfaces.ShortcodeRegistry
Registry exposes the underlying shortcode registry.
type ServiceOption ¶
type ServiceOption func(*Service)
ServiceOption customises service behaviour.
func WithDefaultCache ¶
func WithDefaultCache(cache interfaces.CacheProvider) ServiceOption
WithDefaultCache overrides the fallback cache provider used when none is supplied at call time.
func WithDefaultSanitizer ¶
func WithDefaultSanitizer(sanitizer interfaces.ShortcodeSanitizer) ServiceOption
WithDefaultSanitizer overrides the fallback sanitizer used when none is supplied at call time.
func WithLogger ¶
func WithLogger(logger interfaces.Logger) ServiceOption
WithLogger attaches a logger used for structured diagnostics.
func WithMetrics ¶
func WithMetrics(metrics interfaces.ShortcodeMetrics) ServiceOption
WithMetrics wires the metrics recorder used for telemetry.
func WithParser ¶
func WithParser(parser interfaces.ShortcodeParser) ServiceOption
WithParser overrides the Hugo-style parser used to extract shortcodes.
func WithWordPressPreprocessor ¶
func WithWordPressPreprocessor(pre *parserpkg.WordPressPreprocessor) ServiceOption
WithWordPressPreprocessor allows callers to supply a custom WordPress preprocessor.
func WithWordPressSyntax ¶
func WithWordPressSyntax(enabled bool) ServiceOption
WithWordPressSyntax toggles support for the WordPress-style [] shortcode syntax.
type Validator ¶
type Validator struct{}
Validator performs definition and parameter validation.
func (*Validator) CoerceParams ¶
func (v *Validator) CoerceParams(def interfaces.ShortcodeDefinition, supplied map[string]any) (map[string]any, error)
CoerceParams validates user supplied parameters against the definition schema, returning a normalised map.
func (*Validator) ValidateDefinition ¶
func (v *Validator) ValidateDefinition(def interfaces.ShortcodeDefinition) error
ValidateDefinition ensures the definition contains a name, schema, and valid parameter definitions.