Skip to content

Commit

Permalink
fix: fix broken link behavrior with files in subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Dec 9, 2020
1 parent b5fb88a commit 6c14db9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rules/file-no-broken-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function fileNoBrokenLinks(fs, options) {
const results = await Promise.all(
files.map(async f => {
// render it, if possible
const absMdPath = path.resolve(fs.targetDir, f)
const absMdPath = path.posix.resolve(fs.targetDir, f)
const rendered = await GitHubMarkup.renderMarkup(absMdPath)
if (rendered === null) {
return {
Expand All @@ -60,7 +60,7 @@ async function fileNoBrokenLinks(fs, options) {
)
await htmlChecker.scan(
rendered,
new URL(`file://${path.posix.join(fs.targetDir, '/')}`)
new URL(`file://${path.posix.join(fs.targetDir, f)}`)
)
// find all relative links, and double check the filesystem for their existence
// filter down to broken links
Expand Down
30 changes: 30 additions & 0 deletions tests/rules/file_no_broken_links_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,36 @@ describe('rule', () => {
})
})

it('returns true with link to a file outside a subdirectory markdown', async () => {
const ruleopts = {
globsAll: ['subdirectory/nested_relative_link.md']
}

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({
passed: true,
path: 'subdirectory/nested_relative_link.md'
})
})

it('returns false with an invalid link to a file outside a subdirectory markdown', async () => {
const ruleopts = {
globsAll: ['subdirectory/invalid_nested_relative_link.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: 'subdirectory/invalid_nested_relative_link.md'
})
})

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']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[my file](../notafile)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[my file](../link.md)

0 comments on commit 6c14db9

Please sign in to comment.