mirror of
				https://github.com/theoludwig/markdownlint-rule-relative-links.git
				synced 2025-11-01 00:49:07 +01:00 
			
		
		
		
	feat: validate link in image src
This commit is contained in:
		
							
								
								
									
										37
									
								
								src/index.js
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								src/index.js
									
									
									
									
									
								
							@@ -56,23 +56,38 @@ const customRule = {
 | 
			
		||||
    filterTokens(params, 'inline', (token) => {
 | 
			
		||||
      token.children.forEach((child) => {
 | 
			
		||||
        const { lineNumber, type, attrs } = child
 | 
			
		||||
 | 
			
		||||
        /** @type {string | null} */
 | 
			
		||||
        let hrefSrc = null
 | 
			
		||||
 | 
			
		||||
        if (type === 'link_open') {
 | 
			
		||||
          attrs.forEach((attr) => {
 | 
			
		||||
            if (attr[0] === 'href') {
 | 
			
		||||
              const href = attr[1]
 | 
			
		||||
              const url = new URL(href, pathToFileURL(params.name))
 | 
			
		||||
              url.hash = ''
 | 
			
		||||
              const isRelative =
 | 
			
		||||
                href.startsWith('./') ||
 | 
			
		||||
                href.startsWith('../') ||
 | 
			
		||||
                !EXTERNAL_PROTOCOLS.has(url.protocol)
 | 
			
		||||
              if (isRelative && !fs.existsSync(url.pathname)) {
 | 
			
		||||
                const detail = `Link "${href}" is dead`
 | 
			
		||||
                addError(onError, lineNumber, detail)
 | 
			
		||||
              }
 | 
			
		||||
              hrefSrc = attr[1]
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (type === 'image') {
 | 
			
		||||
          attrs.forEach((attr) => {
 | 
			
		||||
            if (attr[0] === 'src') {
 | 
			
		||||
              hrefSrc = attr[1]
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (hrefSrc != null) {
 | 
			
		||||
          const url = new URL(hrefSrc, pathToFileURL(params.name))
 | 
			
		||||
          url.hash = ''
 | 
			
		||||
          const isRelative =
 | 
			
		||||
            hrefSrc.startsWith('./') ||
 | 
			
		||||
            hrefSrc.startsWith('../') ||
 | 
			
		||||
            !EXTERNAL_PROTOCOLS.has(url.protocol)
 | 
			
		||||
          if (isRelative && !fs.existsSync(url.pathname)) {
 | 
			
		||||
            const detail = `Link "${hrefSrc}" is dead`
 | 
			
		||||
            addError(onError, lineNumber, detail)
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -12,13 +12,23 @@ tap.test('ensure we validate correctly', async (t) => {
 | 
			
		||||
    customRules: [relativeLinks]
 | 
			
		||||
  })
 | 
			
		||||
  t.equal(lintResults['test/fixtures/Valid.md'].length, 0)
 | 
			
		||||
  t.equal(lintResults['test/fixtures/Invalid.md'].length, 1)
 | 
			
		||||
  t.equal(lintResults['test/fixtures/Invalid.md'].length, 2)
 | 
			
		||||
 | 
			
		||||
  t.equal(
 | 
			
		||||
    lintResults['test/fixtures/Invalid.md'][0].ruleDescription,
 | 
			
		||||
    lintResults['test/fixtures/Invalid.md'][0]?.ruleDescription,
 | 
			
		||||
    'Relative links should be valid'
 | 
			
		||||
  )
 | 
			
		||||
  t.equal(
 | 
			
		||||
    lintResults['test/fixtures/Invalid.md'][0].errorDetail,
 | 
			
		||||
    lintResults['test/fixtures/Invalid.md'][0]?.errorDetail,
 | 
			
		||||
    'Link "./basic.test.js" is dead'
 | 
			
		||||
  )
 | 
			
		||||
 | 
			
		||||
  t.equal(
 | 
			
		||||
    lintResults['test/fixtures/Invalid.md'][1]?.ruleDescription,
 | 
			
		||||
    'Relative links should be valid'
 | 
			
		||||
  )
 | 
			
		||||
  t.equal(
 | 
			
		||||
    lintResults['test/fixtures/Invalid.md'][1]?.errorDetail,
 | 
			
		||||
    'Link "../image.png" is dead'
 | 
			
		||||
  )
 | 
			
		||||
})
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								test/fixtures/Invalid.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								test/fixtures/Invalid.md
									
									
									
									
										vendored
									
									
								
							@@ -1,3 +1,5 @@
 | 
			
		||||
# Invalid
 | 
			
		||||
 | 
			
		||||
[basic.js](./basic.test.js)
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								test/fixtures/Valid.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								test/fixtures/Valid.md
									
									
									
									
										vendored
									
									
								
							@@ -1,3 +1,5 @@
 | 
			
		||||
# Valid
 | 
			
		||||
 | 
			
		||||
[basic.js](../basic.test.js)
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								test/fixtures/image.png
									
									
									
									
										vendored
									
									
										Executable file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								test/fixtures/image.png
									
									
									
									
										vendored
									
									
										Executable file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 95 B  | 
		Reference in New Issue
	
	Block a user