forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add disable script from hub (#43048)
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env node | ||
// Disables markdownlint rules in markdown files with same-line comments. This is | ||
// useful when introducing a new rule that causes many failures. The comments | ||
// can be fixed and removed at while updating the file later. | ||
// | ||
// Usage: | ||
// | ||
// script/markdownlint-disable.js no-generic-link-text | ||
|
||
import fs from 'fs' | ||
import { spawn } from 'child_process' | ||
|
||
const rule = process.argv[2] | ||
if (!rule) { | ||
console.error('Please specify a rule to disable.') | ||
process.exit(1) | ||
} | ||
let verbose = false | ||
if (process.argv[3] === '--verbose' || process.argv[3] === '-v') { | ||
verbose = true | ||
} | ||
|
||
// Cleanup from previous run | ||
if (fs.existsSync('markdown-violations.json')) { | ||
fs.unlinkSync('markdown-violations.json') | ||
} | ||
|
||
console.log(`Disabling "${rule}" rule in markdown files...`) | ||
const childProcess = spawn('npm', [ | ||
'run', | ||
'lint-content', | ||
'--', | ||
'-p', | ||
'content', | ||
'data', | ||
'-o', | ||
'markdown-violations.json', | ||
'-r', | ||
rule, | ||
]) | ||
|
||
childProcess.stdout.on('data', (data) => { | ||
if (verbose) console.log(data.toString()) | ||
}) | ||
|
||
childProcess.stderr.on('data', function (data) { | ||
if (verbose) console.log(data.toString()) | ||
}) | ||
|
||
let matchingRulesFound = 0 | ||
childProcess.on('close', (code) => { | ||
if (code === 0) { | ||
console.log(`No violations for rule, "${rule}" found.`) | ||
process.exit(0) | ||
} | ||
|
||
const markdownViolations = JSON.parse(fs.readFileSync('markdown-violations.json', 'utf8')) | ||
console.log(`${Object.values(markdownViolations).flat().length} violations found.`) | ||
|
||
Object.entries(markdownViolations).forEach(([fileName, results]) => { | ||
console.log(fileName) | ||
console.log(results) | ||
const fileLines = fs.readFileSync(fileName, 'utf8').split('\n') | ||
results.forEach((result) => { | ||
matchingRulesFound++ | ||
const lineIndex = result.lineNumber - 1 | ||
const offendingLine = fileLines[lineIndex] | ||
fileLines[lineIndex] = offendingLine.concat(` <!-- markdownlint-disable-line ${rule} -->`) | ||
}) | ||
fs.writeFileSync(fileName, fileLines.join('\n'), 'utf8') | ||
}) | ||
|
||
console.log(`${matchingRulesFound} violations ignored.`) | ||
}) |
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