2024-01-19 04:41:16 +00:00
|
|
|
package markup
|
2024-01-15 22:26:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-01-18 04:34:18 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
2024-01-15 22:26:51 +00:00
|
|
|
"path/filepath"
|
2024-01-18 04:34:18 +00:00
|
|
|
"strings"
|
2024-01-15 22:26:51 +00:00
|
|
|
|
2024-02-22 19:14:05 +00:00
|
|
|
"golang.org/x/net/html"
|
|
|
|
|
2024-01-15 22:26:51 +00:00
|
|
|
"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"
|
2024-01-18 04:34:18 +00:00
|
|
|
"github.com/yuin/goldmark/ast"
|
2024-01-15 22:26:51 +00:00
|
|
|
"github.com/yuin/goldmark/extension"
|
|
|
|
"github.com/yuin/goldmark/parser"
|
|
|
|
goldmarkhtml "github.com/yuin/goldmark/renderer/html"
|
2024-01-18 04:34:18 +00:00
|
|
|
"github.com/yuin/goldmark/text"
|
|
|
|
"github.com/yuin/goldmark/util"
|
2024-01-15 22:26:51 +00:00
|
|
|
)
|
|
|
|
|
2024-01-18 04:34:18 +00:00
|
|
|
var markdown = goldmark.New(
|
2024-01-15 22:26:51 +00:00
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
goldmarkhtml.WithUnsafe(),
|
|
|
|
),
|
|
|
|
goldmark.WithParserOptions(
|
|
|
|
parser.WithAutoHeadingID(),
|
2024-01-18 04:34:18 +00:00
|
|
|
parser.WithASTTransformers(
|
|
|
|
util.Prioritized(astTransformer{}, 100),
|
|
|
|
),
|
2024-01-15 22:26:51 +00:00
|
|
|
),
|
|
|
|
goldmark.WithExtensions(
|
|
|
|
extension.GFM,
|
|
|
|
emoji.Emoji,
|
|
|
|
highlighting.NewHighlighting(
|
|
|
|
highlighting.WithStyle("catppuccin-mocha"),
|
|
|
|
highlighting.WithFormatOptions(
|
|
|
|
chromahtml.WithClasses(true),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
// Readme transforms a readme, potentially from markdown, into HTML
|
2024-01-15 22:26:51 +00:00
|
|
|
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 != "" {
|
2024-01-18 04:34:18 +00:00
|
|
|
ctx := parser.NewContext()
|
|
|
|
mdCtx := markdownContext{
|
|
|
|
repo: repo.Name(),
|
|
|
|
ref: ref,
|
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
ctx.Set(renderContextKey, mdCtx)
|
2024-01-15 22:26:51 +00:00
|
|
|
var buf bytes.Buffer
|
2024-01-18 04:34:18 +00:00
|
|
|
if err := markdown.Convert([]byte(readme), &buf, parser.WithContext(ctx)); err != nil {
|
2024-01-15 22:26:51 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2024-01-18 04:34:18 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
if err := postProcess(buf.String(), mdCtx, &out); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.String(), nil
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-01-18 04:34:18 +00:00
|
|
|
|
|
|
|
var renderContextKey = parser.NewContextKey()
|
|
|
|
|
|
|
|
type markdownContext struct {
|
|
|
|
repo string
|
|
|
|
ref string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
type astTransformer struct{}
|
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
// Transform does two main things
|
|
|
|
// 1. Changes images to work relative to the source and wraps them in links
|
|
|
|
// 2. Changes links to work relative to the source
|
2024-01-18 04:34:18 +00:00
|
|
|
func (a astTransformer) Transform(node *ast.Document, _ text.Reader, pc parser.Context) {
|
|
|
|
_ = ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
|
|
|
|
if !entering {
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := pc.Get(renderContextKey).(markdownContext)
|
|
|
|
|
|
|
|
switch v := n.(type) {
|
|
|
|
case *ast.Image:
|
|
|
|
link := v.Destination
|
|
|
|
if len(link) > 0 && !bytes.HasPrefix(link, []byte("http")) {
|
|
|
|
v.Destination = []byte(resolveLink(ctx.repo, ctx.ref, ctx.path, string(link)) + "?raw&pretty")
|
|
|
|
}
|
|
|
|
|
|
|
|
parent := n.Parent()
|
|
|
|
if _, ok := parent.(*ast.Link); !ok && parent != nil {
|
|
|
|
next := n.NextSibling()
|
|
|
|
wrapper := ast.NewLink()
|
|
|
|
wrapper.Destination = v.Destination
|
|
|
|
wrapper.Title = v.Title
|
|
|
|
wrapper.SetAttributeString("target", []byte("_blank"))
|
|
|
|
img := ast.NewImage(ast.NewLink())
|
|
|
|
img.Destination = link
|
|
|
|
img.Title = v.Title
|
|
|
|
for _, attr := range v.Attributes() {
|
|
|
|
img.SetAttribute(attr.Name, attr.Value)
|
|
|
|
}
|
|
|
|
for child := v.FirstChild(); child != nil; {
|
|
|
|
nextChild := child.NextSibling()
|
|
|
|
img.AppendChild(img, child)
|
|
|
|
child = nextChild
|
|
|
|
}
|
|
|
|
wrapper.AppendChild(wrapper, img)
|
|
|
|
wrapper.SetNextSibling(next)
|
|
|
|
parent.ReplaceChild(parent, n, wrapper)
|
|
|
|
v.SetNextSibling(next)
|
|
|
|
}
|
|
|
|
case *ast.Link:
|
|
|
|
link := v.Destination
|
|
|
|
if len(link) > 0 && !bytes.HasPrefix(link, []byte("http")) && link[0] != '#' && !bytes.HasPrefix(link, []byte("mailto")) {
|
|
|
|
v.Destination = []byte(resolveLink(ctx.repo, ctx.ref, ctx.path, string(link)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.WalkContinue, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func postProcess(in string, ctx markdownContext, out io.Writer) error {
|
|
|
|
node, err := html.Parse(strings.NewReader("<html><body>" + in + "</body></html"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if node.Type == html.DocumentNode {
|
|
|
|
node = node.FirstChild
|
|
|
|
}
|
|
|
|
|
|
|
|
process(ctx, node)
|
|
|
|
|
|
|
|
renderNodes := make([]*html.Node, 0)
|
|
|
|
if node.Data == "html" {
|
|
|
|
node = node.FirstChild
|
|
|
|
for node != nil && node.Data != "body" {
|
|
|
|
node = node.NextSibling
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if node != nil {
|
|
|
|
if node.Data == "body" {
|
|
|
|
child := node.FirstChild
|
|
|
|
for child != nil {
|
|
|
|
renderNodes = append(renderNodes, child)
|
|
|
|
child = child.NextSibling
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
renderNodes = append(renderNodes, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, node := range renderNodes {
|
|
|
|
if err := html.Render(out, node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func process(ctx markdownContext, node *html.Node) {
|
|
|
|
if node.Type == html.ElementNode && node.Data == "img" {
|
|
|
|
for i, attr := range node.Attr {
|
|
|
|
if attr.Key != "src" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(attr.Val) > 0 && !strings.HasPrefix(attr.Val, "http") && !strings.HasPrefix(attr.Val, "data:image/") {
|
|
|
|
attr.Val = resolveLink(ctx.repo, ctx.ref, ctx.path, attr.Val) + "?raw&pretty"
|
|
|
|
}
|
|
|
|
node.Attr[i] = attr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for n := node.FirstChild; n != nil; n = n.NextSibling {
|
|
|
|
process(ctx, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolveLink(repo, ref, path, link string) string {
|
|
|
|
baseURL, err := url.Parse(fmt.Sprintf("/%s/tree/%s/%s", repo, ref, path))
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
linkURL, err := url.Parse(link)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return baseURL.ResolveReference(linkURL).String()
|
|
|
|
}
|