2021-04-14 18:14:01 +00:00
|
|
|
package core
|
2020-12-25 11:14:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
"github.com/mickael-menu/zk/internal/util/errors"
|
2020-12-25 11:14:01 +00:00
|
|
|
)
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
// NotebookStore retrieves or creates new notebooks.
|
|
|
|
type NotebookStore struct {
|
2021-04-25 10:40:53 +00:00
|
|
|
config Config
|
|
|
|
notebookFactory NotebookFactory
|
|
|
|
templateLoader TemplateLoader
|
|
|
|
fs FileStorage
|
2021-04-14 18:14:01 +00:00
|
|
|
|
|
|
|
// Cached opened notebooks.
|
|
|
|
notebooks map[string]*Notebook
|
|
|
|
}
|
|
|
|
|
|
|
|
type NotebookStorePorts struct {
|
2021-04-25 10:40:53 +00:00
|
|
|
NotebookFactory NotebookFactory
|
|
|
|
TemplateLoader TemplateLoader
|
|
|
|
FS FileStorage
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNotebookStore creates a new NotebookStore instance using the given
|
|
|
|
// options and port implementations.
|
|
|
|
func NewNotebookStore(config Config, ports NotebookStorePorts) *NotebookStore {
|
|
|
|
return &NotebookStore{
|
2021-04-25 10:40:53 +00:00
|
|
|
config: config,
|
|
|
|
notebookFactory: ports.NotebookFactory,
|
|
|
|
templateLoader: ports.TemplateLoader,
|
|
|
|
fs: ports.FS,
|
|
|
|
notebooks: map[string]*Notebook{},
|
2021-04-14 18:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 18:47:49 +00:00
|
|
|
// ErrNotebookNotFound is an error returned when a notebook cannot be found at the given path or its parents.
|
|
|
|
type ErrNotebookNotFound string
|
|
|
|
|
|
|
|
func (e ErrNotebookNotFound) Error() string {
|
|
|
|
return fmt.Sprintf("no notebook found in %s or a parent directory", string(e))
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
// Open returns a new Notebook instance for the notebook containing the
|
|
|
|
// given file path.
|
|
|
|
func (ns *NotebookStore) Open(path string) (*Notebook, error) {
|
2021-10-11 18:38:37 +00:00
|
|
|
wrap := errors.Wrapper("failed to open notebook")
|
2021-04-14 18:14:01 +00:00
|
|
|
|
|
|
|
path = ns.fs.Canonical(path)
|
|
|
|
nb := ns.cachedNotebookAt(path)
|
|
|
|
if nb != nil {
|
|
|
|
return nb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := ns.fs.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
path, err = ns.locateNotebook(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := filepath.Join(path, ".zk/config.toml")
|
2023-04-11 15:22:06 +00:00
|
|
|
config, err := OpenConfig(configPath, ns.config, ns.fs, false)
|
2021-04-14 18:14:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nb, err = ns.notebookFactory(path, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
ns.notebooks[path] = nb
|
|
|
|
|
|
|
|
return nb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cachedNotebookAt returns any cached notebook containing the given path.
|
|
|
|
func (ns *NotebookStore) cachedNotebookAt(path string) *Notebook {
|
|
|
|
path, err := ns.fs.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for root, nb := range ns.notebooks {
|
|
|
|
if isDesc, err := ns.fs.IsDescendantOf(root, path); isDesc && err == nil {
|
|
|
|
return nb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:17:38 +00:00
|
|
|
// InitOpts holds the user preferences when creating a new notebook.
|
|
|
|
type InitOpts struct {
|
|
|
|
WikiLinks bool
|
|
|
|
Hashtags bool
|
|
|
|
ColonTags bool
|
|
|
|
MultiwordTags bool
|
|
|
|
}
|
|
|
|
|
2021-12-17 17:07:56 +00:00
|
|
|
// NewDefaultInitOpts creates a new instance of InitOpts with the default values.
|
|
|
|
func NewDefaultInitOpts() InitOpts {
|
|
|
|
return InitOpts{
|
|
|
|
WikiLinks: true,
|
|
|
|
Hashtags: true,
|
|
|
|
ColonTags: false,
|
|
|
|
MultiwordTags: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
// Init creates a new notebook at the given file path.
|
2021-04-24 13:17:38 +00:00
|
|
|
func (ns *NotebookStore) Init(path string, options InitOpts) (*Notebook, error) {
|
2021-04-14 18:14:01 +00:00
|
|
|
wrap := errors.Wrapper("init")
|
|
|
|
|
|
|
|
path, err := ns.fs.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingPath, err := ns.locateNotebook(path); err == nil {
|
|
|
|
return nil, wrap(fmt.Errorf("a notebook already exists in %v", existingPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the default configuration file.
|
2021-04-24 13:17:38 +00:00
|
|
|
config, err := ns.generateConfig(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
err = ns.fs.Write(filepath.Join(path, ".zk/config.toml"), []byte(config))
|
2021-04-14 18:14:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the default template.
|
|
|
|
err = ns.fs.Write(filepath.Join(path, ".zk/templates/default.md"), []byte(defaultTemplate))
|
|
|
|
if err != nil {
|
|
|
|
return nil, wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns.Open(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// locateNotebook finds the root of the notebook containing the given path.
|
|
|
|
func (ns *NotebookStore) locateNotebook(path string) (string, error) {
|
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
panic("absolute path expected")
|
|
|
|
}
|
|
|
|
|
|
|
|
var locate func(string) (string, error)
|
|
|
|
locate = func(currentPath string) (string, error) {
|
2022-12-04 09:11:06 +00:00
|
|
|
// For Windows, the root dir may end with volume name, e.g. E:\\
|
2022-02-22 13:33:30 +00:00
|
|
|
if currentPath == "/" || currentPath == filepath.VolumeName(currentPath)+"\\" || currentPath == "." {
|
2021-04-14 18:14:01 +00:00
|
|
|
return "", ErrNotebookNotFound(path)
|
|
|
|
}
|
|
|
|
exists, err := ns.fs.DirExists(filepath.Join(currentPath, ".zk"))
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return "", err
|
|
|
|
case exists:
|
|
|
|
return currentPath, nil
|
|
|
|
default:
|
|
|
|
return locate(filepath.Dir(currentPath))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return locate(path)
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:17:38 +00:00
|
|
|
func (ns *NotebookStore) generateConfig(options InitOpts) (string, error) {
|
2021-04-25 10:40:53 +00:00
|
|
|
template, err := ns.templateLoader.LoadTemplate(defaultConfig)
|
2021-04-24 13:17:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return template.Render(options)
|
|
|
|
}
|
|
|
|
|
2021-02-14 22:05:11 +00:00
|
|
|
const defaultConfig = `# zk configuration file
|
|
|
|
#
|
|
|
|
# Uncomment the properties you want to customize.
|
|
|
|
|
|
|
|
# NOTE SETTINGS
|
|
|
|
#
|
|
|
|
# Defines the default options used when generating new notes.
|
|
|
|
[note]
|
|
|
|
|
|
|
|
# Language used when writing notes.
|
|
|
|
# This is used to generate slugs or with date formats.
|
|
|
|
#language = "en"
|
|
|
|
|
|
|
|
# The default title used for new note, if no ` + "`" + `--title` + "`" + ` flag is provided.
|
|
|
|
#default-title = "Untitled"
|
|
|
|
|
|
|
|
# Template used to generate a note's filename, without extension.
|
2021-04-24 13:17:38 +00:00
|
|
|
#filename = "\{{id}}"
|
2021-02-14 22:05:11 +00:00
|
|
|
|
|
|
|
# The file extension used for the notes.
|
|
|
|
#extension = "md"
|
|
|
|
|
|
|
|
# Template used to generate a note's content.
|
|
|
|
# If not an absolute path, it is relative to .zk/templates/
|
2021-02-27 13:24:40 +00:00
|
|
|
template = "default.md"
|
2021-02-14 22:05:11 +00:00
|
|
|
|
2021-07-10 10:21:21 +00:00
|
|
|
# Path globs ignored while indexing existing notes.
|
|
|
|
#ignore = [
|
|
|
|
# "drafts/*",
|
|
|
|
# "log.md"
|
|
|
|
#]
|
|
|
|
|
2021-02-14 22:05:11 +00:00
|
|
|
# Configure random ID generation.
|
|
|
|
|
|
|
|
# The charset used for random IDs. You can use:
|
|
|
|
# * letters: only letters from a to z.
|
|
|
|
# * numbers: 0 to 9
|
|
|
|
# * alphanum: letters + numbers
|
|
|
|
# * hex: hexadecimal, from a to f and 0 to 9
|
|
|
|
# * custom string: will use any character from the provided value
|
|
|
|
#id-charset = "alphanum"
|
|
|
|
|
|
|
|
# Length of the generated IDs.
|
|
|
|
#id-length = 4
|
|
|
|
|
|
|
|
# Letter case for the random IDs, among lower, upper or mixed.
|
|
|
|
#id-case = "lower"
|
|
|
|
|
|
|
|
|
|
|
|
# EXTRA VARIABLES
|
|
|
|
#
|
|
|
|
# A dictionary of variables you can use for any custom values when generating
|
2021-04-24 13:17:38 +00:00
|
|
|
# new notes. They are accessible in templates with \{{extra.<key>}}
|
2021-02-14 22:05:11 +00:00
|
|
|
[extra]
|
|
|
|
|
|
|
|
#key = "value"
|
|
|
|
|
|
|
|
|
2021-02-19 21:41:36 +00:00
|
|
|
# GROUP OVERRIDES
|
2021-02-14 22:05:11 +00:00
|
|
|
#
|
|
|
|
# You can override global settings from [note] and [extra] for a particular
|
2021-02-19 21:41:36 +00:00
|
|
|
# group of notes by declaring a [group."<name>"] section.
|
|
|
|
#
|
|
|
|
# Specify the list of directories which will automatically belong to the group
|
|
|
|
# with the optional ` + "`" + `paths` + "`" + ` property.
|
|
|
|
#
|
2021-03-07 16:00:09 +00:00
|
|
|
# Omitting ` + "`" + `paths` + "`" + ` is equivalent to providing a single path equal to the name of
|
2021-02-19 21:41:36 +00:00
|
|
|
# the group. This can be useful to quickly declare a group by the name of the
|
|
|
|
# directory it applies to.
|
2021-02-14 22:05:11 +00:00
|
|
|
|
2021-04-14 18:14:01 +00:00
|
|
|
#[group."<NAME>"]
|
2021-02-19 21:41:36 +00:00
|
|
|
#paths = ["<DIR1>", "<DIR2>"]
|
2021-04-14 18:14:01 +00:00
|
|
|
#[group."<NAME>".note]
|
2022-12-04 09:11:06 +00:00
|
|
|
#filename = "\{{format-date now}}"
|
2021-04-14 18:14:01 +00:00
|
|
|
#[group."<NAME>".extra]
|
2021-02-14 22:05:11 +00:00
|
|
|
#key = "value"
|
|
|
|
|
|
|
|
|
2021-03-11 19:59:53 +00:00
|
|
|
# MARKDOWN SETTINGS
|
|
|
|
[format.markdown]
|
2021-04-18 14:37:54 +00:00
|
|
|
|
|
|
|
# Format used to generate links between notes.
|
|
|
|
# Either "wiki", "markdown" or a custom template. Default is "markdown".
|
2021-04-24 13:17:38 +00:00
|
|
|
{{#if WikiLinks}}
|
|
|
|
link-format = "wiki"
|
|
|
|
{{else}}
|
2021-04-18 14:37:54 +00:00
|
|
|
#link-format = "wiki"
|
2021-04-24 13:17:38 +00:00
|
|
|
{{/if}}
|
2021-04-18 14:37:54 +00:00
|
|
|
# Indicates whether a link's path will be percent-encoded.
|
|
|
|
# Defaults to true for "markdown" format and false for "wiki" format.
|
|
|
|
#link-encode-path = true
|
|
|
|
# Indicates whether a link's path file extension will be removed.
|
|
|
|
# Defaults to true.
|
|
|
|
#link-drop-extension = true
|
|
|
|
|
|
|
|
# Enable support for #hashtags.
|
2021-04-24 13:17:38 +00:00
|
|
|
{{#if Hashtags}}
|
|
|
|
hashtags = true
|
|
|
|
{{else}}
|
|
|
|
hashtags = false
|
|
|
|
{{/if}}
|
2021-04-18 14:37:54 +00:00
|
|
|
# Enable support for :colon:separated:tags:.
|
2021-04-24 13:17:38 +00:00
|
|
|
{{#if ColonTags}}
|
|
|
|
colon-tags = true
|
|
|
|
{{else}}
|
|
|
|
colon-tags = false
|
|
|
|
{{/if}}
|
2021-03-11 19:59:53 +00:00
|
|
|
# Enable support for Bear's #multi-word tags#
|
|
|
|
# Hashtags must be enabled for multi-word tags to work.
|
2021-04-24 13:17:38 +00:00
|
|
|
{{#if MultiwordTags}}
|
|
|
|
multiword-tags = true
|
|
|
|
{{else}}
|
|
|
|
multiword-tags = false
|
|
|
|
{{/if}}
|
2021-03-11 19:59:53 +00:00
|
|
|
|
|
|
|
|
2021-02-14 22:05:11 +00:00
|
|
|
# EXTERNAL TOOLS
|
|
|
|
[tool]
|
|
|
|
|
|
|
|
# Default editor used to open notes. When not set, the EDITOR or VISUAL
|
|
|
|
# environment variables are used.
|
|
|
|
#editor = "vim"
|
|
|
|
|
|
|
|
# Pager used to scroll through long output. If you want to disable paging
|
|
|
|
# altogether, set it to an empty string "".
|
|
|
|
#pager = "less -FIRX"
|
|
|
|
|
|
|
|
# Command used to preview a note during interactive fzf mode.
|
|
|
|
# Set it to an empty string "" to disable preview.
|
|
|
|
|
|
|
|
# bat is a great tool to render Markdown document with syntax highlighting.
|
|
|
|
#https://github.com/sharkdp/bat
|
2021-02-24 20:49:56 +00:00
|
|
|
#fzf-preview = "bat -p --color always {-1}"
|
2021-02-14 22:05:11 +00:00
|
|
|
|
|
|
|
|
2021-05-16 20:11:31 +00:00
|
|
|
# LSP
|
|
|
|
#
|
|
|
|
# Configure basic editor integration for LSP-compatible editors.
|
|
|
|
# See https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md
|
|
|
|
#
|
|
|
|
[lsp]
|
|
|
|
|
|
|
|
[lsp.diagnostics]
|
|
|
|
# Each diagnostic can have for value: none, hint, info, warning, error
|
|
|
|
|
|
|
|
# Report titles of wiki-links as hints.
|
|
|
|
#wiki-title = "hint"
|
|
|
|
# Warn for dead links between notes.
|
|
|
|
dead-link = "error"
|
|
|
|
|
2021-10-23 19:26:23 +00:00
|
|
|
[lsp.completion]
|
|
|
|
# Customize the completion pop-up of your LSP client.
|
|
|
|
|
|
|
|
# Show the note title in the completion pop-up, or fallback on its path if empty.
|
|
|
|
#note-label = "{{title-or-path}}"
|
|
|
|
# Filter out the completion pop-up using the note title or its path.
|
|
|
|
#note-filter-text = "{{title}} {{path}}"
|
|
|
|
# Show the note filename without extension as detail.
|
|
|
|
#note-detail = "{{filename-stem}}"
|
|
|
|
|
2021-05-16 20:11:31 +00:00
|
|
|
|
2021-03-24 20:06:32 +00:00
|
|
|
# NAMED FILTERS
|
|
|
|
#
|
|
|
|
# A named filter is a set of note filtering options used frequently together.
|
|
|
|
#
|
|
|
|
[filter]
|
|
|
|
|
|
|
|
# Matches the notes created the last two weeks. For example:
|
|
|
|
# $ zk list recents --limit 15
|
|
|
|
# $ zk edit recents --interactive
|
|
|
|
#recents = "--sort created- --created-after 'last two weeks'"
|
|
|
|
|
|
|
|
|
2021-02-14 22:05:11 +00:00
|
|
|
# COMMAND ALIASES
|
|
|
|
#
|
|
|
|
# Aliases are user commands called with ` + "`" + `zk <alias> [<flags>] [<args>]` + "`" + `.
|
|
|
|
#
|
|
|
|
# The alias will be executed with ` + "`" + `$SHELL -c` + "`" + `, please refer to your shell's
|
|
|
|
# man page to see the available syntax. In most shells:
|
|
|
|
# * $@ can be used to expand all the provided flags and arguments
|
|
|
|
# * you can pipe commands together with the usual | character
|
|
|
|
#
|
|
|
|
[alias]
|
|
|
|
# Here are a few aliases to get you started.
|
|
|
|
|
|
|
|
# Shortcut to a command.
|
|
|
|
#ls = "zk list $@"
|
|
|
|
|
|
|
|
# Default flags for an existing command.
|
|
|
|
#list = "zk list --quiet $@"
|
|
|
|
|
|
|
|
# Edit the last modified note.
|
|
|
|
#editlast = "zk edit --limit 1 --sort modified- $@"
|
|
|
|
|
|
|
|
# Edit the notes selected interactively among the notes created the last two weeks.
|
|
|
|
# This alias doesn't take any argument, so we don't use $@.
|
|
|
|
#recent = "zk edit --sort created- --created-after 'last two weeks' --interactive"
|
|
|
|
|
|
|
|
# Print paths separated with colons for the notes found with the given
|
|
|
|
# arguments. This can be useful to expand a complex search query into a flag
|
|
|
|
# taking only paths. For example:
|
2021-03-07 16:14:07 +00:00
|
|
|
# zk list --link-to "` + "`" + `zk path -m potatoe` + "`" + `"
|
2021-04-24 13:17:38 +00:00
|
|
|
#path = "zk list --quiet --format \{{path}} --delimiter , $@"
|
2021-02-14 22:05:11 +00:00
|
|
|
|
|
|
|
# Show a random note.
|
|
|
|
#lucky = "zk list --quiet --format full --sort random --limit 1"
|
|
|
|
|
|
|
|
# Returns the Git history for the notes found with the given arguments.
|
|
|
|
# Note the use of a pipe and the location of $@.
|
|
|
|
#hist = "zk list --format path --delimiter0 --quiet $@ | xargs -t -0 git log --patch --"
|
|
|
|
|
|
|
|
# Edit this configuration file.
|
2021-03-18 18:47:49 +00:00
|
|
|
#conf = '$EDITOR "$ZK_NOTEBOOK_DIR/.zk/config.toml"'
|
2021-02-14 22:05:11 +00:00
|
|
|
`
|
2020-12-25 11:14:01 +00:00
|
|
|
|
2021-02-27 13:24:40 +00:00
|
|
|
const defaultTemplate = `# {{title}}
|
|
|
|
|
|
|
|
{{content}}
|
|
|
|
`
|