diff --git a/core/zk/zk.go b/core/zk/zk.go index 86c85d1..5efe7c1 100644 --- a/core/zk/zk.go +++ b/core/zk/zk.go @@ -3,7 +3,6 @@ package zk import ( "fmt" "io/ioutil" - "os" "path/filepath" "strings" @@ -35,7 +34,7 @@ const defaultConfig = `# zk configuration file # Template used to generate a note's content. # If not an absolute path, it is relative to .zk/templates/ -#template = "default.md" +template = "default.md" # Configure random ID generation. @@ -144,6 +143,11 @@ const defaultConfig = `# zk configuration file #conf = '$EDITOR "$ZK_PATH/.zk/config.toml"' ` +const defaultTemplate = `# {{title}} + +{{content}} +` + // Zk (Zettelkasten) represents an opened notebook. type Zk struct { // Notebook root path. @@ -205,18 +209,14 @@ func Create(path string) error { return wrap(fmt.Errorf("a notebook already exists in %v", existingPath)) } - // Create .zk and .zk/templates directories. - err = os.MkdirAll(filepath.Join(path, ".zk/templates"), os.ModePerm) - if err != nil { - return wrap(err) - } - // Write default config.toml. - f, err := os.Create(filepath.Join(path, ".zk/config.toml")) + err = paths.WriteString(filepath.Join(path, ".zk/config.toml"), defaultConfig) if err != nil { return wrap(err) } - _, err = f.WriteString(defaultConfig) + + // Write default template. + err = paths.WriteString(filepath.Join(path, ".zk/templates/default.md"), defaultTemplate) if err != nil { return wrap(err) } diff --git a/docs/daily-journal.md b/docs/daily-journal.md index cb910af..2406fb9 100644 --- a/docs/daily-journal.md +++ b/docs/daily-journal.md @@ -1,7 +1,5 @@ # Maintaining a daily journal - - Let's assume you want to write daily notes named like `2021-02-16.md` in a `journal/daily` sub-directory. This common use case is a good fit for creating a [note group](config-group.md) overriding the default [note creation](note-creation.md) settings. First, create a `group` entry in the [configuration file](config.md) to set the note settings for this directory. Refer to the [template syntax reference](template.md) to understand how to use the `{{date}}` helper. @@ -45,4 +43,3 @@ Let's unpack this alias: * `$ZK_PATH` is set to the absolute path of the current [notebook](notebook.md) when running an alias. Using it allows you to run `zk daily` no matter where you are in the notebook folder hierarchy. * We need to use double quotes around `$ZK_PATH`, otherwise it will not be expanded. - diff --git a/docs/external-processing.md b/docs/external-processing.md index 8c5c558..17bed28 100644 --- a/docs/external-processing.md +++ b/docs/external-processing.md @@ -1,6 +1,5 @@ # Send notes for processing by other programs - A great way to expand `zk` feature set is to explore a wealth of command-line tools available. You can use `zk`'s powerful [searching and filtering](note-filtering.md) capabilities to select notes before delegating further processing to other programs. @@ -59,4 +58,3 @@ In this particular case, we usually want to process only one note at a time. You $ zk list --format {{raw-content}} --limit 1 ``` - diff --git a/docs/note-creation.md b/docs/note-creation.md index 49c5240..a3f38d0 100644 --- a/docs/note-creation.md +++ b/docs/note-creation.md @@ -1,7 +1,5 @@ # Creating a new note - - You can add a new note to a [notebook](notebook.md) using `zk new --title "An interesting concept" []`. `zk` automatically generates a filename and initial content according to rules set in your [configuration file](config.md). These settings can be customized per [group of notes](config-group.md) in your notebook, as illustrated in [Maintaining a daily journal](daily-journal.md). @@ -24,5 +22,3 @@ For example, to use the content of the macOS clipboard as the initial content yo $ pbpaste | zk new ``` - - diff --git a/docs/style.md b/docs/style.md index e8e5915..67dc760 100644 --- a/docs/style.md +++ b/docs/style.md @@ -1,7 +1,5 @@ # Styling - - `zk` supports a `{{style}}` [template helper](template.md) to format its output with colors and font decorations. @@ -29,4 +27,3 @@ or so, nothing continued to happen. * Background color: `black-bg`, `red-bg`, `green-bg`, `yellow-bg`, `blue-bg`, `magenta-bg`, `cyan-bg`, `white-bg` * Background color (bright): `bright-black-bg`, `bright-red-bg`, `bright-green-bg`, `bright-yellow-bg`, `bright-blue-bg`, `bright-magenta-bg`, `bright-cyan-bg`, `bright-white-bg` - diff --git a/docs/template-creation.md b/docs/template-creation.md index b21d110..1ad24d1 100644 --- a/docs/template-creation.md +++ b/docs/template-creation.md @@ -1,7 +1,5 @@ # Template context when creating notes - - The following variables are available in the templates used when [creating new notes](note-creation.md) – both for the filename and the note content. | Variable | Type | Description | @@ -20,4 +18,3 @@ These additional variables are available only to the note content template, once | `filename` | string | Filename generated for this note, including the file extension | | `filename-stem` | string | Filename without the file extension | - diff --git a/docs/template.md b/docs/template.md index 36b8f77..ca5d67e 100644 --- a/docs/template.md +++ b/docs/template.md @@ -1,7 +1,5 @@ # Template syntax - - `zk` uses the [Handlebars template syntax](https://handlebarsjs.com/guide) for its templates. The list of variables available depends of the running command: * [Template context when creating notes](template-creation.md) (i.e. `zk new`) @@ -86,4 +84,3 @@ The `{{style}}` helper is mostly useful when formatting content for the command- {{#style 'underline'}}Another text{{/style}} ``` - diff --git a/util/paths/paths.go b/util/paths/paths.go index 6f2724e..17ff8b5 100644 --- a/util/paths/paths.go +++ b/util/paths/paths.go @@ -51,12 +51,22 @@ func FilenameStem(path string) string { return strings.TrimSuffix(filename, ext) } -// WriteString writes the given content into a new file at the given path. +// WriteString writes the given content into a new file at the given path, +// creating any intermediate directories if needed. func WriteString(path string, content string) error { + dir := filepath.Dir(path) + if dir != "." && dir != ".." { + err := os.MkdirAll(dir, os.ModePerm) + if err != nil { + return err + } + } + f, err := os.Create(path) if err != nil { return err } + defer f.Close() _, err = f.WriteString(content) return err