@@ -36,17 +36,18 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
3636
3737 var result = '' ;
3838 let listTag = '' ;
39+ let listStyle = '' ;
3940 let codeTagFlag = 0 ;
40- let tableFlag = true ;
41+ let tableFlag = false ;
42+ let listFlag = false ;
43+ let listArr = [ ] ;
44+
4145 for ( let entry of sourceText . split ( / \n / gi) ) {
4246 let tag = entry ;
4347 let html_tag = false ;
4448
4549 if ( codeTagFlag == 0 ) {
4650 tag = tag . replace ( / h ( \d + ) \. \s ( [ ^ \n ] + ) / g, "<h$1>$2</h$1>" ) ;
47- tag = tag . replace ( / - { 4 , } / gi, '<hr />' ) ;
48- tag = tag . replace ( / - { 3 } / gi, '—' ) ;
49- tag = tag . replace ( / - { 2 } / gi, '–' ) ;
5051
5152 // tag = tag.replace(/_([^_]*)_/g, "<em>$1</em>");
5253 tag = tag . replace ( / \+ ( [ ^ \+ ] * ) \+ / g, "<u>$1</u>" ) ;
@@ -92,19 +93,20 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
9293 tag = '<img src="' + imageUri ( sourceUri , match [ 1 ] ) + '"/>' ;
9394 }
9495
95- //table
96- let tab_th_re = / \s * \| \| .* \| \| $ / gi;
97- let tab_td_re = / \s * \| .* \| $ / gi;
98- if ( tag . match ( tab_th_re ) ) {
99- tag = tag . replace ( / ^ \| \| / , '<th>' ) ;
100- tag = tag . replace ( / \| \| $ / , '</th>' ) ;
101- tag = tag . replace ( / \| \| / gi, '</th><th>' ) ;
102- tag = '<table><tr>' + tag + '</tr>' ;
103- tableFlag = true ;
104- } else if ( tag . match ( tab_td_re ) ) {
96+ //Table
97+ let tab_th_re = / \s * \| { 2 } .* $ / gi;
98+ let tab_td_re = / \s * \| .* $ / gi;
99+ if ( tag . match ( tab_th_re ) || tag . match ( tab_td_re ) ) {
100+ tag = tag . replace ( / ^ \| { 2 , } / , '\|\|' ) ;
101+ tag = tag . replace ( / ^ \| { 2 } / , '<th>' ) ;
102+ tag = tag . replace ( / \| { 2 } $ / , '</th>' ) ;
103+ tag = tag . replace ( / \| { 2 } / gi, '</th><th>' ) ;
105104 tag = tag . replace ( / ^ \| / , '<td>' ) ;
106105 tag = tag . replace ( / \| $ / , '</td>' ) ;
107106 tag = tag . replace ( / \| / gi, '</td><td>' ) ;
107+ if ( tableFlag == false ) {
108+ tag = '<table>' + tag ;
109+ }
108110 tag = '<tr>' + tag + '</tr>' ;
109111 tableFlag = true ;
110112 }
@@ -133,29 +135,57 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
133135 re = / ^ ( [ - | \* | # ] + ) \s ( .* ) / ;
134136 match = tag . match ( re ) ;
135137 if ( match ) {
136- if ( listTag . length === 0 ) {
137- if ( match [ 1 ] == '#' ) {
138- listTag = 'ol' ;
139- } else {
140- listTag = 'ul' ;
141- if ( match [ 1 ] == '-' ) {
142- listTag += ' style="list-style-type: square;"'
143- }
138+ listFlag = true ;
139+ listStyle = '' ;
140+ tag = '' ;
141+ if ( match [ 1 ] . match ( / # $ / ) ) {
142+ listTag = 'ol' ;
143+ // reset ol after 3rd level
144+ // add count of non-ol elements for mixed lists
145+ if ( ( match [ 1 ] . length + ( match [ 1 ] . match ( / [ - | \* ] / g) || [ ] ) . length ) % 3 === 1 ) {
146+ listStyle = ' class="initial"' ;
144147 }
145- tag = '<' + listTag + '>' ;
146- } else {
147- tag = '' ;
148+ }
149+ if ( match [ 1 ] . match ( / [ - | \* ] $ / ) ) {
150+ listTag = 'ul' ;
151+ }
152+ if ( match [ 1 ] . match ( / - $ / ) ) {
153+ listStyle = ' class="alternate"' ;
154+ }
155+ if ( match [ 1 ] . length > listArr . length ) {
156+ tag = '<' + listTag + listStyle + '>' ;
157+ listArr . push ( listTag ) ;
158+ }
159+ if ( match [ 1 ] . length < listArr . length ) {
160+ // while (listArr.length > match[1].length) {
161+ // tag += '</' + listArr.pop() + '>'
162+ // }
163+ tag = '</' + listArr . slice ( match [ 1 ] . length , listArr . length ) . reverse ( ) . join ( '></' ) + '>' ;
164+ listArr = listArr . slice ( 0 , match [ 1 ] . length ) ;
148165 }
149166 tag += "<li>" + match [ 2 ] + "</li>" ;
150167 }
151168
152- if ( ( tag . length === 0 ) && ( listTag . length !== 0 ) ) {
153- tag += '</' + listTag + '>' ;
154- listTag = '' ;
169+
170+ if ( ( tag . length === 0 ) && ( listArr . length > 0 ) ) {
171+ // if ((!listFlag) && (listArr.length > 0)) {
172+ tag = '' ;
173+ // do {
174+ // tag += '</' + listArr.pop() + '>'
175+ // } while (listArr.length > 0);
176+ tag = '</' + listArr . reverse ( ) . join ( '></' ) + '>'
177+ listArr = [ ] ;
178+ listFlag = false ;
155179 }
156180
181+ // hr and dash lines
182+ tag = tag . replace ( / - { 4 , } / gi, '<hr />' ) ;
183+ tag = tag . replace ( / - { 3 } / gi, '—' ) ;
184+ tag = tag . replace ( / - { 2 } / gi, '–' ) ;
185+ // strong
157186 tag = tag . replace ( / \* ( [ ^ \* ] * ) \* / g, "<strong>$1</strong>" ) ;
158- if ( ( ! html_tag ) && ( ! tag . match ( '<img' ) ) ) {
187+ // line-through
188+ if ( ( ! html_tag ) && ( ! tag . match ( '<img' ) ) && ( ! listFlag ) ) {
159189 tag = tag . replace ( / - ( [ \w ] * ) - / g, "<span style='text-decoration: line-through;'>$1</span>" ) ;
160190 tag = tag . replace ( / _ ( [ \w ] * ) _ / g, "<i>$1</i>" ) ;
161191 }
@@ -168,11 +198,13 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
168198 if ( tag . match ( / ^ s * $ / ) ) {
169199 tag = '<br />' ;
170200 }
201+
171202 //close table
172- if ( ! tag . match ( / < \/ t r > $ / ) ) {
203+ if ( ! tag . match ( / < \/ t r > $ / ) && tableFlag ) {
173204 tag = '</table>' + tag ;
174205 tableFlag = false ;
175206 }
207+
176208 result += tag ;
177209
178210 // console.log("PARSED:" + tag);
0 commit comments