-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 2 new rules headingsStructureRule & metaDescriptionRule
- Loading branch information
Denisoed
committed
Mar 31, 2024
1 parent
b5a90dd
commit 3cc99ac
Showing
6 changed files
with
84 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |