Skip to content

Commit

Permalink
Markdown parser fix (#35)
Browse files Browse the repository at this point in the history
* Fixed markdown parser

Fixes #33. Problem was caused by parser, which didn't set appropriate column widths when separator row is present in table.
  • Loading branch information
rpeshkov authored May 3, 2018
1 parent 27e79e9 commit f468578
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed

- Command `text-tables.gotoNextCell` (`Text Tables: Go to next cell` in command pallete) inserts new row when applied in last data cell of table.
- Fixed markdown parser for tables with separator row where separator row has less columns than other row (#33).

## [0.1.0] - 2018-04-16

Expand Down
6 changes: 5 additions & 1 deletion src/ttMarkdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as tt from './ttTable';
import * as vscode from 'vscode';
import { RowType } from './ttTable';

const verticalSeparator = '|';
const horizontalSeparator = '-';
Expand All @@ -20,7 +21,6 @@ export class MarkdownParser implements tt.Parser {

if (this.isSeparatorRow(cleanedString)) {
result.addRow(tt.RowType.Separator, []);
result.cols.forEach(x => x.width = Math.max(x.width, 3));
const startIndex = cleanedString.startsWith(verticalSeparator) ? 1 : 0;
const endIndex = cleanedString.length - (cleanedString.endsWith(verticalSeparator) ? 1 : 0);
const rowParts = cleanedString.slice(startIndex, endIndex).split('|');
Expand Down Expand Up @@ -59,6 +59,10 @@ export class MarkdownParser implements tt.Parser {
result.addRow(tt.RowType.Data, values);
}

if (result.rows.some(x => x.type === RowType.Separator)) {
result.cols.forEach(x => x.width = Math.max(x.width, 3));
}

return result;
}

Expand Down
19 changes: 19 additions & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,25 @@ suite('Commands', () => {
});
});

test('Test "Format under cursor" for markdown', async () => {
const input =
`| 1 | 2 |
| --- |
| 4 | 5 | 6 |`;

const expected =
`| 1 | 2 | |
| --- | --- | --- |
| 4 | 5 | 6 |`;

await inTextEditor({language: 'markdown', content: input}, async (_, document) => {
await cfg.override({mode: 'markdown'});

await vscode.commands.executeCommand('text-tables.formatUnderCursor');
assert.equal(document.getText(), expected);
});
});

test('Test "Next Row"', async () => {
const input =
`| Row |
Expand Down

0 comments on commit f468578

Please sign in to comment.