diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fac916d..32d48e50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/default.yml b/config/default.yml index 3c32609e..1d428f98 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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' diff --git a/lib/overcommit/hook/pre_commit/mdl.rb b/lib/overcommit/hook/pre_commit/mdl.rb index 17eccc78..4759936e 100644 --- a/lib/overcommit/hook/pre_commit/mdl.rb +++ b/lib/overcommit/hook/pre_commit/mdl.rb @@ -5,8 +5,6 @@ module Overcommit::Hook::PreCommit # # @see https://github.com/mivok/markdownlint class Mdl < Base - MESSAGE_REGEX = /^(?(?:\w:)?[^:]+):(?\d+):\s(?.+)/ - def run result = execute(command, args: applicable_files) output = result.stdout.chomp @@ -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 diff --git a/spec/overcommit/hook/pre_commit/mdl_spec.rb b/spec/overcommit/hook/pre_commit/mdl_spec.rb index dc2aa6cd..e84b52cc 100644 --- a/spec/overcommit/hook/pre_commit/mdl_spec.rb +++ b/spec/overcommit/hook/pre_commit/mdl_spec.rb @@ -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 }