diff --git a/lib/find/find.js b/lib/find/find.js index 66db6df..6ad830c 100644 --- a/lib/find/find.js +++ b/lib/find/find.js @@ -60,7 +60,7 @@ function find(ctx, next) { var data = node.data || {} var props = data.hProperties || {} var id = props.name || props.id || data.id - var info = node.url ? urlToPath(node.url, config) : null + var info = node.url ? urlToPath(node.url, config, node.type) : null var fp var hash @@ -182,7 +182,7 @@ function find(ctx, next) { } } -function urlToPath(value, config) { +function urlToPath(value, config, type) { var url var questionMarkIndex var numberSignIndex @@ -222,7 +222,10 @@ function urlToPath(value, config) { // Currently, we’re ignoring this and just not supporting branches. value = value.split(slash).slice(1).join(slash) - return normalize(path.resolve(config.root, value + url.hash), config) + return normalize( + path.resolve(config.root, value + (type === 'image' ? '' : url.hash)), + config + ) } // Remove the search: `?foo=bar`. @@ -237,6 +240,12 @@ function urlToPath(value, config) { value = value.slice(0, questionMarkIndex) + (numberSignIndex === -1 ? '' : value.slice(numberSignIndex)) + numberSignIndex = value.indexOf(numberSign) + } + + // Ignore "headings" in image links: `image.png#metadata` + if (numberSignIndex !== -1 && type === 'image') { + value = value.slice(0, numberSignIndex) } // Local: `#heading`. diff --git a/test/fixtures/images.md b/test/fixtures/images.md index 848011a..2b1b17b 100644 --- a/test/fixtures/images.md +++ b/test/fixtures/images.md @@ -14,6 +14,8 @@ Absolute ![image reference][abs] Relative with whitespace ![image reference][rel-whitespace] +Relative with heading ![image](./examples/image.jpg#metadata) + Relative ![missing image](./examples/missing.jpg) diff --git a/test/fixtures/query-params.md b/test/fixtures/query-params.md index eb5027e..ca08dcb 100644 --- a/test/fixtures/query-params.md +++ b/test/fixtures/query-params.md @@ -1,9 +1,11 @@ # Query params -Link to relative heading ![link](?foo=bar#query-params) +Link to relative heading [link](?foo=bar#query-params) Link to an ![image](./examples/image.jpg?foo=bar) +Link to an ![image](./examples/image.jpg?foo=bar#client-params) with hash + And a file [link](./examples/github.md?foo=bar). Question mark in hash (invalid) [link](#query-params?). diff --git a/test/index.js b/test/index.js index 1ce0ad8..41ba923 100644 --- a/test/index.js +++ b/test/index.js @@ -1102,11 +1102,11 @@ test('remark-validate-links', function (t) { null, [ 'images.md', - ' 19:10-19:50 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', - ' 21:12-21:42 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', - ' 23:10-23:89 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', - ' 35:1-35:38 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', - ' 37:1-37:77 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', + ' 21:10-21:50 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', + ' 23:12-23:42 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', + ' 25:10-25:89 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', + ' 37:1-37:38 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', + ' 39:1-39:77 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links', '', '⚠ 5 warnings', '' @@ -1141,7 +1141,7 @@ test('remark-validate-links', function (t) { null, [ 'query-params.md', - ' 9:33-9:55 warning Link to unknown heading: `query-params?`. Did you mean `query-params` missing-heading remark-validate-links', + ' 11:33-11:55 warning Link to unknown heading: `query-params?`. Did you mean `query-params` missing-heading remark-validate-links', '', '⚠ 1 warning', ''