Skip to content

Commit

Permalink
stringify: improve table format
Browse files Browse the repository at this point in the history
This change updates markdown-table, the library used to serialize markdown
tables.

With that update comes options that make more sense:

* `looseTable` is no longer supported (it’s a bad GFM feature)
* `spacedTable` is now called `tableCellPadding`
* `paddedTable` is now called `tablePipeAlign`

Closes GH-476.
Related-to remarkjs/remark-lint#217.

Reviewed-by: Junyoung Choi <[email protected]>
Reviewed-by: Christian Murphy <[email protected]>
  • Loading branch information
wooorm authored Mar 28, 2020
1 parent 5f9114f commit 7be53be
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 93 deletions.
5 changes: 2 additions & 3 deletions packages/remark-stringify/lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ module.exports = {
entities: 'false',
setext: false,
closeAtx: false,
looseTable: false,
spacedTable: true,
paddedTable: true,
tableCellPadding: true,
tablePipeAlign: true,
stringLength: stringLength,
incrementListMarker: true,
fences: false,
Expand Down
45 changes: 13 additions & 32 deletions packages/remark-stringify/lib/visitors/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ var markdownTable = require('markdown-table')

module.exports = table

var space = ' '
var verticalBar = '|'

// Stringify table.
//
// Creates a fenced table by default, but not in `looseTable: true` mode:
// Creates a fenced table.
// The table has aligned delimiters by default, but not in
// `tablePipeAlign: false`:
//
// ```markdown
// Foo | Bar
// :-: | ---
// Baz | Qux
//
// NOTE: Be careful with `looseTable: true` mode, as a loose table inside an
// indented code block on GitHub renders as an actual table!
// | Header 1 | Header 2 |
// | :-: | - |
// | Alpha | Bravo |
// ```
//
// Creates a spaced table by default, but not in `spacedTable: false`:
// The table is spaced by default, but not in `tableCellPadding: false`:
//
// ```markdown
// |Foo|Bar|
Expand All @@ -29,40 +26,24 @@ var verticalBar = '|'
function table(node) {
var self = this
var options = self.options
var loose = options.looseTable
var spaced = options.spacedTable
var pad = options.paddedTable
var padding = options.tableCellPadding
var alignDelimiters = options.tablePipeAlign
var stringLength = options.stringLength
var rows = node.children
var index = rows.length
var exit = self.enterTable()
var result = []
var start
var end

while (index--) {
result[index] = self.all(rows[index])
}

exit()

if (loose) {
start = ''
end = ''
} else if (spaced) {
start = verticalBar + space
end = space + verticalBar
} else {
start = verticalBar
end = verticalBar
}

return markdownTable(result, {
align: node.align,
pad: pad,
start: start,
end: end,
stringLength: stringLength,
delimiter: spaced ? space + verticalBar + space : verticalBar
alignDelimiters: alignDelimiters,
padding: padding,
stringLength: stringLength
})
}
2 changes: 1 addition & 1 deletion packages/remark-stringify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"is-whitespace-character": "^1.0.0",
"longest-streak": "^2.0.1",
"markdown-escapes": "^1.0.0",
"markdown-table": "^1.1.0",
"markdown-table": "^2.0.0",
"mdast-util-compact": "^2.0.0",
"parse-entities": "^2.0.0",
"repeat-string": "^1.5.4",
Expand Down
17 changes: 6 additions & 11 deletions packages/remark-stringify/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,15 @@ Other heading levels are compiled as ATX (respecting `closeAtx`).
Compile ATX headings with the same amount of closing hashes as opening hashes
(`boolean`, default: `false`).

###### `options.looseTable`
###### `options.tableCellPadding`

Create tables without fences: initial and final pipes (`boolean`, default:
`false`).

###### `options.spacedTable`

Create tables with a space between a pipe and content (`boolean`, default:
`true`).
Create tables with a space between cell delimiters (`|`) and content (`boolean`,
default: `true`).

###### `options.paddedTable`
###### `options.tablePipeAlign`

Create tables with more spaces so that all cells in a column align (`boolean`,
default: `true`).
Align the delimiters (`|`) between table cells so that they all align nicely and
form a grid (`boolean`, default: `true`).

###### `options.stringLength`

Expand Down
29 changes: 9 additions & 20 deletions packages/remark-stringify/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,33 +221,22 @@ test('remark().stringify(ast, file)', function (t) {
function () {
unified()
.use(stringify)
.data('settings', {looseTable: '!'})
.data('settings', {tableCellPadding: '?'})
.stringify(empty())
},
/options\.looseTable/,
'should throw when `options.looseTable` is not a boolean'
/options\.tableCellPadding/,
'should throw when `options.tableCellPadding` is not a boolean'
)

t.throws(
function () {
unified()
.use(stringify)
.data('settings', {spacedTable: '?'})
.data('settings', {tablePipeAlign: '.'})
.stringify(empty())
},
/options\.spacedTable/,
'should throw when `options.spacedTable` is not a boolean'
)

t.throws(
function () {
unified()
.use(stringify)
.data('settings', {paddedTable: '.'})
.stringify(empty())
},
/options\.paddedTable/,
'should throw when `options.paddedTable` is not a boolean'
/options\.tablePipeAlign/,
'should throw when `options.tablePipeAlign` is not a boolean'
)

t.throws(
Expand Down Expand Up @@ -715,9 +704,9 @@ test('remark().stringify(ast, file)', function (t) {

t.test('should support valid booleans', function (st) {
var compiler = new Compiler()
st.equal(compiler.options.looseTable, false)
compiler.setOptions({looseTable: true})
st.equal(compiler.options.looseTable, true)
st.equal(compiler.options.setext, false)
compiler.setOptions({setext: true})
st.equal(compiler.options.setext, true)
st.end()
})

Expand Down
5 changes: 2 additions & 3 deletions packages/remark-stringify/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ declare namespace remarkStringify {
entities: boolean | 'numbers' | 'escape'
setext: boolean
closeAtx: boolean
looseTable: boolean
spacedTable: boolean
paddedTable: boolean
tableCellPadding: boolean
tablePipeAlign: boolean
stringLength: (s: string) => number
fence: '~' | '`'
fences: boolean
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
| Header 1 | Header 2 |
| -------- | -------- |
| - | - |
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@
"start": {
"line": 3,
"column": 3,
"offset": 50
"offset": 36
},
"end": {
"line": 3,
"column": 9,
"offset": 56
"offset": 42
},
"indent": []
}
Expand All @@ -124,12 +124,12 @@
"start": {
"line": 3,
"column": 3,
"offset": 50
"offset": 36
},
"end": {
"line": 3,
"column": 9,
"offset": 56
"offset": 42
},
"indent": []
}
Expand All @@ -144,12 +144,12 @@
"start": {
"line": 3,
"column": 12,
"offset": 59
"offset": 45
},
"end": {
"line": 3,
"column": 18,
"offset": 65
"offset": 51
},
"indent": []
}
Expand All @@ -159,12 +159,12 @@
"start": {
"line": 3,
"column": 12,
"offset": 59
"offset": 45
},
"end": {
"line": 3,
"column": 18,
"offset": 65
"offset": 51
},
"indent": []
}
Expand All @@ -174,12 +174,12 @@
"start": {
"line": 3,
"column": 1,
"offset": 48
"offset": 34
},
"end": {
"line": 3,
"column": 20,
"offset": 67
"offset": 53
},
"indent": []
}
Expand All @@ -197,12 +197,12 @@
"start": {
"line": 4,
"column": 3,
"offset": 70
"offset": 56
},
"end": {
"line": 4,
"column": 9,
"offset": 76
"offset": 62
},
"indent": []
}
Expand All @@ -212,12 +212,12 @@
"start": {
"line": 4,
"column": 3,
"offset": 70
"offset": 56
},
"end": {
"line": 4,
"column": 9,
"offset": 76
"offset": 62
},
"indent": []
}
Expand All @@ -232,12 +232,12 @@
"start": {
"line": 4,
"column": 12,
"offset": 79
"offset": 65
},
"end": {
"line": 4,
"column": 18,
"offset": 85
"offset": 71
},
"indent": []
}
Expand All @@ -247,12 +247,12 @@
"start": {
"line": 4,
"column": 12,
"offset": 79
"offset": 65
},
"end": {
"line": 4,
"column": 18,
"offset": 85
"offset": 71
},
"indent": []
}
Expand All @@ -262,12 +262,12 @@
"start": {
"line": 4,
"column": 1,
"offset": 68
"offset": 54
},
"end": {
"line": 4,
"column": 20,
"offset": 87
"offset": 73
},
"indent": []
}
Expand All @@ -282,7 +282,7 @@
"end": {
"line": 4,
"column": 20,
"offset": 87
"offset": 73
},
"indent": [
1,
Expand All @@ -301,7 +301,7 @@
"end": {
"line": 5,
"column": 1,
"offset": 88
"offset": 74
}
}
}

0 comments on commit 7be53be

Please sign in to comment.