zk/internal/adapter/handlebars/helpers/link.go
Mickaël Menu dc27a7dd7c
Improve Markdown and wiki links matching and generation (#71)
Fallback wiki link resolution by matching on title or path
Add new template variables when generating Markdown links
Add a {{substring}} template helper
2021-09-25 19:28:29 +02:00

32 lines
793 B
Go

package helpers
import (
"github.com/mickael-menu/zk/internal/core"
"github.com/mickael-menu/zk/internal/util"
)
// NewLinkHelper creates a new template helper to generate an internal link
// using a LinkFormatter.
//
// {{format-link "path/to/note.md" "An interesting subject"}} -> (depends on the LinkFormatter)
// [[path/to/note]]
// [An interesting subject](path/to/note)
func NewLinkHelper(formatter core.LinkFormatter, logger util.Logger) interface{} {
return func(path string, opt interface{}) string {
title, _ := opt.(string)
link, err := formatter(core.LinkFormatterContext{
Path: path,
RelPath: path,
AbsPath: path,
Title: title,
Metadata: map[string]interface{}{},
})
if err != nil {
logger.Err(err)
return ""
}
return link
}
}