Class: Mdlint::Linter::Rules::HeadingStyle
- Inherits:
-
Mdlint::Linter::Rule
- Object
- Mdlint::Linter::Rule
- Mdlint::Linter::Rules::HeadingStyle
- Defined in:
- lib/mdlint/linter/rules/heading_style.rb,
sig/internal.rbs
Instance Attribute Summary
Attributes inherited from Mdlint::Linter::Rule
Instance Method Summary collapse
Methods inherited from Mdlint::Linter::Rule
#add_violation, inherited, #initialize
Constructor Details
This class inherits a constructor from Mdlint::Linter::Rule
Instance Method Details
#check(tokens, _source) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/mdlint/linter/rules/heading_style.rb', line 11 def check(tokens, _source) tokens.each do |token| next unless token.type == :heading_open if token.markup && !token.markup.start_with?("#") add_violation( message: "Setext-style heading should be ATX-style", line: (token.map&.first || 0) + 1, fixable: true ) end end @violations end |
#fix(tokens, source) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mdlint/linter/rules/heading_style.rb', line 26 def fix(tokens, source) lines = source.lines heading_tokens = tokens.select do |token| token.type == :heading_open && token.markup && !token.markup.start_with?("#") end heading_tokens.reverse_each do |token| start_line, end_line = token.map || [] next unless start_line && end_line && end_line == start_line + 2 next unless lines[start_line] && lines[start_line + 1] match = lines[start_line].match(/\A(\s*(?:>\s*)*)(.*?)(?:\r?\n)?\z/) next unless match prefix = match[1].to_s content = match[2].to_s newline = lines[start_line].end_with?("\r\n") ? "\r\n" : "\n" level = token.tag == "h1" ? "#" : "##" lines[start_line] = "#{prefix}#{level} #{content.strip}#{newline}" lines.delete_at(start_line + 1) end lines.compact.join end |