Skip to content

Commit

Permalink
feat(no-broken-links): add option to pass or not pass in external links
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Dec 8, 2020
1 parent 68ce90f commit aaa92f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion rules/file-no-broken-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ async function fileNoBrokenLinks(fs, options) {
const filePath = path.posix.join('/', url.host, url.pathname)
const absPath = path.posix.resolve(targetDir, filePath)
const relPath = path.posix.relative(targetDir, absPath)
if (relPath.startsWith('..')) return null
if (relPath.startsWith('..')) {
if (options['pass-external-relative-links']) return null
else return `${originalURL} (relative link outside project)`
}
// verify the file exists (or at least that we have access to it)
if (!(await fs.relativeFileExists(relPath)))
return `${originalURL} (file does not exist)`
Expand Down
18 changes: 17 additions & 1 deletion tests/rules/file_no_broken_links_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,29 @@ describe('rule', () => {
})
})

it('returns true with a relative link to a file in markdown outside the working directory', async () => {
it('returns false with a relative link to a file in markdown outside the working directory', async () => {
const ruleopts = {
globsAll: ['relative_link_outside_dir.md']
}

const actual = await fileNoBrokenLinks(testFs, ruleopts)

expect(actual.passed).to.equal(false)
expect(actual.targets).to.have.length(1)
expect(actual.targets[0]).to.deep.include({
passed: false,
path: 'relative_link_outside_dir.md'
})
})

it('returns true with a relative link to a file in markdown outside the working directory and pass-external-relative-links', async () => {
const ruleopts = {
globsAll: ['relative_link_outside_dir.md'],
'pass-external-relative-links': true
}

const actual = await fileNoBrokenLinks(testFs, ruleopts)

expect(actual.passed).to.equal(true)
expect(actual.targets).to.have.length(1)
expect(actual.targets[0]).to.deep.include({
Expand Down

0 comments on commit aaa92f8

Please sign in to comment.