Skip to content

Commit 3f803e9

Browse files
tjconcepthaltcase
authored andcommitted
fix: handle pipes in content correctly (#7)
1 parent 304d59d commit 3f803e9

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

fixtures/inputs.js

+10
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,15 @@ module.exports = {
183183
'| Sarah | 22 | true |',
184184
'| Lee | 23 | true |'
185185
].join(os.EOL) + os.EOL
186+
},
187+
pipes: {
188+
input: [
189+
{ content: 'yes | no' }
190+
],
191+
expected: [
192+
'| Content |',
193+
'| --------- |',
194+
'| yes \\| no |'
195+
].join(os.EOL) + os.EOL
186196
}
187197
}

index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = (input, options) => {
1414
}
1515

1616
options = Object.assign({
17-
stringify: v => typeof v === 'undefined' ? '' : String(v)
17+
stringify: toString
1818
}, options, {
1919
wrap: Object.assign({
2020
width: Infinity,
@@ -165,3 +165,9 @@ function padStart (what, target, start) {
165165
function padEnd (what, target, start) {
166166
return what + repeat(' ', target - what.length)
167167
}
168+
169+
function toString (v) {
170+
if (typeof v === 'undefined') return ''
171+
172+
return String(v).replace(/\|/g, '\\|')
173+
}

readme.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ tablemark(input, [options = {}])
7979
- `{Array<Object>} input`: the data to table-ify
8080
- `{Object} [options = {}]`
8181

82-
| key | type | default | description |
83-
| :------------: | :----------: | :--------: | -------------------------------------------- |
84-
| `columns` | `<Array>` | - | Array of column descriptors. |
85-
| `caseHeaders` | `<Boolean>` | `true` | Sentence case headers derived from keys. |
86-
| `stringify` | `<Function>` | - | Provide a custom "toString" function. |
87-
| `wrap.width` | `<Number>` | `Infinity` | Wrap texts at this length. |
88-
| `wrap.gutters` | `<Boolean>` | `false` | Add sides (`| <content> |`) to wrapped rows. |
82+
| key | type | default | description |
83+
| :------------: | :----------: | :--------: | ---------------------------------------------- |
84+
| `columns` | `<Array>` | - | Array of column descriptors. |
85+
| `caseHeaders` | `<Boolean>` | `true` | Sentence case headers derived from keys. |
86+
| `stringify` | `<Function>` | - | Provide a custom "toString" function. |
87+
| `wrap.width` | `<Number>` | `Infinity` | Wrap texts at this length. |
88+
| `wrap.gutters` | `<Boolean>` | `false` | Add sides (`\| <content> \|`) to wrapped rows. |
89+
8990

9091
The `columns` array can either contain objects, in which case their
9192
`name` and `align` properties will be used to alter the display of

test.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ test('gutters', t => {
4747
const result = fn(cases.gutters.input, cases.gutters.options)
4848
t.is(result, cases.gutters.expected)
4949
})
50+
51+
test('pipes in content', t => {
52+
const result = fn(cases.pipes.input, cases.pipes.options)
53+
t.is(result, cases.pipes.expected)
54+
})

0 commit comments

Comments
 (0)