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
- Variables
- func CleanPath(p string) (string, error)
- func ConfigPath() (string, error)
- func Filename(title string) string
- func Folder(rel string) string
- func IsNotFound(err error) bool
- func IsNote(name string) bool
- func NFC(s string) string
- func NotePath(p string) (string, error)
- func Stamp(old, data []byte, now time.Time) []byte
- func Stem(rel string) string
- func Title(body []byte, fallback string) string
- type Config
- type Entry
- type FS
- type Ignore
- type Location
- type MemFS
- func (m *MemFS) MkdirAll(name string) error
- func (m *MemFS) Open(name string) (fs.File, error)
- func (m *MemFS) ReadDir(name string) ([]fs.DirEntry, error)
- func (m *MemFS) ReadFile(name string) ([]byte, error)
- func (m *MemFS) Remove(name string) error
- func (m *MemFS) Rename(oldname, newname string) error
- func (m *MemFS) Stat(name string) (fs.FileInfo, error)
- func (m *MemFS) WriteFile(name string, data []byte) error
- type Note
- type OSPather
- type Options
- type PathError
- type Vault
- func (v *Vault) Bus() *events.Bus
- func (v *Vault) Create(folder string, data []byte) (*Note, error)
- func (v *Vault) Delete(rel string) (string, error)
- func (v *Vault) DeleteFile(rel string) (string, error)
- func (v *Vault) DiskPath(rel string) (string, error)
- func (v *Vault) Exists(rel string) bool
- func (v *Vault) FS() FS
- func (v *Vault) FileExists(rel string) bool
- func (v *Vault) FreeName(rel string) (string, error)
- func (v *Vault) Ignore() *Ignore
- func (v *Vault) Ignored(rel string, isDir bool) bool
- func (v *Vault) Inbox() string
- func (v *Vault) List() ([]Entry, error)
- func (v *Vault) MetaDir() string
- func (v *Vault) Move(rel, folder string) (string, error)
- func (v *Vault) Now() time.Time
- func (v *Vault) Read(rel string) (*Note, error)
- func (v *Vault) ReadEntry(e Entry) ([]byte, error)
- func (v *Vault) ReadFile(rel string) ([]byte, error)
- func (v *Vault) ReloadIgnore() error
- func (v *Vault) Rename(from, to string) (string, error)
- func (v *Vault) RenameFile(from, to string) (string, error)
- func (v *Vault) Root() string
- func (v *Vault) Save(rel string, data []byte) (string, bool, error)
- func (v *Vault) Walk(fn func(Entry) error) error
- func (v *Vault) WalkFiles(fn func(Entry) error) error
- func (v *Vault) WithOrigin(origin string) *Vault
- func (v *Vault) Write(rel string, data []byte) (string, bool, error)
- func (v *Vault) WriteFile(rel string, data []byte) (string, bool, error)
Constants ¶
const DefaultInbox = "Inbox"
DefaultInbox is the folder new notes land in when none is given.
const EnvVault = "NOTA_VAULT"
EnvVault is the environment variable that points at the vault.
const Ext = ".md"
Ext is the file extension of a note.
const IgnoreFile = ".notaignore"
IgnoreFile is the name of the per-vault ignore file at the vault root.
const MetaDirName = ".nota"
MetaDirName is the vault-relative folder of Nota's own files.
const TrashDir = ".trash"
TrashDir is the vault-relative folder deleted notes are moved to.
const UpdatedKey = "updated"
UpdatedKey is the frontmatter key Save keeps current.
Variables ¶
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.
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 ¶
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 ¶
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 ¶
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 IsNotFound ¶
IsNotFound reports whether err means a vault path does not exist.
func NFC ¶
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 Stamp ¶
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.
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 ¶
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).
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 ¶
ParseIgnore builds a matcher from rule lines.
func ParseIgnoreFile ¶
ParseIgnoreFile builds a matcher from DefaultIgnore followed by the contents of a .notaignore file.
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.
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 ¶
NewMemFS returns an empty in-memory file system. files maps names to contents, for setting up fixtures.
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 ¶
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.
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.
type Vault ¶
type Vault struct {
// contains filtered or unexported fields
}
Vault is an open vault.
func OpenFS ¶
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) Create ¶
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 ¶
Delete moves a note to .trash/, keeping its folder structure, and returns its path inside the vault (".trash/…"). Nothing is removed.
func (*Vault) DeleteFile ¶
DeleteFile moves any file to .trash/ and returns where it went.
func (*Vault) DiskPath ¶
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) FileExists ¶
FileExists reports whether any file exists at exactly rel.
func (*Vault) FreeName ¶
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) Ignored ¶
Ignored reports whether a vault-relative path is excluded by the ignore rules.
func (*Vault) Move ¶
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) ReadFile ¶
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 ¶
ReloadIgnore re-reads .notaignore from the vault root.
func (*Vault) Rename ¶
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 ¶
RenameFile moves any file to a new path, refusing to overwrite or to create a case collision.
func (*Vault) Root ¶
Root returns the absolute vault directory (the name given to OpenFS for other file systems).
func (*Vault) Save ¶
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 ¶
Walk calls fn for every note in the vault that is not ignored, in lexical order of the on-disk names.
func (*Vault) WalkFiles ¶
WalkFiles calls fn for every regular file in the vault that the ignore rules do not exclude, notes and attachments alike.
func (*Vault) WithOrigin ¶
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.