Skip to content

Commit

Permalink
Ignore hash (headings) in links to images even in references
Browse files Browse the repository at this point in the history
  • Loading branch information
kliput committed Aug 7, 2023
1 parent 1a7f038 commit b9d4ffe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
43 changes: 42 additions & 1 deletion lib/find/find-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ const lineExpression = /^#l\d/i
const readmeExtensions = new Set(['.markdown', '.mdown', '.mkdn', '.md'])
const readmeBasename = /^readme$/i

const imageExtensions = Object.freeze([
'apng',
'bmp',
'gif',
'ico',
'jp2',
'jpeg',
'jpg',
'png',
'svg',
'tiff',
'webp',
'xbm',
]);

const imagePathRegexp = new RegExp(
`^(.*\/)?[^#]+\.(${imageExtensions.join('|')})(#.*|$)`
);

/**
* @param {{tree: Root, file: VFile, fileSet?: FileSet, options: Options}} ctx
*/
Expand Down Expand Up @@ -217,6 +236,11 @@ export async function findReferences(ctx) {
*/
// eslint-disable-next-line complexity
function urlToPath(value, config, type) {
// Ignore "headings" in image links: `image.png#metadata`
if (isImagePath(value)) {
value = stripHash(value);
}

// Absolute paths: `/wooorm/test/blob/main/directory/example.md`.
if (value.charAt(0) === slash) {
if (!config.urlConfig.hostname) {
Expand Down Expand Up @@ -277,7 +301,8 @@ function urlToPath(value, config, type) {
numberSignIndex = value.indexOf(numberSign)
}

// Ignore "headings" in image links: `image.png#metadata`
// Ignore "headings" in all image links: `image.png#metadata` (even if code removing
// hash from value didn't do that)
if (numberSignIndex !== -1 && type === 'image') {
value = value.slice(0, numberSignIndex)
}
Expand Down Expand Up @@ -347,3 +372,19 @@ function readme(filePath) {
readmeBasename.test(path.basename(filePath, ext))
)
}

/**
*
* @param {string} path
* @returns {boolean}
*/
function isImagePath(path) {
return imagePathRegexp.test(path);
}

/**
* @param {string} path
*/
function stripHash(path) {
return path.replace(/^(.*?)(#.*)$/, '$1');
}
4 changes: 4 additions & 0 deletions test/fixtures/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Relative with whitespace ![image reference][rel-whitespace]

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

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

<!-- Invalid: -->

Relative ![missing image](./examples/missing.jpg)
Expand All @@ -39,3 +41,5 @@ Absolute ![missing image reference][abs-missing]
[abs-missing]: https://github.com/wooorm/test/blob/main/examples/missing.jpg

[rel-whitespace]: "./examples/image with space.jpg"

[rel-heading]: ./examples/image.jpg#metadata
10 changes: 5 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,11 +886,11 @@ test('remark-validate-links', async (t) => {
strip(stderr),
[
'images.md',
' 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',
' 23:10-23:50 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links',
' 25:12-25:42 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links',
' 27:10-27:89 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links',
' 39:1-39:38 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links',
' 41:1-41:77 warning Link to unknown file: `examples/missing.jpg`. Did you mean `examples/image.jpg` missing-file remark-validate-links',
'',
'⚠ 5 warnings',
''
Expand Down

0 comments on commit b9d4ffe

Please sign in to comment.