Add "abs-path" to template format (#60)

pull/67/head
Peter Stuifzand 3 years ago committed by Mickaël Menu
parent dba28a6436
commit 977625bb3d
No known key found for this signature in database
GPG Key ID: 53D73664CD359895

@ -2,7 +2,12 @@
All notable changes to this project will be documented in this file.
<!-- ## Unreleased -->
## Unreleased
### Added
* Use the `{{abs-path}}` template variable when [formatting notes](docs/template-format.md) to print the absolute path to the note (contributed by [@pstuifzand](https://github.com/mickael-menu/zk/pull/60)).
## 0.6.0

@ -5,6 +5,7 @@ The following variables are available in the templates used when formatting note
| Variable | Type | Description |
|---------------|----------|--------------------------------------------------------------------------|
| `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 |
| `link` | string | Markdown link to the note, relative to the current directory<sup>1</sup> |
| `lead` | string | First paragraph extracted from the note content |

@ -23,14 +23,20 @@ func newNoteFormatter(basePath string, template Template, linkFormatter LinkForm
return "", err
}
absPath, err := fs.Abs(filepath.Join(basePath, note.Path))
if err != nil {
return "", err
}
snippets := make([]string, 0)
for _, snippet := range note.Snippets {
snippets = append(snippets, noteTermRegex.ReplaceAllString(snippet, termRepl))
}
return template.Render(noteFormatRenderContext{
Path: path,
Title: note.Title,
Path: path,
AbsPath: absPath,
Title: note.Title,
Link: newLazyStringer(func() string {
link, _ := linkFormatter(path, note.Title)
return link
@ -56,6 +62,7 @@ var noteTermRegex = regexp.MustCompile(`<zk:match>(.*?)</zk:match>`)
// templates.
type noteFormatRenderContext struct {
Path string `json:"path"`
AbsPath string `json:"absPath" handlebars:"abs-path"`
Title string `json:"title"`
Link fmt.Stringer `json:"link"`
Lead string `json:"lead"`

@ -6,6 +6,7 @@ import (
"github.com/mickael-menu/zk/internal/util/opt"
"github.com/mickael-menu/zk/internal/util/paths"
"github.com/mickael-menu/zk/internal/util/test/assert"
)
@ -71,6 +72,7 @@ func TestNewNoteFormatter(t *testing.T) {
assert.Equal(t, test.template.Contexts, []interface{}{
noteFormatRenderContext{
Path: "note1",
AbsPath: "/notebook/note1",
Title: "Note 1",
Link: opt.NewString("[Note 1](note1)"),
Lead: "Lead 1",
@ -89,6 +91,7 @@ func TestNewNoteFormatter(t *testing.T) {
},
noteFormatRenderContext{
Path: "dir/note2",
AbsPath: "/notebook/dir/note2",
Title: "Note 2",
Link: opt.NewString("[Note 2](dir/note2)"),
Lead: "Lead 2",
@ -106,7 +109,7 @@ func TestNewNoteFormatter(t *testing.T) {
}
func TestNoteFormatterMakesPathRelative(t *testing.T) {
test := func(basePath, currentPath, path string, expected string) {
test := func(basePath, currentPath, path string, expected, expectedFull string) {
test := formatTest{
rootDir: basePath,
workingDir: currentPath,
@ -121,6 +124,7 @@ func TestNoteFormatterMakesPathRelative(t *testing.T) {
assert.Equal(t, test.template.Contexts, []interface{}{
noteFormatRenderContext{
Path: expected,
AbsPath: expectedFull,
Link: opt.NewString("[](" + paths.DropExt(expected) + ")"),
Snippets: []string{},
},
@ -128,14 +132,14 @@ func TestNoteFormatterMakesPathRelative(t *testing.T) {
}
// Check that the path is relative to the current directory.
test("", "", "note.md", "note.md")
test("", "", "dir/note.md", "dir/note.md")
test("/abs/zk", "/abs/zk", "note.md", "note.md")
test("/abs/zk", "/abs/zk", "dir/note.md", "dir/note.md")
test("/abs/zk", "/abs/zk/dir", "note.md", "../note.md")
test("/abs/zk", "/abs/zk/dir", "dir/note.md", "note.md")
test("/abs/zk", "/abs", "note.md", "zk/note.md")
test("/abs/zk", "/abs", "dir/note.md", "zk/dir/note.md")
test("", "", "note.md", "note.md", "/notebook/note.md")
test("", "", "dir/note.md", "dir/note.md", "/notebook/dir/note.md")
test("/abs/zk", "/abs/zk", "note.md", "note.md", "/abs/zk/note.md")
test("/abs/zk", "/abs/zk", "dir/note.md", "dir/note.md", "/abs/zk/dir/note.md")
test("/abs/zk", "/abs/zk/dir", "note.md", "../note.md", "/abs/zk/note.md")
test("/abs/zk", "/abs/zk/dir", "dir/note.md", "note.md", "/abs/zk/dir/note.md")
test("/abs/zk", "/abs", "note.md", "zk/note.md", "/abs/zk/note.md")
test("/abs/zk", "/abs", "dir/note.md", "zk/dir/note.md", "/abs/zk/dir/note.md")
}
func TestNoteFormatterStylesSnippetTerm(t *testing.T) {
@ -151,6 +155,7 @@ func TestNoteFormatterStylesSnippetTerm(t *testing.T) {
assert.Equal(t, test.template.Contexts, []interface{}{
noteFormatRenderContext{
Path: ".",
AbsPath: "/notebook",
Link: opt.NewString("[]()"),
Snippets: []string{expected},
},

Loading…
Cancel
Save