Documentation
¶
Overview ¶
Package links models formal task links and finds and classifies links in task text. It is the Go counterpart of lib/tasks/links.rb.
Bodies increasingly carry references into other systems (Slack threads, Jira tickets, PRs, docs); this is the one place that knows how to spot a link and name the system it points into, so every surface (`show`, `links`, `open`, the TUI opener) agrees.
Two shapes are recognized, in this order:
[[url][label]] / [[url]] org-mode links, the canonical form for notes https://… bare URLs in prose
Only web URLs count: an org internal link ([[My Heading]], [[id:…]], [[file:…]]) is navigation within org, not a reference into another system, so it is not reported.
Index ¶
- Constants
- func Classify(raw string, customSystems map[string]string) string
- func Expand(value, template string) string
- func ExpandFormal(raw string, shorthands map[string]string) (url, label string, ok bool)
- func MatchKey(raw string) string
- func MatchesDocPattern(raw string, patterns []string) bool
- func ParseDocPatterns(value string) []string
- func SameLink(left, right string) bool
- func TrimSentenceTail(value string) string
- func UnsafeFormalText(value string) bool
- func ValidFormalLabel(label string) bool
- func ValidFormalURL(raw string) bool
- type FormalLink
- type Link
- type Source
Constants ¶
const ( MaxFormalLinks = 50 MaxURLLength = 2000 MaxLabelLength = 200 )
const SystemDoc = "doc"
SystemDoc is the system a link into a local document is classified as: the note or file a task came from, as opposed to a context link into a chat thread or a ticket. A client tells "where this task came from" apart from everything else with `links[].system == "doc"` rather than its own heuristics.
Variables ¶
This section is empty.
Functions ¶
func Classify ¶
Classify names the system a URL points into: "doc" for a `file:` URL or a relative path to a markdown or text file, then a custom `systems` row (host suffix; user intent wins), then a built-in row on host — and path, when the row has one — else the bare host itself, so unknown systems still group and list meaningfully, else "link" for something unparseable. Configured `doc_link_patterns` are applied over this by the read model (taskquery.Queries.Links), which owns that configuration.
func Expand ¶
Expand turns a shorthand token into its full URL via the configured template — `jira:OPS-1234` with `link.jira = https://…/browse/%s` becomes that URL. A template without %s is treated as a prefix.
func ExpandFormal ¶ added in v1.18.0
ExpandFormal resolves one formal-link input: a web URL passes through unchanged with no default label; `name:value` whose name is a configured `link.<name>` shorthand expands through its template and keeps the raw token as its default label, so a listing shows what was typed. Anything else — an unknown name, an empty value, or an expansion that is not a valid web URL — reports ok=false.
func MatchKey ¶ added in v1.19.0
MatchKey is the identity two spellings of one link share, so "which tasks carry this URL" does not split a single link into several. It is used for matching only; stored and listed URLs keep the spelling the task carries.
Two URLs match when they agree after:
- lowercasing the scheme and host, dropping a leading `www.` and the scheme's default port;
- dropping the fragment and a trailing slash on the path;
- dropping tracking parameters (`utm_*`, `fbclid`, `gclid`) everywhere, and Slack's `thread_ts` / `cid` on a Slack permalink, whose path already names the message;
- decoding percent-escapes (and `+` in the query) and re-encoding one canonical way, then sorting the query parameters that remain.
Everything else stays significant — a query that addresses a resource (`?p=/notes/review.md`, `?id=7`) is part of what the link means. A value that does not parse as an absolute URL matches only itself.
func MatchesDocPattern ¶ added in v1.19.0
MatchesDocPattern reports whether a URL matches one of the configured `doc_link_patterns` — the configured half of doc classification, for documents served from somewhere the built-in rules cannot know (a local note server, say). A pattern is the whole URL with `*` standing for any run of characters; everything else is literal, so `http://127.0.0.1:8080/open?p=*` matches every document that server opens and nothing on another port or path.
func ParseDocPatterns ¶ added in v1.19.0
ParseDocPatterns splits a `doc_link_patterns` config value into patterns. URLs cannot contain whitespace, so whitespace separates them; empty pieces are dropped.
func TrimSentenceTail ¶ added in v1.9.0
TrimSentenceTail peels off what belongs to the sentence rather than to a bare URL — trailing punctuation and unbalanced closers — exactly as Extract does when it finds a URL in prose. A caller lifting a URL out of human text stores what Extract would have found, so the two never disagree about where the URL ends.
func UnsafeFormalText ¶ added in v1.7.0
UnsafeFormalText reports control or line-separator characters forbidden in either member of a stored link.
func ValidFormalLabel ¶ added in v1.7.0
ValidFormalLabel reports whether a present stored label is canonical.
func ValidFormalURL ¶ added in v1.7.0
ValidFormalURL reports whether a stored link is safe to expose to openers.
Types ¶
type FormalLink ¶ added in v1.7.0
FormalLink is one ordered, stored task link before system classification.
func LiftTitleURL ¶ added in v1.18.0
func LiftTitleURL(title string, explicit []FormalLink) (string, []FormalLink)
LiftTitleURL is the title half of capture: a title that ENDS in a bare URL keeps the words and hands the URL to the formal link list, so "read the RFC https://example.com/rfc" files a human title with a real link instead of a row whose title is half address bar.
Three deliberate limits keep it unambiguous, because a title rewrite the caller did not ask for has to be predictable:
- only the LAST whitespace-separated word is considered, and only when it is already a valid formal URL — no guessing at bare hosts, no shorthand expansion. Sentence punctuation the URL picked up from the prose ("… see https://example.com/rfc.") is peeled off by the SAME rule Extract uses and handed back to the title, so the stored link is the one the title's own extraction would have produced rather than a 404 with a period on the end.
- a title that is ONLY a URL keeps its title. There is no human remainder to keep and a blank title is not a thing the store accepts; the URL is still lifted into a link, so the row gains the formal link either way.
- a URL already in the explicit list is not lifted twice, and every explicit link keeps its position and its label. That also makes the function idempotent: applying it to its own output changes nothing.
type Link ¶
Link is one reference found in task text. Label is nil for a bare URL — an absent label and an empty one are different answers, and the JSON says so.
func Extract ¶
Extract returns every web link in `text`, in FILE ORDER (masking preserves string offsets, so the three passes interleave correctly — `open <n>` counts the same way the note reads), de-duplicated by URL.
Dedupe keeps the most useful spelling, not the earliest one: among the occurrences of one URL, the first LABELLED occurrence wins, and only if none is labelled does the first (bare) occurrence stand in. Which spelling survives is therefore independent of where in the text each one sits.
Position still decides ORDER: each surviving link is listed at its URL's FIRST occurrence, whichever spelling that was, so adding a label to a URL already in the text never moves anything in the listing.
`shorthands` (name => URL template with %s, from config `link.<name>` rows) additionally expands compact tokens like `jira:OPS-1234`. `customSystems` (name => host, from config `system.<name>` rows) extends classification for self-hosted instances the built-in registry cannot know.