Skip to content

Commit

Permalink
Add support for checking images, image references
Browse files Browse the repository at this point in the history
Closes GH-29.
Closes GH-30.

Reviewed-by: Rob Hilgefort <rob.hilgefort.me>
Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
  • Loading branch information
wooorm authored Jan 9, 2019
1 parent 28f545c commit 6920834
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,26 +270,26 @@ function gatherReferences(file, tree, info, fileSet) {

lines = info && info.type in lineLinks ? lineLinks[info.type] : false

visit(tree, ['link', 'linkReference'], onlink)
visit(tree, ['link', 'image', 'linkReference', 'imageReference'], onresource)

return cache

// Handle new links.
function onlink(node) {
// Handle resources.
function onresource(node) {
var link = node.url
var definition
var index
var uri
var pathname
var hash

// Handle link-references.
// Handle references.
if (node.identifier) {
definition = getDefinition(node.identifier)
link = definition && definition.url
}

// Ignore definitions without link.
// Ignore definitions without url.
if (!link) {
return
}
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]

[**remark**][remark] plugin to validate if internal links to headings and files
in markdown point to existing things.
[**remark**][remark] plugin to validate if internal links or images to headings
and files in markdown point to existing things.
It does not check external URLs (see [`remark-lint-no-dead-urls`][no-dead-urls]
for that).

Expand All @@ -20,7 +20,7 @@ In addition, when I link to a heading in another document
(`examples/foo.md#hello`), if this file exists but the heading does not,
or if the file does not exist, this plugin will also warn.

Linking to other files, such as `LICENSE` or `index.js` (when they exist)
Linking to other files, such as `license` or `index.js` (when they exist)
is fine.

## Table of Contents
Expand Down
Binary file added test/fixtures/examples/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions test/fixtures/images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Image references

<!-- Valid: -->

Relative ![image](./examples/image.jpg)

No `./` in ![image](examples/image.jpg)

Absolute ![image](https://github.com/wooorm/test/blob/master/examples/image.jpg)

Relative ![image reference][rel]

Absolute ![image reference][abs]

<!-- Invalid: -->

Relative ![missing image](./examples/missing.jpg)

No `./` in ![image](examples/missing.jpg)

Absolute ![missing image](https://github.com/wooorm/test/blob/master/examples/missing.jpg)

Relative ![missing image reference][rel-missing]

Absolute ![missing image reference][abs-missing]

<!-- Definitions: -->

[rel]: ./examples/image.jpg

[abs]: https://github.com/wooorm/test/blob/master/examples/image.jpg

[rel-missing]: ./examples/missing.jpg

[abs-missing]: https://github.com/wooorm/test/blob/master/examples/missing.jpg
29 changes: 29 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,5 +495,34 @@ test('remark-validate-links', function(t) {
}, st.error)
})

t.test('should check images', function(st) {
st.plan(1)

execa(bin, [
'--no-config',
'--no-ignore',
'--use',
'../..=repository:"wooorm/test"',
'--use',
'../sort',
'images.md'
]).then(function(result) {
st.equal(
strip(result.stderr),
[
'images.md',
' 17:10-17:50 warning Link to unknown file: `examples/missing.jpg` missing-file remark-validate-links',
' 19:12-19:42 warning Link to unknown file: `examples/missing.jpg` missing-file remark-validate-links',
' 21:10-21:91 warning Link to unknown file: `examples/missing.jpg` missing-file remark-validate-links',
' 23:10-23:49 warning Link to unknown file: `examples/missing.jpg` missing-file remark-validate-links',
' 25:10-25:49 warning Link to unknown file: `examples/missing.jpg` missing-file remark-validate-links',
'',
'⚠ 5 warnings'
].join('\n'),
'should report'
)
}, st.error)
})

t.end()
})

0 comments on commit 6920834

Please sign in to comment.