1
0
Fork 0
mirror of https://github.com/avelino/awesome-go.git synced 2024-11-13 11:14:37 -05:00

code style

This commit is contained in:
Benjamin Schoch 2022-08-31 09:33:41 +02:00 committed by Avelino
parent 4b54315c48
commit 85cd71ec7d
2 changed files with 5 additions and 1 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/yuin/goldmark/util" "github.com/yuin/goldmark/util"
) )
// ConvertMarkdownToHTML converts markdown byte slice to a HTML byte slice
func ConvertMarkdownToHTML(markdown []byte) ([]byte, error) { func ConvertMarkdownToHTML(markdown []byte) ([]byte, error) {
md := goldmark.New( md := goldmark.New(
goldmark.WithExtensions(extension.GFM), goldmark.WithExtensions(extension.GFM),
@ -36,14 +37,17 @@ func ConvertMarkdownToHTML(markdown []byte) ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
// IDGenerator for goldmark to provide IDs more similar to GitHub's IDs on markdown parsing
type IDGenerator struct { type IDGenerator struct {
used map[string]bool used map[string]bool
} }
// Generate an ID
func (g *IDGenerator) Generate(value []byte, _ ast.NodeKind) []byte { func (g *IDGenerator) Generate(value []byte, _ ast.NodeKind) []byte {
return []byte(slug.Generate(string(value))) return []byte(slug.Generate(string(value)))
} }
// Put an ID to the list of already used IDs
func (g *IDGenerator) Put(value []byte) { func (g *IDGenerator) Put(value []byte) {
g.used[util.BytesToReadOnlyString(value)] = true g.used[util.BytesToReadOnlyString(value)] = true
} }

View file

@ -6,8 +6,8 @@ import (
"github.com/avelino/slugify" "github.com/avelino/slugify"
) )
// Generate slugs similar to GitHub's slugs on markdown parsing
func Generate(text string) string { func Generate(text string) string {
// remove slashes to create slugs similar to GitHub's slugs on markdown parsing
s := strings.ReplaceAll(text, "/", "") s := strings.ReplaceAll(text, "/", "")
return slugify.Slugify(strings.TrimSpace(s)) return slugify.Slugify(strings.TrimSpace(s))
} }