storage

package
v0.1.1-0...-227144d Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package storage provides the functionality for interacting with the zet database.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsMD

func ContainsMD(files []os.DirEntry) bool

ContainsMD reports whether a slice of files contains a README.md file.

func SplitZettel

func SplitZettel(tx *sqlx.Tx, z *Zettel, content string)

SplitZettel breaks a zettel's contents and assigns it's parts to associated fields: title, body, links, and tags.

Example
existingZettels := getTestZettelMap()
db, err := insertTestZettelMap(existingZettels)
if err != nil {
	fmt.Printf("Error inserting zettel map: %v", err)
	return
}
defer db.Close()
tx, err := db.Beginx()
if err != nil {
	return
}
defer tx.Rollback()

const e = `# Example Title

This is some body text.
It can span multiple lines[^f].

See:

* [20231028013031](../20231028013031) Some linked Zettel
* [20240000003031](../20240000003031) Non-existent Zettel

[^f]: [20231028013031](../20231028013031) Another linked Zettel

    #tag1 badTag #tag2`

z := &Zettel{}
SplitZettel(tx, z, e)
fmt.Println("Title:", z.Title)
fmt.Println("Body:")
fmt.Printf("%q\n", z.Body)
fmt.Println("Links:")
for _, l := range z.Links {
	fmt.Printf("\t%s\n", l.Content)
}
fmt.Println("Tags:")
for _, t := range z.Tags {
	fmt.Printf("\t%s\n", t.Name)
}
Output:
Title: Example Title
Body:
"\nThis is some body text.\nIt can span multiple lines[^f].\n\nSee:\n\n\n"
Links:
	[20231028013031](../20231028013031) Some linked Zettel
	[20231028013031](../20231028013031) Another linked Zettel
Tags:
	tag1
	tag2

func ZettelIdDir

func ZettelIdDir(tx *sqlx.Tx, iso string) (int, error)

ZettelIdDir retrieves and returns the zettel using a given unique isosec (director name).

Types

type Link struct {
	ID           int    `db:"id"`             // unique link id
	Content      string `db:"content"`        // zettel link
	FromZettelID int    `db:"from_zettel_id"` // zettel id where link lives
	ToZettelID   int    `db:"to_zettel_id"`   // zettel id where link points to
}

type ResultZettel

type ResultZettel struct {
	Zettel

	// TitleSnippet holds a snippet of the zettel's title as returned by
	// the SQLite snippet function. It's typically a substring of the
	// title that matches the search query, often with added context for
	// highlighting support.
	TitleSnippet string `db:"title_snippet"`

	// BodySnippet contains a snippet of the zettel's body as returned by
	// the SQLite snippet function. Similar to TitleSnippet, it includes
	// a part of the body text that matches the search criteria. If a
	// match was found, it will be surrounded by additional text to
	// support highlighting.
	BodySnippet string `db:"body_snippet"`

	// TagsSnippet holds a snippet of the zettel's tag line returned by the
	// SQLite snippet function. If a match was found, it will be
	// surrounded by additional text to support highlighting.
	TagsSnippet string `db:"tags_snippet"`
}

type Storage

type Storage struct {
	DB *sqlx.DB
}

func NewStorage

func NewStorage(db *sqlx.DB) (*Storage, error)

NewStorage creates and returns a new Storage instance with a given database connection.

func UpdateDB

func UpdateDB(zetPath, dbPath string) (*Storage, error)

UpdateDB initializes the database, retrieve zet state from the database, and updates the database to sync the flat files and the data storage.

func (*Storage) AllZettels

func (s *Storage) AllZettels(sort string) ([]Zettel, error)

AllZettels returns all existing zettel files with optional sorting. Optional argument should be a valid SQL ORDER BY clause, e.g., "mtime DESC".

func (*Storage) Close

func (s *Storage) Close()

Close closes th database connection.

func (*Storage) GetDB

func (s *Storage) GetDB() *sqlx.DB

func (*Storage) Merge

func (s *Storage) Merge(rootContent string) (string, error)

Merge fetches and merges the content of each linked zettel, creating a single body of text.

func (*Storage) SearchZettels

func (s *Storage) SearchZettels(term, before, after string) ([]ResultZettel, error)

SearchZettels searches the zettelkasten for zettels matching the query. The before and after arguments are used for wrapping any matching text. It returns a slice of Zettels.

type Tag

type Tag struct {
	ID   int    `db:"id"`   // unique tag id
	Name string `db:"name"` // unique tag name
}

type Zettel

type Zettel struct {
	ID      int    `db:"id"`    // unique id
	Name    string `db:"name"`  // name of file
	Title   string `db:"title"` // title of file
	Body    string `db:"body"`  // body of file
	Links   []Link // links to other zettels
	Tags    []Tag  // zettels tags
	Mtime   string `db:"mtime"`    // modification time
	DirName string `db:"dir_name"` // modification time
}

func GetZettel

func GetZettel(db *sqlx.DB, id int) (Zettel, error)

GetZettel returns a zettel from the database for a given zettel id.

Jump to

Keyboard shortcuts

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