pageseo

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: MIT Imports: 16 Imported by: 0

README

HTML Page Search Engine Optimization Test Suite

HTML page search engine optimization test and utility Golang test suite. Aims to prevent the following common page degradation scenarios, which lead to loss of page ranking:

  1. Losing relevant metadata when changing HTML view templates or database models.
  2. Duplicating metadata fields on the same page.
  3. Duplicating metadata on different publicly available pages.
  4. Forgetting to enforce minimum and recommended metadata field sizes.
  5. Forgetting to enforce UTF normalization on page content.

The library works by providing a reasonable set of tests that any HTML page should pass in order to fit current search engine optimization expectations. Almost none of the top website fit the "best practice." This indicates that almost nobody is testing search engine optimization in between hiring consultants. The library aims to be minimal and flexible to your use case.

Library Usage

go get -u github.com/dkotik/pageseo@latest
import (
  "bytes"
  "testing"

  "github.com/dkotik/pageseo"
)

func TestSearchEngineOptimization(t *testing.T) {
  validator := pageseo.NewStrict(
    pageseo.Requirements{
      // override requirements as needed
      Title: pageseo.NewTitleValidator(
        pageseo.StringConstraints{
          MinimumLength: 12,
          MaximumLength: pageseo.DefaultMaximumTitleLength * 4,
          Normalizer: pageseo.NomalizeLineToNFC,
        },
      ),
    },
  )

  t.Run("index.html", validator.TestReader(
    t.Name(), // identify the origin for content de-duplication
    bytes.NewReader([]byte("<html><p>index</p></html>")),
  ))

  t.Run("sitemap.html", validator.TestReader(
    t.Name(), // identify the origin for content de-duplication
    bytes.NewReader([]byte("<html><p>sitemap</p></html>")),
  ))
}

Command Line Usage

go install github.com/dkotik/pageseo/cmd/pageseo@latest
pageseo scan --strict ./**/*.html

Development Road Map

Project status: draft in progress. The test suite is minimal, but in strict mode it will find at least one reasonable optimization suggestion for your website.

  • Provide a command line scanner that can validate statically generated websites.
  • Provide a command line scanner that can validate URLs.
  • Add open graph validations.
  • Add twitter validations.
  • Unique contraint by namespace with a namespace flag for CLI.
  • Validate image size.
  • Provide a command line scanner that can crawl live websites.
  • Provide a service that can crawl a target at an interval.
  • Make sure --failfast works for CLI.

Similar Projects

Documentation

Index

Constants

View Source
const (
	DefaultMinimumDescriptionLength = 4
	DefaultMaximumDescriptionLength = 150
)
View Source
const (
	DefaultMinimumHeadingLength = 4
	DefaultMaximumHeadingLength = 55
)
View Source
const (
	DefaultMinimumImageAltTextLength = 0
	DefaultMaximumImageAltTextLength = DefaultMaximumTitleLength * 12
)
View Source
const (
	DefaultMinimumLinkTextLength = 4
	DefaultMaximumLinkTextLength = DefaultMaximumTitleLength * 6
)
View Source
const (
	MetaOpenGraphType        = "og:type"
	MetaOpenGraphTitle       = "og:title"
	MetaOpenGraphDescription = "og:description"
	MetaOpenGraphURL         = "og:url"
	MetaOpenGraphImage       = "og:image"
)
View Source
const (
	DefaultMinimumTitleLength = 4
	DefaultMaximumTitleLength = 55
)
View Source
const (
	MetaTwitterCard        = "twitter:card"
	MetaTwitterTitle       = "twitter:title"
	MetaTwitterDescription = "twitter:description"
	MetaTwitterSite        = "twitter:site"
	MetaTwitterURL         = "twitter:url"
	MetaTwitterImage       = "twitter:image"
)

Variables

This section is empty.

Functions

func GetPictureSourceList added in v0.0.2

func GetPictureSourceList(node *html.Node) (result []string, err error)

func NewDescriptionValidator

func NewDescriptionValidator(s StringConstraints) htmltest.Validator

func NewHeadingValidator added in v0.0.2

func NewHeadingValidator(s StringConstraints) htmltest.Validator

func NewImageAltTextValidator added in v0.0.2

func NewImageAltTextValidator(s StringConstraints) htmltest.Validator

func NewLinkTextValidator added in v0.0.2

func NewLinkTextValidator(s StringConstraints) htmltest.Validator

func NewTitleValidator

func NewTitleValidator(s StringConstraints) htmltest.Validator

func TestDocumentRootHasExactlyDoctypeAndHTMLNodes added in v0.0.2

func TestDocumentRootHasExactlyDoctypeAndHTMLNodes(root *html.Node) func(t *testing.T)

func ValidateDoctypeTag

func ValidateDoctypeTag(node *html.Node) error

Types

type ImageAltTextValidator added in v0.0.2

type ImageAltTextValidator struct {
	Normalizer    Normalizer
	MinimumLength int
	MaximumLength int
}

func (ImageAltTextValidator) Validate added in v0.0.2

func (s ImageAltTextValidator) Validate(value string) error

type Normalizer

type Normalizer interface {
	Normalize(string) (string, error)
}

type NormalizerFunc added in v0.0.2

type NormalizerFunc func(string) (string, error)
var NormalizeLineToNFC NormalizerFunc = func(line string) (string, error) {
	line = norm.NFC.String(strings.TrimSpace(line))
	return reCollapseSpaces.ReplaceAllString(line, " "), nil
}
var NormalizeTextToNFC NormalizerFunc = func(text string) (line string, err error) {
	b := strings.Builder{}
	for _, line = range reCollapseNewlines.Split(text, -1) {
		line, err = NormalizeLineToNFC(line)
		if err != nil {
			return "", err
		}
		b.WriteString(line)
		b.WriteString("\n\n")
	}
	return strings.TrimSuffix(b.String(), "\n\n"), nil
}
var PassthroughNormalizer NormalizerFunc = func(s string) (string, error) {
	return s, nil
}

func (NormalizerFunc) Normalize added in v0.0.2

func (fn NormalizerFunc) Normalize(s string) (string, error)

type PageValidator added in v0.0.3

type PageValidator struct {
	Title                    htmltest.Validator
	Description              htmltest.Validator
	OpenGraphCardTitle       htmltest.Validator
	OpenGraphCardDescription htmltest.Validator
	TwitterCardTitle         htmltest.Validator
	TwitterCardDescription   htmltest.Validator
	Heading                  htmltest.Validator
	Language                 htmltest.Validator

	URL          htmltest.Validator
	LinkText     htmltest.Validator
	ImageAltText htmltest.Validator
	ImageSrc     htmltest.Validator
	// contains filtered or unexported fields
}

func New added in v0.0.3

func NewStrict added in v0.0.3

func NewStrict(r Requirements) *PageValidator

func (PageValidator) Test added in v0.0.3

func (r PageValidator) Test(origin string, node *html.Node) func(t *testing.T)

func (PageValidator) TestFile added in v0.0.3

func (v PageValidator) TestFile(p string) func(t *testing.T)

func (PageValidator) TestHead added in v0.0.3

func (r PageValidator) TestHead(node *html.Node) func(t *testing.T)

func (PageValidator) TestHeadings added in v0.0.3

func (r PageValidator) TestHeadings(node *html.Node) func(t *testing.T)

func (PageValidator) TestImage added in v0.0.3

func (r PageValidator) TestImage(origin string, node *html.Node) func(t *testing.T)
func (r PageValidator) TestLink(origin string, node *html.Node) func(t *testing.T)

func (PageValidator) TestOpenGraphCard added in v0.0.3

func (r PageValidator) TestOpenGraphCard(node *html.Node) func(t *testing.T)

func (PageValidator) TestReader added in v0.0.3

func (v PageValidator) TestReader(origin string, r io.Reader) func(t *testing.T)

func (PageValidator) TestTwitterCard added in v0.0.3

func (r PageValidator) TestTwitterCard(node *html.Node) func(t *testing.T)

func (PageValidator) TestURL added in v0.0.3

func (v PageValidator) TestURL(ctx context.Context, url string) func(t *testing.T)

type Requirements

type Requirements struct {
	// Normalizer is passed to all default validator constructors.
	// If you are using custom validators, you should pass your
	// own normalizer to each constructor manually.
	//
	// Default value is [PassthroughNormalizer] that does not do anything.
	Normalizer Normalizer

	DeduplicationNamespace               string
	TitleDeduplicator                    htmltest.Middleware
	DescriptionDeduplicator              htmltest.Middleware
	OpenGraphCardTitleDeduplicator       htmltest.Middleware
	OpenGraphCardDescriptionDeduplicator htmltest.Middleware
	TwitterCardTitleDeduplicator         htmltest.Middleware
	TwitterCardDescriptionDeduplicator   htmltest.Middleware

	Title       htmltest.Validator
	Description htmltest.Validator
	Heading     htmltest.Validator
	Language    htmltest.Validator

	URL          htmltest.Validator
	LinkText     htmltest.Validator
	ImageAltText htmltest.Validator
	ImageSrc     htmltest.Validator
}

type StringConstraints

type StringConstraints struct {
	Normalizer    Normalizer
	MinimumLength int
	MaximumLength int
}

Directories

Path Synopsis
cmd
pageseo command
Package htmltest provides a set of tools for testing rendered HTML page elements.
Package htmltest provides a set of tools for testing rendered HTML page elements.
Package slug provides functions for generating optimized URL slugs from strings.
Package slug provides functions for generating optimized URL slugs from strings.

Jump to

Keyboard shortcuts

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