3 Add support for mermaid diagrams

This commit is contained in:
Christoph Herb
2024-10-31 18:36:41 +01:00
parent a101814a94
commit b16c697c83
5 changed files with 2374 additions and 0 deletions

View File

@@ -5,6 +5,9 @@ LDFLAGS="-s -w ${LDFLAGS_OPT}"
all: build format lint ## Format, lint and build
run: ## Run
go run main.go
build: ## Build
go build -o bin/go-grip main.go

View File

@@ -19,10 +19,20 @@
* 📱 Dark and white mode
* 🎨 Syntax highlighting for code
* [x] Todo list like the one on GitHub
* Support for mermaid diagrams
```mermaid
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;
```
> [!TIP]
> Support of blockquotes (note, tip, important, warning and caution) [see here](https://github.com/orgs/community/discussions/16925)
## 🚀 Getting started
To install go-grip, simply:

2314
defaults/static/js/mermaid.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,16 @@
<div>
<div class="mermaid">
{{ .Content }}
</div>
<script src="/static/js/mermaid.min.js"></script>
{{if .Darkmode }}
<script>
mermaid.initialize({startOnLoad:true, theme: 'dark'});
</script>
{{else}}
<script>
mermaid.initialize({startOnLoad:true, theme: 'default'});
</script>
{{end}}
</div>

View File

@@ -57,6 +57,15 @@ func renderHookCodeBlock(w io.Writer, node ast.Node, dark bool) (ast.WalkStatus,
style = "github"
}
if string(block.Info) == "mermaid" {
m, err := renderMermaid(string(block.Literal), dark)
if err != nil {
log.Println("Error:", err)
}
fmt.Fprint(w, m)
return ast.GoToNext, true
}
err := quick.Highlight(w, string(block.Literal), string(block.Info), "html", style)
if err != nil {
log.Println("Error:", err)
@@ -198,3 +207,25 @@ func createBlockquoteStart(alert string) (string, error) {
}
return tpl.String(), nil
}
type Mermaid struct {
Content string
Darkmode bool
}
func renderMermaid(content string, darkmode bool) (string, error) {
m := Mermaid{
Content: content,
Darkmode: darkmode,
}
lp := filepath.Join("templates/mermaid/mermaid.html")
tmpl, err := template.ParseFS(defaults.Templates, lp)
if err != nil {
return "", err
}
var tpl bytes.Buffer
if err := tmpl.Execute(&tpl, m); err != nil {
return "", err
}
return tpl.String(), nil
}