Skip to content

Commit

Permalink
fix: convert tabstops correctly (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan authored Jun 27, 2023
1 parent b16abb5 commit 78c17a8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/to-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,43 @@ class Table {
// should probably create a clone and not alter the original mdast
sanitizeBreaks(cell.tree);

cell.value = state.containerFlow(cell.tree, {
const lines = state.containerFlow(cell.tree, {
before: '\n',
after: '\n',
now: { line: 1, column: 1 },
lineShift: 0,
});
}).split('\n');

subexit();
exit();

state.options.lineWidth = oldWidth;
// calculate actual width and height of cell
const lines = cell.value.split('\n');
cell.lines = lines;
cell.height = lines.length;
cell.width = 0;
for (const line of lines) {

// calculate actual width and height of cell and transform the tab stops correctly
const TABS = [
' ',
' ',
' ',
' ',
];
for (let i = 0; i < lines.length; i += 1) {
let line = lines[i];
let idx = line.indexOf('\t');
if (idx >= 0) {
do {
// adjust tabstops
line = line.substring(0, idx) + TABS[idx % 4] + line.substring(idx + 1);
idx = line.indexOf('\t', idx + 1);
} while (idx >= 0);
lines[i] = line;
}
cell.width = Math.max(cell.width, line.length);
}

cell.value = lines.join('\n');
cell.width += 3;
return cell;
}
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/gt-with-tabs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Code with tabs

+-----------------+
| Code Listing |
+-----------------+
| ```js |
| |
| >a = 1; |
| > b = 2; |
| > c = 3; |
| > d = 4; |
| > e = 5; |
| > f = 6; |
| |
| ``` |
+-----------------+
22 changes: 22 additions & 0 deletions test/gridtable-to-md.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,28 @@ describe('gridtable to md', () => {
await assertMD(mdast, 'gt-with-breaks.md');
});

it('code with tabs', async () => {
const mdast = root([
heading(2, text('Code with tabs')),
gridTable([
gtRow([
gtCell(text('Code Listing')),
]),
gtRow([
gtCell(code('js', `
>a = 1;
>\tb = 2;
> \tc = 3;
> \t d = 4;
> \t e = 5;
>\t\t f = 6;
`)),
]),
]),
]);
await assertMD(mdast, 'gt-with-tabs.md');
});

it('table with list start', async () => {
const mdast = root([
heading(2, text('Wrong List start')),
Expand Down

0 comments on commit 78c17a8

Please sign in to comment.