Skip to content

Commit 14a6f5c

Browse files
committed
✨ fix extra whitespace in lists
1 parent afc1eac commit 14a6f5c

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

scripts/bad-linebreaks-test.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const afterMD = './scripts/test-samples/bad-linebreaks-sample-after.md';
77
const beforeMD = './scripts/test-samples/bad-linebreaks-sample-before.md';
88

99
// verify hash values to detect file tampering
10-
const knownAfterHash = 'c2b5b7cc30cf5d4ce28274848eeba743';
11-
const knownBeforeHash = 'c9cf57714ec19de2aeea68d45536b119';
10+
const knownAfterHash = '5f29c1fb4abd747c2dd801e12c600ee3';
11+
const knownBeforeHash = '406e900af5cd9af66abbe5b3ab6bcf3e';
1212
const afterHash = await getHashSlingingSlasher(afterMD);
1313
const beforeHash = await getHashSlingingSlasher(beforeMD);
1414
assert.strictEqual(afterHash, knownAfterHash);
@@ -18,7 +18,7 @@ let fixed, totalMatches;
1818

1919
({ fixed, totalMatches } = findBadStuff(beforeMD, true));
2020
assert.strictEqual(totalMatches.badLinebreaks, 12);
21-
assert.strictEqual(totalMatches.extraWhitespace, 28);
21+
assert.strictEqual(totalMatches.extraWhitespace, 114);
2222
assert.strictEqual(fixed, fs.readFileSync(afterMD, 'utf8').toString());
2323

2424
({ fixed, totalMatches } = findBadStuff(afterMD, true));
@@ -28,7 +28,7 @@ assert.strictEqual(fixed, fs.readFileSync(afterMD, 'utf8').toString());
2828

2929
({ fixed, totalMatches } = findBadStuff(beforeMD));
3030
assert.strictEqual(totalMatches.badLinebreaks, 12);
31-
assert.strictEqual(totalMatches.extraWhitespace, 28);
31+
assert.strictEqual(totalMatches.extraWhitespace, 114);
3232

3333
function getHashSlingingSlasher(file) { // 💀
3434
return new Promise((res, rej) => {

scripts/bad-linebreaks.mjs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ import { glob } from 'glob';
77
// import attributes when?
88
const mdlintConfig = JSON.parse(fs.readFileSync('.markdownlint-cli2.jsonc', 'utf8').toString());
99

10+
// not exhaustive, just the types we care about
11+
const tokenTypeEnum = Object.freeze({
12+
LIST: 'list',
13+
PARAGRAPH: 'paragraph',
14+
SPACE: 'space',
15+
});
16+
1017
const reBadLinebreaks = /(?<=[\w\d ])\n(?=[\w\d ])/g;
11-
const reExtraWhitespace = /^ +| (?= )| +$/gm;
18+
const reExtraWhitespaceParagraph = /^ +| (?= )| +$/gm;
19+
const reExtraWhitespaceList = /(?<=^ {0,}[-*+] |\d+\. ) +|(?<=\w+ ) +| +$/gm;
1220

1321
export function findBadStuff(file, fix = false) {
1422

@@ -24,9 +32,16 @@ export function findBadStuff(file, fix = false) {
2432
const t = tokens[i];
2533
let tokenContent = t.raw;
2634

27-
if (t.type === 'paragraph') {
28-
tokenContent = findBadLinebreaks(tokenContent, totalMatches, fix, file);
29-
tokenContent = findExtraWhitespace(tokenContent, totalMatches, fix, file);
35+
switch (t.type) {
36+
case tokenTypeEnum.PARAGRAPH:
37+
tokenContent = findBadLinebreaks(tokenContent, totalMatches, fix, file, t.type);
38+
// falls through
39+
case tokenTypeEnum.LIST:
40+
case tokenTypeEnum.SPACE:
41+
tokenContent = findExtraWhitespace(tokenContent, totalMatches, fix, file, t.type);
42+
break;
43+
default:
44+
// do nothing
3045
}
3146

3247
// we don't need to build this array if `fix` is `false`, but this keeps complexity down
@@ -41,7 +56,7 @@ export function findBadStuff(file, fix = false) {
4156

4257
}
4358

44-
function findBadLinebreaks(tokenContent, totalMatches, fix, file) {
59+
function findBadLinebreaks(tokenContent, totalMatches, fix, file, tokenType) {
4560

4661
const matches = Array.from(tokenContent.matchAll(reBadLinebreaks));
4762
totalMatches.badLinebreaks += matches.length;
@@ -61,27 +76,41 @@ function findBadLinebreaks(tokenContent, totalMatches, fix, file) {
6176
}
6277

6378
} else if (matches.length > 0) {
64-
console.error(`${file}\nfound paragraph with ${matches.length} erroneous linebreak(s):\n${tokenContent}\n`);
79+
console.error(`${file}\nfound ${tokenType} with ${matches.length} erroneous linebreak(s):\n${tokenContent}\n`);
6580
}
6681

6782
return tokenContent;
6883

6984
}
7085

71-
function findExtraWhitespace(tokenContent, totalMatches, fix, file) {
86+
function findExtraWhitespace(tokenContent, totalMatches, fix, file, tokenType) {
87+
88+
let re;
89+
90+
switch (tokenType) {
91+
case tokenTypeEnum.PARAGRAPH:
92+
case tokenTypeEnum.SPACE:
93+
re = reExtraWhitespaceParagraph;
94+
break;
95+
case tokenTypeEnum.LIST:
96+
re = reExtraWhitespaceList;
97+
break;
98+
default:
99+
throw new TypeError(`unsupported token type: ${tokenType}`);
100+
}
72101

73-
const matches = Array.from(tokenContent.matchAll(reExtraWhitespace));
102+
const matches = Array.from(tokenContent.matchAll(re));
74103
const extraWhitespaceCharacters = matches.join('').length;
75104
totalMatches.extraWhitespace += extraWhitespaceCharacters;
76105

77106
if (fix) {
78107

79108
if (matches.length > 0) {
80-
return tokenContent.replace(reExtraWhitespace, '');
109+
return tokenContent.replace(re, '');
81110
}
82111

83112
} else if (matches.length > 0) {
84-
console.error(`${file}\nfound paragraph with ${extraWhitespaceCharacters} extra whitespace character(s):\n${tokenContent}\n`);
113+
console.error(`${file}\nfound ${tokenType} with ${extraWhitespaceCharacters} extra whitespace character(s):\n${tokenContent}\n`);
85114
}
86115

87116
return tokenContent;

scripts/test-samples/bad-linebreaks-sample-after.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ let biscuits = "delicious";
3838
let biscuits = "delicious";
3939
```
4040

41+
100. First list item
42+
- First nested list item
43+
- Second nested list item
44+
45+
1. biscuits are
46+
1. extremely delicious
47+
1. indeed
48+
49+
- lists
50+
* are
51+
+ fun
52+
- and
53+
* one more
54+
+ time
55+
4156
## story time!
4257

4358
True! nervous, very, very dreadfully nervous I had been and am; but why will you say that I am mad? The disease had sharpened my senses, not destroyed, not dulled them. Above all was the sense of hearing acute. I heard all things in the heaven and in the earth. I heard many things in hell. How then am I mad? Hearken! and observe how healthily, how calmly I can tell you the whole story.

scripts/test-samples/bad-linebreaks-sample-before.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ let biscuits = "delicious";
4444
let biscuits = "delicious";
4545
```
4646

47+
100. First list item
48+
- First nested list item
49+
- Second nested list item
50+
51+
1. biscuits are
52+
1. extremely delicious
53+
1. indeed
54+
55+
- lists
56+
* are
57+
+ fun
58+
- and
59+
* one more
60+
+ time
61+
4762
## story time!
4863

4964
True! nervous, very, very dreadfully nervous I had been and am; but why will you say that I am mad? The disease had sharpened my senses, not destroyed,

0 commit comments

Comments
 (0)