2021-09-03 04:31:27 +00:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-07-07 17:27:57 +00:00
|
|
|
"fmt"
|
2021-09-03 04:31:27 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
chromahtml "github.com/alecthomas/chroma/formatters/html"
|
|
|
|
"github.com/yuin/goldmark"
|
2021-12-26 04:56:32 +00:00
|
|
|
emoji "github.com/yuin/goldmark-emoji"
|
2021-09-03 04:31:27 +00:00
|
|
|
highlighting "github.com/yuin/goldmark-highlighting"
|
|
|
|
"github.com/yuin/goldmark/extension"
|
|
|
|
"github.com/yuin/goldmark/parser"
|
|
|
|
"github.com/yuin/goldmark/renderer/html"
|
2022-07-07 17:27:57 +00:00
|
|
|
_ "go.jolheiser.com/chroma-catppuccin/chroma1"
|
2021-09-03 04:31:27 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 17:27:57 +00:00
|
|
|
var gm = func() goldmark.Markdown {
|
|
|
|
var codeblockIdx int
|
|
|
|
return goldmark.New(
|
|
|
|
goldmark.WithExtensions(
|
|
|
|
extension.GFM,
|
|
|
|
highlighting.NewHighlighting(
|
|
|
|
highlighting.WithStyle("catppuccin"),
|
|
|
|
highlighting.WithFormatOptions(
|
|
|
|
chromahtml.WithLineNumbers(true),
|
|
|
|
chromahtml.LineNumbersInTable(true),
|
|
|
|
),
|
|
|
|
highlighting.WithCodeBlockOptions(func(ctx highlighting.CodeBlockContext) []chromahtml.Option {
|
|
|
|
codeblockIdx++
|
|
|
|
return []chromahtml.Option{
|
|
|
|
chromahtml.LinkableLineNumbers(true, fmt.Sprintf("code%d-", codeblockIdx)),
|
|
|
|
}
|
|
|
|
}),
|
2021-09-03 04:31:27 +00:00
|
|
|
),
|
2022-07-07 17:27:57 +00:00
|
|
|
emoji.Emoji,
|
2021-09-03 04:31:27 +00:00
|
|
|
),
|
2022-07-07 17:27:57 +00:00
|
|
|
goldmark.WithParserOptions(
|
|
|
|
parser.WithAutoHeadingID(),
|
|
|
|
),
|
|
|
|
goldmark.WithRendererOptions(
|
|
|
|
html.WithHardWraps(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2021-09-03 04:31:27 +00:00
|
|
|
|
|
|
|
// Convert transforms a markdown document into HTML
|
2021-12-26 05:13:57 +00:00
|
|
|
func Convert(r io.Reader) (string, error) {
|
|
|
|
content, err := io.ReadAll(r)
|
2021-12-26 04:56:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-09-03 04:31:27 +00:00
|
|
|
var buf bytes.Buffer
|
2022-07-07 17:27:57 +00:00
|
|
|
if err := gm().Convert(content, &buf); err != nil {
|
2021-09-03 04:31:27 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|