From 9eaa0ee444df89849ce9a6eb46c187b5b70bd6db Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 18 Jun 2024 17:34:12 +0500 Subject: [PATCH] fix: made the good importable Markdown module. --- handler.go | 6 +++++- mdx/main.go | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/handler.go b/handler.go index 588f3f9..61ed64f 100644 --- a/handler.go +++ b/handler.go @@ -66,8 +66,11 @@ func DefaultPP(mod string) *tpp.Preprocessor { ctx context.Context, s *tpp.Script, ) { + s.SetImportDir(mod) - s.SetImports(xmodules.Modules) + modules := xmodules.GetModules() + modules.Add("markdown", mdx.GetModule()) + s.SetImports(modules) s.EnableFileImport(true) vals := ctx.Value(KeyValues).(*ContextValues) @@ -78,6 +81,7 @@ func DefaultPP(mod string) *tpp.Preprocessor { s.Add("__index_suffix__", vals.IndexSuffix) s.Add("__tpp_ext__", vals.Ext) s.Add("__source_path__", vals.SourcePath) + }).SetPreCode(func(ctx context.Context) []byte { return []byte(` markdown := func(...args) { diff --git a/mdx/main.go b/mdx/main.go index a5bcd63..6e6734c 100644 --- a/mdx/main.go +++ b/mdx/main.go @@ -5,6 +5,19 @@ import "github.com/gomarkdown/markdown/parser" import "github.com/d5/tengo/v2" import "bytes" +func GetModule() tengo.Importable { + return &tengo.BuiltinModule{Attrs:map[string]tengo.Object{ + "new": &tengo.BuiltinFunction{ + Name: "new", + Value: func( + args ...tengo.Object, + ) (tengo.Object, error){ + return MakeDefaultMarkdown(), nil + }, + }, + }} +} + type Markdown struct { tengo.ObjectImpl MakeParser func() *parser.Parser