Skip to content

Commit a8c0a43

Browse files
authoredDec 24, 2020
feat: scan opengraph and twittercard style meta links (#217)
1 parent 9eb5590 commit a8c0a43

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed
 

‎src/links.ts

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const linksAttr = {
99
icon: ['command'],
1010
longdesc: ['frame', 'iframe'],
1111
manifest: ['html'],
12+
content: ['meta'],
1213
poster: ['video'],
1314
pluginspage: ['embed'],
1415
pluginurl: ['embed'],
@@ -56,6 +57,17 @@ export function getLinks(source: string, baseUrl: string): ParsedUrl[] {
5657
) {
5758
return;
5859
}
60+
61+
// Only for <meta content=""> tags, only validate the url if
62+
// the content actually looks like a url
63+
if (element.tagName === 'meta' && element.attribs['content']) {
64+
try {
65+
new URL(element.attribs['content']);
66+
} catch (e) {
67+
return;
68+
}
69+
}
70+
5971
for (const v of values) {
6072
if (v) {
6173
const link = parseLink(v, realBaseUrl);

‎test/fixtures/twittercard/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<head>
3+
<meta name="twitter:card" content="summary"></meta>
4+
<meta name="twitter:creator" content="@justinbeckwith" />
5+
<meta name="twitter:site" content="@justinbeckwith" />
6+
<meta property="og:url" content="http://fake.local/" />
7+
<meta property="og:title" content="A Twitter for My Sister" />
8+
<meta property="og:description" content="In the early days, Twitter grew so quickly that it was almost impossible to add new features because engineers spent their time trying to keep the rocket ship from stalling." />
9+
<meta property="og:image" content="http://fake.local" />
10+
</head>
11+
</html>

‎test/test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,12 @@ describe('linkinator', () => {
445445
const err = results.links[0].failureDetails![0] as Error;
446446
assert.ok(/Nock: Disallowed net connect for/.test(err.message));
447447
});
448+
449+
it('should scan links in <meta content="URL"> tags', async () => {
450+
const scope = nock('http://fake.local').head('/').reply(200);
451+
const results = await check({path: 'test/fixtures/twittercard'});
452+
assert.ok(results.passed);
453+
scope.done();
454+
assert.strictEqual(results.links.length, 2);
455+
});
448456
});

0 commit comments

Comments
 (0)
Please sign in to comment.