1.0.1 • Published 7 years ago
seo_defects_sb v1.0.1
SEO Defect Parser
A seo defect parser. The parser could detect the seo defect rule.
Installation
npm install seo_defects_sb
Example
- Check the file
test.html
by 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: 1. Detect if any \<img /> tag without alt attribute(imgWithoutAlt()) 2. Detect if any <a \/> tag without rel attribute(aWithoutRel()) 3. In \ tag
- Detect if header doesn’t have \ 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} \ tag in HTML(strongGreaterNum(num))
- Detect if a HTML have more than one \ 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);