Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: scan opengraph and twittercard style meta links #217

Merged
merged 1 commit into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const linksAttr = {
icon: ['command'],
longdesc: ['frame', 'iframe'],
manifest: ['html'],
content: ['meta'],
poster: ['video'],
pluginspage: ['embed'],
pluginurl: ['embed'],
Expand Down Expand Up @@ -56,6 +57,17 @@ export function getLinks(source: string, baseUrl: string): ParsedUrl[] {
) {
return;
}

// Only for <meta content=""> tags, only validate the url if
// the content actually looks like a url
if (element.tagName === 'meta' && element.attribs['content']) {
try {
new URL(element.attribs['content']);
} catch (e) {
return;
}
}

for (const v of values) {
if (v) {
const link = parseLink(v, realBaseUrl);
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/twittercard/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<meta name="twitter:card" content="summary"></meta>
<meta name="twitter:creator" content="@justinbeckwith" />
<meta name="twitter:site" content="@justinbeckwith" />
<meta property="og:url" content="http://fake.local/" />
<meta property="og:title" content="A Twitter for My Sister" />
<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." />
<meta property="og:image" content="http://fake.local" />
</head>
</html>
8 changes: 8 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,12 @@ describe('linkinator', () => {
const err = results.links[0].failureDetails![0] as Error;
assert.ok(/Nock: Disallowed net connect for/.test(err.message));
});

it('should scan links in <meta content="URL"> tags', async () => {
const scope = nock('http://fake.local').head('/').reply(200);
const results = await check({path: 'test/fixtures/twittercard'});
assert.ok(results.passed);
scope.done();
assert.strictEqual(results.links.length, 2);
});
});