-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from 'fs'; | ||
import { glob } from 'glob'; | ||
|
||
function getLine(txt, index) { | ||
|
||
let line = 1; | ||
|
||
for (let i = 0; i < index; i++) { | ||
if (txt[i] === '\n') { | ||
line += 1; | ||
} | ||
} | ||
|
||
return line; | ||
|
||
} | ||
|
||
export function findBadLinebreaks(file, fix = false) { | ||
|
||
let contents = fs.readFileSync(file, 'utf8').toString(); | ||
|
||
const re = /(?<=[\w\d ])\n(?=[\w\d])/g; | ||
const matches = Array.from(contents.matchAll(re)); | ||
|
||
for (const m of matches) { | ||
|
||
if (fix) { | ||
contents = `${contents.substring(0, m.index)} ${contents.substring(m.index + 1)}`; | ||
} | ||
else { | ||
|
||
const start = Math.max(0, m.index - 33); | ||
const end = Math.min(contents.length - 1, m.index + 33); | ||
|
||
console.log(`found erroneous linebreak at line ${getLine(contents, m.index)}:\n${contents.substring(start, end)}\n`); | ||
|
||
} | ||
|
||
} | ||
|
||
if (matches.length > 0) { | ||
|
||
if (fix) { | ||
fs.writeFileSync(file, contents); | ||
console.log(`fixed ${matches.length} erroneous linebreaks`); | ||
} | ||
else { | ||
process.exitCode = 1; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
// patterns match what is in package.json mdlint scripts | ||
const files = await glob( | ||
'**/*.md', | ||
{ | ||
ignore: [ | ||
'node_modules/**', | ||
'meetings/201*/*.md', | ||
'meetings/202[0-2]*/*.md', | ||
'meetings/2023-0[1-3]/*.md', | ||
] | ||
} | ||
); | ||
|
||
const fix = process.argv[2] ? process.argv[2] === 'fix' : false; | ||
|
||
for (const f of files) { | ||
findBadLinebreaks(f, fix); | ||
} |