@@ -102,6 +102,12 @@ export abstract class AbstractMarkdownElementContainer extends AbstractMarkdownE
102102 this . addElement ( element ) ;
103103 return element ;
104104 }
105+
106+ createList ( ordered : boolean ) : ListElement {
107+ const element = new ListElement ( ordered ) ;
108+ this . addElement ( element ) ;
109+ return element ;
110+ }
105111}
106112
107113// --------------------------------------------
@@ -166,6 +172,85 @@ export class CodeElement extends AbstractMarkdownLiteral {
166172 }
167173}
168174
175+ /**
176+ * Represents a markdown table.
177+ */
178+ export class TableElement extends AbstractMarkdownLiteral {
179+ header : string [ ] ;
180+ body : string [ ] [ ] ;
181+
182+ constructor ( header : string [ ] , body : string [ ] [ ] ) {
183+ super ( ) ;
184+
185+ this . header = header ;
186+ this . body = body ;
187+ }
188+
189+ public toString ( ) : string {
190+ const rows = this . body . length ;
191+ if ( rows === 0 ) {
192+ return '' ;
193+ }
194+
195+ const columns = this . header . length ;
196+ if ( columns === 0 ) {
197+ return '' ;
198+ }
199+ for ( const row of this . body ) {
200+ if ( row . length !== columns ) {
201+ throw new Error ( 'Table rows are do not contain the same number of columns.' ) ;
202+ }
203+ }
204+
205+ const longestStringInColumns : number [ ] = [ ] ;
206+
207+ for ( let i = 0 ; i < columns ; i ++ ) {
208+ let longestStringInColumn = 0 ;
209+
210+ if ( this . header [ i ] . length > longestStringInColumn ) {
211+ longestStringInColumn = this . header [ i ] . length ;
212+ }
213+ for ( const row of this . body ) {
214+ if ( row [ i ] . length > longestStringInColumn ) {
215+ longestStringInColumn = row [ i ] . length ;
216+ }
217+ }
218+
219+ longestStringInColumns . push ( longestStringInColumn ) ;
220+ }
221+
222+ let table = '' ;
223+
224+ // build header
225+ table += '|' ;
226+ for ( let j = 0 ; j < columns ; j ++ ) {
227+ let element = this . header [ j ] ;
228+ element += ' ' . repeat ( longestStringInColumns [ j ] - element . length ) ;
229+ table += ' ' + element + ' |' ;
230+ }
231+ table += '\n' ;
232+ // build divider
233+ table += '|' ;
234+ for ( let j = 0 ; j < columns ; j ++ ) {
235+ table += ' ' + '-' . repeat ( longestStringInColumns [ j ] ) + ' |' ;
236+ }
237+ table += '\n' ;
238+
239+ // build body
240+ for ( let i = 0 ; i < rows ; i ++ ) {
241+ table += '|' ;
242+ for ( let j = 0 ; j < columns ; j ++ ) {
243+ let element = this . body [ i ] [ j ] ;
244+ element += ' ' . repeat ( longestStringInColumns [ j ] - element . length ) ;
245+ table += ' ' + element + ' |' ;
246+ }
247+ table += '\n' ;
248+ }
249+
250+ return table ;
251+ }
252+ }
253+
169254// --------------------------------------------
170255// NON LITERALS
171256// --------------------------------------------
@@ -271,85 +356,36 @@ export class CalloutElement extends AbstractMarkdownElementContainer {
271356 }
272357}
273358
274- /**
275- * Represents a markdown table.
276- */
277- export class TableElement extends AbstractMarkdownElementContainer {
278- header : string [ ] ;
279- body : string [ ] [ ] ;
359+ export class ListElement extends AbstractMarkdownElementContainer {
360+ ordered : boolean ;
280361
281- constructor ( header : string [ ] , body : string [ ] [ ] ) {
362+ constructor ( ordered : boolean ) {
282363 super ( ) ;
283364
284- this . header = header ;
285- this . body = body ;
365+ this . ordered = ordered ;
286366 }
287367
288368 public allowElement ( _ : AbstractMarkdownElement ) : boolean {
289369 return true ;
290370 }
291371
292- public toString ( ) : string {
293- const rows = this . body . length ;
294- if ( rows === 0 ) {
295- return '' ;
296- }
297-
298- const columns = this . header . length ;
299- if ( columns === 0 ) {
300- return '' ;
301- }
302- for ( const row of this . body ) {
303- if ( row . length !== columns ) {
304- throw new Error ( 'Table rows are do not contain the same number of columns.' ) ;
305- }
372+ private getPrefix ( i : number ) : string {
373+ if ( this . ordered ) {
374+ return `${ i + 1 } . ` ;
375+ } else {
376+ return `- ` ;
306377 }
378+ }
307379
308- const longestStringInColumns : number [ ] = [ ] ;
309-
310- for ( let i = 0 ; i < columns ; i ++ ) {
311- let longestStringInColumn = 0 ;
312-
313- if ( this . header [ i ] . length > longestStringInColumn ) {
314- longestStringInColumn = this . header [ i ] . length ;
315- }
316- for ( const row of this . body ) {
317- if ( row [ i ] . length > longestStringInColumn ) {
318- longestStringInColumn = row [ i ] . length ;
380+ public toString ( ) : string {
381+ return this . markdownElements
382+ . map ( ( x , i ) => {
383+ if ( x instanceof ListElement ) {
384+ return `\t${ x . toString ( ) . replaceAll ( '\n' , '\n\t' ) } ` ;
319385 }
320- }
321-
322- longestStringInColumns . push ( longestStringInColumn ) ;
323- }
324-
325- let table = '' ;
326386
327- // build header
328- table += '|' ;
329- for ( let j = 0 ; j < columns ; j ++ ) {
330- let element = this . header [ j ] ;
331- element += ' ' . repeat ( longestStringInColumns [ j ] - element . length ) ;
332- table += ' ' + element + ' |' ;
333- }
334- table += '\n' ;
335- // build divider
336- table += '|' ;
337- for ( let j = 0 ; j < columns ; j ++ ) {
338- table += ' ' + '-' . repeat ( longestStringInColumns [ j ] ) + ' |' ;
339- }
340- table += '\n' ;
341-
342- // build body
343- for ( let i = 0 ; i < rows ; i ++ ) {
344- table += '|' ;
345- for ( let j = 0 ; j < columns ; j ++ ) {
346- let element = this . body [ i ] [ j ] ;
347- element += ' ' . repeat ( longestStringInColumns [ j ] - element . length ) ;
348- table += ' ' + element + ' |' ;
349- }
350- table += '\n' ;
351- }
352-
353- return table ;
387+ return `${ this . getPrefix ( i ) } ${ x . toString ( ) . replaceAll ( '\n' , '\n\t' ) } ` ;
388+ } )
389+ . join ( '\n' ) ;
354390 }
355391}
0 commit comments