mirror of
https://github.com/showdownjs/showdown.git
synced 2024-03-22 13:30:55 +08:00
fix: tables parse correctly with new version of jsdom
This commit is contained in:
parent
5fc843e175
commit
db571fbaac
9952
package-lock.json
generated
9952
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -12,7 +12,6 @@ showdown.subParser('makeMarkdown.table',
|
||||||
var txt = '',
|
var txt = '',
|
||||||
tableArray = [[], []],
|
tableArray = [[], []],
|
||||||
headings,
|
headings,
|
||||||
hasHeadings = false,
|
|
||||||
rows = [],
|
rows = [],
|
||||||
colCount,
|
colCount,
|
||||||
i,
|
i,
|
||||||
|
@ -38,7 +37,7 @@ showdown.subParser('makeMarkdown.table',
|
||||||
|
|
||||||
// first lets look for <thead>
|
// first lets look for <thead>
|
||||||
// we will ignore thead without <tr> children
|
// we will ignore thead without <tr> children
|
||||||
// also, since markdown doesn't support tables with multiple heading rows, onyl the first one will be transformed
|
// also, since markdown doesn't support tables with multiple heading rows, only the first one will be transformed
|
||||||
// the rest will count as regular rows
|
// the rest will count as regular rows
|
||||||
if (node.querySelectorAll(':scope>thead').length !== 0 && node.querySelectorAll(':scope>thead>tr').length !== 0) {
|
if (node.querySelectorAll(':scope>thead').length !== 0 && node.querySelectorAll(':scope>thead>tr').length !== 0) {
|
||||||
var thead = node.querySelectorAll(':scope>thead>tr');
|
var thead = node.querySelectorAll(':scope>thead>tr');
|
||||||
|
@ -47,7 +46,6 @@ showdown.subParser('makeMarkdown.table',
|
||||||
for (i = 0; i < thead.length; ++i) {
|
for (i = 0; i < thead.length; ++i) {
|
||||||
rows.push(iterateRow(thead[i]));
|
rows.push(iterateRow(thead[i]));
|
||||||
}
|
}
|
||||||
hasHeadings = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// now let's look for tbody
|
// now let's look for tbody
|
||||||
|
@ -71,16 +69,16 @@ showdown.subParser('makeMarkdown.table',
|
||||||
|
|
||||||
// lastly look for naked tr
|
// lastly look for naked tr
|
||||||
if (node.querySelectorAll(':scope>tr').length !== 0) {
|
if (node.querySelectorAll(':scope>tr').length !== 0) {
|
||||||
|
|
||||||
var tr = node.querySelectorAll(':scope>tr');
|
var tr = node.querySelectorAll(':scope>tr');
|
||||||
// tfoot>tr can have td and th children, although th are not very screen reader friendly
|
// tfoot>tr can have td and th children, although th are not very screen reader friendly
|
||||||
for (i = 0; i < tr.length; ++i) {
|
for (i = 0; i < tr.length; ++i) {
|
||||||
rows.push(iterateRow(tr[i]));
|
rows.push(iterateRow(tr[i]));
|
||||||
}
|
}
|
||||||
hasHeadings = true; // kinda, first row counts as heading
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement <caption> in tables https://developer.mozilla.org/pt-BR/docs/Web/HTML/Element/caption
|
// TODO: implement <caption> in tables https://developer.mozilla.org/pt-BR/docs/Web/HTML/Element/caption
|
||||||
// note: colgroups are ignored, since they are basically styling
|
// note: <colgroup> is ignored, since they are basically styling
|
||||||
|
|
||||||
// we need now to account for cases of completely empty tables, like <table></table> or equivalent
|
// we need now to account for cases of completely empty tables, like <table></table> or equivalent
|
||||||
if (rows.length === 0) {
|
if (rows.length === 0) {
|
||||||
|
@ -91,41 +89,31 @@ showdown.subParser('makeMarkdown.table',
|
||||||
// count the first row. We need it to trim the table (if table rows have inconsistent number of columns)
|
// count the first row. We need it to trim the table (if table rows have inconsistent number of columns)
|
||||||
colCount = rows[0].length;
|
colCount = rows[0].length;
|
||||||
|
|
||||||
// let's shift the first row as a heading if it is a heading
|
// let's shift the first row as a heading
|
||||||
if (hasHeadings) {
|
|
||||||
headings = rows.shift();
|
headings = rows.shift();
|
||||||
|
|
||||||
for (i = 0; i < headings.length; ++i) {
|
for (i = 0; i < headings.length; ++i) {
|
||||||
var headContent = showdown.subParser('makeMarkdown.tableCell')(headings[i], globals),
|
var headContent = showdown.subParser('makeMarkdown.tableCell')(headings[i], globals),
|
||||||
allign = '---';
|
align = '---';
|
||||||
|
|
||||||
if (headings[i].hasAttribute('style')) {
|
if (headings[i].hasAttribute('style')) {
|
||||||
var style = headings[i].getAttribute('style').toLowerCase().replace(/\s/g, '');
|
var style = headings[i].getAttribute('style').toLowerCase().replace(/\s/g, '');
|
||||||
switch (style) {
|
switch (style) {
|
||||||
case 'text-align:left;':
|
case 'text-align:left;':
|
||||||
allign = ':---';
|
align = ':---';
|
||||||
break;
|
break;
|
||||||
case 'text-align:right;':
|
case 'text-align:right;':
|
||||||
allign = '---:';
|
align = '---:';
|
||||||
break;
|
break;
|
||||||
case 'text-align:center;':
|
case 'text-align:center;':
|
||||||
allign = ':---:';
|
align = ':---:';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tableArray[0][i] = headContent.trim();
|
tableArray[0][i] = headContent.trim();
|
||||||
tableArray[1][i] = allign;
|
tableArray[1][i] = align;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
|
||||||
// since there's no headings, we will simulate it, counting the columns in the first line of the table
|
|
||||||
for (i = 0; i < colCount; ++i) {
|
|
||||||
tableArray[0][i] = ' ';
|
|
||||||
tableArray[1][i] = '---';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// now iterate through the rows and create the pseudo output (not pretty yet)
|
// now iterate through the rows and create the pseudo output (not pretty yet)
|
||||||
for (i = 0; i < rows.length; ++i) {
|
for (i = 0; i < rows.length; ++i) {
|
||||||
var r = tableArray.push([]) - 1;
|
var r = tableArray.push([]) - 1;
|
||||||
|
@ -133,7 +121,7 @@ showdown.subParser('makeMarkdown.table',
|
||||||
for (ii = 0; ii < colCount; ++ii) {
|
for (ii = 0; ii < colCount; ++ii) {
|
||||||
var cellContent = ' ';
|
var cellContent = ' ';
|
||||||
if (typeof rows[i][ii] !== 'undefined') {
|
if (typeof rows[i][ii] !== 'undefined') {
|
||||||
// Note: if rows[i][ii] is undefined, it means the row has less elements than the header,
|
// Note: if rows[i][ii] is undefined, it means the row has fewer elements than the header,
|
||||||
// and empty content will be added
|
// and empty content will be added
|
||||||
cellContent = showdown.subParser('makeMarkdown.tableCell')(rows[i][ii], globals);
|
cellContent = showdown.subParser('makeMarkdown.tableCell')(rows[i][ii], globals);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user