Skip to content

Commit d0c8dd2

Browse files
authored
Merge pull request #80 from darkriszty/feature/support-tab-indentation
Feature/support tab indentation
2 parents ef68f56 + ffbd18c commit d0c8dd2

11 files changed

Lines changed: 510 additions & 12 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
os: [ ubuntu-latest, macos-latest, windows-latest ]
16-
node-version: [ '20.x', '22.x' ]
16+
node-version: [ 'lts/*', 'current' ]
1717

1818
steps:
1919
- name: Checkout

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ All notable changes will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8+
### Added
9+
- Issue #78: Support tab indentation for tables without a border.
10+
- Updated Node to the latest LTS for the Docker image.
11+
- Updated the devDependencies.
12+
- Updated the versions for running the tests.
13+
14+
## 3.7.0 - TBD
15+
16+
## 3.6.0 - 2021-10-29
17+
### Added
18+
- Issue #62: Config option for spacing.
819

920
## 3.5.0 - 2021-08-20
1021
### Added

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Makes tables more readable for humans. Compatible with the Markdown writer plugi
1717
- Support column alignment options with ":".
1818
- Find and format multiple tables.
1919
- Support \``code blocks`\` and ignore blocks with `<!-- markdown-table-prettify-ignore-start -->` and `<!-- markdown-table-prettify-ignore-end -->`.
20-
- Support indented tables.
20+
- Support indented tables (tables with borders or tab indented).
2121

2222
## Visual Studio Code
2323

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "markdown-table-prettify",
33
"displayName": "Markdown Table Prettifier",
44
"description": "Transforms markdown tables to be more readable.",
5-
"version": "3.6.0",
5+
"version": "3.7.0",
66
"publisher": "darkriszty",
77
"repository": {
88
"type": "git",

src/modelFactory/tableIndentationDetector.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,51 @@ export class FairTableIndentationDetector extends TableIndentationDetector {
2525
}
2626

2727
protected getIndentationChars(leftPadsPerLine: string[]): string {
28-
const nonEmptyLeftPads = leftPadsPerLine.filter(l => l.length > 0);
28+
const nonEmptyLeftPads: string[] = leftPadsPerLine.filter(l => l.length > 0);
29+
30+
// special handling for tab-based indentation: find longest common tab prefix
31+
let commonTabPrefixLength: number = 0;
32+
if (nonEmptyLeftPads.length > 0) {
33+
// find the shortest indent length from all lines
34+
let shortestLength: number = nonEmptyLeftPads[0].length;
35+
for (let i: number = 1; i < nonEmptyLeftPads.length; i++) {
36+
if (nonEmptyLeftPads[i].length < shortestLength) {
37+
shortestLength = nonEmptyLeftPads[i].length;
38+
}
39+
}
40+
41+
// check each char across all pads up untl the shortest length
42+
for (let pos: number = 0; pos < shortestLength; pos++) {
43+
if (nonEmptyLeftPads.every(pad => pad[pos] === '\t')) {
44+
// tabs are persistent indent char(s) up until here on all lines
45+
commonTabPrefixLength = pos + 1;
46+
} else {
47+
// early exit when first non-tab is found
48+
break;
49+
}
50+
}
51+
}
52+
53+
if (commonTabPrefixLength > 0) {
54+
return '\t'.repeat(commonTabPrefixLength);
55+
}
56+
57+
// Fallback to original logic
2958
let indentCounters: Map<string, number> = new Map();
3059

3160
for (const leftPad of nonEmptyLeftPads) {
32-
let count = 1;
61+
let count: number = 1;
3362
if (indentCounters.has(leftPad)) {
34-
count += indentCounters.get(leftPad);
63+
count += indentCounters.get(leftPad)!;
3564
}
3665
indentCounters.set(leftPad, ++count);
3766
}
3867

3968
// if there is an indentation used for at least 2 distinct lines, then use that, otherwise use the first line's indentation
40-
const indentWithMostRepeats = [...indentCounters.entries()].reduce((prev, curr) => curr[1] > prev[1] ? curr : prev)
69+
const indentWithMostRepeats: [string, number] = [...indentCounters.entries()].reduce((prev, curr) => curr[1] > prev[1] ? curr : prev)
70+
4171
return indentWithMostRepeats[1] > 1
4272
? indentWithMostRepeats[0]
43-
: indentCounters[0];
73+
: nonEmptyLeftPads[0];
4474
}
4575
}

src/modelFactory/transformers/borderTransformer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export class BorderTransformer extends Transformer {
1414
const rows: Row[] = this.rowsWithoutEmptyFirstAndLastColumn(input.rows, hasLeftBorder, hasRightBorder);
1515
const alignments = this.alignmentsWithoutEmptyFirstAndLastColumn(input.alignments, hasLeftBorder, hasRightBorder);
1616

17-
const leftPad = hasLeftBorder
17+
// allow indentation if the table has a clear left border or if it was indented with tabs (possibly with additional spaces for alignment)
18+
const leftPad = hasLeftBorder || /^\t/.test(input.leftPad)
1819
? input.leftPad
1920
: "";
2021
let result = new Table(rows, input.separatorEOL, alignments, leftPad);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
* Single tab
2+
Type | Range | Size
3+
------|------------------|-------------------------
4+
sbyte | -128 to 127 | Signed 8-bit integer
5+
byte | 0 to 255 | Unsigned 8-bit integer
6+
char | U+0000 to U+ffff | Unicode 16-bit character
7+
* Two tabs
8+
Type | Range | Size
9+
------|------------------|-------------------------
10+
sbyte | -128 to 127 | Signed 8-bit integer
11+
byte | 0 to 255 | Unsigned 8-bit integer
12+
char | U+0000 to U+ffff | Unicode 16-bit character
13+
* Mixed tabs and spaces, but more tab
14+
Type | Range | Size
15+
------|------------------|-------------------------
16+
sbyte | -128 to 127 | Signed 8-bit integer
17+
byte | 0 to 255 | Unsigned 8-bit integer
18+
char | U+0000 to U+ffff | Unicode 16-bit character
19+
* Left aligned
20+
Type | Range | Size
21+
:-----|:-----------------|:------------------------
22+
sbyte | -128 to 127 | Signed 8-bit integer
23+
byte | 0 to 255 | Unsigned 8-bit integer
24+
char | U+0000 to U+ffff | Unicode 16-bit character
25+
* Right aligned
26+
Type | Range | Size
27+
-----:|-----------------:|------------------------:
28+
sbyte | -128 to 127 | Signed 8-bit integer
29+
byte | 0 to 255 | Unsigned 8-bit integer
30+
char | U+0000 to U+ffff | Unicode 16-bit character
31+
* Center aligned
32+
Type | Range | Size
33+
:----:|:-----------------|:-----------------------:
34+
sbyte | -128 to 127 | Signed 8-bit integer
35+
byte | 0 to 255 | Unsigned 8-bit integer
36+
char | U+0000 to U+ffff | Unicode 16-bit character
37+
* Already right aligned should remain untouched
38+
Type | Range | Size
39+
-----:|-----------------:|------------------------:
40+
sbyte | -128 to 127 | Signed 8-bit integer
41+
byte | 0 to 255 | Unsigned 8-bit integer
42+
char | U+0000 to U+ffff | Unicode 16-bit character
43+
* Already center aligned should remain untouched
44+
Type | Range | Size
45+
:----:|:-----------------|:-----------------------:
46+
sbyte | -128 to 127 | Signed 8-bit integer
47+
byte | 0 to 255 | Unsigned 8-bit integer
48+
char | U+0000 to U+ffff | Unicode 16-bit character
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
* Single tab
2+
Type | Range| Size
3+
-|-|-
4+
sbyte | -128 to 127| Signed 8-bit integer
5+
byte| 0 to 255| Unsigned 8-bit integer
6+
char| U+0000 to U+ffff| Unicode 16-bit character
7+
* Two tabs
8+
Type | Range| Size
9+
-|-|-
10+
sbyte | -128 to 127| Signed 8-bit integer
11+
byte| 0 to 255| Unsigned 8-bit integer
12+
char| U+0000 to U+ffff| Unicode 16-bit character
13+
* Mixed tabs and spaces, but more tab
14+
Type | Range| Size
15+
-|-|-
16+
sbyte | -128 to 127| Signed 8-bit integer
17+
byte| 0 to 255| Unsigned 8-bit integer
18+
char| U+0000 to U+ffff| Unicode 16-bit character
19+
* Left aligned
20+
Type | Range| Size
21+
:-|:-|:-
22+
sbyte | -128 to 127| Signed 8-bit integer
23+
byte| 0 to 255| Unsigned 8-bit integer
24+
char| U+0000 to U+ffff| Unicode 16-bit character
25+
* Right aligned
26+
Type | Range| Size
27+
-:|-:|-:
28+
sbyte | -128 to 127| Signed 8-bit integer
29+
byte| 0 to 255| Unsigned 8-bit integer
30+
char| U+0000 to U+ffff| Unicode 16-bit character
31+
* Center aligned
32+
Type | Range| Size
33+
:-:|:-|:-:
34+
sbyte | -128 to 127| Signed 8-bit integer
35+
byte| 0 to 255| Unsigned 8-bit integer
36+
char| U+0000 to U+ffff| Unicode 16-bit character
37+
* Already right aligned should remain untouched
38+
Type | Range | Size
39+
-----:|-----------------:|------------------------:
40+
sbyte | -128 to 127 | Signed 8-bit integer
41+
byte | 0 to 255 | Unsigned 8-bit integer
42+
char | U+0000 to U+ffff | Unicode 16-bit character
43+
* Already center aligned should remain untouched
44+
Type | Range | Size
45+
:----:|:-----------------|:-----------------------:
46+
sbyte | -128 to 127 | Signed 8-bit integer
47+
byte | 0 to 255 | Unsigned 8-bit integer
48+
char | U+0000 to U+ffff | Unicode 16-bit character

0 commit comments

Comments
 (0)