@@ -42,12 +42,12 @@ func WriteMD(w io.Writer, env string, nodes ...types.Node) error {
42
42
}
43
43
44
44
type 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
51
51
}
52
52
53
53
func (mw * mdWriter ) writeBytes (b []byte ) {
@@ -130,42 +130,36 @@ func (mw *mdWriter) write(nodes ...types.Node) error {
130
130
}
131
131
132
132
func (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 ("`" )
153
148
}
154
149
155
150
mw .writeString (t )
156
151
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 ("*" )
167
157
}
168
- mw .writeString (strings .Repeat (" " , rs ))
158
+ if n .Bold {
159
+ mw .writeString ("**" )
160
+ }
161
+
162
+ mw .writeString (right )
169
163
}
170
164
171
165
func (mw * mdWriter ) image (n * types.ImageNode ) {
@@ -304,18 +298,18 @@ func (mw *mdWriter) table(n *types.GridNode) {
304
298
}
305
299
306
300
// Write cell separator
307
- if ( cellIndex != len (row ) - 1 ) {
301
+ if cellIndex != len (row )- 1 {
308
302
mw .writeString (" | " )
309
303
} else {
310
304
mw .writeBytes (newLine )
311
305
}
312
306
}
313
307
314
308
// Write header bottom border
315
- if ( rowIndex == 0 ) {
309
+ if rowIndex == 0 {
316
310
for index , _ := range row {
317
311
mw .writeString ("---" )
318
- if ( index != len (row ) - 1 ) {
312
+ if index != len (row )- 1 {
319
313
mw .writeString (" | " )
320
314
}
321
315
}
@@ -324,4 +318,4 @@ func (mw *mdWriter) table(n *types.GridNode) {
324
318
325
319
mw .isWritingTableCell = false
326
320
}
327
- }
321
+ }
0 commit comments