plugin

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package plugin implements Fern's Lua plugin system.

Index

Constants

View Source
const (
	// RegistryURL is the canonical URL for the fern-terranium plugin registry.
	RegistryURL = "https://codeberg.org/InodeLabs/fern-terranium/raw/branch/main/registry.json"
)

Variables

This section is empty.

Functions

func DownloadPlugin

func DownloadPlugin(r Registry, name, destDir string) error

DownloadPlugin downloads the named plugin tarball from the registry and extracts it into destDir/name/.

func InstalledNames

func InstalledNames(dir string) ([]string, error)

InstalledNames returns the names of plugin directories inside dir that contain a main.lua file, sorted lexicographically.

func InstalledVersion

func InstalledVersion(dir, name string) string

InstalledVersion reads the version string written by DownloadPlugin for the named plugin. Returns "" if no version file exists.

func NormalizeName

func NormalizeName(name string) string

NormalizeName converts an "author/repo" style name to "author-repo". Plain names (no slash) are returned unchanged.

func RemovePlugin

func RemovePlugin(name, destDir string) error

RemovePlugin deletes the destDir/name/ directory and all its contents.

func SaveRegistryCacheTo

func SaveRegistryCacheTo(r Registry, path string) error

SaveRegistryCacheTo writes r as JSON to path, creating the file if needed.

Types

type CallCtx

type CallCtx struct {
	Content       string
	FilePath      string
	NoteStems     []string
	JournalStems  []string
	TemplateStems []string
	NotesDir      string
	JournalsDir   string
	TemplatesDir  string
	Vault         string
}

CallCtx holds a snapshot of app state provided to Lua during a call.

type Effect

type Effect struct {
	Kind    EffectKind
	Payload string
}

Effect is a single side-effect produced by a Lua call.

type EffectKind

type EffectKind int

EffectKind identifies what kind of side-effect a Lua call produced.

const (
	EffectNotify     EffectKind = iota // show success message in cmdbar
	EffectNotifyErr                    // show error message in cmdbar
	EffectSetContent                   // replace active file content
	EffectOpen                         // open file by stem/kind; Payload: stem+"\x1f"+kind
	EffectOpenPicker                   // request picker UI; Payload: JSON array of {label,value}
)

type KVStore

type KVStore interface {
	// PluginKVGet retrieves the value for plugin+key.
	// Returns ("", false, nil) when the key is not present.
	PluginKVGet(plugin, key string) (string, bool, error)
	// PluginKVSet writes value for plugin+key, overwriting any prior value.
	PluginKVSet(plugin, key, value string) error
}

KVStore is the interface the plugin manager uses for persistent key-value storage. The implementation lives in vfiles.Index and writes to the vault's fern.db.

type Manager

type Manager struct {
	L *lua.LState
	// contains filtered or unexported fields
}

Manager owns the shared LState and all plugin registrations. All methods must be called from the BubbleTea main goroutine.

func New

func New(cfg *config.Config, kv KVStore, querier VaultQuerier, approved []string, builtinCmds []string) *Manager

New creates a Manager. If plugins are not enabled or the directory is missing/unreadable, returns a disabled manager that is safe to call but does nothing. querier may be nil; vault query API functions return empty results when no querier is available. approved is the list of plugin names allowed to load; unapproved plugins are blocked and recorded in PendingApproval.

func (*Manager) ApproveAndLoad

func (m *Manager) ApproveAndLoad(name, dir string) []Effect

ApproveAndLoad executes dir/name/main.lua into the live LState, removes name from pendingApproval, and returns the resulting effects. Returns nil when the manager is disabled or name/main.lua does not exist.

func (*Manager) BlockRenderers

func (m *Manager) BlockRenderers() map[string]*lua.LFunction

BlockRenderers returns the registered fence-name -> Lua function map.

func (*Manager) CallBlockRenderer

func (m *Manager) CallBlockRenderer(fence, content string) ([]string, bool)

CallBlockRenderer calls the Lua renderer registered for fence with the given block content. Returns (display lines, true) on success or (nil, false) when no renderer is registered for that fence or the Lua call fails. The caller must be on the BubbleTea main goroutine.

func (*Manager) CallCommand

func (m *Manager) CallCommand(name string, ctx *CallCtx) bool

CallCommand calls the named Lua command with the provided context. Returns false if no such command is registered.

func (*Manager) CallKeyMap

func (m *Manager) CallKeyMap(mode, key string, ctx *CallCtx) bool

CallKeyMap checks whether key+mode matches a registered binding and calls it. Returns true if a binding was found.

func (*Manager) CancelPicker

func (m *Manager) CancelPicker()

CancelPicker clears the pending picker callback without calling it.

func (*Manager) Close

func (m *Manager) Close()

Close releases the Lua state.

func (*Manager) Enabled

func (m *Manager) Enabled() bool

Enabled reports whether the plugin system is active.

func (*Manager) FireDueTimers

func (m *Manager) FireDueTimers(ctx *CallCtx) time.Duration

FireDueTimers fires timers whose nextFireAt is past, updates nextFireAt, and returns the duration until the nearest next fire time (0 = no timers).

func (*Manager) FireEvent

func (m *Manager) FireEvent(event string, ctx *CallCtx)

FireEvent calls all handlers registered for the named event.

func (*Manager) HasTimers

func (m *Manager) HasTimers() bool

HasTimers reports whether any Lua timers are registered.

func (*Manager) LuaCommandNames

func (m *Manager) LuaCommandNames() map[string]struct{}

LuaCommandNames returns the set of registered Lua command names.

func (*Manager) PendingApproval

func (m *Manager) PendingApproval() []string

PendingApproval returns a copy of the list of plugin names blocked at load time because they were not in the approved set.

func (*Manager) ResolvePicker

func (m *Manager) ResolvePicker(selected string) []Effect

ResolvePicker calls the stored picker callback with the selected value, clears it, and returns the resulting effects.

func (*Manager) RevokePlugin

func (m *Manager) RevokePlugin(name string)

RevokePlugin moves name into pendingApproval so it shows as unapproved in the UI. Because the Lua state does not support unloading individual scripts the plugin remains functional for the current session; it will not load on the next restart once the caller also removes it from the persisted approved list. If name is already in pendingApproval this is a no-op.

func (*Manager) TakeEffects

func (m *Manager) TakeEffects() []Effect

TakeEffects returns all accumulated effects and clears the internal list.

type Registry

type Registry struct {
	BaseURL string          `json:"base_url"`
	Plugins []RegistryEntry `json:"plugins"`
}

Registry is the parsed registry.json from fern-terranium.

func FetchRegistry

func FetchRegistry() (Registry, error)

FetchRegistry downloads and parses the fern-terranium registry.json.

func LoadRegistryCacheFrom

func LoadRegistryCacheFrom(path string) Registry

LoadRegistryCacheFrom reads a cached Registry from path. Returns an empty Registry if the file does not exist or cannot be parsed.

type RegistryEntry

type RegistryEntry struct {
	Name        string   `json:"name"`
	Main        string   `json:"main"`
	Description string   `json:"description"`
	Version     string   `json:"version,omitempty"`
	URL         string   `json:"url,omitempty"`
	Maintainer  string   `json:"maintainer,omitempty"`
	Repo        string   `json:"repo,omitempty"`
	Permissions []string `json:"permissions,omitempty"`
}

RegistryEntry describes a single plugin in the registry.

func LoadManifest

func LoadManifest(dir, name string) (RegistryEntry, bool)

LoadManifest reads {dir}/{name}/manifest.json and parses it as a RegistryEntry. Returns the entry and ok=true on success; zero entry and ok=false on missing file or parse error.

type VaultQuerier

type VaultQuerier interface {
	// ListStemsForTag returns the stems of all notes/journals carrying tag.
	ListStemsForTag(tag string) ([]string, error)
	// FrontmatterForStem returns the scalar frontmatter fields for stem.
	// kind should be "note", "journal", or "template".
	FrontmatterForStem(stem, kind string) (map[string]string, error)
	// SearchVault runs a full-text search against the vault. Each hit is a
	// map with keys "stem", "kind", and "snippet".
	SearchVault(query string) ([]map[string]string, error)
}

VaultQuerier provides read-only vault queries available to Lua plugins. The implementation lives in vfiles.Index. A nil VaultQuerier is valid; all methods check for nil and return empty results rather than panicking.

Jump to

Keyboard shortcuts

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