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