From 7be53be4784e77613df78feeddf21dd82ed0b1f4 Mon Sep 17 00:00:00 2001 From: Titus Date: Sat, 28 Mar 2020 09:35:04 +0100 Subject: [PATCH] stringify: improve table format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Christian Murphy --- packages/remark-stringify/lib/defaults.js | 5 +-- .../remark-stringify/lib/visitors/table.js | 45 ++++++------------- packages/remark-stringify/package.json | 2 +- packages/remark-stringify/readme.md | 17 +++---- packages/remark-stringify/test.js | 29 ++++-------- packages/remark-stringify/types/index.d.ts | 5 +-- ...-padding.output.notable-cell-padding.text} | 0 ...ut.text => table-cell-padding.output.text} | 0 ...tput.loose-table.text => table-loose.text} | 0 ...pipe-align.output.notable-pipe-align.text} | 2 +- ...tput.text => table-pipe-align.output.text} | 0 ...ut.json => table-cell-padding.output.json} | 0 ...-padding.output.notable-cell-padding.json} | 0 ...tput.loose-table.json => table-loose.json} | 0 ...tput.json => table-pipe-align.output.json} | 0 ...pipe-align.output.notable-pipe-align.json} | 44 +++++++++--------- 16 files changed, 56 insertions(+), 93 deletions(-) rename test/fixtures/input/{table-spaced.output.nospaced-table.text => table-cell-padding.output.notable-cell-padding.text} (100%) rename test/fixtures/input/{table-spaced.output.text => table-cell-padding.output.text} (100%) rename test/fixtures/input/{table-loose.output.loose-table.text => table-loose.text} (100%) rename test/fixtures/input/{table-padded.output.nopadded-table.text => table-pipe-align.output.notable-pipe-align.text} (72%) rename test/fixtures/input/{table-padded.output.text => table-pipe-align.output.text} (100%) rename test/fixtures/tree/{table-spaced.output.json => table-cell-padding.output.json} (100%) rename test/fixtures/tree/{table-spaced.output.nospaced-table.json => table-cell-padding.output.notable-cell-padding.json} (100%) rename test/fixtures/tree/{table-loose.output.loose-table.json => table-loose.json} (100%) rename test/fixtures/tree/{table-padded.output.json => table-pipe-align.output.json} (100%) rename test/fixtures/tree/{table-padded.output.nopadded-table.json => table-pipe-align.output.notable-pipe-align.json} (92%) diff --git a/packages/remark-stringify/lib/defaults.js b/packages/remark-stringify/lib/defaults.js index fa19a368b..a9f153dc2 100644 --- a/packages/remark-stringify/lib/defaults.js +++ b/packages/remark-stringify/lib/defaults.js @@ -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, diff --git a/packages/remark-stringify/lib/visitors/table.js b/packages/remark-stringify/lib/visitors/table.js index e246103b6..33771508f 100644 --- a/packages/remark-stringify/lib/visitors/table.js +++ b/packages/remark-stringify/lib/visitors/table.js @@ -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| @@ -29,16 +26,13 @@ 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]) @@ -46,23 +40,10 @@ function table(node) { 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 }) } diff --git a/packages/remark-stringify/package.json b/packages/remark-stringify/package.json index 095ec994b..1127ab836 100644 --- a/packages/remark-stringify/package.json +++ b/packages/remark-stringify/package.json @@ -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", diff --git a/packages/remark-stringify/readme.md b/packages/remark-stringify/readme.md index 401f17a6c..78bf2b968 100644 --- a/packages/remark-stringify/readme.md +++ b/packages/remark-stringify/readme.md @@ -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` diff --git a/packages/remark-stringify/test.js b/packages/remark-stringify/test.js index dd091a66b..08ad8f512 100644 --- a/packages/remark-stringify/test.js +++ b/packages/remark-stringify/test.js @@ -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( @@ -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() }) diff --git a/packages/remark-stringify/types/index.d.ts b/packages/remark-stringify/types/index.d.ts index 95f446fbe..44dc5103a 100644 --- a/packages/remark-stringify/types/index.d.ts +++ b/packages/remark-stringify/types/index.d.ts @@ -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 diff --git a/test/fixtures/input/table-spaced.output.nospaced-table.text b/test/fixtures/input/table-cell-padding.output.notable-cell-padding.text similarity index 100% rename from test/fixtures/input/table-spaced.output.nospaced-table.text rename to test/fixtures/input/table-cell-padding.output.notable-cell-padding.text diff --git a/test/fixtures/input/table-spaced.output.text b/test/fixtures/input/table-cell-padding.output.text similarity index 100% rename from test/fixtures/input/table-spaced.output.text rename to test/fixtures/input/table-cell-padding.output.text diff --git a/test/fixtures/input/table-loose.output.loose-table.text b/test/fixtures/input/table-loose.text similarity index 100% rename from test/fixtures/input/table-loose.output.loose-table.text rename to test/fixtures/input/table-loose.text diff --git a/test/fixtures/input/table-padded.output.nopadded-table.text b/test/fixtures/input/table-pipe-align.output.notable-pipe-align.text similarity index 72% rename from test/fixtures/input/table-padded.output.nopadded-table.text rename to test/fixtures/input/table-pipe-align.output.notable-pipe-align.text index ea5e7474b..5a68372cb 100644 --- a/test/fixtures/input/table-padded.output.nopadded-table.text +++ b/test/fixtures/input/table-pipe-align.output.notable-pipe-align.text @@ -1,4 +1,4 @@ | Header 1 | Header 2 | -| -------- | -------- | +| - | - | | Cell 1 | Cell 2 | | Cell 3 | Cell 4 | diff --git a/test/fixtures/input/table-padded.output.text b/test/fixtures/input/table-pipe-align.output.text similarity index 100% rename from test/fixtures/input/table-padded.output.text rename to test/fixtures/input/table-pipe-align.output.text diff --git a/test/fixtures/tree/table-spaced.output.json b/test/fixtures/tree/table-cell-padding.output.json similarity index 100% rename from test/fixtures/tree/table-spaced.output.json rename to test/fixtures/tree/table-cell-padding.output.json diff --git a/test/fixtures/tree/table-spaced.output.nospaced-table.json b/test/fixtures/tree/table-cell-padding.output.notable-cell-padding.json similarity index 100% rename from test/fixtures/tree/table-spaced.output.nospaced-table.json rename to test/fixtures/tree/table-cell-padding.output.notable-cell-padding.json diff --git a/test/fixtures/tree/table-loose.output.loose-table.json b/test/fixtures/tree/table-loose.json similarity index 100% rename from test/fixtures/tree/table-loose.output.loose-table.json rename to test/fixtures/tree/table-loose.json diff --git a/test/fixtures/tree/table-padded.output.json b/test/fixtures/tree/table-pipe-align.output.json similarity index 100% rename from test/fixtures/tree/table-padded.output.json rename to test/fixtures/tree/table-pipe-align.output.json diff --git a/test/fixtures/tree/table-padded.output.nopadded-table.json b/test/fixtures/tree/table-pipe-align.output.notable-pipe-align.json similarity index 92% rename from test/fixtures/tree/table-padded.output.nopadded-table.json rename to test/fixtures/tree/table-pipe-align.output.notable-pipe-align.json index 84e1af841..7b57e32ff 100644 --- a/test/fixtures/tree/table-padded.output.nopadded-table.json +++ b/test/fixtures/tree/table-pipe-align.output.notable-pipe-align.json @@ -109,12 +109,12 @@ "start": { "line": 3, "column": 3, - "offset": 50 + "offset": 36 }, "end": { "line": 3, "column": 9, - "offset": 56 + "offset": 42 }, "indent": [] } @@ -124,12 +124,12 @@ "start": { "line": 3, "column": 3, - "offset": 50 + "offset": 36 }, "end": { "line": 3, "column": 9, - "offset": 56 + "offset": 42 }, "indent": [] } @@ -144,12 +144,12 @@ "start": { "line": 3, "column": 12, - "offset": 59 + "offset": 45 }, "end": { "line": 3, "column": 18, - "offset": 65 + "offset": 51 }, "indent": [] } @@ -159,12 +159,12 @@ "start": { "line": 3, "column": 12, - "offset": 59 + "offset": 45 }, "end": { "line": 3, "column": 18, - "offset": 65 + "offset": 51 }, "indent": [] } @@ -174,12 +174,12 @@ "start": { "line": 3, "column": 1, - "offset": 48 + "offset": 34 }, "end": { "line": 3, "column": 20, - "offset": 67 + "offset": 53 }, "indent": [] } @@ -197,12 +197,12 @@ "start": { "line": 4, "column": 3, - "offset": 70 + "offset": 56 }, "end": { "line": 4, "column": 9, - "offset": 76 + "offset": 62 }, "indent": [] } @@ -212,12 +212,12 @@ "start": { "line": 4, "column": 3, - "offset": 70 + "offset": 56 }, "end": { "line": 4, "column": 9, - "offset": 76 + "offset": 62 }, "indent": [] } @@ -232,12 +232,12 @@ "start": { "line": 4, "column": 12, - "offset": 79 + "offset": 65 }, "end": { "line": 4, "column": 18, - "offset": 85 + "offset": 71 }, "indent": [] } @@ -247,12 +247,12 @@ "start": { "line": 4, "column": 12, - "offset": 79 + "offset": 65 }, "end": { "line": 4, "column": 18, - "offset": 85 + "offset": 71 }, "indent": [] } @@ -262,12 +262,12 @@ "start": { "line": 4, "column": 1, - "offset": 68 + "offset": 54 }, "end": { "line": 4, "column": 20, - "offset": 87 + "offset": 73 }, "indent": [] } @@ -282,7 +282,7 @@ "end": { "line": 4, "column": 20, - "offset": 87 + "offset": 73 }, "indent": [ 1, @@ -301,7 +301,7 @@ "end": { "line": 5, "column": 1, - "offset": 88 + "offset": 74 } } }