Documentation
¶
Overview ¶
Package microdata is a package to extract Microdata and JSON-LD from HTML documents.
HTML Microdata is a markup specification often used in combination with the schema collection to make it easier for search engines to identify and understand content on web pages.
JSON-LD is a lightweight Linked Data format. It is easy for humans to read and write. It is based on the already successful JSON format and provides a way to help JSON data interoperate at Web-scale.
Use cases:
// Pass a URL to the ParseURL function.
data, err := microdata.ParseURL("https://example.com/page")
// Pass a context and a URL to the ParseURLWithContext function.
data, err := microdata.ParseURLWithContext(ctx, "https://example.com/page")
// Pass a io.Reader, content-type and a base URL to the ParseHTML function.
data, err := microdata.ParseHTML(reader, contentType, baseURL)
// Pass a html.Node and a base URL to the ParseNode function.
data, err := microdata.ParseNode(rootNode, baseURL)
Note: The parser tolerates JSON-LD "type" attributes without the leading "@" character, and it intentionally drops empty properties instead of recording empty strings.
Index ¶
- type Item
- func (i *Item) CountPaths(prefix string, paths map[string]int)
- func (i *Item) GetNested(keys ...string) (data Microdata, ok bool)
- func (i *Item) GetNestedItem(keys ...string) (val *Item, ok bool)
- func (i *Item) GetProperties(keys ...string) (arr []any, ok bool)
- func (i *Item) GetProperty(keys ...string) (val any, ok bool)
- func (i *Item) IsOfSchemaType(itemType string) bool
- func (i *Item) IsOfType(itemType ...string) bool
- type Microdata
- func ParseHTML(r io.Reader, contentType, urlStr string) (*Microdata, error)
- func ParseNode(root *html.Node, urlStr string) (*Microdata, error)
- func ParseURL(urlStr string) (*Microdata, error)
- func ParseURLWithClient(ctx context.Context, client *http.Client, urlStr string) (*Microdata, error)
- func ParseURLWithContext(ctx context.Context, urlStr string) (*Microdata, error)
- type PropertyMap
- type ValueList
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Item ¶
type Item struct {
Types []string `json:"type"`
Properties PropertyMap `json:"properties"`
ID string `json:"id,omitempty"`
}
Item represents a single microdata item.
func (*Item) CountPaths ¶ added in v1.0.0
CountPaths recursively counts the occurrences of item paths, storing the results in the provided paths map.
func (*Item) GetNested ¶ added in v1.0.0
GetNested returns the extracted microdata for the properties of the first key that has at least one value.
func (*Item) GetNestedItem ¶ added in v1.0.0
GetNestedItem returns the first item from the properties of the first key that has at least one value.
func (*Item) GetProperties ¶ added in v1.0.0
GetProperties returns the values of the first key that has at least one value.
func (*Item) GetProperty ¶ added in v1.0.0
GetProperty returns the first value of the first key that has at least one value.
func (*Item) IsOfSchemaType ¶ added in v1.0.1
IsOfSchemaType returns whether the item is of the given type within the schema.org context.
type Microdata ¶
type Microdata struct {
Items []*Item `json:"items"`
}
Microdata represents the extracted microdata from a HTML document.
func ParseHTML ¶
ParseHTML parses the HTML document available in the given reader and returns the microdata. The given url is used to resolve the URLs in the attributes. The given contentType is used to convert the content of r to UTF-8. When the given contentType is equal to "", the content type will be detected using `http.DetectContentType`.
func ParseURL ¶
ParseURL parses the HTML document available at the given URL and returns the microdata.
func ParseURLWithClient ¶ added in v1.2.0
func ParseURLWithClient(ctx context.Context, client *http.Client, urlStr string) (*Microdata, error)
ParseURLWithClient parses the HTML document available at the given URL using the provided context and client.
func ParseURLWithContext ¶ added in v1.1.0
ParseURLWithContext parses the HTML document available at the given URL using the provided context and returns the microdata. It uses http.DefaultClient to fetch the document.
func (*Microdata) GetFirstOfSchemaType ¶ added in v1.0.1
GetFirstOfSchemaType returns the first item of the given type with a possible https://schema.org/ context.
func (*Microdata) GetFirstOfType ¶ added in v1.0.0
GetFirstOfType returns the first item of the given type.
type PropertyMap ¶
PropertyMap represents a map of property names to their corresponding value lists.