-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
143 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package render | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"github.com/Kunde21/markdownfmt/v3" | ||
"github.com/rs/zerolog/log" | ||
"github.com/yuin/goldmark/ast" | ||
astex "github.com/yuin/goldmark/extension/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"strconv" | ||
) | ||
|
||
type checkboxTransformer struct{} | ||
|
||
func (t *checkboxTransformer) Transform(node *ast.Document, _ text.Reader, _ parser.Context) { | ||
i := 1 | ||
err := ast.Walk(node, func(n ast.Node, entering bool) (ast.WalkStatus, error) { | ||
if entering { | ||
if _, ok := n.(*astex.TaskCheckBox); ok { | ||
listitem := n.Parent().Parent() | ||
listitem.SetAttribute([]byte("data-checkbox-nb"), []byte(strconv.Itoa(i))) | ||
i += 1 | ||
} | ||
} | ||
return ast.WalkContinue, nil | ||
}) | ||
if err != nil { | ||
log.Err(err) | ||
} | ||
} | ||
|
||
func Checkbox(content string, checkboxNb int) (string, error) { | ||
buf := bytes.Buffer{} | ||
w := bufio.NewWriter(&buf) | ||
|
||
source := []byte(content) | ||
markdown := markdownfmt.NewGoldmark() | ||
reader := text.NewReader(source) | ||
document := markdown.Parser().Parse(reader) | ||
|
||
i := 1 | ||
err := ast.Walk(document, func(n ast.Node, entering bool) (ast.WalkStatus, error) { | ||
if entering { | ||
if listItem, ok := n.(*astex.TaskCheckBox); ok { | ||
if i == checkboxNb { | ||
listItem.IsChecked = !listItem.IsChecked | ||
} | ||
i += 1 | ||
} | ||
} | ||
return ast.WalkContinue, nil | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if err = markdown.Renderer().Render(w, source, document); err != nil { | ||
return "", err | ||
} | ||
_ = w.Flush() | ||
|
||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package render | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"github.com/yuin/goldmark" | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/renderer" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
type svgToImg struct{} | ||
|
||
func (e *svgToImg) Extend(m goldmark.Markdown) { | ||
m.Parser().AddOptions(parser.WithInlineParsers( | ||
util.Prioritized(newInlineSvgParser(), 1), | ||
)) | ||
m.Renderer().AddOptions(renderer.WithNodeRenderers( | ||
util.Prioritized(newInlineSvgRenderer(), 1), | ||
)) | ||
} | ||
|
||
type inlineSvgParser struct{} | ||
|
||
func newInlineSvgParser() parser.InlineParser { | ||
return &inlineSvgParser{} | ||
} | ||
|
||
func (p *inlineSvgParser) Trigger() []byte { | ||
return []byte{'<'} | ||
} | ||
|
||
func (p *inlineSvgParser) Parse(_ ast.Node, block text.Reader, pc parser.Context) ast.Node { | ||
line, _ := block.PeekLine() | ||
if bytes.HasPrefix(line, []byte("<svg")) { | ||
node := ast.NewRawHTML() | ||
_, savedSegment := block.Position() | ||
node.Segments.Append(text.NewSegment(savedSegment.Start, savedSegment.Start+len(line))) | ||
block.Advance(len(line)) | ||
return node | ||
} | ||
return nil | ||
} | ||
|
||
func (p *inlineSvgParser) CloseBlock() {} | ||
|
||
type inlineSvgRenderer struct{} | ||
|
||
func newInlineSvgRenderer() renderer.NodeRenderer { | ||
return &inlineSvgRenderer{} | ||
} | ||
|
||
func (r *inlineSvgRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { | ||
reg.Register(ast.KindRawHTML, r.renderSVG) | ||
} | ||
|
||
func (r *inlineSvgRenderer) renderSVG(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { | ||
if !entering { | ||
return ast.WalkContinue, nil | ||
} | ||
rawHTML := node.(*ast.RawHTML) | ||
var svgContent []byte | ||
for i := 0; i < rawHTML.Segments.Len(); i++ { | ||
segment := rawHTML.Segments.At(i) | ||
svgContent = append(svgContent, segment.Value(source)...) | ||
} | ||
encoded := base64.StdEncoding.EncodeToString(svgContent) | ||
imgTag := `<img src="data:image/svg+xml;base64,` + encoded + `" />` | ||
_, _ = w.Write([]byte(imgTag)) | ||
return ast.WalkContinue, nil | ||
} |