diff --git a/scripts/bad-linebreaks-test.mjs b/scripts/bad-linebreaks-test.mjs index 6bf1d4c4..f4a79d89 100644 --- a/scripts/bad-linebreaks-test.mjs +++ b/scripts/bad-linebreaks-test.mjs @@ -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); @@ -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)); @@ -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) => { diff --git a/scripts/bad-linebreaks.mjs b/scripts/bad-linebreaks.mjs index 9f3a43ad..d3e9f22c 100644 --- a/scripts/bad-linebreaks.mjs +++ b/scripts/bad-linebreaks.mjs @@ -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; @@ -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 @@ -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; @@ -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; @@ -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; diff --git a/scripts/test-samples/bad-linebreaks-sample-after.md b/scripts/test-samples/bad-linebreaks-sample-after.md index ef824a26..0ce1eb20 100644 --- a/scripts/test-samples/bad-linebreaks-sample-after.md +++ b/scripts/test-samples/bad-linebreaks-sample-after.md @@ -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. diff --git a/scripts/test-samples/bad-linebreaks-sample-before.md b/scripts/test-samples/bad-linebreaks-sample-before.md index e3043594..20b6e702 100644 --- a/scripts/test-samples/bad-linebreaks-sample-before.md +++ b/scripts/test-samples/bad-linebreaks-sample-before.md @@ -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,