convert markdown to gemtext and display README.md files
") => %s here ", httpURL)) ") ----------------------------- ")
This commit is contained in:
parent
9b100e88a2
commit
63775d57e0
3 changed files with 84 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package codeview
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
|
@ -9,6 +10,10 @@ 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"
|
||||
)
|
||||
|
||||
|
|
@ -84,10 +89,11 @@ func serveDir(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
|||
}
|
||||
|
||||
var dirRes []struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
URL string `json:"html_url"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
URL string `json:"html_url"`
|
||||
Type string `json:"type"`
|
||||
RawURL string `json:"download_url"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &dirRes); err != nil {
|
||||
|
|
@ -99,7 +105,7 @@ func serveDir(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
|||
|
||||
var content strings.Builder
|
||||
w.WriteStatusMsg(gemini.StatusSuccess, "text/gemini")
|
||||
content.WriteString("# Dir CodeView\n")
|
||||
content.WriteString("# Directory CodeView\n")
|
||||
httpURL := fmt.Sprintf("https://git.travisshears.com/travisshears/personal-gemini-capsule/src/branch/main/%s", filepath)
|
||||
if branch != "" {
|
||||
httpURL = fmt.Sprintf("https://git.travisshears.com/travisshears/personal-gemini-capsule/src/branch/%s/%s", branch, filepath)
|
||||
|
|
@ -108,6 +114,22 @@ func serveDir(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
|||
}
|
||||
content.WriteString(fmt.Sprintf("This is a code view which proxies my personal git server. On the clearnet the following dir is available:\n=> %s here\n\n", httpURL))
|
||||
|
||||
// print readme page
|
||||
// var readmeURL 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)
|
||||
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("\n\n-----------------------------\n\n")
|
||||
}
|
||||
}
|
||||
|
||||
for _, file := range dirRes {
|
||||
switch file.Type {
|
||||
case "dir":
|
||||
|
|
@ -123,6 +145,45 @@ func serveDir(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
|||
w.WriteBody([]byte(content.String()))
|
||||
}
|
||||
|
||||
func getRemoteMarkdownFile(url string) (string, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
slog.Error("Failed to fetch remote markdown 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")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
slog.Error("Failed to read remote markdown 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()
|
||||
}
|
||||
|
||||
func serveFile(w gemini.ResponseWriter, req *gemini.Request, path string) {
|
||||
path = strings.TrimPrefix(path, "/codeview/raw/")
|
||||
rawURL := strings.Replace(path, "src", "raw", 1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue