Files
go-grip/pkg/parser.go

182 lines
4.3 KiB
Go
Raw Normal View History

2024-10-09 22:03:53 +02:00
package pkg
import (
2024-10-10 18:31:41 +02:00
"bytes"
"fmt"
"html/template"
2024-10-09 22:03:53 +02:00
"io"
2024-10-09 22:49:34 +02:00
"log"
2024-10-10 18:31:41 +02:00
"path/filepath"
"strings"
2024-10-09 22:03:53 +02:00
"github.com/alecthomas/chroma/v2/quick"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/ast"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
)
2024-10-10 18:31:41 +02:00
var blockquotes = []string{"Note", "Tip", "Important", "Warning", "Caution"}
2024-10-09 22:49:34 +02:00
func (client *Client) MdToHTML(bytes []byte) []byte {
2024-10-09 22:03:53 +02:00
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
p := parser.NewWithExtensions(extensions)
doc := p.Parse(bytes)
htmlFlags := html.CommonFlags
2024-10-10 18:31:41 +02:00
opts := html.RendererOptions{Flags: htmlFlags, RenderNodeHook: client.renderHook}
2024-10-09 22:03:53 +02:00
renderer := html.NewRenderer(opts)
2024-10-09 22:49:34 +02:00
return markdown.Render(doc, renderer)
2024-10-09 22:03:53 +02:00
}
2024-10-10 18:31:41 +02:00
func (client *Client) renderHook(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
switch node.(type) {
case *ast.BlockQuote:
return renderHookBlockquote(w, node, entering)
case *ast.Text:
return renderHookText(w, node)
case *ast.ListItem:
return renderHookListItem(w, node, entering)
2024-10-10 18:31:41 +02:00
case *ast.CodeBlock:
return renderHookCodeBlock(w, node, client.Dark)
2024-10-09 22:03:53 +02:00
}
2024-10-10 18:31:41 +02:00
return ast.GoToNext, false
}
func renderHookCodeBlock(w io.Writer, node ast.Node, dark bool) (ast.WalkStatus, bool) {
block := node.(*ast.CodeBlock)
2024-10-09 22:03:53 +02:00
var style string
2024-10-10 18:31:41 +02:00
switch dark {
2024-10-09 22:03:53 +02:00
case true:
style = "github-dark"
default:
style = "github"
}
2024-10-09 22:49:34 +02:00
err := quick.Highlight(w, string(block.Literal), string(block.Info), "html", style)
if err != nil {
log.Println("Error:", err)
}
2024-10-10 18:31:41 +02:00
return ast.GoToNext, true
}
func renderHookBlockquote(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
block := node.(*ast.BlockQuote)
paragraph, ok := (block.GetChildren()[0]).(*ast.Paragraph)
if !ok {
return ast.GoToNext, false
}
t, ok := (paragraph.GetChildren()[0]).(*ast.Text)
if !ok {
return ast.GoToNext, false
}
// Get the text content of the blockquote
content := string(t.Literal)
var alert string
for _, b := range blockquotes {
if strings.HasPrefix(content, fmt.Sprintf("[!%s]", strings.ToUpper(b))) {
alert = strings.ToLower(b)
}
}
if alert == "" {
return ast.GoToNext, false
}
// Set the message type based on the content of the blockquote
if entering {
s, _ := createBlockquoteStart(alert)
io.WriteString(w, s)
} else {
io.WriteString(w, "</div>")
}
2024-10-09 22:03:53 +02:00
return ast.GoToNext, true
}
2024-10-10 18:31:41 +02:00
func renderHookText(w io.Writer, node ast.Node) (ast.WalkStatus, bool) {
block := node.(*ast.Text)
paragraph, ok := block.GetParent().(*ast.Paragraph)
if !ok {
return ast.GoToNext, false
}
_, ok = paragraph.GetParent().(*ast.BlockQuote)
if ok {
// Remove prefixes
for _, b := range blockquotes {
content, found := strings.CutPrefix(string(block.Literal), fmt.Sprintf("[!%s]", strings.ToUpper(b)))
if found {
io.WriteString(w, content)
return ast.GoToNext, true
}
}
}
_, ok = paragraph.GetParent().(*ast.ListItem)
if ok {
content, found := strings.CutPrefix(string(block.Literal), "[ ]")
content = `<input type="checkbox" disabled class="task-list-item-checkbox"> ` + content
if found {
io.WriteString(w, content)
return ast.GoToNext, true
}
content, found = strings.CutPrefix(string(block.Literal), "[x]")
content = `<input type="checkbox" disabled class="task-list-item-checkbox" checked> ` + content
if found {
io.WriteString(w, content)
return ast.GoToNext, true
}
}
return ast.GoToNext, false
}
func renderHookListItem(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) {
block := node.(*ast.ListItem)
paragraph, ok := (block.GetChildren()[0]).(*ast.Paragraph)
2024-10-10 18:31:41 +02:00
if !ok {
return ast.GoToNext, false
}
t, ok := (paragraph.GetChildren()[0]).(*ast.Text)
if !ok {
return ast.GoToNext, false
2024-10-10 18:31:41 +02:00
}
if !(strings.HasPrefix(string(t.Literal), "[ ]") || strings.HasPrefix(string(t.Literal), "[x]")) {
return ast.GoToNext, false
}
if entering {
io.WriteString(w, "<li class=\"task-list-item\">")
} else {
io.WriteString(w, "</li>")
}
return ast.GoToNext, true
2024-10-10 18:31:41 +02:00
}
func createBlockquoteStart(alert string) (string, error) {
lp := filepath.Join("templates/alert", fmt.Sprintf("%s.html", alert))
tmpl, err := template.ParseFiles(lp)
if err != nil {
return "", err
}
var tpl bytes.Buffer
if err := tmpl.Execute(&tpl, alert); err != nil {
return "", err
}
return tpl.String(), nil
}