docs/pkg/help/render.go
Snider 6b0443c6f7 feat: import go-help library as pkg/help
All source, tests, and templates from forge.lthn.ai/core/go-help.
94% test coverage preserved. All tests pass.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-06 16:30:16 +00:00

36 lines
909 B
Go

// SPDX-Licence-Identifier: EUPL-1.2
package help
import (
"bytes"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
)
// RenderMarkdown converts Markdown content to an HTML fragment.
// It uses goldmark with GitHub Flavoured Markdown (tables, strikethrough,
// autolinks), smart quotes/dashes (typographer), and allows raw HTML
// in the source for embedded code examples.
//
// The returned string is an HTML fragment without <html>/<body> wrappers;
// the server templates handle the page structure.
func RenderMarkdown(content string) (string, error) {
md := goldmark.New(
goldmark.WithExtensions(
extension.GFM,
extension.Typographer,
),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
)
var buf bytes.Buffer
if err := md.Convert([]byte(content), &buf); err != nil {
return "", err
}
return buf.String(), nil
}