Documentation
¶
Overview ¶
Package schemaorg parses the JSON-LD embedded in an HTML page into a flat, addressable graph of typed nodes. It is a shared helper, not an analyzer: it emits no findings and is never registered. Both the structured and shopify analyzers read a page through it, so both see the same nodes and resolve @id references the same way. Nothing is cached: each caller parses the page again (three times per page today: once in structured, twice in shopify).
Index ¶
- func Parse(doc *goquery.Document) (Graph, []ParseError)
- type Graph
- func (g Graph) HasType(types ...string) bool
- func (g Graph) HasValue(n Node, path string) bool
- func (g Graph) NodesAt(n Node, path string) []Node
- func (g Graph) OfType(t string) []Node
- func (g Graph) Resolve(id string) (Node, bool)
- func (g Graph) Str(n Node, path string) string
- func (g Graph) Strs(n Node, path string) []string
- func (g Graph) Types() []string
- func (g Graph) Value(n Node, path string) (any, bool)
- func (g Graph) Values(n Node, path string) []any
- type Node
- type ParseError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
func Parse(doc *goquery.Document) (Graph, []ParseError)
Parse extracts every JSON-LD block in the document and flattens it into a Graph. Blocks that fail to decode are returned as ParseErrors rather than aborting the parse, so one broken block from a misbehaving app does not hide the rest of a page's markup. Empty blocks are skipped silently — a theme that renders an empty script tag has no data, but it does not have a syntax error either.
Types ¶
type Graph ¶
type Graph struct {
Nodes []Node
// contains filtered or unexported fields
}
Graph is every typed node found on one page, in a stable order.
func (Graph) HasType ¶
HasType reports whether any node on the page declares any one of the given types.
func (Graph) HasValue ¶
HasValue reports whether n carries a non-empty value at path. Empty strings, whitespace, empty arrays, and empty objects all count as absent — markup that declares a property and leaves it blank is no more useful to a search engine than omitting it.
func (Graph) NodesAt ¶
NodesAt returns the typed nodes reachable at path below n. It matches against the parsed graph by object identity of the underlying map, so a value reached through an @id reference comes back as the node that declared it.
func (Graph) Str ¶
Str returns the first value at path rendered as a string. Numbers are formatted without a trailing ".0" so that a price written as 19.99 and one written as "19.99" compare equal.
func (Graph) Strs ¶
Strs returns every value at path rendered as a string, skipping values that have no sensible scalar form (nested objects, nulls).
func (Graph) Value ¶
Value returns the first value at a dotted path below n, e.g. "offers.price". Path resolution absorbs the three shapes schema.org allows interchangeably at every step: a bare value, an array of values, and a bare {"@id": ...} reference to a node declared elsewhere on the page. Callers get to write "offers.price" and never branch on which was used.
func (Graph) Values ¶
Values returns every raw, undecoded value at a dotted path below n. Callers that need to distinguish a JSON string from a JSON number — a price written "19.99" is well-formed, one written "$1,299.00" is not, and both arrive as strings while a bare 19.99 does not — use this rather than Strs, which renders everything as a string.
type Node ¶
type Node struct {
// Types holds the node's @type, always as a slice — schema.org permits a bare string or
// an array, and normalizing here means no caller has to care which was used.
Types []string
// ID is the node's @id, empty when it declares none.
ID string
// Props is the node's raw decoded JSON object, @-prefixed keys included.
Props map[string]any
// Block is the index of the <script> element the node came from. Two nodes of the same
// type in different blocks usually mean two sources competing to describe one page;
// two in the same block are a deliberate modelling choice. Distinguishing the two is
// the whole reason this field exists.
Block int
// Path is the node's dotted position within its block, e.g. "Product.offers". It lets a
// finding name where in the markup a problem sits rather than only which type it hit.
Path string
}
Node is one typed schema.org object, lifted out of whatever nesting it arrived in.
type ParseError ¶
ParseError records a JSON-LD block that failed to decode.