Fixed handling of tables with empty headers

This commit is contained in:
Laurent Cozic 2019-05-11 00:26:27 +01:00
parent 54f84c12fd
commit 1452200c79
2 changed files with 19 additions and 5 deletions

10
build_for_test.sh Normal file
View File

@ -0,0 +1,10 @@
#!/bin/bash
ROOT_DIR=/mnt/d/Web/www/joplin
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
set -e
npm run build
# rsync -a ./dist/ $ROOT_DIR/ElectronClient/app/node_modules/joplin-turndown-plugin-gfm/dist/
# rsync -a ./lib/ $ROOT_DIR/ElectronClient/app/node_modules/joplin-turndown-plugin-gfm/lib/
rm -rf $ROOT_DIR/CliClient/node_modules/joplin-turndown-plugin-gfm
ln -s "$CURRENT_DIR" $ROOT_DIR/CliClient/node_modules/joplin-turndown-plugin-gfm
$ROOT_DIR/CliClient/run_test.sh HtmlToMd

View File

@ -49,16 +49,20 @@ rules.table = {
replacement: function (content, node) {
if (tableShouldBeSkipped(node)) return content;
// Ensure there are no blank lines
content = content.replace(/\n+/g, '\n')
// If table has no heading, add an empty one so as to get a valid Markdown table
var firstRow = node.rows.length ? node.rows[0] : null
var columnCount = tableColCount(node); //firstRow ? firstRow.childNodes.length : 0
var secondLine = content.split('\n');
if (secondLine.length >= 2) secondLine = secondLine[1]
var secondLineIsDivider = secondLine.indexOf('| ---') === 0
var columnCount = tableColCount(node);
var emptyHeader = ''
if (columnCount && !isHeadingRow(firstRow)) {
if (columnCount && !secondLineIsDivider) {
emptyHeader = '|' + ' |'.repeat(columnCount) + '\n' + '|' + ' --- |'.repeat(columnCount)
}
// Ensure there are no blank lines
content = content.replace(/\n+/g, '\n')
return '\n\n' + emptyHeader + content + '\n\n'
}
}