reference

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package reference models the directed edges of the corpus: links between documents (and sections), their type, their resolution target, and their health classification.

This package is pure domain (standard library only, plus the leaf identity package). It is a lower layer than corpus: corpus depends on reference (a Document holds []RawReference). The authoritative DocumentID type lives in the even-lower identity package, which both reference and corpus import without any cycle. The resolver logic itself lands in a later phase; this file defines the type spine only.

Index

Constants

View Source
const DefaultResolutionPolicy = LongestSuffix

DefaultResolutionPolicy is the policy used when none is configured.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssetExistence

type AssetExistence interface {
	// AssetExists reports whether the given root-relative slash path exists as a
	// non-markdown asset: an existing non-markdown file or directory. Markdown
	// is excluded (tracked via the corpus, not as an asset).
	AssetExists(relPath string) bool
}

AssetExistence answers whether a cleaned, root-relative path points at an existing NON-markdown asset — either a regular file (image/pdf/etc.) or a directory. It is injected so the domain stays free of filesystem access (ADR 0003/0004): the path is always in-root and already cleaned by the resolver before this is consulted. A nil AssetExistence is treated as "no assets exist".

type Catalog

type Catalog interface {
	// HasDocument reports whether id is a known markdown document.
	HasDocument(id identity.DocumentID) bool
	// DocumentIDs returns all known markdown document identities. Order is not
	// relied upon; the resolver sorts candidates itself for determinism.
	DocumentIDs() []identity.DocumentID
	// HasHeading reports whether document id contains the given canonical slug
	// (ADR 0006). The slug must be in the exact dialect the parser produced.
	HasHeading(id identity.DocumentID, slug string) bool
	// LookupAlias returns the documents declaring the given front-matter alias,
	// sorted by DocumentID. Empty if unknown.
	LookupAlias(alias string) []identity.DocumentID
}

Catalog is the read-only view of the corpus that the resolver needs. It is a small domain interface (a real test seam) implemented by *corpus.Corpus and by in-test fakes. Defining it here — over identity.DocumentID only — keeps the resolver pure and avoids a reference→corpus import cycle (corpus already imports reference).

type LinkHealth

type LinkHealth int

LinkHealth classifies the resolution outcome of a reference.

const (
	// Unresolved is the zero value: the reference has not been resolved yet.
	Unresolved LinkHealth = iota
	// Valid means the target exists and (if applicable) the anchor exists.
	Valid
	// Broken means the target document does not exist in the corpus.
	Broken
	// BrokenAnchor means the target document exists but the fragment does not.
	BrokenAnchor
	// NonNote means the target resolved to a non-markdown asset (e.g. image).
	NonNote
	// Ambiguous means the raw target matched more than one candidate document.
	Ambiguous
	// HealthExternal means the reference points off-corpus (not checked unless
	// --check-external is enabled).
	HealthExternal
	// Ignored means the reference was deliberately excluded from analysis.
	Ignored
)

func (LinkHealth) String

func (h LinkHealth) String() string

String returns the canonical name of the health classification.

func (LinkHealth) Valid

func (h LinkHealth) Valid() bool

Valid reports whether h is a defined LinkHealth.

type LinkType

type LinkType int

LinkType classifies the syntactic origin of a reference edge.

const (
	// RelativeLink is a CommonMark relative link, e.g. [text](other.md).
	RelativeLink LinkType = iota
	// Wikilink is an Obsidian-style link, e.g. [[target]] or [[target|alias]].
	Wikilink
	// Anchor is a same- or cross-document fragment link, e.g. #heading.
	Anchor
	// ImageEmbed is an inline image, e.g. ![alt](img.png).
	ImageEmbed
	// Transclusion is an embedded note, e.g. ![[target]].
	Transclusion
	// FrontmatterRelated is an edge derived from front-matter fields such as
	// parent/related. Produced in P3 (graph/hierarchy build); the resolver
	// already routes it through relative-style resolution.
	FrontmatterRelated
	// External is a link to an off-corpus resource (http/https/mailto/etc.).
	External
)

func (LinkType) String

func (t LinkType) String() string

String returns the canonical name of the link type.

func (LinkType) Valid

func (t LinkType) Valid() bool

Valid reports whether t is a defined LinkType.

type RawReference

type RawReference struct {
	// Origin is the document the reference was found in.
	Origin identity.DocumentID
	// RawTarget is the link target text as written (e.g. "../other.md").
	RawTarget string
	// Fragment is the anchor portion without the leading '#', if any.
	Fragment string
	// Type is the syntactic classification of the link.
	Type LinkType
	// Line is the 1-based source line the reference appears on.
	Line int
	// AnchorText is the human-facing display text of the link as written: the
	// inline link/image label ([this](x)), the image alt text (![alt](x)), or a
	// wikilink alias ([[t|alias]], else the bare target). It is a pure-data string
	// the resolver ignores (ADR 0001 keys identity on the target, never the label);
	// it is carried through to the graph edge so the information-scent analysis can
	// score a link's label against its target's title (ADR 0016). Empty when the
	// link has no display text.
	AnchorText string
}

RawReference is a reference edge as extracted from a document, before resolution. It captures everything the resolver needs and nothing it computes.

type Reference

type Reference struct {
	RawReference
	Target ResolvedTarget
	Health LinkHealth
	// Candidates is populated only when Health is Ambiguous: the set of
	// documents the target matched, sorted by DocumentID for deterministic
	// reporting.
	Candidates []identity.DocumentID
}

Reference is a fully classified edge: the raw edge, its resolved target, and its health. Built once by the Resolver (see resolver.go) and treated as immutable.

type ResolutionPolicy

type ResolutionPolicy int

ResolutionPolicy selects how a raw target string is mapped to a DocumentID.

const (
	// Exact requires the raw target to equal a DocumentID after cleaning.
	Exact ResolutionPolicy = iota
	// LongestSuffix matches the candidate DocumentID with the longest matching
	// path suffix. This is the default (see ADR 0001).
	LongestSuffix
	// Basename matches on basename alone (least precise; opt-in).
	Basename
)

func (ResolutionPolicy) String

func (p ResolutionPolicy) String() string

String returns the canonical name of the policy.

func (ResolutionPolicy) Valid

func (p ResolutionPolicy) Valid() bool

Valid reports whether p is a defined ResolutionPolicy.

type ResolvedTarget

type ResolvedTarget struct {
	// Kind tags which fields are meaningful.
	Kind TargetKind
	// DocumentID is set when Kind is TargetDocument, TargetSection or
	// TargetAsset. For TargetDirectory it is the directory's index document
	// (README.md / index.md) when one exists, else empty (ADR 0008).
	DocumentID identity.DocumentID
	// Anchor is the resolved section slug, set when Kind is TargetSection.
	Anchor string
	// Directory is the cleaned directory path, set only when Kind is
	// TargetDirectory (ADR 0008).
	Directory string
	// Children is the sorted set of markdown documents located DIRECTLY in the
	// directory (one level — no recursion), set only when Kind is
	// TargetDirectory. The index document, if any, is included. These are the
	// docs a directory link makes reachable under the default policy (ADR 0008).
	Children []identity.DocumentID
}

ResolvedTarget is the (tagged-union) outcome of resolving a RawReference. The valid fields depend on Kind.

type Resolver

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

Resolver turns RawReferences into health-classified References. It is a stateless domain service: construct once with a Catalog, an optional AssetExistence, and a ResolutionPolicy, then call Resolve per reference.

func NewResolver

func NewResolver(catalog Catalog, assets AssetExistence, policy ResolutionPolicy) *Resolver

NewResolver builds a Resolver. An invalid policy falls back to the default (LongestSuffix). A nil assets lookup means non-markdown targets that are not known documents resolve to Broken rather than NonNote.

func (*Resolver) Resolve

func (r *Resolver) Resolve(raw RawReference) Reference

Resolve classifies a single RawReference. It performs only path arithmetic and catalog lookups — never any filesystem access (asset existence is delegated to the injected AssetExistence).

Classification summary (see ADR 0001/0003/0006):

  • External targets (http/https/mailto/autolink, or Type External) → HealthExternal (not fetched in P2).
  • Anchor-only targets (no path, has fragment) → resolved within the origin document; Valid if the slug exists there, else BrokenAnchor.
  • Relative links: resolved relative to the origin's directory and cleaned. A target that escapes the corpus root → Broken (recorded as a finding, never read). A target resolving to a known markdown doc → Valid (anchor checked if present). A target resolving to an existing non-markdown asset → NonNote. Otherwise → Broken.
  • Wikilinks/transclusions: resolved by ResolutionPolicy against known docs; exactly one candidate → Valid (anchor checked), more than one → Ambiguous (candidates surfaced), zero → alias table, else Broken.

func (*Resolver) ResolveAll

func (r *Resolver) ResolveAll(raws []RawReference) []Reference

ResolveAll resolves every reference and returns the classified edges in input order (callers sort findings later for output determinism).

type TargetKind

type TargetKind int

TargetKind tags the resolved target of a reference.

const (
	// TargetNone is the zero value: no target resolved.
	TargetNone TargetKind = iota
	// TargetDocument means the reference resolved to a document.
	TargetDocument
	// TargetSection means the reference resolved to a section within a document.
	TargetSection
	// TargetAsset means the reference resolved to a non-markdown asset.
	TargetAsset
	// TargetExternal means the reference points to an off-corpus resource.
	TargetExternal
	// TargetDirectory means the reference resolved to a directory in the corpus
	// (a folder containing markdown). See ADR 0008. The ResolvedTarget then
	// carries Directory (the folder path), DocumentID (the directory's index doc,
	// if any), and Children (the markdown docs directly in the folder).
	TargetDirectory
)

func (TargetKind) String

func (k TargetKind) String() string

String returns the canonical name of the target kind.

func (TargetKind) Valid

func (k TargetKind) Valid() bool

Valid reports whether k is a defined TargetKind.

Jump to

Keyboard shortcuts

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