Home

ugit @ead3a998de30acf260f65df2b59f923b8bb76b07 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / html / markdown.go
- raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package html

import (
	"bytes"
	"path/filepath"

	"go.jolheiser.com/ugit/internal/git"

	chromahtml "github.com/alecthomas/chroma/v2/formatters/html"
	"github.com/yuin/goldmark"
	emoji "github.com/yuin/goldmark-emoji"
	highlighting "github.com/yuin/goldmark-highlighting/v2"
	"github.com/yuin/goldmark/extension"
	"github.com/yuin/goldmark/parser"
	goldmarkhtml "github.com/yuin/goldmark/renderer/html"
)

var Markdown = goldmark.New(
	goldmark.WithRendererOptions(
		goldmarkhtml.WithUnsafe(),
	),
	goldmark.WithParserOptions(
		parser.WithAutoHeadingID(),
	),
	goldmark.WithExtensions(
		extension.GFM,
		emoji.Emoji,
		highlighting.NewHighlighting(
			highlighting.WithStyle("catppuccin-mocha"),
			highlighting.WithFormatOptions(
				chromahtml.WithClasses(true),
				chromahtml.WithLineNumbers(true),
				chromahtml.WithLinkableLineNumbers(true, "md-"),
				chromahtml.LineNumbersInTable(true),
			),
		),
	),
)

func Readme(repo *git.Repo, ref, path string) (string, error) {
	var readme string
	var err error
	for _, md := range []string{"README.md", "readme.md"} {
		readme, err = repo.FileContent(ref, filepath.Join(path, md))
		if err == nil {
			break
		}
	}

	if readme != "" {
		var buf bytes.Buffer
		if err := Markdown.Convert([]byte(readme), &buf); err != nil {
			return "", err
		}
		return buf.String(), nil
	}

	for _, md := range []string{"README.txt", "README", "readme.txt", "readme"} {
		readme, err = repo.FileContent(ref, filepath.Join(path, md))
		if err == nil {
			return readme, nil
		}
	}

	return "", nil
}