From e9a64f3b41b5eae47dec7c0ecfd1caae83136abc Mon Sep 17 00:00:00 2001 From: Alexander NeonXP Kiryukhin Date: Sat, 20 Jul 2024 21:56:37 +0300 Subject: initial --- utils/date.go | 7 +++++++ utils/markdown.go | 21 +++++++++++++++++++++ utils/render.go | 15 +++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 utils/date.go create mode 100644 utils/markdown.go create mode 100644 utils/render.go (limited to 'utils') diff --git a/utils/date.go b/utils/date.go new file mode 100644 index 0000000..3a6197d --- /dev/null +++ b/utils/date.go @@ -0,0 +1,7 @@ +package utils + +import "time" + +func FormatDate(ts int64) string { + return time.Unix(ts, 0).Format("15:04 02.01.2006") +} diff --git a/utils/markdown.go b/utils/markdown.go new file mode 100644 index 0000000..b47a02b --- /dev/null +++ b/utils/markdown.go @@ -0,0 +1,21 @@ +package utils + +import ( + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" +) + +func MarkdownToHTML(md string) string { + // create markdown parser with extensions + extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock + p := parser.NewWithExtensions(extensions) + doc := p.Parse([]byte(md)) + + // create HTML renderer with extensions + htmlFlags := html.CommonFlags | html.HrefTargetBlank + opts := html.RendererOptions{Flags: htmlFlags} + renderer := html.NewRenderer(opts) + + return string(markdown.Render(doc, renderer)) +} diff --git a/utils/render.go b/utils/render.go new file mode 100644 index 0000000..787c453 --- /dev/null +++ b/utils/render.go @@ -0,0 +1,15 @@ +package utils + +import ( + "net/http" + + "github.com/a-h/templ" + "github.com/labstack/echo/v4" +) + +func Render(c echo.Context, cmp templ.Component) error { + c.Response().WriteHeader(http.StatusOK) + c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8) + + return cmp.Render(c.Request().Context(), c.Response()) +} -- cgit v1.2.3