lint.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var markdownlint = require("markdownlint");
  2. var glob = require("glob");
  3. var fs = require("fs");
  4. var inputFiles = glob.sync("**/*.md", { ignore: "node_modules/**/*" });
  5. var options = {
  6. files: inputFiles,
  7. config: {
  8. MD001: false, // Header levels should only increment by one level at a time
  9. MD002: false, // First header should be a h1 header
  10. MD003: "atx", // Header style
  11. MD004: { style: "asterisk" }, // Unordered list style
  12. MD005: true, // Inconsistent indentation for list items at the same level
  13. MD006: true, // Consider starting bulleted lists at the beginning of the line
  14. MD007: { indent: 2 }, // Unordered list indentation
  15. MD009: true, // Trailing spaces
  16. MD010: true, // Hard tabs
  17. MD011: true, // Reversed link syntax
  18. MD012: true, // Multiple consecutive blank lines
  19. MD013: false, // Line length
  20. MD014: false, // Dollar signs used before commands without showing output
  21. MD018: true, // No space after hash on atx style header
  22. MD019: true, // Multiple spaces after hash on atx style header
  23. MD020: false, // No space inside hashes on closed atx style header
  24. MD021: false, // Multiple spaces inside hashes on closed atx style header
  25. MD022: true, // Headers should be surrounded by blank lines
  26. MD023: true, // Headers must start at the beginning of the line
  27. MD024: false, // Multiple headers with the same content
  28. MD025: false, // Multiple top level headers in the same document
  29. MD026: { punctuation: ".,;:!" }, // Trailing punctuation in header
  30. MD027: true, // Multiple spaces after blockquote symbol
  31. MD028: true, // Blank line inside blockquote
  32. MD029: { style: "ordered" }, // Ordered list item prefix
  33. MD030: true, // Spaces after list markers
  34. MD031: true, // Fenced code blocks should be surrounded by blank lines
  35. MD032: true, // Lists should be surrounded by blank lines
  36. MD033: false, // Inline HTML
  37. MD034: true, // Bare URL used
  38. MD035: "---", // Horizontal rule style
  39. MD036: false, // Emphasis used instead of a header
  40. MD037: true, // Spaces inside emphasis markers
  41. MD038: false, // Spaces inside code span elements
  42. MD039: true, // Spaces inside link text
  43. MD040: true, // Fenced code blocks should have a language specified
  44. MD041: false, // First line in file should be a top level header
  45. }
  46. };
  47. var result = markdownlint.sync(options);
  48. console.log(result.toString());
  49. var exitCode = 0;
  50. Object.keys(result).forEach(function (file) {
  51. var fileResults = result[file];
  52. Object.keys(fileResults).forEach(function (rule) {
  53. var ruleResults = fileResults[rule];
  54. exitCode += ruleResults.length;
  55. });
  56. });
  57. inputFiles.forEach(function(fileName) {
  58. var text = fs.readFileSync(fileName, "utf8")
  59. exitCode += checkForImproperlyIndentedFencedCodeBlocks(fileName, text);
  60. });
  61. process.exit(exitCode);
  62. /**
  63. * @param {string} fileName
  64. * @param {string} text
  65. */
  66. function checkForImproperlyIndentedFencedCodeBlocks(fileName, text) {
  67. var lines = text.split(/\r?\n/g);
  68. var numErrors = 0;
  69. for (var i = 0; i < lines.length; i++) {
  70. var line = lines[i];
  71. var codeBlockMatch = line.match(/^(\s*)```\S+/);
  72. if (codeBlockMatch) {
  73. var startingColumn = codeBlockMatch[1].length;
  74. if (startingColumn === 0 || startingColumn === getCorrectStartingColumnForLine(lines, i)) {
  75. continue;
  76. }
  77. numErrors++;
  78. console.log(fileName + ": " +
  79. i + 1 + ": A fenced code block following a list item must be indented to the first non-whitespace character of the list item.")
  80. }
  81. }
  82. return numErrors;
  83. }
  84. /**
  85. * @param {string[]} line
  86. * @param {number} lineIndex
  87. */
  88. function getCorrectStartingColumnForLine(lines, lineIndex) {
  89. for (var i = lineIndex - 1; i >= 0; i--) {
  90. var line = lines[i];
  91. if (line.length === 0) {
  92. continue;
  93. }
  94. var m;
  95. if (m = line.match(/^\s*([\*\-]|(\d+\.))\s*/)) {
  96. return m[0].length;
  97. }
  98. if (m = line.match(/^(\s*)/)) {
  99. return m[0].length;
  100. }
  101. }
  102. return 0;
  103. }