seo_defects_sb
v1.0.1
Published
seo defects detector assignment
Readme
SEO Defect Parser
A seo defect parser. The parser could detect the seo defect rule.
Installation
npm install seo_defects_sbExample
- Check the file
test.htmlby the default rule, and output toSTDOut
const seoDefects = require('seo_defects_sb');
const parser = new seoDefects.SeoParser();
parser.init(
seoDefects.fileReadableStream('test.html'),
seoDefects.consoleWritableStream()
);
parser.parse(function() {
console.log('parse end');
})API Readable/Writable Stream
fileReadableStream(filename)
/**
* Create readable file stream
* @param {string} filename input file name
* @return {ReadableStream} stream
*/consoleReadableStream()
/**
* Create stdin stream
* @return {ReadableStream} process stdin
*/fileWritableStream(filename)
/**
* Create writable file stream
* @param {string} filename output file name
* @return {WritableStream} stream
*/consoleWritableStream()
/**
* Create stdout stream
* @return {WritableStream} process stdin
*/SeoParser
SeoParser.init(input, output, rules=null)
/**
* init input and output streams, defect rules
* @param {readableStream} input
* @param {WritableStream} output
* @param {Rule} rules, if null, check by default rules
* @throws {SeoParserInputError} invalid typeof streams
*/SeoParser.parse(completeCb=null)
/**
* Start to parse
* @param {function} completeCb complete callback
*/Default rules:
- Detect if any <img /> tag without alt attribute(imgWithoutAlt())
- Detect if any <a /> tag without rel attribute(aWithoutRel())
- In <head> tag
- Detect if header doesn’t have <title> tag(inHeadNoTitle())
- Detect if header doesn’t have <meta name=“descriptions” … /> tag(inHeadMetaNameIsDescriptions())
- Detect if header doesn’t have <meta name=“keywords” … /> tag(inHeadMetaNameIsKeywords())
- Detect if there’re more than {num} <strong> tag in HTML(strongGreaterNum(num))
- Detect if a HTML have more than one <H1> tag(h1Unique())
Rule
A object decides whether to write the error message to the writable stream.
Rule(condition)
/**
* Constructor
* @param {Cond} condition, the condition to be checked
*/Rule.equal(val), Rule.less(val), Rule.greater(val)
/**
* Check the num of counts which satify the condition
* @param {number} val
* @return {Rule} rule object
*/Rule.msg(val)
/**
* Set error message
* @param {string} val, error message to be written
*/Cond
Cond.tag(tag)
/**
* Set html tag, the condition want to check
* @param {string} tag
* @return {Cond} condition object
*/Cond.attrib(name)
/**
* Set attribute, the condition want to check
* @param {string} name
* @return {Cond} condition object
*/Cond.exist(val=null), Cond.notExist(val=null)
/**
* Set attribute value(default null), the condition want to check
* @param {string} val, attribute value
* @return {Cond} condition object
*/Cond.has(cond)
/**
* A nested condition pattern
* @param {Cond} cond
* @return {Cond} condition object
*/User-defined Rule
- Detect if any <meta /> tag with name attribute, and its value equal to 'test' exists.
let condObj = new Cond();
condObj = condObj.tag('meta').attrib('name').exist('test');
const rule = new Rule(condObj).greater(0);- In <p> tag, detect if <span> tag with style attribute does not exists.
const condObj = new Cond();
let parentCondObj = new Cond();
parentCondObj = parentCondObj.tag('p').has(
condObj.tag('span').attrib('style').exist());
const rule = new Rule(parentCondObj).equal(0);
