html

package module
v0.0.0-...-f5d2471 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 4 Imported by: 0

README

html

Go Reference Go Report Card

go get git.gorbe.io/go/html@latest

Get the latest commit (if Go module proxy is not updated):

go get "git.gorbe.io/go/html@$(curl -s 'https://git.gorbe.io/api/v1/repos/go/html/commits' | jq -r '.[0].sha')"

Documentation

Index

Examples

Constants

View Source
const (
	TitleSelector       = "html head title"
	DescriptionSelector = "meta[name=\"description\" i][content]"
	IconSelector        = "link[rel=\"icon\" i], link[rel=\"shortcut icon\" i]"
	KeywordsSelector    = "meta[name=\"keywords\" i][content]"
	RobotsSelector      = "meta[name=\"robots\" i][content]"
	RatingSelector      = "meta[name=\"rating\" i][content]"
	CanonicalSelector   = "link[rel=\"canonical\" i][href]"
	AlternateSelector   = "link[rel=\"alternate\" i]"
)
View Source
const (
	OpenGraphSelector            = "meta[property*=\"og:\" i][content]" // Selector to get every OpenGraph element
	OpenGraphURLSelector         = "meta[property=\"og:url\" i][content]"
	OpenGraphSiteNameSelector    = "meta[property=\"og:site_name\" i][content]"
	OpenGraphTitleSelector       = "meta[property=\"og:title\" i][content]"
	OpenGraphDescriptionSelector = "meta[property=\"og:description\" i][content]"
	OpenGraphLocaleSelector      = "meta[property=\"og:locale\" i][content]"
	OpenGraphTypeSelector        = "meta[property=\"og:type\" i][content]"
	OpenGraphImageSelector       = "meta[property=\"og:image\" i][content]"
)

Variables

This section is empty.

Functions

func RemoveSelections

func RemoveSelections(doc []byte, selectors []string) ([]byte, error)

RemoveSelections removes elements from HTML document doc that matches any of the selectors.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/html"
)

func main() {

	doc := []byte(`
	<!DOCTYPE html>
	<html>
		<head>
			<title>Test Title</title>
		</head>
		<body>
			<h1>Body</h1>
			<h2>Sub Body</h2>
			<p class="removeme">Remove Me</p>
			<p class="deleteme">Delete Me</p>
		</body>
	</html>`)

	newDoc, err := html.RemoveSelections(doc, []string{".removeme", ".deleteme"})
	if err != nil {
		// Handle error
	}

	fmt.Printf("%s\n", newDoc)
}

Types

type Document

type Document struct {
	// contains filtered or unexported fields
}

func ParseDocument

func ParseDocument(b []byte) (*Document, error)

func ReadDocument

func ReadDocument(r io.Reader) (*Document, error)

func (*Document) Attribute

func (d *Document) Attribute(selector, attribute string) []byte

Attribute returns the content of attribute from the first element that match selector.

If attribute not exist, returns nil.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/html"
)

func main() {

	doc := []byte(`
	<!DOCTYPE html>
	<html>
		<head>
			<title>Test Title</title>
			<link rel="canonical" href="https://example.com">
		</head>
		<body>
			<h1>Body</h1>
			<h2>Sub Body</h2>
		</body>
	</html>`)

	newDoc, err := html.ParseDocument(doc)
	if err != nil {
		// Handle error
		return
	}

	href := newDoc.Attribute("link[rel=\"canonical\"]", "href")

	fmt.Printf("%s", href)

}
Output:

https://example.com

func (*Document) Canonical

func (d *Document) Canonical() []byte

func (*Document) Description

func (d *Document) Description() []byte

func (*Document) HTML

func (d *Document) HTML() []byte

HTML returns the HTML of the Document d.

If cant render the HTML, returns nil.

func (*Document) InnerHTML

func (d *Document) InnerHTML(selector string) []byte

InnerHTML returns the inner HTML of the first element that match selector.

If not found or cant render the HTML, returns nil.

func (*Document) OpenGraph

func (d *Document) OpenGraph() *OpenGraph

func (*Document) OuterHTML

func (d *Document) OuterHTML(selector string) []byte

OuterHTML returns the outer HTML of the first element that match selector.

If not found or cant render the HTML, returns nil.

func (*Document) RemoveSelection

func (d *Document) RemoveSelection(selector string)

RemoveSelection removes elements from d Document that matches the selector.

func (*Document) RemoveSelections

func (d *Document) RemoveSelections(selectors []string)

RemoveSelections removes elements from d Document that matches any of the selectors.

func (*Document) Text

func (d *Document) Text(selector string) []byte

Text returns the first text element that match selector.

func (*Document) Title

func (d *Document) Title() []byte

type OpenGraph

type OpenGraph struct {
	URL         string
	SiteName    string
	Title       string
	Description string
	Locale      string
	Type        string
	Image       string
}

Jump to

Keyboard shortcuts

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