MD text renderer: handle backslashes

Handles the same characters as the html renderer.
This commit is contained in:
Asaf Gartner 2021-12-26 11:36:12 +02:00
parent f8e7779b7d
commit 80f0e3b176
1 changed files with 4 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package parsing
import (
"io"
"regexp"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
@ -11,6 +12,8 @@ type plaintextRenderer struct{}
var _ renderer.Renderer = plaintextRenderer{}
var backslashRegex = regexp.MustCompile("\\\\(?P<char>[\\\\\\x60!\"#$%&'()*+,-./:;<=>?@\\[\\]^_{|}~])")
func (r plaintextRenderer) Render(w io.Writer, source []byte, n ast.Node) error {
return ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
@ -20,8 +23,7 @@ func (r plaintextRenderer) Render(w io.Writer, source []byte, n ast.Node) error
switch n.Kind() {
case ast.KindText:
n := n.(*ast.Text)
_, err := w.Write(n.Text(source))
_, err := w.Write(backslashRegex.ReplaceAll(n.Text(source), []byte("$1")))
if err != nil {
return ast.WalkContinue, err
}