preview

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package preview runs the live-preview HTTP server: serves the SPA, an iframe-hosted paged.js view of the document, pushes a reload signal over WebSocket on file changes, and exposes /print to trigger PDF generation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a single-client live-preview server bound to one source document.

func New

func New(docPath, version string) *Server

New returns a server for the given document. The document is re-read from disk on every render so edits in any external editor are picked up.

func (*Server) CurrentIncludes added in v0.2.0

func (s *Server) CurrentIncludes() []string

CurrentIncludes re-reads the document and reports the absolute paths of the files it pulls in via `:::include`, so the watcher can follow them. A document that can't be read (e.g. mid-edit) yields nil — the render path reports that error separately; here we just leave the include watch set unchanged-worthy.

func (*Server) CurrentTheme added in v0.1.1

func (s *Server) CurrentTheme() (path, warning, docErr string)

CurrentTheme re-resolves the current document's theme and reports what the watcher and live logger need: the resolved theme file path ("" for a built-in, so the file watch is dropped), the current non-fatal theme warning ("" when the theme is fine), and docErr — set when the document itself can't be read or parsed (which is fatal for rendering, unlike a theme problem).

func (*Server) DocPath

func (s *Server) DocPath() string

DocPath is the absolute path of the document this server is serving.

func (*Server) Port

func (s *Server) Port() int

Port returns the port the server is listening on. Only valid after Start.

func (*Server) PushReload

func (s *Server) PushReload() error

PushReload notifies the connected client that the underlying document or theme changed and the iframe should be reloaded.

func (*Server) Shutdown

func (s *Server) Shutdown() error

Shutdown stops the HTTP server.

func (*Server) Start

func (s *Server) Start(port int) error

Start binds the server (port 0 = auto-pick) and serves in the background.

func (*Server) URL

func (s *Server) URL() string

URL returns the preview origin (no path).

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher watches a set of files and invokes onChange (debounced) when any of them is written. onChange receives the path of the file whose change triggered the (debounced) fire, so callers can log what reloaded.

func NewWatcher

func NewWatcher(onChange func(changed string), paths ...string) (*Watcher, error)

NewWatcher creates a watcher and adds each given path. A path that doesn't exist (e.g. a themes/ directory the project never created) is skipped silently — it's a normal, expected case, not an error. Other Add failures are logged but non-fatal.

func (*Watcher) Close

func (w *Watcher) Close() error

Close stops the watcher.

func (*Watcher) Run

func (w *Watcher) Run()

Run blocks until Close is called. A 100ms debounce coalesces editor save bursts (some editors emit several writes in quick succession).

func (*Watcher) WatchIncludes added in v0.2.0

func (w *Watcher) WatchIncludes(paths []string)

WatchIncludes follows the document's `:::include` chapter files for content edits, so editing a chapter reloads the preview. The include set can change mid-session (the user adds or removes a `:::include`), so this is called on every reload with the freshly-resolved set: paths no longer present are dropped, newly-appeared ones are added. A file that can't be watched (e.g. it was just deleted) is skipped — a broken include surfaces as a render error, not a watcher failure.

func (*Watcher) WatchTheme added in v0.1.1

func (w *Watcher) WatchTheme(path string)

WatchTheme follows the document's active theme file for content edits. The active theme can change mid-session (the user edits the frontmatter, or fixes a previously-missing theme), so this is called on every reload to keep the watch pointed at the right file. Passing "" — which is what the built-in default theme resolves to — just drops the previous theme watch.

Theme directories are watched separately and statically (see NewWatcher) so that creating or switching theme files is noticed even on platforms where a directory watch doesn't report writes to existing files.

Jump to

Keyboard shortcuts

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