-
Notifications
You must be signed in to change notification settings - Fork 226
/
rules.rb
801 lines (749 loc) · 24.3 KB
/
rules.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
docs do |id, description|
url_hash = [id.downcase,
description.downcase.gsub(/[^a-z]+/, '-')].join('---')
"https://github.com/markdownlint/markdownlint/blob/main/docs/RULES.md##{url_hash}"
end
rule 'MD001', 'Header levels should only increment by one level at a time' do
tags :headers
aliases 'header-increment'
check do |doc|
headers = doc.find_type(:header)
old_level = nil
errors = []
headers.each do |h|
errors << h[:location] if old_level && (h[:level] > old_level + 1)
old_level = h[:level]
end
errors
end
end
rule 'MD002', 'First header should be a top level header' do
tags :headers
aliases 'first-header-h1'
params :level => 1
check do |doc|
first_header = doc.find_type(:header).first
if first_header && (first_header[:level] != @params[:level])
[first_header[:location]]
end
end
end
rule 'MD003', 'Header style' do
# Header styles are things like ### and adding underscores
# See https://daringfireball.net/projects/markdown/syntax#header
tags :headers
aliases 'header-style'
# :style can be one of :consistent, :atx, :atx_closed, :setext
params :style => :consistent
check do |doc|
headers = doc.find_type_elements(:header, false)
if headers.empty?
nil
else
doc_style = if @params[:style] == :consistent
doc.header_style(headers.first)
else
@params[:style]
end
if doc_style == :setext_with_atx
headers.map do |h|
doc.element_linenumber(h) \
unless (doc.header_style(h) == :setext) || \
((doc.header_style(h) == :atx) && \
(h.options[:level] > 2))
end.compact
else
headers.map do |h|
doc.element_linenumber(h) \
if doc.header_style(h) != doc_style
end.compact
end
end
end
end
rule 'MD004', 'Unordered list style' do
tags :bullet, :ul
aliases 'ul-style'
# :style can be one of :consistent, :asterisk, :plus, :dash, :sublist
params :style => :consistent
check do |doc|
bullets = doc.find_type_elements(:ul).map do |l|
doc.find_type_elements(:li, false, l.children)
end.flatten
if bullets.empty?
nil
else
doc_style = case @params[:style]
when :consistent
doc.list_style(bullets.first)
when :sublist
{}
else
@params[:style]
end
results = []
bullets.each do |b|
if @params[:style] == :sublist
level = b.options[:element_level]
if doc_style[level]
if doc_style[level] != doc.list_style(b)
results << doc.element_linenumber(b)
end
else
doc_style[level] = doc.list_style(b)
end
elsif doc.list_style(b) != doc_style
results << doc.element_linenumber(b)
end
end
results.compact
end
end
end
rule 'MD005', 'Inconsistent indentation for list items at the same level' do
tags :bullet, :ul, :indentation
aliases 'list-indent'
check do |doc|
bullets = doc.find_type(:li)
errors = []
indent_levels = []
bullets.each do |b|
indent_level = doc.indent_for(doc.element_line(b))
if indent_levels[b[:element_level]].nil?
indent_levels[b[:element_level]] = indent_level
end
if indent_level != indent_levels[b[:element_level]]
errors << doc.element_linenumber(b)
end
end
errors
end
end
rule 'MD006', 'Consider starting bulleted lists at the beginning of the line' do
# Starting at the beginning of the line means that indentation for each
# bullet level can be identical.
tags :bullet, :ul, :indentation
aliases 'ul-start-left'
check do |doc|
doc.find_type(:ul, false).reject do |e|
doc.indent_for(doc.element_line(e)) == 0
end.map { |e| e[:location] }
end
end
rule 'MD007', 'Unordered list indentation' do
tags :bullet, :ul, :indentation
aliases 'ul-indent'
# Do not default to < 3, see PR#373 or the comments in RULES.md
params :indent => 3
check do |doc|
errors = []
indents = doc.find_type(:ul).map do |e|
[doc.indent_for(doc.element_line(e)), doc.element_linenumber(e)]
end
curr_indent = indents[0][0] unless indents.empty?
indents.each do |indent, linenum|
if (indent > curr_indent) && (indent - curr_indent != @params[:indent])
errors << linenum
end
curr_indent = indent
end
errors
end
end
rule 'MD009', 'Trailing spaces' do
tags :whitespace
aliases 'no-trailing-spaces'
params :br_spaces => 2
check do |doc|
errors = doc.matching_lines(/\s$/)
if params[:br_spaces] > 1
errors -= doc.matching_lines(/\S\s{#{params[:br_spaces]}}$/)
end
errors
end
end
rule 'MD010', 'Hard tabs' do
tags :whitespace, :hard_tab
aliases 'no-hard-tabs'
params :ignore_code_blocks => false
check do |doc|
# Every line in the document that is part of a code block. Blank lines
# inside of a code block are acceptable.
codeblock_lines = doc.find_type_elements(:codeblock).map do |e|
(doc.element_linenumber(e)..
doc.element_linenumber(e) + e.value.lines.count).to_a
end.flatten
# Check for lines with hard tab
hard_tab_lines = doc.matching_lines(/\t/)
# Remove lines with hard tabs, if they stem from codeblock
hard_tab_lines -= codeblock_lines if params[:ignore_code_blocks]
hard_tab_lines
end
end
rule 'MD011', 'Reversed link syntax' do
tags :links
aliases 'no-reversed-links'
check do |doc|
doc.matching_text_element_lines(/\([^)]+\)\[[^\]]+\]/)
end
end
rule 'MD012', 'Multiple consecutive blank lines' do
tags :whitespace, :blank_lines
aliases 'no-multiple-blanks'
check do |doc|
# Every line in the document that is part of a code block. Blank lines
# inside of a code block are acceptable.
codeblock_lines = doc.find_type_elements(:codeblock).map do |e|
(doc.element_linenumber(e)..
doc.element_linenumber(e) + e.value.lines.count).to_a
end.flatten
blank_lines = doc.matching_lines(/^\s*$/)
cons_blank_lines = blank_lines.each_cons(2).select do |p, n|
n - p == 1
end.map { |_p, n| n }
cons_blank_lines - codeblock_lines
end
end
rule 'MD013', 'Line length' do
tags :line_length
aliases 'line-length'
params :line_length => 80, :ignore_code_blocks => false, :code_blocks => true,
:tables => true
check do |doc|
# Every line in the document that is part of a code block.
codeblock_lines = doc.find_type_elements(:codeblock).map do |e|
(doc.element_linenumber(e)..
doc.element_linenumber(e) + e.value.lines.count).to_a
end.flatten
# Every line in the document that is part of a table.
locations = doc.elements
.map { |e| [e.options[:location], e] }
.reject { |l, _| l.nil? }
table_lines = locations.map.with_index do |(l, e), i|
if e.type == :table
if i + 1 < locations.size
(l..locations[i + 1].first - 1).to_a
else
(l..doc.lines.count).to_a
end
end
end.flatten
overlines = doc.matching_lines(/^.{#{@params[:line_length]}}.*\s/)
if !params[:code_blocks] || params[:ignore_code_blocks]
overlines -= codeblock_lines
unless params[:code_blocks]
warn 'MD013 warning: Parameter :code_blocks is deprecated.'
warn ' Please replace \":code_blocks => false\" by '\
'\":ignore_code_blocks => true\" in your configuration.'
end
end
overlines -= table_lines unless params[:tables]
overlines
end
end
rule 'MD014', 'Dollar signs used before commands without showing output' do
tags :code
aliases 'commands-show-output'
check do |doc|
doc.find_type_elements(:codeblock).select do |e|
!e.value.empty? &&
!e.value.split(/\n+/).map { |l| l.match(/^\$\s/) }.include?(nil)
end.map { |e| doc.element_linenumber(e) }
end
end
rule 'MD018', 'No space after hash on atx style header' do
tags :headers, :atx, :spaces
aliases 'no-missing-space-atx'
check do |doc|
doc.find_type_elements(:header).select do |h|
doc.header_style(h) == :atx && doc.element_line(h).match(/^#+[^#\s]/)
end.map { |h| doc.element_linenumber(h) }
end
end
rule 'MD019', 'Multiple spaces after hash on atx style header' do
tags :headers, :atx, :spaces
aliases 'no-multiple-space-atx'
check do |doc|
doc.find_type_elements(:header).select do |h|
doc.header_style(h) == :atx && doc.element_line(h).match(/^#+\s\s/)
end.map { |h| doc.element_linenumber(h) }
end
end
rule 'MD020', 'No space inside hashes on closed atx style header' do
tags :headers, :atx_closed, :spaces
aliases 'no-missing-space-closed-atx'
check do |doc|
doc.find_type_elements(:header).select do |h|
doc.header_style(h) == :atx_closed \
&& (doc.element_line(h).match(/^#+[^#\s]/) \
|| doc.element_line(h).match(/[^#\s\\]#+$/))
end.map { |h| doc.element_linenumber(h) }
end
end
rule 'MD021', 'Multiple spaces inside hashes on closed atx style header' do
tags :headers, :atx_closed, :spaces
aliases 'no-multiple-space-closed-atx'
check do |doc|
doc.find_type_elements(:header).select do |h|
doc.header_style(h) == :atx_closed \
&& (doc.element_line(h).match(/^#+\s\s/) \
|| doc.element_line(h).match(/\s\s#+$/))
end.map { |h| doc.element_linenumber(h) }
end
end
rule 'MD022', 'Headers should be surrounded by blank lines' do
tags :headers, :blank_lines
aliases 'blanks-around-headers'
check do |doc|
errors = []
doc.find_type_elements(:header, false).each do |h|
header_bad = false
linenum = doc.element_linenumber(h)
# Check previous line
header_bad = true if (linenum > 1) && !doc.lines[linenum - 2].empty?
# Check next line
next_line_idx = doc.header_style(h) == :setext ? linenum + 1 : linenum
next_line = doc.lines[next_line_idx]
header_bad = true if !next_line.nil? && !next_line.empty?
errors << linenum if header_bad
end
# Kramdown requires that headers start on a block boundary, so in most
# cases it won't pick up a header without a blank line before it. We need
# to check regular text and pick out headers ourselves too
doc.find_type_elements(:p, false).each do |p|
linenum = doc.element_linenumber(p)
text = p.children.select { |e| e.type == :text }.map(&:value).join
lines = text.split("\n")
prev_lines = ['', '']
lines.each do |line|
# First look for ATX style headers without blank lines before
errors << linenum if line.match(/^\#{1,6}/) && !prev_lines[1].empty?
# Next, look for setext style
if line.match(/^(-+|=+)\s*$/) && !prev_lines[0].empty?
errors << (linenum - 1)
end
linenum += 1
prev_lines << line
prev_lines.shift
end
end
errors.sort
end
end
rule 'MD023', 'Headers must start at the beginning of the line' do
tags :headers, :spaces
aliases 'header-start-left'
check do |doc|
errors = []
# The only type of header with spaces actually parsed as such is setext
# style where only the text is indented. We check for that first.
doc.find_type_elements(:header, false).each do |h|
errors << doc.element_linenumber(h) if doc.element_line(h).match(/^\s/)
end
# Next we have to look for things that aren't parsed as headers because
# they start with spaces.
doc.find_type_elements(:p, false).each do |p|
linenum = doc.element_linenumber(p)
lines = doc.extract_text(p)
prev_line = ''
lines.each do |line|
# First look for ATX style headers
errors << linenum if line.match(/^\s+\#{1,6}/)
# Next, look for setext style
if line.match(/^\s+(-+|=+)\s*$/) && !prev_line.empty?
errors << (linenum - 1)
end
linenum += 1
prev_line = line
end
end
errors.sort
end
end
rule 'MD024', 'Multiple headers with the same content' do
tags :headers
aliases 'no-duplicate-header'
params :allow_different_nesting => false
check do |doc|
headers = doc.find_type(:header)
allow_different_nesting = params[:allow_different_nesting]
duplicates = headers.select do |h|
headers.any? do |e|
e[:location] < h[:location] &&
e[:raw_text] == h[:raw_text] &&
(allow_different_nesting == false || e[:level] != h[:level])
end
end.to_set
if allow_different_nesting
same_nesting_duplicates = Set.new
stack = []
current_level = 0
doc.find_type(:header).each do |header|
level = header[:level]
text = header[:raw_text]
if current_level > level
stack.pop
elsif current_level < level
stack.push([text])
elsif stack.last.include?(text)
same_nesting_duplicates.add(header)
end
current_level = level
end
duplicates += same_nesting_duplicates
end
duplicates.map { |h| doc.element_linenumber(h) }
end
end
rule 'MD025', 'Multiple top level headers in the same document' do
tags :headers
aliases 'single-h1'
params :level => 1
check do |doc|
headers = doc.find_type(:header, false).select do |h|
h[:level] == params[:level]
end
if !headers.empty? && (doc.element_linenumber(headers[0]) == 1)
headers[1..].map { |h| doc.element_linenumber(h) }
end
end
end
rule 'MD026', 'Trailing punctuation in header' do
tags :headers
aliases 'no-trailing-punctuation'
params :punctuation => '.,;:!?'
check do |doc|
doc.find_type(:header).select do |h|
h[:raw_text].match(/[#{params[:punctuation]}]$/)
end.map do |h|
doc.element_linenumber(h)
end
end
end
rule 'MD027', 'Multiple spaces after blockquote symbol' do
tags :blockquote, :whitespace, :indentation
aliases 'no-multiple-space-blockquote'
check do |doc|
errors = []
doc.find_type_elements(:blockquote).each do |e|
linenum = doc.element_linenumber(e)
lines = doc.extract_as_text(e)
# Handle first line specially as whitespace is stripped from the text
# element
errors << linenum if doc.element_line(e).match(/^\s*> /)
lines.each do |line|
errors << linenum if line.start_with?(' ')
linenum += 1
end
end
errors
end
end
rule 'MD028', 'Blank line inside blockquote' do
tags :blockquote, :whitespace
aliases 'no-blanks-blockquote'
check do |doc|
def check_blockquote(errors, elements)
prev = [nil, nil, nil]
elements.each do |e|
prev.shift
prev << e.type
if prev == %i{blockquote blank blockquote}
# The current location is the start of the second blockquote, so the
# line before will be a blank line in between the two, or at least the
# lowest blank line if there are more than one.
errors << (e.options[:location] - 1)
end
check_blockquote(errors, e.children)
end
end
errors = []
check_blockquote(errors, doc.elements)
errors
end
end
rule 'MD029', 'Ordered list item prefix' do
tags :ol
aliases 'ol-prefix'
# Style can be :one or :ordered
params :style => :one
check do |doc|
case params[:style]
when :ordered
doc.find_type_elements(:ol).map do |l|
doc.find_type_elements(:li, false, l.children)
.map.with_index do |i, idx|
unless doc.element_line(i).strip.start_with?("#{idx + 1}. ")
doc.element_linenumber(i)
end
end
end.flatten.compact
when :one
doc.find_type_elements(:ol).map do |l|
doc.find_type_elements(:li, false, l.children)
end.flatten.map do |i|
unless doc.element_line(i).strip.start_with?('1. ')
doc.element_linenumber(i)
end
end.compact
end
end
end
rule 'MD030', 'Spaces after list markers' do
tags :ol, :ul, :whitespace
aliases 'list-marker-space'
params :ul_single => 1, :ol_single => 1, :ul_multi => 1, :ol_multi => 1
check do |doc|
errors = []
doc.find_type_elements(%i{ul ol}).each do |l|
list_type = l.type.to_s
items = doc.find_type_elements(:li, false, l.children)
# The entire list is to use the multi-paragraph spacing rule if any of
# the items in it have multiple paragraphs/other block items.
srule = items.map { |i| i.children.length }.max > 1 ? 'multi' : 'single'
items.each do |i|
line = doc.element_line(i)
# See #278 - sometimes we think non-printable characters are list
# items even if they are not, so this ignore those and prevents
# us from crashing
next if line.empty?
actual_spaces = line.gsub(/^> /, '').match(/^\s*\S+(\s+)/)[1].length
required_spaces = params["#{list_type}_#{srule}".to_sym]
errors << doc.element_linenumber(i) if required_spaces != actual_spaces
end
end
errors
end
end
rule 'MD031', 'Fenced code blocks should be surrounded by blank lines' do
tags :code, :blank_lines
aliases 'blanks-around-fences'
check do |doc|
errors = []
# Some parsers (including kramdown) have trouble detecting fenced code
# blocks without surrounding whitespace, so examine the lines directly.
in_code = false
fence = nil
lines = [''] + doc.lines + ['']
lines.each_with_index do |line, linenum|
line.strip.match(/^(`{3,}|~{3,})/)
unless Regexp.last_match(1) &&
(
!in_code ||
(Regexp.last_match(1).slice(0, fence.length) == fence)
)
next
end
fence = in_code ? nil : Regexp.last_match(1)
in_code = !in_code
if (in_code && !lines[linenum - 1].empty?) ||
(!in_code && !lines[linenum + 1].empty?)
errors << linenum
end
end
errors
end
end
rule 'MD032', 'Lists should be surrounded by blank lines' do
tags :bullet, :ul, :ol, :blank_lines
aliases 'blanks-around-lists'
check do |doc|
errors = []
# Some parsers (including kramdown) have trouble detecting lists
# without surrounding whitespace, so examine the lines directly.
in_list = false
in_code = false
fence = nil
prev_line = ''
doc.lines.each_with_index do |line, linenum|
next if line.strip == '{:toc}'
unless in_code
list_marker = line.strip.match(/^([*+\-]|(\d+\.))\s/)
if list_marker && !in_list && !prev_line.match(/^($|\s)/)
errors << (linenum + 1)
elsif !list_marker && in_list && !line.match(/^($|\s)/)
errors << linenum
end
in_list = list_marker
end
line.strip.match(/^(`{3,}|~{3,})/)
if Regexp.last_match(1) && (
!in_code || (Regexp.last_match(1).slice(0, fence.length) == fence)
)
fence = in_code ? nil : Regexp.last_match(1)
in_code = !in_code
in_list = false
end
prev_line = line
end
errors.uniq
end
end
rule 'MD033', 'Inline HTML' do
tags :html
aliases 'no-inline-html'
params :allowed_elements => ''
check do |doc|
doc.element_linenumbers(doc.find_type(:html_element))
allowed = params[:allowed_elements].delete(" \t\r\n").downcase.split(',')
errors = doc.find_type_elements(:html_element).reject do |e|
allowed.include?(e.value)
end
doc.element_linenumbers(errors)
end
end
rule 'MD034', 'Bare URL used' do
tags :links, :url
aliases 'no-bare-urls'
check do |doc|
doc.matching_text_element_lines(%r{https?://})
end
end
rule 'MD035', 'Horizontal rule style' do
tags :hr
aliases 'hr-style'
params :style => :consistent
check do |doc|
hrs = doc.find_type(:hr)
if hrs.empty?
[]
else
doc_style = if params[:style] == :consistent
doc.element_line(hrs[0])
else
params[:style]
end
doc.element_linenumbers(
hrs.reject { |e| doc.element_line(e) == doc_style },
)
end
end
end
rule 'MD036', 'Emphasis used instead of a header' do
tags :headers, :emphasis
aliases 'no-emphasis-as-header'
params :punctuation => '.,;:!?'
check do |doc|
# We are looking for a paragraph consisting entirely of emphasized
# (italic/bold) text.
errors = []
doc.find_type_elements(:p, false).each do |p|
next if p.children.length > 1
next unless %i{em strong}.include?(p.children[0].type)
lines = doc.extract_text(p.children[0], '', false)
next if lines.length > 1
next if lines.empty?
next if lines[0].match(/[#{params[:punctuation]}]$/)
errors << doc.element_linenumber(p)
end
errors
end
end
rule 'MD037', 'Spaces inside emphasis markers' do
tags :whitespace, :emphasis
aliases 'no-space-in-emphasis'
check do |doc|
# Kramdown doesn't parse emphasis with spaces, which means we can just
# look for emphasis patterns inside regular text with spaces just inside
# them.
(doc.matching_text_element_lines(/\s(\*\*?|__?)\s.+\1/) | \
doc.matching_text_element_lines(/(\*\*?|__?).+\s\1\s/)).sort
end
end
rule 'MD038', 'Spaces inside code span elements' do
tags :whitespace, :code
aliases 'no-space-in-code'
check do |doc|
# We only want to check single line codespan elements and not fenced code
# block that happen to be parsed as code spans.
doc.element_linenumbers(
doc.find_type_elements(:codespan).select do |i|
i.value.match(/(^\s|\s$)/) && !i.value.include?("\n")
end,
)
end
end
rule 'MD039', 'Spaces inside link text' do
tags :whitespace, :links
aliases 'no-space-in-links'
check do |doc|
doc.element_linenumbers(
doc.find_type_elements(:a).reject { |e| e.children.empty? }.select do |e|
e.children.first.type == :text && e.children.last.type == :text && (
e.children.first.value.start_with?(' ') ||
e.children.last.value.end_with?(' '))
end,
)
end
end
rule 'MD040', 'Fenced code blocks should have a language specified' do
tags :code, :language
aliases 'fenced-code-language'
check do |doc|
# Kramdown parses code blocks with language settings as code blocks with
# the class attribute set to language-languagename.
doc.element_linenumbers(doc.find_type_elements(:codeblock).select do |i|
!i.attr['class'].to_s.start_with?('language-') &&
!doc.element_line(i).start_with?(' ')
end)
end
end
rule 'MD041', 'First line in file should be a top level header' do
tags :headers
aliases 'first-line-h1'
params :level => 1
check do |doc|
first_header = doc.find_type(:header).first
[1] if first_header.nil? || (first_header[:location] != 1) \
|| (first_header[:level] != params[:level])
end
end
rule 'MD046', 'Code block style' do
tags :code
aliases 'code-block-style'
params :style => :fenced
check do |doc|
style = @params[:style]
doc.element_linenumbers(
doc.find_type_elements(:codeblock).select do |i|
# for consistent we determine the first one
if style == :consistent
style = if doc.element_line(i).start_with?(' ')
:indented
else
:fenced
end
end
if style == :fenced
# if our parent is a list or a codeblock, we need to ignore
# its spaces, plus 4 more
parent = i.options[:parent]
ignored_spaces = 0
if parent
parent.options.delete(:children)
parent.options.delete(:parent)
if %i{li codeblock}.include?(parent.type)
linenum = doc.element_linenumbers([parent]).first
indent = doc.indent_for(doc.lines[linenum - 1])
ignored_spaces = indent + 4
end
end
start = ' ' * ignored_spaces
doc.element_line(i).start_with?("#{start} ")
else
!doc.element_line(i).start_with?(' ')
end
end,
)
end
end
rule 'MD047', 'File should end with a single newline character' do
tags :blank_lines
aliases 'single-trailing-newline'
check do |doc|
error_lines = []
last_line = doc.lines[-1]
error_lines.push(doc.lines.length) unless last_line.nil? || last_line.empty?
error_lines
end
end