Skip to content

Commit

Permalink
Fix table alignment for empy cells
Browse files Browse the repository at this point in the history
Related-to: 639271a.
Closes GH-309.
  • Loading branch information
wooorm committed Jul 29, 2023
1 parent ef0f459 commit c410a91
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 13 additions & 3 deletions packages/remark-lint-table-pipe-alignment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@
* 3:17-3:18: Misaligned table fence
*
* @example
* {"name": "ok-empty-cells.md", "gfm": true}
* {"name": "ok-empty-columns.md", "gfm": true}
*
* | | B | |
* |-| ----- | - |
* | | Bravo | |
*
* @example
* {"name": "ok-empty-cells.md", "gfm": true}
*
* | | | |
* | - | --- | ------- |
* | A | Bra | Charlie |
*/

/**
Expand Down Expand Up @@ -95,12 +101,12 @@ const remarkLintTablePipeAlignment = lintRule(
const cell = row.children[column]
const nextColumn = column + 1
const next = row.children[nextColumn]
const initial = cell
let initial = cell
? cell.children.length === 0
? pointStart(cell).offset
: pointEnd(cell.children[cell.children.length - 1]).offset
: pointStart(row).offset
const final = next
let final = next
? next.children.length === 0
? pointEnd(next).offset
: pointStart(next.children[0]).offset
Expand All @@ -114,9 +120,13 @@ const remarkLintTablePipeAlignment = lintRule(
continue
}

if (cell && cell.children.length === 0) initial++
if (next && next.children.length === 0) final--

const fence = value.slice(initial, final)
const pos = initial + fence.indexOf('|') - begin.offset + 1

// First cell at this column.
if (indices[nextColumn] === undefined) {
indices[nextColumn] = pos
} else if (pos !== indices[nextColumn]) {
Expand Down
18 changes: 17 additions & 1 deletion packages/remark-lint-table-pipe-alignment/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ No messages.
3:17-3:18: Misaligned table fence
```

##### `ok-empty-cells.md`
##### `ok-empty-columns.md`

###### In

Expand All @@ -200,6 +200,22 @@ No messages.

No messages.

##### `ok-empty-cells.md`

###### In

> 👉 **Note**: this example uses GFM ([`remark-gfm`][gfm]).
```markdown
| | | |
| - | --- | ------- |
| A | Bra | Charlie |
```

###### Out

No messages.

## Compatibility

Projects maintained by the unified collective are compatible with all maintained
Expand Down

0 comments on commit c410a91

Please sign in to comment.