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 ¶
- type Server
- func (s *Server) CurrentIncludes() []string
- func (s *Server) CurrentTheme() (path, warning, docErr string)
- func (s *Server) DocPath() string
- func (s *Server) Port() int
- func (s *Server) PushReload() error
- func (s *Server) Shutdown() error
- func (s *Server) Start(port int) error
- func (s *Server) URL() string
- type Watcher
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 ¶
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
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
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) PushReload ¶
PushReload notifies the connected client that the underlying document or theme changed and the iframe should be reloaded.
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 ¶
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) 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
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
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.