Skip to content

Commit cd2e032

Browse files
committed
fix #38, fix table tags
1 parent 78eddc5 commit cd2e032

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/markupParser.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,29 @@ export function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
125125
//Table
126126
const tab_th_re = /\s*[^{]*\|{2}[^}]*$/gi;
127127
const tab_td_re = /\s*[^{]*\|[^}]*$/gi;
128-
if (!html_tag && (tag.match(tab_th_re) || tag.match(tab_td_re))) {
129-
tag = tag.replace(/^\|{2,}/, '||');
130-
tag = tag.replace(/^\|{2}/, '<th>');
131-
tag = tag.replace(/\|{2}$/, '</th>');
132-
tag = tag.replace(/\|{2}/gi, '</th><th>');
133-
tag = tag.replace(/^\|/, '<td>');
134-
tag = tag.replace(/\|$/, '</td>');
135-
tag = tag.replace(/\|/gi, '</td><td>');
136-
if (tableFlag == false) {
137-
tag = '<table>' + tag;
128+
if ((tag.match(tab_th_re) || tag.match(tab_td_re))) {
129+
let closeTableCell = '';
130+
if (tag.match(tab_th_re)) {
131+
tag = tag.replace(/^\|{2,}/, '||');
132+
tag = tag.replace(/^\|{2}/, '<th>');
133+
tag = tag.replace(/\|{2}$/, '</th>');
134+
tag = tag.replace(/\|{2}/gi, '</th><th>');
135+
tag = tag.replace(/\|/, '</th><td>'); // row heading
136+
closeTableCell = '</th>';
137+
}
138+
if (tag.match(tab_td_re)) {
139+
tag = tag.replace(/^\|/, '<td>');
140+
tag = tag.replace(/\|$/, '</td>'); //.replace(/$/, '</td>');
141+
tag = tag.replace(/\|/gi, '</td><td>');
142+
closeTableCell = '</td>';
143+
}
144+
if (!tag.endsWith('</th>') && !tag.endsWith('</td>')){
145+
tag += closeTableCell;
138146
}
139147
tag = '<tr>' + tag + '</tr>';
148+
if (tableFlag == false) {
149+
tag = '<table><tbody>' + tag;
150+
}
140151
tableFlag = true;
141152
}
142153
}
@@ -242,7 +253,7 @@ export function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
242253

243254
if (! codeTagFlag) {
244255
// lists
245-
const li_re = /^([-|*|#]+)\s(.*)/;
256+
const li_re = /^([-*#]+)\s(.*)/;
246257
const li_match = tag.match(li_re);
247258
if (li_match) {
248259
listFlag = true;
@@ -252,11 +263,11 @@ export function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
252263
listTag = 'ol';
253264
// reset ol after 3rd level
254265
// add count of non-ol elements for mixed lists
255-
if ((li_match[1].length + (li_match[1].match(/[-|*]/g) || []).length) % 3 === 1) {
266+
if ((li_match[1].length + (li_match[1].match(/[-*]/g) || []).length) % 3 === 1) {
256267
listStyle = ' class="initial"';
257268
}
258269
}
259-
if (li_match[1].match(/[-|*]$/)) {
270+
if (li_match[1].match(/[-*]$/)) {
260271
listTag = 'ul';
261272
}
262273
if (li_match[1].match(/-$/)) {
@@ -302,10 +313,10 @@ export function parseMarkup(sourceUri: vscode.Uri, sourceText: string) {
302313

303314
//close table
304315
if (!tag.match(/<\/tr>$/) && tableFlag) {
305-
tag = '</table>' + tag;
316+
tag = '</tbody></table>' + tag;
306317
tableFlag = false;
307318
}
308-
result += "<p>" + tag + "</p>";
319+
result += `<p>${tag}</p>`;
309320
}
310321

311322
return result;

0 commit comments

Comments
 (0)