mirror of
https://github.com/apricote/releaser-pleaser.git
synced 2026-02-06 17:57:05 +00:00
feat: format markdown in changelog entry
This commit is contained in:
parent
4cb22eae10
commit
929e335a29
6 changed files with 72 additions and 8 deletions
|
|
@ -1,8 +1,12 @@
|
|||
package markdown
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
markdown "github.com/teekennedy/goldmark-markdown"
|
||||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/util"
|
||||
|
||||
"github.com/apricote/releaser-pleaser/internal/markdown/extensions"
|
||||
)
|
||||
|
|
@ -10,6 +14,23 @@ import (
|
|||
func New() goldmark.Markdown {
|
||||
return goldmark.New(
|
||||
goldmark.WithExtensions(extensions.Section),
|
||||
goldmark.WithParserOptions(parser.WithASTTransformers(
|
||||
util.Prioritized(&newLineTransformer{}, 1),
|
||||
)),
|
||||
goldmark.WithRenderer(markdown.NewRenderer()),
|
||||
)
|
||||
}
|
||||
|
||||
// Format the Markdown document in a style mimicking Prettier. This is done for compatibility with other tools
|
||||
// users might have installed in their IDE. This does not guarantee that the output matches Prettier exactly.
|
||||
func Format(input string) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
buf.Grow(len(input))
|
||||
|
||||
err := New().Convert([]byte(input), &buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
|
|
|||
31
internal/markdown/prettier.go
Normal file
31
internal/markdown/prettier.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package markdown
|
||||
|
||||
import (
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/text"
|
||||
)
|
||||
|
||||
type newLineTransformer struct{}
|
||||
|
||||
var _ parser.ASTTransformer = (*newLineTransformer)(nil) // interface compliance
|
||||
|
||||
func (t *newLineTransformer) Transform(doc *ast.Document, _ text.Reader, _ parser.Context) {
|
||||
// No error can happen as they can only come from the walker function
|
||||
_ = ast.Walk(doc, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||
if !entering || node.Type() != ast.TypeBlock {
|
||||
return ast.WalkContinue, nil
|
||||
}
|
||||
|
||||
switch node.Kind() {
|
||||
case ast.KindListItem:
|
||||
// Do not add empty lines between every list item
|
||||
break
|
||||
default:
|
||||
// Add empty lines between every other block
|
||||
node.SetBlankPreviousLines(true)
|
||||
}
|
||||
|
||||
return ast.WalkContinue, nil
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue