seo-defects v1.0.0
Synopsis
seo-defects
let you create rules to detect SEO defects in a HTML, and report accordingly.
Features
- DSL to create condtions on tags and their attributes.
- DSL to create rules to detect and report SEO defects.
- Work on files and streams.
Installation
Install it like a npm module:
npm install seo-defects
Usage
Import seo-defects
and get API classes:
var seodd = require('seo-defects');
var Detector = seodd.Detector;
var Rule = seodd.Rule;
var Condition = seodd.Condition;
DSL to create conditions
Use Condition
class to create conditions for tags.
Examples:
- a
meta
tag has attributename="description"
:
Condition().tag('meta').attr('name').is('description')
- an
img
tag has no attributealt
:
Condition().tag('img').attr('alt').notExist()
- a
head
tag doesn't have a<meta name="descriptions" ...>
tag:
Condition().tag('head').notHave(Condition().tag('meta').attr('name').is('descriptions'))
DSL to create rules
A Rule
has:
a
Condition
:Detector
will count the number of tags matched with this condition.many messages to report, based on the number of matched values.
%NUM%
is the placeholder thatDetector
will insert the number of matched values in to reported messages.
Examples:
- report
This HTML without <title> tag
if theheader
tag doesn't havetitle
tag:
Rule().cond(Condition().tag('head').notHave(Condition().tag('title')))
.greaterThan(0).msg('This HTML without <title> tag');
- report
There are %NUM% <img> tag without alt attribute
:
Rule().cond(Condition().tag('img').attr('alt').notExist())
.greaterThan(0).msg('There are %NUM% <img> tag without alt attribute');
Pre-defined rules:
SEODefectDetector.imgWithoutAlt
SEODefectDetector.aWithoutRel
SEODefectDetector.headerNoTitle
SEODefectDetector.headerNoMetaDescriptions
SEODefectDetector.headerNoMetaKeywords
SEODefectDetector.moreThan15Strong
SEODefectDetector.moreThan1H1
Detector
Create a Detector
with input, output and the list of rules to apply checking. Input and output can be filenames or streams.
Call method detect
to perform checking for SEO defects. Register a callback for event complete
when the checking is done.
Examples:
var detector = new Detector('input1', process.stdout, [
Detector.imgWithoutAlt,
Detector.aWithoutRel,
Detector.headerNoTitle,
Detector.headerNoMetaDescription,
Detector.headerNoMetaKeywords,
Detector.moreThan15Strong,
Detector.moreThan1H1,
Rule().cond(Condition().tag('meta').attr('name').is('robots'))
.equal(0).msg('This HTML doesnt have meta robots')
]);
detector.detect().on('complete', function() {
console.log('\ndone');
});
API
Condition Instance Methods
tag()
function Condition#tag(type)
@param type {String} Tag type (eg: head, meta, ...).
This method will create a condition based on the tag provided. Then other methods can be chained to each other.
attr()
function Condition#attr(name)
@param name {String} Attribute name (eg: id, rel, alt, ...).
is(), isNot()
function Condition#is(value)
@param value {String} The value of the attribute specified in the `attr` clause.
function Condition#isNot(value)
@param value {String} The value of the attribute specified in the `attr` clause.
exist(), notExist()
function Condition#exists()
function Condition#notExist()
has(), notHave()
function Condition#has(elem)
@param elem {Condition} The child tag condition that the current tag must have.
function Condition#notHave(elem)
@param elem {Condition} The child tag condition that the current tag must not have.
descend()
function Condition#descend(elem)
@param elem {Condition} Create a new condition whose main tag that Detector counted for is this tag condition.
Rule Instance Methods
cond()
function Rule#cond(condition)
@param condition {Condition} Set the condition for this rule.
This method will create a rule based on the condition provided. Then other methods can be chained to each other.
msg()
function Rule#msg(message)
@param message {String} The message for current comparison.
equal(), lessThan(), greaterThan()
function Rule#equal(value)
@param value {Number} The value to be used in an equal comparison.
function Rule#lessThan(value)
@param value {Number} The value to be used in an less-than comparison.
function Rule#greaterThan(value)
@param value {Number} The value to be used in an greater-than comparison.
Detector Constructor
function Detector(input, output, rules)
@param input {String,Readable} The input source contains the HTML.
@param output {String,Writable} The output.
@param rules {Array} The array of rules.
Detector Instance Methods
detect()
function Detector#detect()
This method performs the checking and then reports to the output stream. When done, it emits the complete event.
License
Copyright (c) 2018 Vu To vutu1211@gmail.com
7 years ago