From 0134492489ac70306486678aae81192d9cc0228d Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 4 Jul 2024 17:36:48 +0200 Subject: Split gemtext function output into Body and Title --- http/handlers/templates/functions/gemtext.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'http/handlers/templates/functions') diff --git a/http/handlers/templates/functions/gemtext.go b/http/handlers/templates/functions/gemtext.go index 5419290..57ea751 100644 --- a/http/handlers/templates/functions/gemtext.go +++ b/http/handlers/templates/functions/gemtext.go @@ -51,10 +51,16 @@ func sanitizeText(str string) string { return html.EscapeString(strings.TrimSpace(str)) } -func (*Gemtext) funcGemtext(input any) (string, error) { +type gemtextResult struct { + Title string + Body string +} + +func (*Gemtext) funcGemtext(input any) (gemtextResult, error) { var ( r = bufio.NewReader(strings.NewReader(caddy.ToString(input))) w = new(bytes.Buffer) + title string pft, list bool writeErr error ) @@ -69,7 +75,7 @@ func (*Gemtext) funcGemtext(input any) (string, error) { loop: for { if writeErr != nil { - return "", fmt.Errorf("writing line: %w", writeErr) + return gemtextResult{}, fmt.Errorf("writing line: %w", writeErr) } line, err := r.ReadString('\n') @@ -79,7 +85,7 @@ loop: break loop case err != nil: - return "", fmt.Errorf("reading next line: %w", err) + return gemtextResult{}, fmt.Errorf("reading next line: %w", err) case strings.HasPrefix(line, "```"): if !pft { @@ -132,7 +138,11 @@ loop: write("

%s

\n", sanitizeText(line[2:])) case strings.HasPrefix(line, "#"): - write("

%s

\n", sanitizeText(line[1:])) + line = sanitizeText(line[1:]) + if title == "" { + title = line + } + write("

%s

\n", line) case strings.HasPrefix(line, ">"): write("
%s
\n", sanitizeText(line[1:])) @@ -143,7 +153,10 @@ loop: } } - return w.String(), nil + return gemtextResult{ + Title: title, + Body: w.String(), + }, nil } // UnmarshalCaddyfile implements caddyfile.Unmarshaler. -- cgit v1.2.3