vault

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 21 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 vault is the file layer of Nota: a vault is a folder of markdown notes, and the files on disk are the source of truth.

A vault works on an FS: DirFS for a real directory, MemFS in tests.

All paths in the API are vault-relative, slash-separated and in Unicode NFC, whatever the file system stores. Writes are atomic (temp file and rename), deletes move notes to .trash/, and a path that differs from an existing one only in letter case is refused, because it would overwrite that note on macOS and Windows.

Index

Constants

View Source
const DefaultInbox = "Inbox"

DefaultInbox is the folder new notes land in when none is given.

View Source
const EnvVault = "NOTA_VAULT"

EnvVault is the environment variable that points at the vault.

View Source
const Ext = ".md"

Ext is the file extension of a note.

View Source
const IgnoreFile = ".notaignore"

IgnoreFile is the name of the per-vault ignore file at the vault root.

View Source
const MetaDirName = ".nota"

MetaDirName is the vault-relative folder of Nota's own files.

View Source
const TrashDir = ".trash"

TrashDir is the vault-relative folder deleted notes are moved to.

View Source
const UpdatedKey = "updated"

UpdatedKey is the frontmatter key Save keeps current.

Variables

View Source
var (
	ErrNotFound      = errors.New("not found")
	ErrExists        = errors.New("already exists")
	ErrCaseCollision = errors.New("differs only in case from an existing path")
	ErrInvalidPath   = errors.New("invalid path")
	ErrNotNote       = errors.New("not a markdown note")
)

Errors returned by vault operations. Use errors.Is to test for them.

View Source
var DefaultIgnore = []string{".git/", ".obsidian/", ".nota/*", "!.nota/hooks.toml", "!.nota/views/", "!.nota/plugins/",
	".trash/", ".DS_Store"}

DefaultIgnore lists what every vault ignores, in .notaignore syntax. Nota's own state in .nota/ (index, sync state, merge bases) stays local; the shared configuration in it (hooks.toml, views/, plugins/) is part of the vault and syncs.

Functions

func CleanPath

func CleanPath(p string) (string, error)

CleanPath turns user input into a canonical vault-relative path: NFC, forward slashes, no "." or ".." segments, no leading slash. It refuses paths that leave the vault. The empty string means the vault root.

func ConfigPath

func ConfigPath() (string, error)

ConfigPath returns the config file location: $XDG_CONFIG_HOME/nota/config.toml, or ~/.config/nota/config.toml. The same path is used on every OS so the CLI is configured the same way on macOS, Linux and Windows.

func Filename

func Filename(title string) string

Filename turns a note title into a file name stem: characters that are unsafe on some file system or special in wikilinks are replaced, runs of white space collapse, and leading dots and trailing dots or spaces are removed. Letters such as å, ä and ö are kept. It never returns "".

func Folder

func Folder(rel string) string

Folder returns the folder part of a vault-relative path ("" for the root).

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err means a vault path does not exist.

func IsNote

func IsNote(name string) bool

IsNote reports whether a file name is a markdown note.

func NFC

func NFC(s string) string

NFC returns s in Unicode normalization form C. macOS file systems may hand out decomposed (NFD) names, Linux and the sync protocol use NFC.

func NotePath

func NotePath(p string) (string, error)

NotePath cleans p and adds ".md" when it has no extension.

func Stamp

func Stamp(old, data []byte, now time.Time) []byte

Stamp returns data with `updated` set to now, when both the old and the new content have a frontmatter block that parses. Otherwise data is returned unchanged. Only the `updated` line changes.

func Stem

func Stem(rel string) string

Stem returns the file name of a vault-relative path without extension.

func Title

func Title(body []byte, fallback string) string

Title returns the title of a note body: its first non-empty line with any ATX heading marker ("# ", "## " …) removed, or fallback when the body has no text.

Types

type Config

type Config struct {
	// Vault is the vault directory. A leading "~/" means the home directory.
	Vault string `toml:"vault"`
	// Inbox is the vault-relative folder for unsorted notes.
	Inbox string `toml:"inbox"`
}

Config is the user configuration in ~/.config/nota/config.toml.

func LoadConfig

func LoadConfig(path string) (Config, error)

LoadConfig reads the config file at path. A missing file is not an error and yields the zero Config.

type Entry

type Entry struct {
	// Path is the vault-relative NFC path.
	Path string
	// Disk is the name in the vault's FS, as the file system stores it
	// (possibly NFD). Read it with ReadEntry.
	Disk    string
	ModTime time.Time
	Size    int64
}

Entry is a note found on disk.

type FS

type FS interface {
	fs.ReadDirFS
	fs.ReadFileFS
	fs.StatFS

	// WriteFile replaces name with data atomically: readers see the old
	// content or the new one, never a mix. An existing file's
	// permissions are kept. The parent directory must exist.
	WriteFile(name string, data []byte) error
	// Rename moves a file. The target's parent directory must exist.
	Rename(oldname, newname string) error
	// Remove deletes a file or an empty directory.
	Remove(name string) error
	// MkdirAll creates a directory and any missing parents.
	MkdirAll(name string) error
}

FS is the file system a vault lives on. Reads use the io/fs interfaces; the write methods are the few operations the vault needs on top.

Names follow the io/fs rules: slash-separated, relative to the vault root, "." for the root itself, and as the file system stores them (so a name may be NFD on macOS; the vault maps them onto NFC paths).

func DirFS

func DirFS(dir string) FS

DirFS returns the operating system's file system rooted at dir, which should be absolute.

type Ignore

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

Ignore matches vault-relative paths against gitignore-style rules.

Supported syntax: blank lines and "#" comments, "!" negation, a trailing "/" for directories only, a leading or inner "/" to anchor the pattern at the vault root, and the globs "*", "?", "[...]" and "**". The last matching rule wins, and a path inside an ignored directory is ignored.

func ParseIgnore

func ParseIgnore(lines []string) *Ignore

ParseIgnore builds a matcher from rule lines.

func ParseIgnoreFile

func ParseIgnoreFile(data []byte) *Ignore

ParseIgnoreFile builds a matcher from DefaultIgnore followed by the contents of a .notaignore file.

func (*Ignore) Match

func (m *Ignore) Match(rel string, isDir bool) bool

Match reports whether the slash-separated vault-relative path is ignored, either itself or through one of its parent directories.

type Location

type Location struct {
	// Dir is the absolute vault directory.
	Dir string
	// Source says where Dir came from: "flag", "env", "config" or "default".
	Source string
	// Config is the loaded user configuration.
	Config Config
}

Location is the result of vault discovery.

func Discover

func Discover(flag string) (Location, error)

Discover finds the vault directory. The order is: the flag value (if not empty), $NOTA_VAULT, the `vault` key in the config file, ~/Notes. The directory does not have to exist yet.

type MemFS

type MemFS struct {

	// Now stamps modification times; time.Now when nil.
	Now func() time.Time
	// contains filtered or unexported fields
}

MemFS is an in-memory FS for tests. It is case-sensitive and stores names byte for byte, like ext4. It is safe for concurrent use.

func NewMemFS

func NewMemFS(files map[string]string) *MemFS

NewMemFS returns an empty in-memory file system. files maps names to contents, for setting up fixtures.

func (*MemFS) MkdirAll

func (m *MemFS) MkdirAll(name string) error

MkdirAll implements FS.

func (*MemFS) Open

func (m *MemFS) Open(name string) (fs.File, error)

Open implements fs.FS.

func (*MemFS) ReadDir

func (m *MemFS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir implements fs.ReadDirFS.

func (*MemFS) ReadFile

func (m *MemFS) ReadFile(name string) ([]byte, error)

ReadFile implements fs.ReadFileFS.

func (*MemFS) Remove

func (m *MemFS) Remove(name string) error

Remove implements FS.

func (*MemFS) Rename

func (m *MemFS) Rename(oldname, newname string) error

Rename implements FS. Like rename(2) it replaces an existing target file.

func (*MemFS) Stat

func (m *MemFS) Stat(name string) (fs.FileInfo, error)

Stat implements fs.StatFS.

func (*MemFS) WriteFile

func (m *MemFS) WriteFile(name string, data []byte) error

WriteFile implements FS.

type Note

type Note struct {
	// Path is the vault-relative, slash-separated NFC path, e.g. "Inbox/Möte.md".
	Path string `json:"path"`
	// Title comes from the first line of the body (without "# "), or the
	// file name when the body is empty.
	Title string `json:"title"`
	// Props is the decoded frontmatter. It is empty, not nil, without one.
	Props map[string]any `json:"properties"`
	// Body is everything after the frontmatter.
	Body string `json:"body"`
	// Raw is the whole file, byte for byte.
	Raw []byte `json:"-"`
	// ModTime and Size come from the file system.
	ModTime time.Time `json:"modified"`
	Size    int64     `json:"size"`
}

Note is one markdown file in the vault.

func ParseNote

func ParseNote(rel string, raw []byte) (*Note, error)

ParseNote builds a Note from a file's contents. Broken frontmatter YAML does not fail the note: Props is then empty and the error is returned alongside the note so callers can warn about it.

func (*Note) Folder

func (n *Note) Folder() string

Folder returns the note's vault-relative folder ("" for the root).

func (*Note) Name

func (n *Note) Name() string

Name returns the file name without the extension.

type OSPather

type OSPather interface {
	// OSPath returns the absolute OS path of name.
	OSPath(name string) string
}

OSPather is implemented by file systems backed by real OS paths, such as the one returned by DirFS.

type Options

type Options struct {
	// Inbox is the vault-relative folder for unsorted notes; DefaultInbox if empty.
	Inbox string
	// Create makes the vault directory when it does not exist yet.
	Create bool
	// Bus receives the vault's events. Nil drops them.
	Bus *events.Bus
	// Now is the clock for timestamps written into notes; time.Now when nil.
	Now func() time.Time
}

Options configure Open.

type PathError

type PathError struct {
	Op   string
	Path string
	// Existing is the path already on disk, for ErrExists and ErrCaseCollision.
	Existing string
	Err      error
}

PathError describes a failed operation on one vault path.

func (*PathError) Error

func (e *PathError) Error() string

func (*PathError) Unwrap

func (e *PathError) Unwrap() error

type Vault

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

Vault is an open vault.

func Open

func Open(dir string, opts Options) (*Vault, error)

Open opens the vault in the OS directory dir and loads its .notaignore.

func OpenFS

func OpenFS(fsys FS, name string, opts Options) (*Vault, error)

OpenFS opens a vault on any file system, such as a MemFS in tests. name identifies the vault in events and logs (Root returns it).

func (*Vault) Bus

func (v *Vault) Bus() *events.Bus

Bus returns the event bus the vault emits on (possibly nil).

func (*Vault) Create

func (v *Vault) Create(folder string, data []byte) (*Note, error)

Create writes a new note into folder ("" for the vault root). The file name comes from the note's title; " 2", " 3" … are added until the name is free, comparing names case-insensitively. The note is returned as written; event handlers may have moved it since (see events.Bus.Follow).

func (*Vault) Delete

func (v *Vault) Delete(rel string) (string, error)

Delete moves a note to .trash/, keeping its folder structure, and returns its path inside the vault (".trash/…"). Nothing is removed.

func (*Vault) DeleteFile

func (v *Vault) DeleteFile(rel string) (string, error)

DeleteFile moves any file to .trash/ and returns where it went.

func (*Vault) DiskPath

func (v *Vault) DiskPath(rel string) (string, error)

DiskPath returns the absolute OS path of an existing note, with the name as the file system stores it (which may be NFD on macOS). It fails on file systems that are not backed by the OS.

func (*Vault) Exists

func (v *Vault) Exists(rel string) bool

Exists reports whether the note path exists exactly (after NFC).

func (*Vault) FS

func (v *Vault) FS() FS

FS returns the file system the vault lives on.

func (*Vault) FileExists

func (v *Vault) FileExists(rel string) bool

FileExists reports whether any file exists at exactly rel.

func (*Vault) FreeName

func (v *Vault) FreeName(rel string) (string, error)

FreeName returns a path in the folder of rel that no file uses, case insensitively: rel itself, or rel with " 2", " 3" … before the extension.

func (*Vault) Ignore

func (v *Vault) Ignore() *Ignore

Ignore returns the vault's ignore matcher.

func (*Vault) Ignored

func (v *Vault) Ignored(rel string, isDir bool) bool

Ignored reports whether a vault-relative path is excluded by the ignore rules.

func (*Vault) Inbox

func (v *Vault) Inbox() string

Inbox returns the vault-relative inbox folder.

func (*Vault) List

func (v *Vault) List() ([]Entry, error)

List returns every note entry in the vault.

func (*Vault) MetaDir

func (v *Vault) MetaDir() string

MetaDir returns the absolute path of the vault's .nota directory.

func (*Vault) Move

func (v *Vault) Move(rel, folder string) (string, error)

Move moves a note into another folder, keeping its file name, and returns the new path. It refuses to overwrite an existing note.

func (*Vault) Now

func (v *Vault) Now() time.Time

Now returns the current time from the vault's clock.

func (*Vault) Read

func (v *Vault) Read(rel string) (*Note, error)

Read reads a note by vault-relative path. The ".md" may be left out.

func (*Vault) ReadEntry

func (v *Vault) ReadEntry(e Entry) ([]byte, error)

ReadEntry reads the content of a note found by Walk.

func (*Vault) ReadFile

func (v *Vault) ReadFile(rel string) ([]byte, error)

ReadFile reads any file by vault path. A path that does not exist exactly fails with ErrNotFound (with Existing set when another case of it exists).

func (*Vault) ReloadIgnore

func (v *Vault) ReloadIgnore() error

ReloadIgnore re-reads .notaignore from the vault root.

func (*Vault) Rename

func (v *Vault) Rename(from, to string) (string, error)

Rename moves a note to a new vault-relative path and returns it. It refuses to overwrite an existing note or to create a case collision; renaming a note to a different case of its own name is allowed.

func (*Vault) RenameFile

func (v *Vault) RenameFile(from, to string) (string, error)

RenameFile moves any file to a new path, refusing to overwrite or to create a case collision.

func (*Vault) Root

func (v *Vault) Root() string

Root returns the absolute vault directory (the name given to OpenFS for other file systems).

func (*Vault) Save

func (v *Vault) Save(rel string, data []byte) (string, bool, error)

Save is Write for edits made through Nota (the CLI, the app, MCP, hooks): when the note on disk already has frontmatter and the new content keeps it, `updated` is set to the current local time (RFC 3339). Notes without frontmatter are not given one just for the timestamp. Saving the content the note already has writes nothing and emits no event. A note that does not exist yet is written as is.

func (*Vault) Walk

func (v *Vault) Walk(fn func(Entry) error) error

Walk calls fn for every note in the vault that is not ignored, in lexical order of the on-disk names.

func (*Vault) WalkFiles

func (v *Vault) WalkFiles(fn func(Entry) error) error

WalkFiles calls fn for every regular file in the vault that the ignore rules do not exclude, notes and attachments alike.

func (*Vault) WithOrigin

func (v *Vault) WithOrigin(origin string) *Vault

WithOrigin returns a view of the vault whose events carry origin in Event.Origin, so subscribers can tell who made a change. Hooks use it to keep their own writes from triggering them again.

func (*Vault) Write

func (v *Vault) Write(rel string, data []byte) (string, bool, error)

Write replaces the content of a note, or creates it (and its folders) when it does not exist. It returns the canonical path and whether the note was created.

func (*Vault) WriteFile

func (v *Vault) WriteFile(rel string, data []byte) (string, bool, error)

WriteFile writes any file raw, like Write does for notes: no timestamps are touched. It creates missing folders and refuses ignored paths and case collisions.

Jump to

Keyboard shortcuts

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