structured

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package structured extracts the machine-readable data pages already publish for SEO and social embeds — JSON-LD, OpenGraph and microdata — as pure functions over the HTML.

Embedded structured data is usually the most drift-resistant thing on a page: sites break their CSS classes weekly and their schema.org markup almost never, because search ranking depends on it. Use these extractors as the sturdy rung of a fallback strategy, or as a cross-check on selector-based extraction.

Everything here follows quarry's determinism rules: no network, no clock, and no URL resolution (relative URLs are returned as written — a base URL is external state). Output shapes are uniform maps and slices rather than typed structs, preserving whatever the page declared; order is document order throughout.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONLD

func JSONLD(htmlBytes []byte) ([]map[string]any, error)

JSONLD returns every JSON-LD object embedded in <script type="application/ld+json"> blocks, in document order. A block holding a top-level array contributes each object element; @graph containers are returned intact for the caller to unpack.

Malformed blocks are common in the wild, so parsing is lenient per block: unparseable blocks are skipped, every block that parses is returned, and a non-nil joined error names what was dropped. Callers may ignore the error and use the items — the drop is loud, not fatal. Legacy HTML comment wrappers (<!-- ... -->) inside a block are stripped before parsing.

func OpenGraph

func OpenGraph(htmlBytes []byte) (map[string][]string, error)

OpenGraph returns the page's og:* meta properties, keyed by full property name ("og:title", "og:image:width", ...), with repeated properties preserved in document order — og:image legitimately appears once per image, and a flat map[string]string would silently drop all but one.

Properties are read from <meta property="og:..."> and, because real sites get this wrong (go.dev among them), from <meta name="og:..."> too; when both attributes are present, property wins. Content values are returned exactly as written, including empty strings — this is a syntax extractor, and deciding that an empty og:title is unusable is the caller's (or a fallback chain's) job.

Types

type Item

type Item struct {
	// Types holds the itemtype tokens (usually schema.org URLs).
	Types []string
	// ID is the itemid value, as written (not resolved).
	ID string
	// Properties maps each itemprop name to its values in document
	// order. A value is a string, or a *Item for nested itemscopes.
	Properties map[string][]any
}

Item is one HTML microdata item: an element carrying itemscope.

func Microdata

func Microdata(htmlBytes []byte) ([]*Item, error)

Microdata returns the document's top-level microdata items (elements with itemscope but no itemprop), in document order.

Property values follow the HTML microdata algorithm: meta reads content; img/audio/video/embed/iframe/source/track read src; a, area and link read href; object reads data; data and meter read value; time reads datetime (falling back to its text); anything else contributes its trimmed text content. URL-valued properties are returned as written, unresolved. A space-separated itemprop contributes its value under each name. The itemref attribute is not supported (rare in the wild, and it breaks the tree-shaped model); such properties are simply not collected.

Jump to

Keyboard shortcuts

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