Add a default template

pull/6/head
Mickaël Menu 3 years ago
parent 7c1bd8b318
commit 81f3aaa6fa
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -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)
}

@ -1,7 +1,5 @@
# Maintaining a daily journal
<!--{% raw %}-->
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.
<!--{% endraw %}-->

@ -1,6 +1,5 @@
# Send notes for processing by other programs
<!--{% raw %}-->
<!-- TODO: --color=none -->
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
```
<!--{% endraw %}-->

@ -1,7 +1,5 @@
# Creating a new note
<!--{% raw %}-->
You can add a new note to a [notebook](notebook.md) using `zk new --title "An interesting concept" [<directory>]`.
`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
```
<!--{% endraw %}-->

@ -1,7 +1,5 @@
# Styling
<!--{% raw %}-->
<!-- TODO: semantic rules -->
`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`
<!--{% endraw %}-->

@ -1,7 +1,5 @@
# Template context when creating notes
<!--{% raw %}-->
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 |
<!--{% endraw %}-->

@ -1,7 +1,5 @@
# Template syntax
<!--{% raw %}-->
`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}}
```
<!--{% endraw %}-->

@ -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

Loading…
Cancel
Save