graph

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMentionNotFound = errors.New("mention not found")

ErrMentionNotFound is returned by LinkifyMention when the target line is out of range or holds no unlinked, whole-word occurrence of the name to wrap.

View Source
var FenceDelimiterRe = regexp.MustCompile("^\\s*(?:[-*+]\\s+)?(```|~~~)")

FenceDelimiterRe matches a code-fence delimiter line: optional indentation, an optional list-bullet prefix (Logseq nests fences inside bullets), then a backtick or tilde fence marker. Exported so the editor tint (internal/views) classifies delimiter rows with the same grammar the index and renderer use.

Functions

func FilenameFromPageName

func FilenameFromPageName(name string) string

FilenameFromPageName is the inverse of PageNameFromFilename for the cases the App needs to construct a path for: journal page names (YYYY-MM-DD → YYYY_MM_DD.md) and namespace pages (proj/nested → proj___nested.md). Returns "" for any page name whose filename shape isn't covered here — callers should fall back to a more permissive resolution (or treat it as a programmer error) in that case. Used by App.Update's "." handler to derive the journal-file path when creating today's journal on demand.

func InlineCodeSpans added in v2.3.0

func InlineCodeSpans(line string) []search.Span

InlineCodeSpans returns the byte ranges of inline code spans on line — the odd segments of a backtick split, mirroring how parse.go (appendWikiLinks) and render.replaceWikiLinksOutsideInlineCode treat backticks: split the line on "`", even-indexed segments are literal text, odd-indexed segments are inline code. Each returned span includes its leading backtick (and trailing backtick, when the code run is closed). An unpaired trailing backtick still opens a code span that runs to the end of the line — strings.Split leaves that final segment at an odd index too, so parse/render already treat it as code, and this must match.

Exported because internal/render also needs it (hideMarkdownLinkURLsOutsideInlineCode): render and parse must agree byte-for-byte on what counts as inline code.

func IsJournalPageName

func IsJournalPageName(name string) bool

IsJournalPageName reports whether name is a journal page name (YYYY-MM-DD). This is the form PageNameFromFilename produces; it answers "is this page a journal?" without requiring the file to exist on disk.

func LinkifyMention

func LinkifyMention(body string, line int, target string) (string, search.Span, error)

LinkifyMention wraps the first unlinked, whole-word, case-insensitive occurrence of target on the 1-based line in body as [[<matched>]]. The matched bytes are preserved verbatim (Resolve is fold-keyed, so casing need not be normalised). The splice is by absolute byte offset, so line endings (incl. CRLF) and any trailing newline are preserved untouched. span is the matched range in the original body. It returns ErrMentionNotFound when no such occurrence exists — which also guards against double-wrapping an already [[linked]] mention and against a line that changed since detection.

func PageNameFromFilename

func PageNameFromFilename(name string) string

PageNameFromFilename returns the logical page name for a Logseq filename. Journals (YYYY_MM_DD.md) become YYYY-MM-DD; namespace separator "___" becomes "/".

Types

type FenceState added in v2.3.0

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

FenceState tracks fenced-code state across a top-to-bottom line walk. The zero value means "outside any fence". A fence opened with backticks is closed only by backticks (tildes only by tildes), matching CommonMark: the other marker inside an open fence is content.

func (*FenceState) Step added in v2.3.0

func (f *FenceState) Step(line string) bool

Step consumes one line and reports whether that line belongs to fenced code — either as content or as a fence delimiter itself.

type Index

type Index struct {
	GraphPath  string
	Pages      []PageMeta
	ByName     map[string]*PageMeta // case-preserving (filename-derived)
	ByNameFold map[string]*PageMeta // case-folded (lowercase key) — for [[ALPHA]] → Alpha
	Backlinks  map[string][]Ref     // keyed by strings.ToLower(target) — resolution is case-insensitive
	Todos      []TodoBullet
	Journals   []string // journal page names, sorted ascending
}

Index is the read-only in-memory view of a Logseq graph.

func BuildIndex

func BuildIndex(graphPath string) (*Index, error)

BuildIndex walks <graphPath>/pages and <graphPath>/journals once and returns the populated Index. Unreadable files are logged to stderr and skipped.

func (*Index) Resolve

func (idx *Index) Resolve(name string) (*PageMeta, bool)

Resolve returns the page whose name matches name exactly, or whose case-folded form matches name's lower-case. The case-preserving PageMeta is always returned, so callers see the on-disk name.

type LinkHit

type LinkHit struct {
	Target string
	Line   int // 1-based
}

LinkHit is one wiki-link occurrence in a page body.

func ExtractWikiLinks(body string) []LinkHit

ExtractWikiLinks returns every [[link]] in body, skipping fenced code blocks. Targets like [[A|alias]] are recorded as "A".

type PageMeta

type PageMeta struct {
	Name      string    // logical page name (e.g., "proj/nested" or "2026-05-24")
	Path      string    // absolute filesystem path
	IsJournal bool      // true when the page lives under journals/
	ModTime   time.Time // file modification time; zero if stat failed
}

PageMeta is the indexed metadata for one .md file in the graph.

type Ref

type Ref struct {
	FromPage   string
	LineNumber int    // 1-based
	Context    string // the source line, trimmed
}

Ref points from one page's body to another page name.

type TodoBullet

type TodoBullet struct {
	Page       string
	LineNumber int    // 1-based
	Marker     string // TODO | LATER | DOING | WAITING
	Priority   string // "" | "A" | "B" | "C"
	Text       string // remainder after marker (and priority), trimmed
	Ordinal    int    // 0-based index among open todos on the same page (document order)
}

TodoBullet is one open task bullet (TODO/LATER/DOING/WAITING).

type TodoHit

type TodoHit struct {
	Marker   string // TODO | LATER | DOING | WAITING
	Priority string // "" | "A" | "B" | "C"
	Text     string
	Line     int // 1-based
}

TodoHit is one open task bullet occurrence in a page body.

func ExtractTodos

func ExtractTodos(body string) []TodoHit

ExtractTodos returns open TODO/LATER/DOING/WAITING bullets in body. DONE and CANCELED are intentionally ignored (we only surface open tasks).

type UnlinkedRef

type UnlinkedRef struct {
	FilePath string
	PageName string
	Line     int
	Context  string
	Match    search.Span
}

UnlinkedRef is a bare-text mention of a page elsewhere in the graph that is not already a [[link]]. PageName is the referencing page (for the row label and navigation); Match locates the mention within Context.

func FilterUnlinked

func FilterUnlinked(hits []search.Hit, targetPath string, read func(path string) (string, error)) []UnlinkedRef

FilterUnlinked turns raw ripgrep hits for the target page into unlinked references, dropping any hit that is in the target's own file, inside a fenced code block, or already inside a [[…]] link. read returns a file's body; a read error drops every hit from that file (we can't verify its fence state).

Jump to

Keyboard shortcuts

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