schemaorg

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 5 Imported by: 0

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

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

func (g Graph) HasType(types ...string) bool

HasType reports whether any node on the page declares any one of the given types.

func (Graph) HasValue

func (g Graph) HasValue(n Node, path string) bool

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

func (g Graph) NodesAt(n Node, path string) []Node

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) OfType

func (g Graph) OfType(t string) []Node

OfType returns every node declaring the given @type, in a stable order.

func (Graph) Resolve

func (g Graph) Resolve(id string) (Node, bool)

Resolve returns the node declaring the given @id.

func (Graph) Str

func (g Graph) Str(n Node, path string) string

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

func (g Graph) Strs(n Node, path string) []string

Strs returns every value at path rendered as a string, skipping values that have no sensible scalar form (nested objects, nulls).

func (Graph) Types

func (g Graph) Types() []string

Types returns every @type on the page, de-duplicated, in a stable order.

func (Graph) Value

func (g Graph) Value(n Node, path string) (any, bool)

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

func (g Graph) Values(n Node, path string) []any

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.

func (Node) Is

func (n Node) Is(t string) bool

Is reports whether the node declares the given @type.

type ParseError

type ParseError struct {
	Block int
	Err   string
}

ParseError records a JSON-LD block that failed to decode.

Jump to

Keyboard shortcuts

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