remove the md to gemtext stuff
") ----------------------------- ") %s ```", code) ") => %s %s ", remoteURL, path)) ") ") ") ----------------------------- => / Back to home ")
This commit is contained in:
parent
d636da952b
commit
5c669f241f
3 changed files with 17 additions and 74 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package codeview
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -10,10 +9,6 @@ import (
|
|||
"net/url"
|
||||
"strings"
|
||||
|
||||
gemtext "git.sr.ht/~kota/goldmark-gemtext"
|
||||
goldmark "github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
|
||||
"github.com/kulak/gemini"
|
||||
)
|
||||
|
||||
|
|
@ -119,13 +114,13 @@ func serveDir(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
|||
for _, file := range dirRes {
|
||||
if file.Type == "file" && file.Name == "README.md" {
|
||||
slog.Info("Found README.md", "file", file)
|
||||
gemtext, err := getRemoteMarkdownFile(file.RawURL)
|
||||
body, err := getRemoteFile(file.RawURL)
|
||||
if err != nil {
|
||||
slog.Error("Failed to fetch README.md", "url", file.RawURL, "error", err)
|
||||
continue
|
||||
}
|
||||
content.WriteString("README.md found. The following is a markdown to gemtext conversion with directory listing below\n")
|
||||
content.WriteString(gemtext)
|
||||
content.WriteString("README.md found, directory listing below.\n")
|
||||
content.WriteString(renderCodeFile(body))
|
||||
content.WriteString("\n\n-----------------------------\n\n")
|
||||
}
|
||||
}
|
||||
|
|
@ -145,43 +140,32 @@ func serveDir(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
|||
w.WriteBody([]byte(content.String()))
|
||||
}
|
||||
|
||||
func getRemoteMarkdownFile(url string) (string, error) {
|
||||
func renderCodeFile(code string) string {
|
||||
// Escape any triple backticks in the code to avoid prematurely ending Gemini preformatted blocks
|
||||
code = strings.ReplaceAll(code, "```", "\\`\\`\\`")
|
||||
return fmt.Sprintf("```code\n%s\n```", code)
|
||||
}
|
||||
|
||||
func getRemoteFile(url string) (string, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
slog.Error("Failed to fetch remote markdown file", "url", url, "error", err)
|
||||
slog.Error("Failed to fetch remote file", "url", url, "error", err)
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
slog.Error("Failed to fetch remote markdown file", "url", url, "status", resp.Status)
|
||||
return "", fmt.Errorf("failed to fetch remote markdown file")
|
||||
slog.Error("Failed to fetch remote file", "url", url, "status", resp.Status)
|
||||
return "", fmt.Errorf("failed to fetch remote file")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
slog.Error("Failed to read remote markdown file", "url", url, "error", err)
|
||||
slog.Error("Failed to read remote file", "url", url, "error", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return convertMdToGemtext(string(body)), nil
|
||||
}
|
||||
|
||||
func convertMdToGemtext(markdown string) string {
|
||||
var buf bytes.Buffer
|
||||
md := goldmark.New(
|
||||
goldmark.WithExtensions(
|
||||
extension.Linkify,
|
||||
extension.Strikethrough,
|
||||
),
|
||||
)
|
||||
|
||||
// set some options
|
||||
// options := []Option{WithHeadingLink(HeadingLinkAuto), WithCodeSpan(CodeSpanMarkdown)}
|
||||
|
||||
md.SetRenderer(gemtext.New())
|
||||
_ = md.Convert([]byte(markdown), &buf) // ignoring errors for example
|
||||
return buf.String()
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func serveFile(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
||||
|
|
@ -189,32 +173,12 @@ func serveFile(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
|||
rawURL := strings.Replace(path, "src", "raw", 1)
|
||||
rawURL = "https://git.travisshears.com/travisshears/" + rawURL
|
||||
remoteURL := "https://git.travisshears.com/travisshears/" + path
|
||||
httpReq, err := http.NewRequest("GET", rawURL, nil)
|
||||
body, err := getRemoteFile(rawURL)
|
||||
if err != nil {
|
||||
slog.Error("Failed to create request to git server", "error", err)
|
||||
w.WriteStatusMsg(gemini.StatusGeneralPermFail, "Problem connecting to git server")
|
||||
return
|
||||
}
|
||||
res, err := http.DefaultClient.Do(httpReq)
|
||||
if err != nil {
|
||||
slog.Error("Failed to send request to git server", "error", err)
|
||||
w.WriteStatusMsg(gemini.StatusGeneralPermFail, "Problem connecting to git server")
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
slog.Error("Failed to read res body", "error", err)
|
||||
w.WriteStatusMsg(gemini.StatusGeneralPermFail, "Problem connecting to git server")
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
||||
slog.Error("unexpected status code", "statusCode", res.StatusCode, "body", string(body))
|
||||
w.WriteStatusMsg(gemini.StatusGeneralPermFail, "Problem connecting to git server")
|
||||
return
|
||||
}
|
||||
|
||||
var content strings.Builder
|
||||
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
|
||||
|
|
@ -223,9 +187,7 @@ func serveFile(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
|||
content.WriteString("# File CodeView\n")
|
||||
content.WriteString(fmt.Sprintf("This is a code view which proxies my personal git server. On the clearnet the following file is available at:\n=> %s %s\n\n", remoteURL, path))
|
||||
content.WriteString("----------------------------\n\n")
|
||||
content.WriteString("```code\n")
|
||||
content.Write(body)
|
||||
content.WriteString("```\n")
|
||||
content.WriteString(renderCodeFile(body))
|
||||
content.WriteString("\n\n-----------------------------\n\n=> / Back to home\n")
|
||||
w.WriteBody([]byte(content.String()))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue