From 47a2e1df40c7bfb9fe9d2d1a2651d24e87ea9219 Mon Sep 17 00:00:00 2001 From: tstibbs Date: Sun, 26 Jan 2020 20:54:20 +0000 Subject: [PATCH] Fixed handling of newlines within table cells. closes #332 --- src/02-section/table/parse/index.js | 6 ++++-- tests/table.test.js | 30 ++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/02-section/table/parse/index.js b/src/02-section/table/parse/index.js index bab82a48..72495a45 100644 --- a/src/02-section/table/parse/index.js +++ b/src/02-section/table/parse/index.js @@ -93,8 +93,10 @@ const firstRowHeader = function(rows) { //turn a {|...table string into an array of arrays const parseTable = function(wiki) { - let lines = wiki.replace(/\r/g, '').split(/\n/) - lines = lines.map(l => l.trim()) + let lines = wiki.replace(/\r/g, '') + .replace(/\n(\s*[^|!{\s])/g, ' $1')//remove unecessary newlines + .split(/\n/) + .map(l => l.trim()) let rows = findRows(lines) //support colspan, rowspan... rows = handleSpans(rows) diff --git a/tests/table.test.js b/tests/table.test.js index 2d592829..63562e02 100644 --- a/tests/table.test.js +++ b/tests/table.test.js @@ -486,7 +486,7 @@ test('junky-table', t => { t.end() }) -test('table newlines', t => { +test('table double bar', t => { var str = `{| class="wikitable" |- ! h1 @@ -516,3 +516,31 @@ test('table newlines', t => { t.equal(data[2].h3, 'ccc', 'h3') t.end() }) + +//testing https://github.com/spencermountain/wtf_wikipedia/issues/332 +test('table newline', t => { + var str = `{| class="wikitable" +|- +! h1 +! h2 +! h3 +|- +| a +| b1
b2 +| c +|- +| a +| b1 +b2 +| c +|}` + var doc = wtf(str) + var data = doc.tables(0).keyValue() + t.equal(data[0].h1, 'a', 'h1') + t.equal(data[0].h2, 'b1 b2', 'h2') + t.equal(data[0].h3, 'c', 'h3') + t.equal(data[0].h1, 'a', 'h1') + t.equal(data[0].h2, 'b1 b2', 'h2') + t.equal(data[0].h3, 'c', 'h3') + t.end() +})