Skip to content

Commit f68bd91

Browse files
committed
MarkdownBear: Add validate-links plugin
Add `validate-links` plugin and since the `validate-links` plugin is incompatible with stdin input, `use_stdin=True` was removed. Closes #924
1 parent c9be1bf commit f68bd91

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

bears/markdown/MarkdownBear.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99

1010
@linter(executable='remark',
11-
use_stdin=True,
1211
use_stdout=True,
1312
use_stderr=True)
1413
class MarkdownBear:
@@ -21,7 +20,8 @@ class MarkdownBear:
2120

2221
LANGUAGES = {'Markdown'}
2322
REQUIREMENTS = {NpmRequirement('remark-cli', '2'),
24-
NpmRequirement('remark-lint', '5')}
23+
NpmRequirement('remark-lint', '5'),
24+
NpmRequirement('remark-validate-links', '5')}
2525
AUTHORS = {'The coala developers'}
2626
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
2727
LICENSE = 'AGPL-3.0'
@@ -65,7 +65,8 @@ def create_arguments(filename, file, config_file,
6565
horizontal_rule: str='*',
6666
horizontal_rule_spaces: bool=False,
6767
horizontal_rule_repeat: int=3,
68-
max_line_length: int=None):
68+
max_line_length: int=None,
69+
check_links: bool=False):
6970
"""
7071
:param bullets:
7172
Character to use for bullets in lists. Can be "-", "*" or "+".
@@ -108,6 +109,8 @@ def create_arguments(filename, file, config_file,
108109
The number of times the horizontal rule character will be repeated.
109110
:param max_line_length:
110111
The maximum line length allowed.
112+
:param check_links:
113+
Checks if links to headings and files in markdown are valid.
111114
"""
112115
remark_configs_settings = {
113116
'bullet': bullets, # - or *
@@ -136,13 +139,16 @@ def create_arguments(filename, file, config_file,
136139
# Remove { and } as remark adds them on its own
137140
settings = config_json[1:-1]
138141

139-
args = ['--no-color', '--quiet', '--setting', settings]
142+
args = [filename, '--no-color', '--quiet', '--setting', settings]
140143

141144
if remark_configs_plugins:
142145
config_json = json.dumps(remark_configs_plugins)
143146
plugins = 'lint=' + config_json[1:-1]
144147
args += ['--use', plugins]
145148

149+
if check_links:
150+
args += ['--use', 'validate-links']
151+
146152
return args
147153

148154
def process_output(self, output, filename, file):

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"ramllint": ">=1.2.2 <1.2.4 || >=1.2.5 <1.3.0",
2020
"remark-cli": "~2",
2121
"remark-lint": "~5",
22+
"remark-validate-links": "~5",
2223
"standard": "~7",
2324
"stylelint": "~7",
2425
"stylint": "~1.5.9",

tests/markdown/MarkdownBearTest.py

+28
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
2. nopqrstuvwxyz
2323
"""
2424

25+
test_file4 = """# Hello
26+
27+
Read more [This link does not exist](#world).
28+
"""
29+
30+
test_file5 = """# world
31+
32+
Read more [This link exists](#world).
33+
"""
34+
2535
MarkdownBearTest = verify_local_bear(MarkdownBear,
2636
valid_files=(test_file2,),
2737
invalid_files=(test_file1,))
@@ -55,3 +65,21 @@ def test_invalid_message(self):
5565
'Line must be at most 10 characters'
5666
' maximum-line-length remark-lint')
5767
self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
68+
69+
def test_invalid_link(self):
70+
content = test_file4.splitlines()
71+
self.section.append(Setting('check_links', True))
72+
with prepare_file(content, None) as (file, fname):
73+
with execute_bear(self.uut, fname, file) as results:
74+
self.assertEqual(results[0].message,
75+
'Link to unknown heading: `world`'
76+
' remark-validate-links '
77+
'remark-validate-links')
78+
self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)
79+
80+
def test_valid_link(self):
81+
content = test_file5.splitlines()
82+
self.section.append(Setting('check_links', True))
83+
with prepare_file(content, None) as (file, fname):
84+
with execute_bear(self.uut, fname, file) as results:
85+
self.assertEqual(results, [])

0 commit comments

Comments
 (0)