shortcode

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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")
)
View Source
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) Sanitize

func (NoOpSanitizer) Sanitize(html string) (string, error)

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

Get returns the stored definition.

func (*Registry) List

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.

func (*Registry) Remove

func (r *Registry) Remove(name string)

Remove deletes the definition if it exists.

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

func (s *Sanitizer) Sanitize(html string) (string, error)

Sanitize rejects obvious script injections while preserving safe markup.

func (*Sanitizer) ValidateAttributes

func (s *Sanitizer) ValidateAttributes(attrs map[string]any) error

ValidateAttributes rejects inline event handlers like onload/onerror.

func (*Sanitizer) ValidateURL

func (s *Sanitizer) ValidateURL(raw string) error

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.

func (*Service) Render

func (s *Service) Render(ctx interfaces.ShortcodeContext, shortcode string, params map[string]any, inner string) (template.HTML, error)

Render executes a single shortcode definition and returns the HTML output.

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 NewValidator

func NewValidator() *Validator

NewValidator returns a Validator instance.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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