@@ -35,11 +35,12 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
3535 //TODO: use Tokenazer instead of line loop
3636
3737 var result = '' ;
38-
3938 let listTag = '' ;
4039 let codeTagFlag = 0 ;
40+ let tableFlag = true ;
4141 for ( let entry of sourceText . split ( / \n / gi) ) {
4242 let tag = entry ;
43+ let html_tag = false ;
4344
4445 if ( codeTagFlag == 0 ) {
4546 tag = tag . replace ( / h ( \d + ) \. \s ( [ ^ \n ] + ) / g, "<h$1>$2</h$1>" ) ;
@@ -71,27 +72,51 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
7172 tag = tag . replace ( / \( x \) / g, '<img alt="(cross)" src="' + emoticonUri ( 'error.png' ) + '"/>' ) ;
7273 tag = tag . replace ( / \( ! \) / g, '<img alt="(warning)" src="' + emoticonUri ( 'warning.png' ) + '"/>' ) ;
7374
74- tag = tag . replace ( / \[ ( [ ^ | ] * ) ? \| ? ( [ ^ | ] * ) \] / g, function ( m0 , m1 , m2 ) {
75- if ( ( m1 . length !== 0 ) && ( m2 . length !== 0 ) ) {
76- return "<a href='" + m2 + "'>" + m1 + "</a>" ;
77- } else {
78- return "<a href='" + m1 + "'>" + m1 + "</a>" ;
79- }
80- } ) ;
75+ tag = tag . replace ( / \\ \\ / gi, '<br/>' ) ;
8176
77+ let re = / \[ ( [ ^ | ] * ) ? \| ? ( [ ^ | ] * ) \] / g
78+ if ( tag . match ( re ) ) {
79+ tag = tag . replace ( re , function ( m0 , m1 , m2 ) {
80+ if ( ( m1 . length !== 0 ) && ( m2 . length !== 0 ) ) {
81+ return "<a href='" + m2 + "'>" + m1 + "</a>" ;
82+ } else {
83+ return "<a href='" + m1 + "'>" + m1 + "</a>" ;
84+ }
85+ } ) ;
86+ html_tag = true ;
87+ }
8288 //img
8389 let img = / ! ( .* ) ! / ;
8490 let match = tag . match ( img ) ;
8591 if ( match ) {
8692 tag = '<img src="' + imageUri ( sourceUri , match [ 1 ] ) + '"/>' ;
8793 }
8894
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 ) ) {
105+ tag = tag . replace ( / ^ \| / , '<td>' ) ;
106+ tag = tag . replace ( / \| $ / , '</td>' ) ;
107+ tag = tag . replace ( / \| / gi, '</td><td>' ) ;
108+ tag = '<tr>' + tag + '</tr>' ;
109+ tableFlag = true ;
110+ }
89111 }
90112
91113 // code
92114 // online code tag
93- tag = tag . replace ( / \{ c o d e [ ^ \} ] * \} ( .* ) \{ c o d e \} / , "<pre><code>$1</code></pre>" ) ;
94- let re = / \{ c o d e .* \} / ;
115+ tag = tag . replace ( / \{ ( n o f o r m a t | c o d e ) [ ^ \} ] * \} ( .* ) \{ ( n o f o r m a t | c o d e ) \} / , function ( m0 , m1 , m2 ) {
116+ return "<pre><code>" + m2 . replace ( / < / gi, '<' ) + "</code></pre>" ;
117+ } ) ;
118+
119+ let re = / \{ [ ( c o d e ) | ( n o f o r m a t ) ] .* \} / ;
95120 let match = tag . match ( re ) ;
96121 if ( match ) {
97122 if ( codeTagFlag === 0 ) {
@@ -130,13 +155,26 @@ export async function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
130155 }
131156
132157 tag = tag . replace ( / \* ( [ ^ \* ] * ) \* / g, "<strong>$1</strong>" ) ;
133- tag = tag . replace ( / \B - ( \w * ) - \B / g , "<span style='text-decoration: line-through;'>striket-hrough</span>" ) ;
134- }
135- if ( tag === '<pre><code>' ) {
136- result += tag ;
158+ if ( ( ! html_tag ) && ( ! tag . match ( '<img' ) ) ) {
159+ tag = tag . replace ( / - ( [ \w ] * ) - / g , "<span style='text-decoration: line-through;'>$1</span>" ) ;
160+ tag = tag . replace ( / _ ( [ \w ] * ) _ / g , "<i>$1</i>" ) ;
161+ }
137162 } else {
138- result += tag + '<br />' ;
163+ if ( tag !== '<pre><code>' ) {
164+ tag = tag . replace ( / < / gi, '<' ) + '<br />' ;
165+ }
139166 }
167+
168+ if ( tag . match ( / ^ s * $ / ) ) {
169+ tag = '<br />' ;
170+ }
171+ //close table
172+ if ( ! tag . match ( / < \/ t r > $ / ) ) {
173+ tag = '</table>' + tag ;
174+ tableFlag = false ;
175+ }
176+ result += tag ;
177+
140178 // console.log("PARSED:" + tag);
141179 }
142180
0 commit comments