Skip to content

Commit

Permalink
feat: scan all href, src, or other valid url attrs (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Feb 13, 2019
1 parent 629b83c commit ddf1fa4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cli.ts
Expand Up @@ -3,7 +3,7 @@
import * as meow from 'meow';
import * as updateNotifier from 'update-notifier';
import chalk from 'chalk';
import {LinkChecker, LinkState, LinkResult, CheckOptions, check} from './index';
import {LinkChecker, LinkState, LinkResult, CheckOptions} from './index';

const pkg = require('../../package.json');
updateNotifier({pkg}).notify();
Expand Down
37 changes: 31 additions & 6 deletions src/links.ts
Expand Up @@ -2,14 +2,39 @@ import * as cheerio from 'cheerio';
import isAbsoluteUrl = require('is-absolute-url');
import {URL} from 'url';

const linksAttr = {
background: ['body'],
cite: ['blockquote', 'del', 'ins', 'q'],
data: ['object'],
href: ['a', 'area', 'embed', 'link'],
icon: ['command'],
longdesc: ['frame', 'iframe'],
manifest: ['html'],
poster: ['video'],
pluginspage: ['embed'],
pluginurl: ['embed'],
src: [
'audio', 'embed', 'frame', 'iframe', 'img', 'input', 'script', 'source',
'track', 'video'
],
} as {[index: string]: string[]};

export function getLinks(source: string, baseUrl: string) {
const $ = cheerio.load(source);
const links = $('a').toArray().map(e => e.attribs.href).filter(x => !!x);
const sanitized = links.map(link => {
const slink =
isAbsoluteUrl(link) ? new URL(link) : (new URL(link, baseUrl));
slink.hash = '';
return slink.href;
const links = new Array<string>();
Object.keys(linksAttr).forEach(attr => {
const elements = linksAttr[attr].map(tag => `${tag}[${attr}]`).join(',');
$(elements).each((i, element) => {
links.push(element.attribs[attr]);
});
});
const sanitized = links.filter(link => !!link)
.map(link => normalizeLink(link, baseUrl).href);
return sanitized;
}

function normalizeLink(link: string, baseUrl: string): URL {
const slink = isAbsoluteUrl(link) ? new URL(link) : (new URL(link, baseUrl));
slink.hash = '';
return slink;
}
Binary file added test/fixtures/image/boo.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/fixtures/image/index.html
@@ -0,0 +1,6 @@
<html>
<body>
<img src="missing.png">
<img src="boo.jpg">
</body>
</html>
8 changes: 8 additions & 0 deletions test/test.ts
Expand Up @@ -67,4 +67,12 @@ describe('linkinator', () => {
assert.strictEqual(
results.links.filter(x => x.state === LinkState.SKIPPED).length, 1);
});

it('should detect broken image links', async () => {
const results = await check({path: 'test/fixtures/image'});
assert.strictEqual(
results.links.filter(x => x.state === LinkState.BROKEN).length, 1);
assert.strictEqual(
results.links.filter(x => x.state === LinkState.OK).length, 2);
});
});

0 comments on commit ddf1fa4

Please sign in to comment.