Skip to content

Commit

Permalink
Added 2 new rules headingsStructureRule & metaDescriptionRule
Browse files Browse the repository at this point in the history
  • Loading branch information
Denisoed committed Mar 31, 2024
1 parent b5a90dd commit 3cc99ac
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
40 changes: 22 additions & 18 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<head>
<title>D</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta property="og:url">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="My site">
<link rel="canonical" href="">
</head>
<body>
<h1>Title 1</h1>
<h1>Title 2</h1>
<h3>Title 3</h3>
<p>Lorem ipsum, dolor sit <strong>amet</strong> consectetur adipisicing <strong>elit</strong>. Nesciunt recusandae <strong>animi</strong> esse sunt enim iusto repellendus expedita sit provident delectus?</p>
<img src="icon.png" alt="Alt">
<img src="icon2.png">
<img src='' alt=''>
<a href="#">Link</a>
</body>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta property="og:url" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="My site" />
<link rel="canonical" href="" />
</head>
<body>
<h1>Title 1</h1>
<h3>Title 3</h3>
<h2>Title 2</h2>
<p>
Lorem ipsum, dolor sit <strong>amet</strong> consectetur adipisicing
<strong>elit</strong>. Nesciunt recusandae <strong>animi</strong> esse
sunt enim iusto repellendus expedita sit provident delectus?
</p>
<img src="icon.png" alt="Alt" />
<img src="icon2.png" />
<img src="" alt="" />
<a href="#">Link</a>
</body>
</html>
23 changes: 23 additions & 0 deletions src/rules/HeadingsStructureRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function headingsStructureRule(dom) {
return new Promise(resolve => {
const headings = dom.window.document.querySelectorAll(
'h1, h2, h3, h4, h5, h6'
);
let previousLevel = 0;
headings.forEach(heading => {
const level = parseInt(heading.tagName.substring(1), 10);
if (level < previousLevel) {
resolve(
`Incorrect headings structure: ${heading.tagName} follows ${
previousLevel ? 'H' + previousLevel : 'no heading'
}.`
);
}
previousLevel = level;
});

resolve(null);
});
}

export default headingsStructureRule;
2 changes: 1 addition & 1 deletion src/rules/MetaBaseRule.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { META_BASE_RULE } from './config/defaults';

function metaBaseRule(dom, options = { list: [] }) {
function metaBaseRule(dom, options = { names: [] }) {
return new Promise(resolve => {
const report = [];
const list = options?.names || META_BASE_RULE.names;
Expand Down
28 changes: 28 additions & 0 deletions src/rules/MetaDescriptionRule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { META_DESCRIPTION_LENGTH_RULE } from './config/defaults';

function metaDescriptionRule(dom, options) {
return new Promise(resolve => {
const description = dom.window.document.querySelector(
"meta[name='description']"
);
if (!description) {
resolve('Meta description tag is missing.');
}
const descriptionTags =
dom.window.document.querySelectorAll("meta[name='description']") || 0;
const descriptionLength = description.getAttribute('content').length || 0;
const min = options?.min || META_DESCRIPTION_LENGTH_RULE.min;
const max = options?.max || META_DESCRIPTION_LENGTH_RULE.max;
if (descriptionTags.length > 1) {
resolve('More than one meta description tag found.');
}
if (descriptionLength < min || descriptionLength > max) {
resolve(
`The meta description length(${descriptionLength}) should be between ${min} and ${max} characters.`
);
}
resolve(null);
});
}

export default metaDescriptionRule;
5 changes: 5 additions & 0 deletions src/rules/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ export const TITLE_LENGTH_RULE = {
min: 30,
max: 60
};

export const META_DESCRIPTION_LENGTH_RULE = {
min: 160,
max: 300
};
6 changes: 5 additions & 1 deletion src/rules/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import titleLengthRule from './TitleLengthRule';
import metaDescriptionRule from './MetaDescriptionRule';
import imgTagWithAltAttributeRule from './ImgTagWithAltAttributeRule';
import aTagWithRelAttributeRule from './ATagWithRelAttributeRule';
import canonicalLinkRule from './CanonicalLinkRule';
import metaBaseRule from './MetaBaseRule';
import metaSocialRule from './MetaSocialRule';
import headingsStructureRule from './HeadingsStructureRule';

const defaultRules = {
titleLengthRule,
metaDescriptionRule,
imgTagWithAltAttributeRule,
aTagWithRelAttributeRule,
canonicalLinkRule,
metaBaseRule,
metaSocialRule
metaSocialRule,
headingsStructureRule
};

export default defaultRules;

0 comments on commit 3cc99ac

Please sign in to comment.