summaryrefslogtreecommitdiffstats
path: root/modules/markup/renderer.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/markup/renderer.go')
-rw-r--r--modules/markup/renderer.go21
1 files changed, 21 insertions, 0 deletions
diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go
index 53ecbfce2b..6e4ae4e08c 100644
--- a/modules/markup/renderer.go
+++ b/modules/markup/renderer.go
@@ -5,6 +5,7 @@
package markup
import (
+ "bytes"
"context"
"errors"
"fmt"
@@ -93,6 +94,12 @@ type Renderer interface {
Render(ctx *RenderContext, input io.Reader, output io.Writer) error
}
+// RendererContentDetector detects if the content can be rendered
+// by specified renderer
+type RendererContentDetector interface {
+ CanRender(filename string, input io.Reader) bool
+}
+
var (
extRenderers = make(map[string]Renderer)
renderers = make(map[string]Renderer)
@@ -117,6 +124,20 @@ func GetRendererByType(tp string) Renderer {
return renderers[tp]
}
+// DetectRendererType detects the markup type of the content
+func DetectRendererType(filename string, input io.Reader) string {
+ buf, err := io.ReadAll(input)
+ if err != nil {
+ return ""
+ }
+ for _, renderer := range renderers {
+ if detector, ok := renderer.(RendererContentDetector); ok && detector.CanRender(filename, bytes.NewReader(buf)) {
+ return renderer.Name()
+ }
+ }
+ return ""
+}
+
// Render renders markup file to HTML with all specific handling stuff.
func Render(ctx *RenderContext, input io.Reader, output io.Writer) error {
if ctx.Type != "" {