Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/template-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
11 changes: 9 additions & 2 deletions internal/core/note_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
Expand Down
23 changes: 14 additions & 9 deletions internal/core/note_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -121,21 +124,22 @@ 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{},
},
})
}

// 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) {
Expand All @@ -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},
},
Expand Down