links

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package links knows which published content of a site links to which address on it: the Markdown of the published pages, and what modules report of their published content (Func), such as the text of a post or the address of a banner's button. The admin uses it to warn editors before a page leaves its address, to offer redirects for the addresses links lead to, and to list the links that lead nowhere.

Links are compared as addresses on the site, such as "/en/about": a relative link is resolved from the address of the content it is in, the way a browser resolves it, an absolute URL to the site's own host (Options.BaseURL) counts as a link on the site, and the query and the fragment are dropped. Links to other websites, e-mail and phone links, media files (which cannot be deleted while content uses them) and the addresses the app serves itself (Options.IsCorePath) are not indexed.

The index is made when it is asked for, never while a visitor's page is served, and kept until what visitors see changes in this process (Options.Version) or the published page tree does; changes by other processes show within Options.TTL. The links of a published revision are kept apart, per revision and language, as revisions never change: like the search texts, they are found once.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Address

func Address(locale, pagePath string) string

Address is the address of a page's published path in locale, as links are compared: "/en/about" for "/about", and "/en" for Home ("/").

func Within

func Within(address, base string) bool

Within reports whether address is base or an address below it, such as "/en/about/team" below "/en/about".

Types

type Check

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

Check finds the links between the published content of a site. It is safe for concurrent use once the modules are added (Use, SetRedirects).

func New

func New(tree *pages.Service, provider locales.Provider, options Options) *Check

New returns the Check of the published pages of tree, in the languages provider enables, and of the modules added with Use.

func (*Check) Broken

func (c *Check) Broken(ctx context.Context) ([]Link, error)

Broken returns the links in published content to addresses the site does not answer: no page is there (see Link.Found), and no redirect leads from there to a page or another website.

func (*Check) Index

func (c *Check) Index(ctx context.Context) (*Index, error)

Index returns the links of the published content, made again when what visitors see has changed (see Options).

func (*Check) Redirects

func (c *Check) Redirects() (Redirector, string)

Redirects returns the module instance that redirects old addresses, with its key, such as for the rights of its admin section; nil without one.

func (*Check) SetRedirects

func (c *Check) SetRedirects(key string, redirects Redirector)

SetRedirects sets the module instance, with its key, that redirects old addresses. Call it before the Check is used.

func (*Check) Table

func (c *Check) Table(ctx context.Context) (*pages.Table, error)

Table returns the published page table the links are resolved against, such as for the pages an address can redirect to.

func (*Check) Title

func (c *Check) Title(key string) string

Title is the title of the module instance with key, as Use was given it; empty for pages and unknown keys.

func (*Check) Use

func (c *Check) Use(key, title string, source Func)

Use adds the links of a module instance, with its key and title. Call it before the Check is used.

type Func

type Func func(ctx context.Context, locale string) ([]Source, error)

Func lists the published content of a module instance in locale with the links in it, for webthings.Parts.Links: what visitors can see, and no drafts.

type Index

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

Index is the links of the published content at one moment (see Check.Index). It never changes.

func (i *Index) Links() []Link

Links returns every link, those of pages first, in tree order, then those of modules, per language: the Index's own list, to read and never to change.

func (*Index) Source

func (i *Index) Source(address string) (*Source, bool)

Source returns the published content whose own address is address, such as the post at "/en/news/hello", in that address's language, and whether there is any; Source.Title names it for editors. Content without links counts too.

func (*Index) To

func (i *Index) To(addresses ...string) []Link

To returns the links to one of addresses or below it, such as "/en/about" and "/en/about/team" for "/en/about", leaving out the links in content that is there itself (Source.URL), which leaves or moves with it.

type Link struct {
	// Module is the instance key of the module the content belongs to;
	// empty for a page.
	Module string
	// Locale is the language of the content.
	Locale string
	// Source is the content the link is in; it is shared by its links.
	Source *Source
	// Href is the link as written, such as "../contact#form".
	Href string
	// Address is where the link leads on the site, cleaned and without
	// query and fragment, such as "/en/contact".
	Address string
	// Page is the ID of the published page that answers at Address, or of
	// the module page above it; empty when none does.
	Page string
	// Found reports whether the site answers at Address with a page: a
	// published page, an address below a module page, which the module
	// answers, or the search screen.
	Found bool
}

Link is a link in published content to an address on the site.

type Options

type Options struct {
	// BaseURL is the site's address, such as "https://example.com": full
	// URLs to its host are links on the site.
	BaseURL string
	// IsCorePath reports the addresses the app serves itself, such as the
	// media files and visitor actions (webthings.App.IsCorePath): links to
	// them do not lead to pages.
	IsCorePath func(address string) bool
	// Version moves on with every change in this process of what visitors
	// see, such as the app's output cache version: the index is made again
	// once it did. Nil only follows the page tree and TTL.
	Version func() uint64
	// TTL is how long an index is kept at most, so changes by other
	// processes show; zero is 30 seconds, less than zero keeps none.
	TTL time.Duration
}

Options configure a Check.

type Redirector

type Redirector interface {
	// Redirected returns where a redirect sends the visitors of address,
	// an address on the site such as "/en/about"; ok is false when no
	// redirect has it.
	Redirected(ctx context.Context, address string) (target string, ok bool)
	// Redirect sends the visitors of address to target, an address on the
	// site, from now on, with a permanent redirect (301), replacing a
	// redirect of address to elsewhere. A redirect of target itself, which
	// would hide the page that is there now, is removed.
	Redirect(ctx context.Context, address, target string) error
}

Redirector is a module that redirects old addresses to new ones, such as the redirects module (webthings.Parts.Redirects): the admin offers to redirect the addresses that links lead to when a page leaves them.

type Source

type Source struct {
	// ID identifies the content in its module, such as a post's ID; for a
	// page it is the page's ID.
	ID string
	// Title names the content for editors.
	Title string
	// URL is the content's own address on the site, such as
	// "/en/news/hello": its relative links lead from there. Content without
	// an address of its own, such as a banner, leaves it empty; its
	// relative links lead from the home page of its language.
	URL string
	// EditURL opens the content in the admin, such as the editor of a post
	// (see webthings.Env.AdminURL). Pages leave it empty: the admin knows
	// their editor.
	EditURL string
	// Markdown holds the content's texts in Markdown, such as a body; their
	// links count (see markdown.Links).
	Markdown []string
	// Addresses are the content's link fields, such as a button's address.
	Addresses []string
	// Revision names the revision Markdown comes from, for content with
	// revisions: the links found in it are then kept with the revision, as
	// those of pages are. Empty finds them again for every index.
	Revision string
}

Source is published content in one language with the links in it, as a module reports it (Func).

Jump to

Keyboard shortcuts

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