Skip to content

Commit

Permalink
✨ fix extra whitespace in lists
Browse files Browse the repository at this point in the history
  • Loading branch information
ctcpip committed Oct 13, 2023
1 parent afc1eac commit 5cff241
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
8 changes: 4 additions & 4 deletions scripts/bad-linebreaks-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const afterMD = './scripts/test-samples/bad-linebreaks-sample-after.md';
const beforeMD = './scripts/test-samples/bad-linebreaks-sample-before.md';

// verify hash values to detect file tampering
const knownAfterHash = 'c2b5b7cc30cf5d4ce28274848eeba743';
const knownBeforeHash = 'c9cf57714ec19de2aeea68d45536b119';
const knownAfterHash = '68c0dd4fcb1c19013abf770c035b855e';
const knownBeforeHash = 'bb8c74dc4d1c661b8d233756c551b892';
const afterHash = await getHashSlingingSlasher(afterMD);
const beforeHash = await getHashSlingingSlasher(beforeMD);
assert.strictEqual(afterHash, knownAfterHash);
Expand All @@ -18,7 +18,7 @@ let fixed, totalMatches;

({ fixed, totalMatches } = findBadStuff(beforeMD, true));
assert.strictEqual(totalMatches.badLinebreaks, 12);
assert.strictEqual(totalMatches.extraWhitespace, 28);
assert.strictEqual(totalMatches.extraWhitespace, 34);
assert.strictEqual(fixed, fs.readFileSync(afterMD, 'utf8').toString());

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

({ fixed, totalMatches } = findBadStuff(beforeMD));
assert.strictEqual(totalMatches.badLinebreaks, 12);
assert.strictEqual(totalMatches.extraWhitespace, 28);
assert.strictEqual(totalMatches.extraWhitespace, 34);

function getHashSlingingSlasher(file) { // 💀
return new Promise((res, rej) => {
Expand Down
26 changes: 19 additions & 7 deletions scripts/bad-linebreaks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { glob } from 'glob';
// import attributes when?
const mdlintConfig = JSON.parse(fs.readFileSync('.markdownlint-cli2.jsonc', 'utf8').toString());

// not exhaustive, just the types we care about
const tokenTypeEnum = Object.freeze({
LIST: 'list',
PARAGRAPH: 'paragraph',
});

const reBadLinebreaks = /(?<=[\w\d ])\n(?=[\w\d ])/g;
const reExtraWhitespace = /^ +| (?= )| +$/gm;

Expand All @@ -24,9 +30,15 @@ export function findBadStuff(file, fix = false) {
const t = tokens[i];
let tokenContent = t.raw;

if (t.type === 'paragraph') {
tokenContent = findBadLinebreaks(tokenContent, totalMatches, fix, file);
tokenContent = findExtraWhitespace(tokenContent, totalMatches, fix, file);
switch (t.type) {
case tokenTypeEnum.PARAGRAPH:
tokenContent = findBadLinebreaks(tokenContent, totalMatches, fix, file, t.type);
// falls through
case tokenTypeEnum.LIST:
tokenContent = findExtraWhitespace(tokenContent, totalMatches, fix, file, t.type);
break;
default:
// do nothing
}

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

}

function findBadLinebreaks(tokenContent, totalMatches, fix, file) {
function findBadLinebreaks(tokenContent, totalMatches, fix, file, tokenType) {

const matches = Array.from(tokenContent.matchAll(reBadLinebreaks));
totalMatches.badLinebreaks += matches.length;
Expand All @@ -61,14 +73,14 @@ function findBadLinebreaks(tokenContent, totalMatches, fix, file) {
}

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

return tokenContent;

}

function findExtraWhitespace(tokenContent, totalMatches, fix, file) {
function findExtraWhitespace(tokenContent, totalMatches, fix, file, tokenType) {

const matches = Array.from(tokenContent.matchAll(reExtraWhitespace));
const extraWhitespaceCharacters = matches.join('').length;
Expand All @@ -81,7 +93,7 @@ function findExtraWhitespace(tokenContent, totalMatches, fix, file) {
}

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

return tokenContent;
Expand Down
6 changes: 6 additions & 0 deletions scripts/test-samples/bad-linebreaks-sample-after.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ let biscuits = "delicious";
let biscuits = "delicious";
```

- this is the first item in a list
- second list item
- third list item
- For the «Nice PDF» Versions next steps will be taken to find a solution for next year.
- something something something

## story time!

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.
Expand Down
6 changes: 6 additions & 0 deletions scripts/test-samples/bad-linebreaks-sample-before.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ let biscuits = "delicious";
let biscuits = "delicious";
```

- this is the first item in a list
- second list item
- third list item
- For the «Nice PDF» Versions next steps will be taken to find a solution for next year.
- something something something

## story time!

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,
Expand Down

0 comments on commit 5cff241

Please sign in to comment.