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 ¶
- func Address(locale, pagePath string) string
- func Within(address, base string) bool
- type Check
- func (c *Check) Broken(ctx context.Context) ([]Link, error)
- func (c *Check) Index(ctx context.Context) (*Index, error)
- func (c *Check) Redirects() (Redirector, string)
- func (c *Check) SetRedirects(key string, redirects Redirector)
- func (c *Check) Table(ctx context.Context) (*pages.Table, error)
- func (c *Check) Title(key string) string
- func (c *Check) Use(key, title string, source Func)
- type Func
- type Index
- type Link
- type Options
- type Redirector
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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 ¶
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 ¶
Table returns the published page table the links are resolved against, such as for the pages an address can redirect to.
type Func ¶
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 (*Index) Links ¶
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.
type Link ¶
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).