Notebooks: Fix Table Generation into Pure Markdown When No thead Exists (#15423)

* works without alignment

* Alignment working

* Add comment

* Remove outdated comment
This commit is contained in:
Chris LaFreniere
2021-05-12 16:28:23 -07:00
committed by GitHub
parent 89db1266d2
commit 624c07947c
3 changed files with 72 additions and 23 deletions

View File

@@ -266,6 +266,15 @@ export class HTMLMarkdownConverter {
return delimiter + leadingSpace + content + trailingSpace + delimiter;
}
});
this.turndownService.addRule('p', {
filter: 'p',
replacement: function (content, node) {
// If inside of a table cell, extra newlines would break table rendering
return isInsideTable(node) ? content : '\n\n' + content + '\n\n';
}
});
this.turndownService.escape = escapeMarkdown;
}
}
@@ -281,10 +290,16 @@ function blankReplacement(content, node) {
// When outdenting a nested list, an empty list will still remain. Need to handle this case.
if (node.nodeName === 'UL' || node.nodeName === 'OL') {
return '\n';
} else if (isInsideTable(node)) {
return ' ';
}
return node.isBlock ? '\n\n' : '';
}
function isInsideTable(node): boolean {
return node.parentNode?.nodeName === 'TH' || node.parentNode?.nodeName === 'TD';
}
export function findPathRelativeToContent(notebookFolder: string, contentPath: URI | undefined): string {
if (notebookFolder) {
if (contentPath?.scheme === 'file') {

View File

@@ -68,36 +68,23 @@ rules['tableCell'] = {
rules['tableRow'] = {
filter: 'tr',
replacement: function (content, node) {
let borderCells = '';
let alignMap = { left: ':--', right: '--:', center: ':-:' };
if (isHeadingRow(node)) {
for (let i = 0; i < node.childNodes.length; i++) {
let border = '---';
let align = (
node.childNodes[i].getAttribute('align') || ''
).toLowerCase();
if (align) {
border = alignMap[align] || border;
}
borderCells += cell(border, node.childNodes[i]);
}
}
const borderCells = isHeadingRow(node) ? constructBorderCells(node) : '';
return '\n' + content + (borderCells ? '\n' + borderCells : '');
}
};
rules['table'] = {
// Only convert tables with a heading row.
// Tables with no heading row are kept using `keep` (see below).
filter: function (node) {
return node.nodeName === 'TABLE' && isHeadingRow(node.rows[0]);
return node.nodeName === 'TABLE';
},
replacement: function (content, node) {
// Ensure there are no blank lines
content = content.replace('\n\n', '\n');
// if the headings are empty, add border line and headings to keep table format
if (!isHeadingRow(node.rows[0])) {
let emptyHeader = '\n\n|' + ' |'.repeat(node.rows[0].childNodes.length) + '\n';
return emptyHeader + constructBorderCells(node.rows[0]) + content + '\n\n';
}
return '\n\n' + content + '\n\n';
}
};
@@ -148,10 +135,25 @@ function cell(content, node) {
return prefix + content + ' |';
}
function constructBorderCells(node): string {
const alignMap = { left: ':--', right: '--:', center: ':-:' };
let borderCells = '';
for (let i = 0; i < node.childNodes.length; i++) {
let border = '---';
let align = (
node.childNodes[i].getAttribute('align') || ''
).toLowerCase();
if (align) {
border = alignMap[align] || border;
}
borderCells += cell(border, node.childNodes[i]);
}
return borderCells;
}
export function tables(turndownService) {
turndownService.keep(function (node) {
return node.nodeName === 'TABLE' && !isHeadingRow(node.rows[0]);
});
for (let key in rules) {
turndownService.addRule(key, rules[key]);
}