logseqext

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package logseqext provides generic extensions to the logseq-go library. Functions here are candidates for upstreaming into logseq-go once that library is restructured.

This package must remain self-contained: it may only import logseq-go, the Go standard library, and golang.org/x packages. It must never import logseq-doctor internal packages (doing so would also create a cyclic import).

Index

Constants

View Source
const JournalDayDivisorMonth = 100

JournalDayDivisorMonth is used to extract the month from a journalDay integer (YYYYMMDD).

View Source
const JournalDayDivisorYear = 10000

JournalDayDivisorYear is used to extract the year from a journalDay integer (YYYYMMDD).

Variables

View Source
var ErrNoParagraph = errors.New("block has no paragraph to insert priority into")

ErrNoParagraph is returned when SetPriority is called on a block with no paragraph.

Functions

func AddSibling

func AddSibling(page logseq.Page, newBlock, before *content.Block, after ...*content.Block)

AddSibling inserts newBlock into page relative to the given anchor blocks. It inserts after the last non-nil block in after, or before the before block, or appends to the page if neither is provided.

func AddTask

func AddTask(opts *AddTaskOptions) error

AddTask adds a task to Logseq. If Key is provided, it searches for an existing task containing that key (case-insensitive) and updates it. Otherwise, creates a new task. If Page is provided, adds to that page. Otherwise, adds to journal for Date. If BlockText is provided, adds as a child of the first block containing that text.

func BlockContentText

func BlockContentText(block *content.Block) string

BlockContentText extracts the text content from a block's content nodes.

func BlockProperties

func BlockProperties(block *content.Block) *content.Properties

BlockProperties returns the Properties node for a task block. logseq-go's block.Properties() only checks the first child — task blocks have a Paragraph first, so it would prepend a new empty Properties node before the TODO line. The parser creates an empty Properties placeholder at position 0; the real properties (id::, collapsed::, etc.) appear after the first Paragraph. This helper finds the first Properties that comes after a Paragraph in the content.

func CleanTaskName

func CleanTaskName(taskContent, marker string) string

CleanTaskName extracts the task name from content: first line, marker stripped, time prefix stripped.

func DateYYYYMMDD

func DateYYYYMMDD(time time.Time) int

DateYYYYMMDD returns the current date in YYYYMMDD format.

func ExtractBlockRefUUID

func ExtractBlockRefUUID(block *content.Block) string

ExtractBlockRefUUID extracts the UUID from a block ref inside a child block.

func ExtractBlockRefUUIDs

func ExtractBlockRefUUIDs(page logseq.Page) []string

ExtractBlockRefUUIDs extracts all block ref UUIDs from a page (ordered).

func ExtractDirectTags

func ExtractDirectTags(contentText string) []string

ExtractDirectTags extracts #hashtags and [[page refs]] from content text.

func ExtractFirstLine

func ExtractFirstLine(taskContent string) string

ExtractFirstLine extracts the first line of task content, stripping the marker and priority.

func FindBlockByIDProperty

func FindBlockByIDProperty(page logseq.Page, uuid string) *content.Block

FindBlockByIDProperty finds a block in a page by its id:: property value. It searches Properties nodes within block content because logseq-go's block.Properties() only checks the first child — task blocks have a paragraph first, then properties.

func FindBlockByKey

func FindBlockByKey(page logseq.Page, parentBlock *content.Block, key string) *content.Block

FindBlockByKey searches for a block containing the specified key (case-insensitive). If parentBlock is provided, searches only among its children. Otherwise, searches in the entire page. Returns the Block if found, nil otherwise.

func FindBlockContainingText

func FindBlockContainingText(page logseq.Page, searchText string) *content.Block

FindBlockContainingText searches for the first block containing the specified text using FindDeep.

func FindTaskMarkerByKey

func FindTaskMarkerByKey(page logseq.Page, parentBlock *content.Block, key string) *content.TaskMarker

FindTaskMarkerByKey searches for a task marker containing the specified key (case-insensitive). If parentBlock is provided, searches only among its children. Otherwise, searches in the entire page. Returns the TaskMarker if found, nil otherwise.

func FormatLogseqDate

func FormatLogseqDate(t time.Time) string

FormatLogseqDate formats a time.Time as a Logseq date string: "[[Saturday, 21.03.2026]]".

func JournalDayToTime

func JournalDayToTime(journalDay int) time.Time

JournalDayToTime converts a Logseq journalDay integer (YYYYMMDD) to a time.Time. Returns zero time for zero input.

func NormalizeTagPrefixes

func NormalizeTagPrefixes(tags []string)

NormalizeTagPrefixes ensures all tags start with "#", are lowercase, and slugified (accents removed, non-alphanumeric chars stripped) to match Python's slugify(t, separator=").

func OpenGraphFromPath

func OpenGraphFromPath(path string) *logseq.Graph

OpenGraphFromPath opens a Logseq graph from the given directory path. Aborts the program if path is empty or the graph cannot be opened, to avoid error-handling boilerplate at every call site.

func OpenPage

func OpenPage(graph *logseq.Graph, pageTitle string) logseq.Page

OpenPage opens a page in the Logseq graph. Aborts the program in case of error to avoid boilerplate at every call site.

func ParseLogseqDate

func ParseLogseqDate(dateStr string) (time.Time, error)

ParseLogseqDate parses a Logseq date string like "[[Saturday, 21.03.2026]]" into a time.Time. Returns zero time (not error) for empty or unparseable strings.

func ParsePriorityFromContent

func ParsePriorityFromContent(contentStr string) content.PriorityValue

ParsePriorityFromContent extracts a priority value from a Logseq API content string. It looks for [#A], [#B], or [#C] patterns and returns the corresponding PriorityValue. Returns PriorityNone if no priority marker is found.

func ReadJournalTitleFormat

func ReadJournalTitleFormat(graphPath string) string

ReadJournalTitleFormat reads the JS-style date format string used for journal page titles from logseq/config.edn (e.g. "EEEE, dd.MM.yyyy"). Returns the Logseq default "EEE do, MMM yyyy" if the file cannot be read or the key is absent. This is a candidate for upstreaming to logseq-go.

func RemoveEmptyBlocks

func RemoveEmptyBlocks(save bool, blocks ...*content.Block) bool

RemoveEmptyBlocks removes blocks that have no child blocks and returns true if any were removed.

func SetPriority

func SetPriority(block *content.Block, priority content.PriorityValue) error

SetPriority sets or replaces the priority marker ([#A]/[#B]/[#C]) on a block. If a Priority node exists, it is updated in place. Otherwise, a new Priority node is inserted after the TaskMarker (or at the start of the first paragraph for plain blocks).

func SetTaskCanceled

func SetTaskCanceled(block *content.Block) error

SetTaskCanceled changes the task marker to CANCELED using logseq-go's WithStatus API.

func UniqueStrings

func UniqueStrings(items []string) []string

UniqueStrings deduplicates a string slice preserving order.

Types

type AddTaskOptions

type AddTaskOptions struct {
	Graph     *logseq.Graph
	Date      time.Time
	Page      string           // Page name to add the task to (empty = journal)
	BlockText string           // Partial text to search for in parent blocks
	Key       string           // Unique key to search for existing task (case-insensitive)
	Name      string           // Short name of the task
	TimeNow   func() time.Time // For testing
}

AddTaskOptions contains options for adding a task to Logseq.

type LogseqFinder

type LogseqFinder interface {
	FindFirstQuery(pageTitle string) string
}

LogseqFinder provides methods for searching within a Logseq graph.

func NewLogseqFinder

func NewLogseqFinder(graph *logseq.Graph) LogseqFinder

NewLogseqFinder creates a new LogseqFinder backed by the given graph.

type SectionedUUID

type SectionedUUID struct {
	UUID   string
	Ranked bool
}

SectionedUUID pairs a block-ref UUID with whether it lives under a section header (ranked=false means it is under Overdue, New tasks, Triaged, Scheduled, or Unranked — any divider that is not the implicit "ranked" area at the top).

func ExtractSectionedBlockRefUUIDs

func ExtractSectionedBlockRefUUIDs(page logseq.Page, unrankedSectionTexts []string) []SectionedUUID

ExtractSectionedBlockRefUUIDs scans a backlog page and returns every block-ref UUID together with whether it is in the ranked area (above all section dividers) or the unranked area (under any divider whose text matches one of unrankedSectionTexts, case-insensitive substring).

The ranked area is defined as top-level blocks that are NOT section-header blocks and NOT descendants of any section-header block.

Jump to

Keyboard shortcuts

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