Add markdown support
This commit is contained in:
parent
0da05d465e
commit
b395963709
1
go.mod
1
go.mod
@ -7,6 +7,7 @@ require (
|
||||
git.tebibyte.media/sashakoshka/go-service v0.1.1
|
||||
git.tebibyte.media/sashakoshka/goutil v0.6.0
|
||||
github.com/Masterminds/sprig/v3 v3.3.0
|
||||
github.com/yuin/goldmark v1.7.8
|
||||
)
|
||||
|
||||
require (
|
||||
|
2
go.sum
2
go.sum
@ -40,6 +40,8 @@ github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
|
||||
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
|
||||
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
@ -3,11 +3,13 @@ package providers
|
||||
import "git.tebibyte.media/sashakoshka/step"
|
||||
import fpos "git.tebibyte.media/sashakoshka/step/providers/os"
|
||||
import fpsprig "git.tebibyte.media/sashakoshka/step/providers/sprig"
|
||||
import fpmarkdown "git.tebibyte.media/sashakoshka/step/providers/markdown"
|
||||
|
||||
// All returns a slice of all providers defined in sub-packages.
|
||||
func All () []step.FuncProvider {
|
||||
return []step.FuncProvider {
|
||||
new(fpsprig.Provider),
|
||||
new(fpos.Provider),
|
||||
new(fpsprig.Provider),
|
||||
new(fpmarkdown.Provider),
|
||||
}
|
||||
}
|
||||
|
50
providers/markdown/markdown.go
Normal file
50
providers/markdown/markdown.go
Normal file
@ -0,0 +1,50 @@
|
||||
package markdown
|
||||
|
||||
import "fmt"
|
||||
import "bytes"
|
||||
import "strings"
|
||||
import "html/template"
|
||||
import "github.com/yuin/goldmark"
|
||||
import "github.com/yuin/goldmark/parser"
|
||||
import "git.tebibyte.media/sashakoshka/step"
|
||||
|
||||
var _ step.FuncProvider = new(Provider)
|
||||
|
||||
// Provider provides Markdown functions.
|
||||
type Provider struct {
|
||||
// Options contains parse options that are passed to the converter.
|
||||
Options []parser.ParseOption
|
||||
}
|
||||
|
||||
// FuncMap fulfills the step.FuncProvider interface.
|
||||
func (this *Provider) FuncMap () template.FuncMap {
|
||||
stat := &state {
|
||||
options: this.Options,
|
||||
}
|
||||
return template.FuncMap {
|
||||
"markdown": stat.funcMarkdown,
|
||||
}
|
||||
}
|
||||
|
||||
type state struct {
|
||||
options []parser.ParseOption
|
||||
}
|
||||
|
||||
func (this *state) funcMarkdown (input any) (template.HTML, error) {
|
||||
builder := strings.Builder { }
|
||||
err := goldmark.Convert(toBytes(input), &builder, this.options...)
|
||||
if err != nil { return "", nil }
|
||||
return template.HTML(builder.String()), nil
|
||||
}
|
||||
|
||||
func toBytes (data any) []byte {
|
||||
switch data := data.(type) {
|
||||
case []byte: return data
|
||||
case string: return []byte(data)
|
||||
case template.HTML: return []byte(data)
|
||||
default:
|
||||
buffer := bytes.Buffer { }
|
||||
fmt.Fprint(&buffer, data)
|
||||
return buffer.Bytes()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user