From 1452200c79f7300b57f7b6f493ab9c6cccd1dce6 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Sat, 11 May 2019 00:26:27 +0100 Subject: [PATCH] Fixed handling of tables with empty headers --- build_for_test.sh | 10 ++++++++++ src/tables.js | 14 +++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 build_for_test.sh diff --git a/build_for_test.sh b/build_for_test.sh new file mode 100644 index 000000000..3283e1c5d --- /dev/null +++ b/build_for_test.sh @@ -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 \ No newline at end of file diff --git a/src/tables.js b/src/tables.js index 980ed1e3d..b76d87255 100644 --- a/src/tables.js +++ b/src/tables.js @@ -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' } }