@@ -42,12 +42,12 @@ func WriteMD(w io.Writer, env string, nodes ...types.Node) error {
4242}
4343
4444type mdWriter struct {
45- w io.Writer // output writer
46- env string // target environment
47- err error // error during any writeXxx methods
48- lineStart bool
49- isWritingTableCell bool // used to override lineStart for correct cell formatting
50- Prefix string // prefix for e.g. blockquote content
45+ w io.Writer // output writer
46+ env string // target environment
47+ err error // error during any writeXxx methods
48+ lineStart bool
49+ isWritingTableCell bool // used to override lineStart for correct cell formatting
50+ Prefix string // prefix for e.g. blockquote content
5151}
5252
5353func (mw * mdWriter ) writeBytes (b []byte ) {
@@ -130,42 +130,36 @@ func (mw *mdWriter) write(nodes ...types.Node) error {
130130}
131131
132132func (mw * mdWriter ) text (n * types.TextNode ) {
133- t := strings .TrimSpace (n .Value )
134- tl := len ([]rune (t ))
135- nl := len ([]rune (n .Value ))
136- ls := nl - len ([]rune (strings .TrimLeft (n .Value , " " )))
137- // Don't just copy above and TrimRight instead of TrimLeft to avoid " " counting as 1
138- // left space and 1 right space. Instead, number of right spaces is
139- // length of whole string - length of string with spaces trimmed - number of left spaces.
140- rs := nl - tl - ls
141-
142- mw .writeString (strings .Repeat (" " , ls ))
143- if tl > 0 {
144- if n .Bold {
145- mw .writeString ("**" )
146- }
147- if n .Italic {
148- mw .writeString ("*" )
149- }
150- if n .Code {
151- mw .writeString ("`" )
152- }
133+ tr := strings .TrimLeft (n .Value , " \t \n \r \f \v " )
134+ left := n .Value [0 :(len (n .Value ) - len (tr ))]
135+ t := strings .TrimRight (tr , " \t \n \r \f \v " )
136+ right := tr [len (t ):len (tr )]
137+
138+ mw .writeString (left )
139+
140+ if n .Bold {
141+ mw .writeString ("**" )
142+ }
143+ if n .Italic {
144+ mw .writeString ("*" )
145+ }
146+ if n .Code {
147+ mw .writeString ("`" )
153148 }
154149
155150 mw .writeString (t )
156151
157- if tl > 0 {
158- if n .Code {
159- mw .writeString ("`" )
160- }
161- if n .Italic {
162- mw .writeString ("*" )
163- }
164- if n .Bold {
165- mw .writeString ("**" )
166- }
152+ if n .Code {
153+ mw .writeString ("`" )
154+ }
155+ if n .Italic {
156+ mw .writeString ("*" )
167157 }
168- mw .writeString (strings .Repeat (" " , rs ))
158+ if n .Bold {
159+ mw .writeString ("**" )
160+ }
161+
162+ mw .writeString (right )
169163}
170164
171165func (mw * mdWriter ) image (n * types.ImageNode ) {
@@ -304,18 +298,18 @@ func (mw *mdWriter) table(n *types.GridNode) {
304298 }
305299
306300 // Write cell separator
307- if ( cellIndex != len (row ) - 1 ) {
301+ if cellIndex != len (row )- 1 {
308302 mw .writeString (" | " )
309303 } else {
310304 mw .writeBytes (newLine )
311305 }
312306 }
313307
314308 // Write header bottom border
315- if ( rowIndex == 0 ) {
309+ if rowIndex == 0 {
316310 for index , _ := range row {
317311 mw .writeString ("---" )
318- if ( index != len (row ) - 1 ) {
312+ if index != len (row )- 1 {
319313 mw .writeString (" | " )
320314 }
321315 }
@@ -324,4 +318,4 @@ func (mw *mdWriter) table(n *types.GridNode) {
324318
325319 mw .isWritingTableCell = false
326320 }
327- }
321+ }
0 commit comments