Skip to content

Commit

Permalink
MarkdownBear: Add validate-links plugin
Browse files Browse the repository at this point in the history
Closes coala#924
  • Loading branch information
yash-nisar committed Jul 14, 2017
1 parent 404db94 commit 65183c5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
20 changes: 11 additions & 9 deletions bears/markdown/MarkdownBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ class MarkdownBear:

LANGUAGES = {'Markdown'}
REQUIREMENTS = {NpmRequirement('remark-cli', '2'),
NpmRequirement('remark-lint', '5')}
NpmRequirement('remark-lint', '5'),
NpmRequirement('remark-validate-links', '5')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_FIX = {'Formatting'}

_output_regex = re.compile(
r'\s*(?P<line>\d+):(?P<column>\d+)'
r'\s*(?P<severity>warning)\s*(?P<message>.*)')
r'\s*(?P<severity>warning)\s*(?P<message>.+?)(?: .*|\n|$)')

@staticmethod
@deprecate_settings(bullets='markdown_bullets',
Expand Down Expand Up @@ -127,22 +128,23 @@ def create_arguments(filename, file, config_file,
'ruleSpaces': horizontal_rule_spaces, # Bool
'ruleRepetition': horizontal_rule_repeat, # int
}
remark_configs_plugins = {}
remark_lint_configs = {}

if max_line_length:
remark_configs_plugins['maximumLineLength'] = max_line_length
remark_lint_configs['maximumLineLength'] = max_line_length

config_json = json.dumps(remark_configs_settings)
# Remove { and } as remark adds them on its own
settings = config_json[1:-1]

args = ['--no-color', '--quiet', '--setting', settings]
args = [filename, '--no-color', '--quiet', '--setting', settings]

if remark_configs_plugins:
config_json = json.dumps(remark_configs_plugins)
plugins = 'lint=' + config_json[1:-1]
args += ['--use', plugins]
if remark_lint_configs:
config_json = json.dumps(remark_lint_configs)
lint = 'lint=' + config_json[1:-1]
args += ['--use', lint]

args += ['--use', 'validate-links']
return args

def process_output(self, output, filename, file):
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"ramllint": ">=1.2.2 <1.2.4 || >=1.2.5 <1.3.0",
"remark-cli": "~2",
"remark-lint": "~5",
"remark-validate-links": "~5",
"standard": "~7",
"stylelint": "~7",
"stylint": "~1.5.9",
Expand Down
27 changes: 25 additions & 2 deletions tests/markdown/MarkdownBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
2. nopqrstuvwxyz
"""

test_file4 = """# Hello
Read more [This link does not exist](#world).
"""

test_file5 = """# world
Read more [This link exists](#world).
"""

MarkdownBearTest = verify_local_bear(MarkdownBear,
valid_files=(test_file2,),
invalid_files=(test_file1,))
Expand Down Expand Up @@ -52,6 +62,19 @@ def test_invalid_message(self):
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[0].message,
'Line must be at most 10 characters'
' maximum-line-length remark-lint')
'Line must be at most 10 characters')
self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)

def test_invalid_link(self):
content = test_file4.splitlines()
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[0].message,
'Link to unknown heading: `world`')
self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)

def test_valid_link(self):
content = test_file5.splitlines()
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results, [])

0 comments on commit 65183c5

Please sign in to comment.