Skip to content

Commit

Permalink
Extended the OG checks
Browse files Browse the repository at this point in the history
  • Loading branch information
hisorange committed Jul 13, 2022
1 parent 18d2f66 commit ab026ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
31 changes: 28 additions & 3 deletions src/checks/og-tags.check.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { IChecker, IRecommendation, ICheckerContext } from '../interfaces';
import { IChecker, ICheckerContext, IRecommendation } from '../interfaces';

const documentation = 'https://docs.prerender.io/docs/open-graph';

export const check_og_tags: IChecker = ({ $, raw_html }: ICheckerContext) => {
const recommendations: IRecommendation[] = [];
let score_delta = 4.0;
let score_delta = 0;

// required fields
const ogTitle = $('meta[property="og:title"]').attr('content');
const ogType = $('meta[property="og:type"]').attr('content');
const ogImage = $('meta[property="og:image"]').attr('content');
const ogURL = $('meta[property="og:url"]').attr('content');

const penalty = -1;
const penalty = -2;
const optionalPenalty = -1;

if (!ogTitle) {
score_delta += penalty;

Expand All @@ -21,6 +23,8 @@ export const check_og_tags: IChecker = ({ $, raw_html }: ICheckerContext) => {
documentation: documentation,
scoreDelta: penalty,
});
} else {
score_delta += Math.abs(penalty);
}

if (!ogType) {
Expand All @@ -31,6 +35,8 @@ export const check_og_tags: IChecker = ({ $, raw_html }: ICheckerContext) => {
documentation: documentation,
scoreDelta: penalty,
});
} else {
score_delta += Math.abs(penalty);
}

if (!ogImage) {
Expand All @@ -41,6 +47,8 @@ export const check_og_tags: IChecker = ({ $, raw_html }: ICheckerContext) => {
documentation: documentation,
scoreDelta: penalty,
});
} else {
score_delta += Math.abs(penalty);
}

if (!ogURL) {
Expand All @@ -51,6 +59,23 @@ export const check_og_tags: IChecker = ({ $, raw_html }: ICheckerContext) => {
documentation: documentation,
scoreDelta: penalty,
});
} else {
score_delta += Math.abs(penalty);
}

// optional fields
const ogDescription = $('meta[property="og:description"]').attr('content');

if (!ogDescription) {
score_delta -= optionalPenalty;

recommendations.push({
description: 'Missing og:description meta tag',
documentation: documentation,
scoreDelta: penalty,
});
} else {
score_delta += Math.abs(optionalPenalty) * 2;
}

return {
Expand Down
9 changes: 5 additions & 4 deletions src/checks/og-tags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ describe('Og Tags Test', () => {
<meta property="og:type" content="video.movie" />
<meta property="og:url" content="https://www.imdb.com/title/tt0117500/" />
<meta property="og:image" content="https://ia.media-imdb.com/images/rock.jpg" />
<meta property="og:description" content="Stone fights nature" />
</head>
</html>
`;
const result = scorer(html, [check_og_tags]);

expect(result.score).toBe(4.0);
expect(result.score).toBe(10.0);
expect(result.recommendations.length).toBe(0);
});

test('should remove 1 point for each missing element', () => {
test('should remove 2 point for each missing element', () => {
const html = `
<html>
<head>
Expand All @@ -29,7 +30,7 @@ describe('Og Tags Test', () => {
</html>`;
const result = scorer(html, [check_og_tags]);

expect(result.score).toBe(2);
expect(result.recommendations.length).toBe(2);
expect(result.score).toBe(1);
expect(result.recommendations.length).toBe(3);
});
});

0 comments on commit ab026ed

Please sign in to comment.