Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Relax `childprocess` dependency to allow version 1.x
* Add `CodeSpellCheck` pre-commit hook
* Fix deadlock which was more likely to occur when setting `parallelize` on a hook to `false`
* Fix `Mdl` hook to use JSON output and not fail on unexpected output

## 0.48.1

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ PreCommit:
enabled: false
description: 'Analyze markdown files with mdl'
required_executable: 'mdl'
flags: ['--json']
install_command: 'gem install mdl'
include: '**/*.md'

Expand Down
18 changes: 11 additions & 7 deletions lib/overcommit/hook/pre_commit/mdl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Overcommit::Hook::PreCommit
#
# @see https://github.com/mivok/markdownlint
class Mdl < Base
MESSAGE_REGEX = /^(?<file>(?:\w:)?[^:]+):(?<line>\d+):\s(?<msg>.+)/

def run
result = execute(command, args: applicable_files)
output = result.stdout.chomp
Expand All @@ -15,11 +13,17 @@ def run
return [:fail, result.stderr] unless result.stderr.empty?

# example message:
# path/to/file.md:1: MD001 Error message
extract_messages(
output.split("\n"),
MESSAGE_REGEX
)
# [{"filename":"file1.md","line":1,"rule":"MD013","aliases":["line-length"],
# "description":"Line length"}]
json_messages = JSON.parse(output)
json_messages.map do |message|
Overcommit::Hook::Message.new(
:error,
message[:filename],
message[:line],
message[:description]
)
end
end
end
end
5 changes: 4 additions & 1 deletion spec/overcommit/hook/pre_commit/mdl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
let(:success) { false }

context 'and it reports an error' do
let(:stdout) { 'file1.md:1: MD013 Line length' }
let(:stdout) do
'[{"filename":"file1.md","line":1,"rule":"MD013","aliases":["line-length"],'\
'"description":"Line length"}]'
end
let(:stderr) { '' }

it { should fail_hook }
Expand Down