obsidian

package
v0.206.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package obsidian imports an Obsidian vault into Second Brain (ADR-079; 03-contracts.md § Obsidian vaults). It reads Markdown files on this machine and never writes a note of the user's: a note becomes a node, its [[links]] edges, its #tags topics. The importer makes no network or model calls; sharing imported content with agents is a separate opt-in. The one thing it writes is a copy of 2nd Brain, into a subfolder of its own, when the user switches that on (export.go, ADR-117).

Index

Constants

View Source
const (
	MaxNotes     = 50_000
	MaxNoteBytes = 4 << 20
	// Interval is how often every vault is looked at: a walk that stats each
	// note and reads only the ones whose time or size changed.
	Interval = 10 * time.Minute
	// FirstLook is how long after the daemon starts the first look waits.
	FirstLook = 2 * time.Minute
)

Limits on what one vault may make the daemon do (resources are the moat): how many notes a vault may hold, how much of one note is read, and how often the vaults are looked at without being asked.

View Source
const ExcerptRunes = 400

ExcerptRunes is how much of a note's body is kept: Second Brain's summary length (store.BrainSummaryRunes).

View Source
const (
	// ExportDebounce is how long after 2nd Brain changed a copy is written:
	// a session's read adds memories in a burst, and one write covers it.
	ExportDebounce = 3 * time.Minute
)

Variables

View Source
var ErrBadSubfolder = errors.New("the subfolder must be one folder name: no slashes, not starting with a dot")

ErrBadSubfolder is a subfolder that is not one plain folder name.

View Source
var ErrNotAbsolute = errors.New("the vault's path must be absolute")

ErrNotAbsolute is a vault path that is not absolute.

View Source
var ErrNotFolder = errors.New("not a folder on this computer")

ErrNotFolder is a vault path that is not a folder on this machine.

View Source
var ErrTooMany = fmt.Errorf("more than %d notes: pick the vault's own folder", MaxNotes)

ErrTooMany is a folder with more notes than a vault may hold.

Functions

func CleanPath

func CleanPath(dir string) (string, error)

CleanPath checks a folder the user picked: absolute, a folder, cleaned.

func CleanSubfolder added in v0.206.0

func CleanSubfolder(s string) (string, error)

CleanSubfolder checks the folder a copy is written in: one plain folder name that SafeName leaves as it is. "" is the default.

func Render added in v0.206.0

func Render(d store.BrainExportData, o RenderOptions) (map[string]string, int)

Render is a copy's files: path (slash-separated, from the copy's folder, starting with the subfolder) to content, and how many are memories. It reads no disk; the same brain renders the same bytes.

func SafeName added in v0.206.0

func SafeName(s string, max int) string

SafeName makes s a file name every OS takes and Obsidian links to: the characters Windows refuses and the ones that break a [[link]] (# ^ [ ] |) become spaces, control characters go, no leading or trailing dot or space, at most max bytes cut on a character, a reserved Windows name prefixed with "_". Every script is kept as it is. "" becomes "Untitled".

func TitleOf

func TitleOf(rel string) string

TitleOf is a note's title from its path in the vault: the file name without the .md extension.

func Walk

func Walk(ctx context.Context, root string, skip ...string) ([]file, error)

Walk lists a vault's Markdown notes: every .md file under root, outside folders whose name starts with a dot (.obsidian, .trash, .git), node_modules and the skip folders (absolute: a copy of 2nd Brain Caprock writes, ADR-117), not following links. Attachments are never opened.

Types

type ExportResult added in v0.206.0

type ExportResult struct {
	// Memories is how many memory files the copy holds; Index how many
	// topic, people and project notes.
	Memories int `json:"memories"`
	Index    int `json:"index"`
	// Written files were new or changed; Unchanged were already right;
	// Removed were Caprock's, unchanged, and no longer wanted.
	Written   int `json:"written"`
	Unchanged int `json:"unchanged"`
	Removed   int `json:"removed"`
	// Kept is what was left alone: edited by the user, or not Caprock's.
	Kept []store.BrainExportKept `json:"kept"`
	Ms   int64                   `json:"ms"`
}

ExportResult is what one write of a copy did.

type Exporter added in v0.206.0

type Exporter struct {
	Store *store.Store
	Now   func() time.Time
	// Exclude is the projects of the folders excluded from 2nd Brain
	// (Settings → 2nd Brain); their memories are not copied.
	Exclude func() []string
	Log     *slog.Logger
	// contains filtered or unexported fields
}

Exporter keeps copies of 2nd Brain: one write at a time.

func (*Exporter) Add added in v0.206.0

func (x *Exporter) Add(ctx context.Context, dir, subfolder string, personal bool) (store.BrainExport, ExportResult, error)

Add starts a copy in dir (switched on) and writes it at once.

func (*Exporter) Poke added in v0.206.0

func (x *Exporter) Poke()

Poke says 2nd Brain changed: the copies are written ExportDebounce later.

func (*Exporter) Remove added in v0.206.0

func (x *Exporter) Remove(ctx context.Context, id int64) (RemoveResult, error)

Remove deletes a copy: the files in its subfolder that are Caprock's and unchanged since it wrote them, the folders that leaves empty, and the setting. Every other file stays, and is reported.

func (*Exporter) Run added in v0.206.0

func (x *Exporter) Run(ctx context.Context)

Run writes the copies FirstLook after it starts, then ExportDebounce after each Poke, until ctx ends.

func (*Exporter) Set added in v0.206.0

func (x *Exporter) Set(ctx context.Context, id int64, on, personal *bool) (store.BrainExport, *ExportResult, error)

Set switches a copy on or off and Personal memories in or out; a copy that is on afterwards is written at once.

func (*Exporter) Write added in v0.206.0

Write writes one copy now, switched on or not: Write now.

func (*Exporter) WriteAll added in v0.206.0

func (x *Exporter) WriteAll(ctx context.Context)

WriteAll writes every copy that is switched on.

type Note

type Note struct {
	// Title is the file's name without .md, as Obsidian names a note.
	Title string
	// Aliases are the frontmatter's aliases (or alias), in order, without
	// duplicates.
	Aliases []string
	// Tags are the frontmatter's tags and the inline #tags, without the
	// '#', in order of first appearance, without duplicates (compared in
	// lower case).
	Tags []string
	// Links are the targets of [[links]], the note's name only: no heading
	// (#…), block (^…), display text (|…), folder or .md. Self-links and
	// empty targets ([[#Heading]]) are left out.
	Links []string
	// Excerpt is the body, frontmatter and Markdown dropped, flattened to
	// one paragraph and clipped (ExcerptRunes): what Second Brain shows as
	// the note's summary.
	Excerpt string
	// Caprock is a note Caprock wrote: its frontmatter says `caprock: true`
	// (a copy of 2nd Brain, ADR-117). The import leaves it out, so a copy
	// never comes back in as notes.
	Caprock bool
}

Note is what one Markdown file says, as the import uses it.

func Parse

func Parse(rel string, data []byte) Note

Parse reads one note. rel is its path in the vault, slash-separated.

type RemoveResult added in v0.206.0

type RemoveResult struct {
	Removed int                     `json:"removed"`
	Kept    []store.BrainExportKept `json:"kept"`
}

RemoveResult is what removing a copy did.

type RenderOptions added in v0.206.0

type RenderOptions struct {
	// Subfolder is the folder the files go in, inside the copy's folder.
	Subfolder string
	// Exclude is project labels whose memories are left out.
	Exclude []string
	// Personal is folders labelled Personal: a memory whose session ran in
	// one, or under one, is left out.
	Personal []string
}

RenderOptions says what a copy holds.

type Result

type Result struct {
	// Notes is how many Markdown notes the vault holds.
	Notes int `json:"notes"`
	// Read is how many were read: new or changed since the last scan.
	Read int `json:"read"`
	// Added, Changed and Removed count note files new to Second Brain,
	// changed, and gone from the vault.
	Added   int `json:"added"`
	Changed int `json:"changed"`
	Removed int `json:"removed"`
	// Merged is how many added notes joined a node that was already there
	// (the same title or alias) instead of making one.
	Merged int `json:"merged"`
	// Links is how many edges the vault's import wants now; Linked and
	// Unlinked how many it added and dropped in this scan.
	Links    int `json:"links"`
	Linked   int `json:"linked"`
	Unlinked int `json:"unlinked"`
	// Skipped is how many notes could not be kept (no title, unreadable).
	Skipped int `json:"skipped"`
	// Ms is how long the scan took.
	Ms int64 `json:"ms"`
	// WriteMs includes acquiring, running and committing the write transaction.
	WriteMs int64 `json:"write_ms"`
}

Result is what one scan of a vault did.

type Service

type Service struct {
	Store *store.Store
	Log   *slog.Logger
	// Changed is told when a scan changed Second Brain, to publish a frame.
	Changed func(vaultID int64, removed bool)
	Now     func() time.Time
	// contains filtered or unexported fields
}

Service imports and refreshes vaults: one scan at a time.

func (*Service) Add

func (s *Service) Add(ctx context.Context, dir string, agents bool) (store.BrainVault, Result, error)

Add imports a vault: the folder is checked, kept, and scanned at once.

func (*Service) Refresh

func (s *Service) Refresh(ctx context.Context, id int64, force bool) (store.BrainVault, Result, error)

Refresh scans one vault again. force rereads every note and resolves every link and tag, including edits that kept their previous size and timestamp.

func (*Service) RefreshAll

func (s *Service) RefreshAll(ctx context.Context)

RefreshAll looks at every vault: a walk, and reads only of what changed.

func (*Service) Remove

func (s *Service) Remove(ctx context.Context, id int64) error

Remove takes a vault out of Second Brain.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

Run looks at every vault FirstLook after it starts and every Interval after that, until ctx ends. No file watcher: a walk that stats is cheap on a large vault, a watcher per folder is not.

func (*Service) SetAgents

func (s *Service) SetAgents(ctx context.Context, id int64, on bool) (store.BrainVault, error)

SetAgents turns a vault's notes reaching agents on or off.

Jump to

Keyboard shortcuts

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