@@ -7,34 +7,70 @@ export class ListItem {
77 // The original line read from file.
88 public readonly originalMarkdown : string ;
99
10- public readonly parent : ListItem | null = null ;
10+ public readonly parent : ListItem | null ;
1111 public readonly children : ListItem [ ] = [ ] ;
12- public readonly indentation : string = '' ;
13- public readonly listMarker : string = '' ;
12+ public readonly indentation : string ;
13+ public readonly listMarker : string ;
1414 public readonly description : string ;
15- public readonly statusCharacter : string | null = null ;
15+ public readonly statusCharacter : string | null ;
1616
1717 public readonly taskLocation : TaskLocation ;
1818
19- constructor ( originalMarkdown : string , parent : ListItem | null , taskLocation : TaskLocation ) {
20- this . description = originalMarkdown . replace ( TaskRegularExpressions . listItemRegex , '' ) . trim ( ) ;
21- const nonTaskMatch = RegExp ( TaskRegularExpressions . nonTaskRegex ) . exec ( originalMarkdown ) ;
22- if ( nonTaskMatch ) {
23- this . indentation = nonTaskMatch [ 1 ] ;
24- this . listMarker = nonTaskMatch [ 2 ] ;
25- this . description = nonTaskMatch [ 5 ] . trim ( ) ;
26- this . statusCharacter = nonTaskMatch [ 4 ] ?? null ;
27- }
19+ constructor ( {
20+ originalMarkdown,
21+ indentation,
22+ listMarker,
23+ statusCharacter,
24+ description,
25+ parent,
26+ taskLocation,
27+ } : {
28+ originalMarkdown : string ;
29+ indentation : string ;
30+ listMarker : string ;
31+ statusCharacter : string | null ;
32+ description : string ;
33+ parent : ListItem | null ;
34+ taskLocation : TaskLocation ;
35+ } ) {
36+ this . indentation = indentation ;
37+ this . listMarker = listMarker ;
38+ this . statusCharacter = statusCharacter ;
39+ this . description = description ;
2840 this . originalMarkdown = originalMarkdown ;
29- this . parent = parent ;
3041
42+ this . parent = parent ;
3143 if ( parent !== null ) {
3244 parent . children . push ( this ) ;
3345 }
3446
3547 this . taskLocation = taskLocation ;
3648 }
3749
50+ public static fromListItemLine ( originalMarkdown : string , parent : ListItem | null , taskLocation : TaskLocation ) {
51+ const nonTaskMatch = RegExp ( TaskRegularExpressions . nonTaskRegex ) . exec ( originalMarkdown ) ;
52+ let indentation = '' ;
53+ let listMarker = '' ;
54+ let statusCharacter = null ;
55+ let description = '' ;
56+ if ( nonTaskMatch ) {
57+ indentation = nonTaskMatch [ 1 ] ;
58+ listMarker = nonTaskMatch [ 2 ] ;
59+ statusCharacter = nonTaskMatch [ 4 ] ?? null ;
60+ description = nonTaskMatch [ 5 ] . trim ( ) ;
61+ }
62+
63+ return new ListItem ( {
64+ originalMarkdown,
65+ indentation,
66+ listMarker,
67+ statusCharacter,
68+ description,
69+ taskLocation,
70+ parent,
71+ } ) ;
72+ }
73+
3874 /**
3975 * Return the top-level parent of this list item or task,
4076 * which will not be indented.
@@ -184,7 +220,7 @@ export class ListItem {
184220 `[${ newStatusCharacter } ]` ,
185221 ) ;
186222
187- return new ListItem ( newMarkdown , null , this . taskLocation ) ;
223+ return ListItem . fromListItemLine ( newMarkdown , null , this . taskLocation ) ;
188224 }
189225
190226 public toFileLineString ( ) : string {
0 commit comments