mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-08 01:02:51 +00:00
Update markdown.js
This commit is contained in:
@@ -284,16 +284,18 @@ const spanTable = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let prevRow;
|
||||||
|
|
||||||
// Get any remaining header rows
|
// Get any remaining header rows
|
||||||
l = item.header.length;
|
l = item.header.length;
|
||||||
for (i = 1; i < l; i++) {
|
for (i = 1; i < l; i++) {
|
||||||
item.header[i] = splitCells(item.header[i], colCount);
|
item.header[i] = splitCells(item.header[i], colCount, item.header[i-1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get main table cells
|
// Get main table cells
|
||||||
l = item.rows.length;
|
l = item.rows.length;
|
||||||
for (i = 0; i < l; i++) {
|
for (i = 0; i < l; i++) {
|
||||||
item.rows[i] = splitCells(item.rows[i], colCount);
|
item.rows[i] = splitCells(item.rows[i], colCount, item.rows[i-1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// header child tokens
|
// header child tokens
|
||||||
@@ -330,7 +332,7 @@ const spanTable = {
|
|||||||
for (j = 0; j < row.length; j++) {
|
for (j = 0; j < row.length; j++) {
|
||||||
cell = row[j];
|
cell = row[j];
|
||||||
text = this.parseInline(cell.tokens);
|
text = this.parseInline(cell.tokens);
|
||||||
output += getTableCell(text, cell.colspan, 'th', token.align[col]);
|
output += getTableCell(text, cell.colspan, cell.rowspan, 'th', token.align[col]);
|
||||||
col += cell.colspan;
|
col += cell.colspan;
|
||||||
}
|
}
|
||||||
output += `</tr>`;
|
output += `</tr>`;
|
||||||
@@ -345,7 +347,7 @@ const spanTable = {
|
|||||||
for (j = 0; j < row.length; j++) {
|
for (j = 0; j < row.length; j++) {
|
||||||
cell = row[j];
|
cell = row[j];
|
||||||
text = this.parseInline(cell.tokens);
|
text = this.parseInline(cell.tokens);
|
||||||
output += getTableCell(text, cell.colspan, 'td', token.align[col]);
|
output += getTableCell(text, cell.colspan, cell.rowspan, 'td', token.align[col]);
|
||||||
col += cell.colspan;
|
col += cell.colspan;
|
||||||
}
|
}
|
||||||
output += `</tr>`;
|
output += `</tr>`;
|
||||||
@@ -357,14 +359,18 @@ const spanTable = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getTableCell = (text, colspan, type, align)=>{
|
const getTableCell = (text, colspan, rowspan, type, align)=>{
|
||||||
|
if(!rowspan) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
const tag = `<${type}`
|
const tag = `<${type}`
|
||||||
+ `${colspan > 1 ? ` colspan=${colspan}` : ''}`
|
+ `${colspan > 1 ? ` colspan=${colspan}` : ''}`
|
||||||
|
+ `${rowspan > 1 ? ` rowspan=${rowspan}` : ''}`
|
||||||
+ `${align ? ` align=${align}` : ''}>`;
|
+ `${align ? ` align=${align}` : ''}>`;
|
||||||
return `${tag + text}</${type}>\n`;
|
return `${tag + text}</${type}>\n`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const splitCells = (tableRow, count)=>{
|
const splitCells = (tableRow, count, prevRow = [])=>{
|
||||||
// ensure that every cell-delimiting pipe has a space
|
// ensure that every cell-delimiting pipe has a space
|
||||||
// before it to distinguish it from an escaped pipe
|
// before it to distinguish it from an escaped pipe
|
||||||
const row = tableRow.replace(/(\|+)/g, (match, p1, offset, str)=>{
|
const row = tableRow.replace(/(\|+)/g, (match, p1, offset, str)=>{
|
||||||
@@ -391,12 +397,34 @@ const splitCells = (tableRow, count)=>{
|
|||||||
let numCols = 0;
|
let numCols = 0;
|
||||||
|
|
||||||
for (; i < cells.length; i++) {
|
for (; i < cells.length; i++) {
|
||||||
const trimmedCell = cells[i].split(/(?<!\\)\|+$/)[0];
|
const trimmedCell = cells[i].split(/ \|+$/)[0];
|
||||||
cells[i] = {
|
cells[i] = {
|
||||||
colspan : 1 + cells[i].length - trimmedCell.length,
|
rowspan : 1,
|
||||||
|
colspan : Math.max(cells[i].length - trimmedCell.length, 1),
|
||||||
text : trimmedCell.trim().replace(/\\\|/g, '|')
|
text : trimmedCell.trim().replace(/\\\|/g, '|')
|
||||||
// leading or trailing whitespace is ignored per the gfm spec
|
// leading or trailing whitespace is ignored per the gfm spec
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Handle Rowspan
|
||||||
|
if(trimmedCell.slice(-1) == '^' && prevRow.length) {
|
||||||
|
// Find matching cell in previous row
|
||||||
|
let prevCols = 0;
|
||||||
|
let j, prevCell;
|
||||||
|
for (j = 0; j < prevRow.length; j++) {
|
||||||
|
let prevCell = prevRow[j];
|
||||||
|
if((prevCols == numCols) && (prevCell.colspan == cells[i].colspan)) {
|
||||||
|
cells[i].rowSpanTarget = prevCell.rowSpanTarget ?? prevCell;
|
||||||
|
cells[i].rowSpanTarget.text += ' ' + cells[i].text.slice(0, -1);
|
||||||
|
cells[i].rowSpanTarget.rowspan += 1;
|
||||||
|
cells[i].rowspan = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
prevCols += prevCell.colspan;
|
||||||
|
if(prevCols > numCols)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
numCols += cells[i].colspan;
|
numCols += cells[i].colspan;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user