Add filename and filename-stem template variables (#91)

pull/92/head
Mickaël Menu 3 years ago committed by GitHub
parent 39467a1b7c
commit b1c69b4765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
## Unreleased
### Added
* New template variables `filename` and `filename-stem` when formatting notes (e.g. with `zk list --format`) and for the [`fzf-line`](docs/tool-fzf.md) config key.
### Fixed
* [#89](https://github.com/mickael-menu/zk/issues/89) Calling `zk index` from outside the notebook (contributed by [@adamreese](https://github.com/mickael-menu/zk/pull/90)).

@ -3,7 +3,9 @@
The following variables are available in the templates used when formatting notes, for example with `zk list --format <template>`.
| Variable | Type | Description |
|---------------|----------|--------------------------------------------------------------------------|
|-----------------|----------|--------------------------------------------------------------------------|
| `filename` | string | Filename of the note, including its extension |
| `filename-stem` | string | Filename of the note without the file extension |
| `path` | string | File path to the note, relative to the current directory |
| `abs-path` | string | File path to the note, absolute path including the notebook directory |
| `title` | string | Note title |

@ -43,6 +43,8 @@ The following variables are available in the line template.
| Variable | Type | Description |
|-----------------|----------|--------------------------------------------------------------------|
| `filename` | string | Filename of the note, including its extension |
| `filename-stem` | string | Filename of the note without the file extension |
| `path` | string | File path to the note, relative to the notebook root |
| `abs-path` | string | Absolute file path to the note |
| `rel-path` | string | File path to the note, relative to the current directory |

@ -108,6 +108,8 @@ func (f *NoteFilter) Apply(notes []core.ContextualNote) ([]core.ContextualNote,
for i, note := range notes {
context := lineRenderContext{
Filename: note.Filename(),
FilenameStem: note.FilenameStem(),
Path: note.Path,
AbsPath: absPaths[i],
RelPath: relPaths[i],
@ -157,6 +159,8 @@ func (f *NoteFilter) Apply(notes []core.ContextualNote) ([]core.ContextualNote,
var defaultLineTemplate = `{{style "title" title-or-path}} {{style "understate" body}}`
type lineRenderContext struct {
Filename string
FilenameStem string `handlebars:"filename-stem"`
Path string
AbsPath string `handlebars:"abs-path"`
RelPath string `handlebars:"rel-path"`

@ -1,7 +1,10 @@
package core
import (
"path/filepath"
"time"
"github.com/mickael-menu/zk/internal/util/paths"
)
// NoteID represents the unique ID of a note collection relative to a given
@ -63,6 +66,17 @@ func (n Note) AsMinimalNote() MinimalNote {
}
}
// Filename returns the filename portion of the note path.
func (n Note) Filename() string {
return filepath.Base(n.Path)
}
// FilenameStem returns the filename portion of the note path, excluding its
// file extension.
func (n Note) FilenameStem() string {
return paths.FilenameStem(n.Path)
}
// ContextualNote holds a Note and context-sensitive content snippets.
//
// This is used for example:

@ -34,6 +34,8 @@ func newNoteFormatter(basePath string, template Template, linkFormatter LinkForm
}
return template.Render(noteFormatRenderContext{
Filename: note.Filename(),
FilenameStem: note.FilenameStem(),
Path: path,
AbsPath: absPath,
Title: note.Title,
@ -67,6 +69,8 @@ var noteTermRegex = regexp.MustCompile(`<zk:match>(.*?)</zk:match>`)
// noteFormatRenderContext holds the variables available to the note formatting
// templates.
type noteFormatRenderContext struct {
Filename string `json:"filename"`
FilenameStem string `json:"filenameStem" handlebars:"filename-stem"`
Path string `json:"path"`
AbsPath string `json:"absPath" handlebars:"abs-path"`
Title string `json:"title"`

@ -1,6 +1,7 @@
package core
import (
"path/filepath"
"testing"
"time"
@ -28,7 +29,7 @@ func TestNewNoteFormatter(t *testing.T) {
res, err := formatter(ContextualNote{
Note: Note{
ID: 1,
Path: "note1",
Path: "note1.md",
Title: "Note 1",
Lead: "Lead 1",
Body: "Body 1",
@ -51,7 +52,7 @@ func TestNewNoteFormatter(t *testing.T) {
res, err = formatter(ContextualNote{
Note: Note{
ID: 2,
Path: "dir/note2",
Path: "dir/note2.md",
Title: "Note 2",
Lead: "Lead 2",
Body: "Body 2",
@ -71,8 +72,10 @@ func TestNewNoteFormatter(t *testing.T) {
// Check that the template received the proper contexts
assert.Equal(t, test.template.Contexts, []interface{}{
noteFormatRenderContext{
Path: "note1",
AbsPath: "/notebook/note1",
Filename: "note1.md",
FilenameStem: "note1",
Path: "note1.md",
AbsPath: "/notebook/note1.md",
Title: "Note 1",
Link: opt.NewString("[Note 1](note1)"),
Lead: "Lead 1",
@ -90,8 +93,10 @@ func TestNewNoteFormatter(t *testing.T) {
Checksum: "checksum1",
},
noteFormatRenderContext{
Path: "dir/note2",
AbsPath: "/notebook/dir/note2",
Filename: "note2.md",
FilenameStem: "note2",
Path: "dir/note2.md",
AbsPath: "/notebook/dir/note2.md",
Title: "Note 2",
Link: opt.NewString("[Note 2](dir/note2)"),
Lead: "Lead 2",
@ -123,6 +128,8 @@ func TestNoteFormatterMakesPathRelative(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, test.template.Contexts, []interface{}{
noteFormatRenderContext{
Filename: filepath.Base(expected),
FilenameStem: paths.FilenameStem(expected),
Path: expected,
AbsPath: expectedFull,
Link: opt.NewString("[](" + paths.DropExt(expected) + ")"),
@ -154,6 +161,8 @@ func TestNoteFormatterStylesSnippetTerm(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, test.template.Contexts, []interface{}{
noteFormatRenderContext{
Filename: ".",
FilenameStem: ".",
Path: ".",
AbsPath: "/notebook",
Link: opt.NewString("[]()"),

Loading…
Cancel
Save