github.com/sightmap/sightmap/go is the reference implementation behind the sightmap CLI — and it’s importable. Consumers such as snapshot enrichers, analysis scripts, and custom agents use the library directly instead of re-implementing component matching, so semantics can’t diverge between tools.
Design goals
- One canonical component model and rule matcher. Downstream consumers import this library rather than re-implementing matching.
- Tree-level matching, not DOM queries. Matching operates against a component tree — every node carries a
SelectorPart— rather than viadocument.querySelectorAll. That enables offline analysis of exported snapshots and native-mobile compatibility via synthetic selectors. - One canonical probe. The browser-side extractor (
cdp-probe.js) is embedded once, in theprobepackage; consumers embed it from there so bounds, visibility, and interactivity logic cannot diverge.
Package map
All import paths are rooted atgithub.com/sightmap/sightmap/go/.
Load a corpus and match a tree
The core flow is three calls:DirLoader points at a .sightmap/ directory, NewSession wraps it in a compiled-query cache, and MatchTree applies the corpus to a component tree for a page URL.
main.go
MatchTree returns map[*comps.ComponentNode]*match.SightmapMatch — each match carries the component Name and its Memory notes. The view is selected from the URL’s pathname using the same most-specific-wins route matching the CLI uses; if no view matches, global components still apply.
Sessions
sightmap.Session is safe for concurrent use. It caches compiled match queries per (corpus generation, page URL) pair, so repeated MatchTree calls against the same URL are cheap. Useful methods beyond MatchTree:
Besides
DirLoader, the package provides StaticLoader (wrap an already-built *Corpus) and LoaderFunc (adapt any func() (*Corpus, error)) — handy for tests and for corpora that don’t live on disk.
Validate and lint from Go
The same checks behindsightmap validate and sightmap lint are plain functions:
LintWithCounts augments lint with real per-component match counts from a snapshot, which is how sightmap lint --snapshot works.
Where component trees come from
- Saved captures — every capture under
.sightmap/snapshots/{view}/has a sibling.snap.tree.jsonfile holding the rawComponentNodetree;sightmap snapshot --tree-out FILEwrites one anywhere you like. Unmarshal it straight into*comps.ComponentNodeas in the example above. - Live pages — the
browserpackage dials a Chrome debugging port (DialCDP), andExtractComponentsruns the embedded probe against a page to produce the same tree shape. - Your own extraction — the
extractpackage builds a tree from raw probe and accessibility data with no browser dependency, and native consumers can synthesizeSelectorPartvalues for non-DOM trees.
The
conformance package ships tree-plus-corpus fixtures with expected matches. If you build a matching consumer in another language, run them to prove your semantics agree with the reference implementation — see Conformance fixtures.Related
- CLI overview — the command-line surface built on these packages
- Curator vs. consumer — where a library consumer fits in the ecosystem
- Schema reference — the YAML contract the loader reads