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