Skip to content

Commit

Permalink
Merge pull request #333 from tstibbs/bugfix/332
Browse files Browse the repository at this point in the history
Fixed handling of newlines within table cells. closes #332
  • Loading branch information
spencermountain authored Jan 28, 2020
2 parents 32da0b5 + 47a2e1d commit ad965f4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/02-section/table/parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 29 additions & 1 deletion tests/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ test('junky-table', t => {
t.end()
})

test('table newlines', t => {
test('table double bar', t => {
var str = `{| class="wikitable"
|-
! h1
Expand Down Expand Up @@ -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<br />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()
})

0 comments on commit ad965f4

Please sign in to comment.