Skip to content

Commit

Permalink
Fix to ignore hash in images
Browse files Browse the repository at this point in the history
Closes GH-60.

Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
kliput authored Mar 3, 2021
1 parent 6935702 commit 0bd3369
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
15 changes: 12 additions & 3 deletions lib/find/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -182,7 +182,7 @@ function find(ctx, next) {
}
}

function urlToPath(value, config) {
function urlToPath(value, config, type) {
var url
var questionMarkIndex
var numberSignIndex
Expand Down Expand Up @@ -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`.
Expand All @@ -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`.
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Absolute ![image reference][abs]

Relative with whitespace ![image reference][rel-whitespace]

Relative with heading ![image](./examples/image.jpg#metadata)

<!-- Invalid: -->

Relative ![missing image](./examples/missing.jpg)
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/query-params.md
Original file line number Diff line number Diff line change
@@ -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?).
12 changes: 6 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
''
Expand Down Expand Up @@ -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',
''
Expand Down

0 comments on commit 0bd3369

Please sign in to comment.