21
21
* so this transformation is not guaranteed to equivalent if you roundtrip
22
22
* markdown content. The resulting AST *should* be equivalent however.
23
23
*/
24
- class ToStringVisitor {
24
+ class ToMarkdownStringVisitor {
25
+ /**
26
+ * Construct the visitor.
27
+ * @param {object } [options] configuration options
28
+ * @param {boolean } [options.noIndex] do not index ordered list (i.e., use 1. everywhere)
29
+ */
30
+ constructor ( options ) {
31
+ this . options = options ;
32
+ }
33
+
25
34
/**
26
35
* Visits a sub-tree and return the markdown
27
36
* @param {* } visitor the visitor to use
@@ -122,7 +131,7 @@ class ToStringVisitor {
122
131
123
132
switch ( thing . getType ( ) ) {
124
133
case 'CodeBlock' :
125
- ToStringVisitor . newParagraph ( parameters ) ;
134
+ ToMarkdownStringVisitor . newParagraph ( parameters ) ;
126
135
parameters . result += `\`\`\`${ thing . info ? ' ' + thing . info : '' } \n${ thing . text } \`\`\`\n\n` ;
127
136
break ;
128
137
case 'Code' :
@@ -132,28 +141,28 @@ class ToStringVisitor {
132
141
parameters . result += thing . text ;
133
142
break ;
134
143
case 'Emph' :
135
- parameters . result += `*${ ToStringVisitor . visitChildren ( this , thing ) } *` ;
144
+ parameters . result += `*${ ToMarkdownStringVisitor . visitChildren ( this , thing ) } *` ;
136
145
break ;
137
146
case 'Strong' :
138
- parameters . result += `**${ ToStringVisitor . visitChildren ( this , thing ) } **` ;
147
+ parameters . result += `**${ ToMarkdownStringVisitor . visitChildren ( this , thing ) } **` ;
139
148
break ;
140
149
case 'BlockQuote' : {
141
- const parametersIn = ToStringVisitor . mkParametersIn ( parameters ) ;
142
- ToStringVisitor . newParagraph ( parameters ) ;
143
- parameters . result += `> ${ ToStringVisitor . visitChildren ( this , thing , parametersIn ) } ` ;
150
+ const parametersIn = ToMarkdownStringVisitor . mkParametersIn ( parameters ) ;
151
+ ToMarkdownStringVisitor . newParagraph ( parameters ) ;
152
+ parameters . result += `> ${ ToMarkdownStringVisitor . visitChildren ( this , thing , parametersIn ) } ` ;
144
153
}
145
154
break ;
146
155
case 'Heading' : {
147
156
const level = parseInt ( thing . level ) ;
148
157
if ( level < 3 ) {
149
- parameters . result += `${ ToStringVisitor . visitChildren ( this , thing ) } \n${ ToStringVisitor . mkSetextHeading ( level ) } ` ;
158
+ parameters . result += `${ ToMarkdownStringVisitor . visitChildren ( this , thing ) } \n${ ToMarkdownStringVisitor . mkSetextHeading ( level ) } ` ;
150
159
} else {
151
- parameters . result += `${ ToStringVisitor . mkATXHeading ( level ) } ${ ToStringVisitor . visitChildren ( this , thing ) } ` ;
160
+ parameters . result += `${ ToMarkdownStringVisitor . mkATXHeading ( level ) } ${ ToMarkdownStringVisitor . visitChildren ( this , thing ) } ` ;
152
161
}
153
162
}
154
163
break ;
155
164
case 'ThematicBreak' :
156
- ToStringVisitor . newParagraph ( parameters ) ;
165
+ ToMarkdownStringVisitor . newParagraph ( parameters ) ;
157
166
parameters . result += '---\n' ;
158
167
break ;
159
168
case 'Linebreak' :
@@ -163,17 +172,17 @@ class ToStringVisitor {
163
172
parameters . result += '\n' ;
164
173
break ;
165
174
case 'Link' :
166
- parameters . result += `[${ ToStringVisitor . visitChildren ( this , thing ) } ](${ thing . destination } )` ;
175
+ parameters . result += `[${ ToMarkdownStringVisitor . visitChildren ( this , thing ) } ](${ thing . destination } )` ;
167
176
break ;
168
177
case 'Image' :
169
- parameters . result += `` ;
178
+ parameters . result += `` ;
170
179
break ;
171
180
case 'Paragraph' :
172
- ToStringVisitor . newParagraph ( parameters ) ;
173
- parameters . result += `${ ToStringVisitor . visitChildren ( this , thing ) } ` ;
181
+ ToMarkdownStringVisitor . newParagraph ( parameters ) ;
182
+ parameters . result += `${ ToMarkdownStringVisitor . visitChildren ( this , thing ) } ` ;
174
183
break ;
175
184
case 'HtmlBlock' :
176
- ToStringVisitor . newParagraph ( parameters ) ;
185
+ ToMarkdownStringVisitor . newParagraph ( parameters ) ;
177
186
parameters . result += `${ thing . text } ` ;
178
187
break ;
179
188
case 'Text' :
@@ -185,15 +194,15 @@ class ToStringVisitor {
185
194
// Always start with a new line
186
195
parameters . result += '\n' ;
187
196
thing . nodes . forEach ( item => {
188
- const parametersIn = ToStringVisitor . mkParametersInList ( parameters ) ;
197
+ const parametersIn = ToMarkdownStringVisitor . mkParametersInList ( parameters ) ;
189
198
if ( thing . tight === 'false' && index !== first ) {
190
199
parameters . result += '\n' ;
191
200
}
192
201
if ( thing . type === 'ordered' ) {
193
- parameters . result += `\n${ ToStringVisitor . mkIndent ( parameters ) } ${ index } . ${ ToStringVisitor . visitChildren ( this , item , parametersIn ) } ` ;
202
+ parameters . result += `\n${ ToMarkdownStringVisitor . mkIndent ( parameters ) } ${ this . options . noIndex ? 1 : index } . ${ ToMarkdownStringVisitor . visitChildren ( this , item , parametersIn ) } ` ;
194
203
}
195
204
else {
196
- parameters . result += `\n${ ToStringVisitor . mkIndent ( parameters ) } - ${ ToStringVisitor . visitChildren ( this , item , parametersIn ) } ` ;
205
+ parameters . result += `\n${ ToMarkdownStringVisitor . mkIndent ( parameters ) } - ${ ToMarkdownStringVisitor . visitChildren ( this , item , parametersIn ) } ` ;
197
206
}
198
207
index ++ ;
199
208
} ) ;
@@ -202,7 +211,7 @@ class ToStringVisitor {
202
211
case 'Item' :
203
212
throw new Error ( 'Item node should not occur outside of List nodes' ) ;
204
213
case 'Document' :
205
- parameters . result += ToStringVisitor . visitChildren ( this , thing ) ;
214
+ parameters . result += ToMarkdownStringVisitor . visitChildren ( this , thing ) ;
206
215
break ;
207
216
default :
208
217
throw new Error ( `Unhandled type ${ thing . getType ( ) } ` ) ;
@@ -211,4 +220,4 @@ class ToStringVisitor {
211
220
}
212
221
}
213
222
214
- module . exports = ToStringVisitor ;
223
+ module . exports = ToMarkdownStringVisitor ;
0 commit comments