gordon_lin-sb-seo
v1.0.8
Published
seo challenge
Downloads
2
Readme
SEO challenge
Usage
var { seo, exists, nestedExists, occurrence } = require('gordon_lin-sb-seo')
let html = __basedir + '/resources/html/input.html'
seo.readFile(html)
.createCriteria()
.useDefault()
.execute().then(
ouput => ouput.printf()
)
Output
<head> dosen't have <title /> tag
<head> dosen't have <meta name="descriptions" content="anycontent..." /> tag
<head> dosen't have <meta name="keywords" /> tag
There are 3 <img> tag without alt attribute.
There are 1 <a> tag without rel attribute.
This HTML has less than 15 <strong> tag.
This HTML has more than 1 <h1> tag.
Read/Write File or output on Console
- run execute() first, and get Output object from Promise.
- you can chain printf() or writeFile() at will.
- File Input/Output
- can read file from file path or Readable stream
- write file can be file path or WriteStream
let html = __basedir + '/resources/html/input.html'
let seoOut = __basedir + '/resources/html/output.html'
let readStream = fs.createReadStream(html, {
flags: 'r',
encoding: 'utf8',
fd: null,
mode: 0o666,
autoClose: true,
highWaterMark: 64 * 1024
})
let writeStream = fs.createWriteStream(seoOut, {
flags: 'w',
encoding: 'utf8',
fd: null,
mode: 0o666,
autoClose: true
})
seo.readFile(html)
.createCriteria()
.useDefault()
.execute().then(
ouput => ouput.writeFile(writeStream)
)
- Console Output
seo.readFile(html)
.createCriteria()
.useDefault()
.execute().then(
ouput => ouput.printf()
)
Define Constraints
p.s: run createCriteria() to create constraint block in background and then invoke useDefault() or addConstraint() to define rules.
Pre-defined Rules
- useDefault() you still can chain your own rules to the end by addConstraint()
1. Detect if any <img /> tag without alt attribute
2. Detect if any <a /> tag without rel attribute
3. In <head> tag
1) Detect if header doesn’t have <title> tag
2) Detect if header doesn’t have <meta name=“descriptions” … /> tag
3) Detect if header doesn’t have <meta name=“keywords” … /> tag
4. Detect if there’re more than 15 <strong> tag in HTML (15 is a value should be
configurable by user)
5. Detect if a HTML have more than one <H1> tag.
seo.readFile(html)
.createCriteria()
.useDefault()
.execute().then(
ouput => ouput.printf()
)
- Default with 5 pre-defined rules
seo.prototype.useDefault = function() {
this.addConstraint(new nestedExists({
tag: "head",
exists: {
tags: [{
tag: "title"
}, {
tag: "meta",
attrs: [{
attr: "name",
value: "descriptions"
}, {
attr: "content",
value: "anycontent..."
}]
}, {
tag: "meta",
attrs: [{
attr: "name",
value: "keywords"
}]
}]
}
}))
.addConstraint(new exists({
tag: "img",
exists: {
attrs: [{
attr: "alt"
}]
}
}))
.addConstraint(new exists({
tag: "A",
exists: {
attrs: [{
attr: "rel"
}]
}
}))
.addConstraint(new occurrence({
tag: "strong",
occurrence: {
gt: 15
}
}))
.addConstraint(new occurrence({
tag: "h1",
occurrence: {
gt: 1
}
}))
return this;
}
Contraints Class
- exists: detect multiple attributes within single tag is exist or not.
{
tag: "img",
exists: { attrs: [{ attr: "alt" }] }
}
e.g. Detect if any <img/> tag without alt attribute
- nestedExists: detect nested tags and attributes within target tag is exist or not.
{
tag: "head",
exists: {
tags: [{
tag: "meta",
attrs: [{
attr: "name",
value: "descriptions"
}]
}]
}
}
e.g. In tag, detect if header doesn’t have <meta name=“descriptions” … /> tag
- ocurrence: detect ocurrence count of tag or attribute is greater or less than digits.
{
tag: "strong",
occurrence:{gt:15}
}
e.g. Detect if there’re more than 15 <strong> tag in HTML
Develop Custom Constraint Class
SeoRuleHandler.prototype.onTag = function(name) {}
SeoRuleHandler.prototype.onAttrs = function(attr) {}
SeoRuleHandler.prototype.onTagClose = function(name) {}
SeoRuleHandler.prototype.printf = function() {}
- Constructor naming must be start with 'SeoRule'.
- implement onTag, onAttrs, onTagClose, printf function
- onTag(), onAttrs(), onTagClose() will be invoked in sequence. e.g. will be parsing to onTag(name)=>meta and onAttrs(attr)=>{name:'abc', value:'xyz'}
- printf() will be invoked at the last of the execute().