1.0.0 • Published 6 years ago

seo-rule-detector v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

seo-detector

This package can validate pre-defined or customized SEO rules for web pages. The detector can handle streams or file input and supports streams, file, or console for output.

Installation

npm install --save seo-rule-detector

Usage

Here's an example in NodeJS using Promises with async/await:

  • With NodeJS (>= 7.6)
const Detector = require('seo-detector');
const detector = new Detector();

const rule = [
        predefined.detectATagWithRelAttr, 
        predefined.detectImgTagWithAltAttr,
        predefined.detectTitleInHead,
        predefined.detectMetaWithDescriptionInHead,
        predefined.detectMetaWithKeywordsInHead,
        predefined.detectStrongTag,
        predefined.detectH1Tag,
        {
            rule: config.ruleCategory.CHILD_TAG_WITH_ATTRIBUTE, 
            tag: 'meta', 
            attr: 'name', 
            value: 'robots'
        }
];

(async () => {
    await detector.detect(rule);
})();
  • With NodeJS (< 7.6)
const async = require('asyncawait/async');
const await = require('asyncawait/await');
const Detector = require('seo-detector');
const detector = new Detector();

const execute = async(() => {
    const Detector = require('seo-detector');
    const detector = new Detector();

    const rule = [
        predefined.detectATagWithRelAttr, 
        predefined.detectImgTagWithAltAttr,
        predefined.detectTitleInHead,
        predefined.detectMetaWithDescriptionInHead,
        predefined.detectMetaWithKeywordsInHead,
        predefined.detectStrongTag,
        predefined.detectH1Tag,
        {
            rule: config.ruleCategory.CHILD_TAG_WITH_ATTRIBUTE, 
            tag: 'meta', 
            attr: 'name', 
            value: 'robots'
        }
    ];
    await(detector.detect(rule));
});

execute()
.then(function() {
    console.log('success');
})
.catch(function(err) {
    console.log(err);
});

Check the result on Console

There are 3 <a> tag without rel attribute.
There are 1 <img> tag without alt attribute.
Valid
Valid
There is no <meta> tag with name attribute and keywords value.
Valid
Valid
There is no <meta> tag with name attribute and robots value.
success

Input Settings

You can detect Html file from a URL or a readable stream. The default input is an example html in static/.

  • File path
input: {
    mode: config.inputMode.FILE,
    source: <file path>
}
  • Readable Stream
input: {
    mode: config.inputMode.STREAM,
    source: <readable stream>
}

Output Settings

The detection result can be exported into a file, a writable stream, or console. The default output is console mode.

  • File path
output: {
    mode: config.outputMode.FILE,
    destination: <file path>
}
  • Writable Stream
output: {
    mode: config.outputMode.STREAM,
    destination: <writable stream>
}
  • Console
output: {
    mode: config.outputMode.CONSOLE,
    destination: ''
}

Rules Configuration

The detector can detect with multiple rules pre-defined or customized by yourself.

Pre-defined Rules

Here is a list of pre-defined currently available. It's defined in predefined.js file. Add the rule keys to a rule array, and pass to a detector.

Rule keyDescription
detectATagWithRelAttrDetect if any tag without alt attribute
detectImgTagWithAltAttrDetect if any tag without rel attribute
detectTitleInHeadIn tag, detect if header doesn’t have tag
detectMetaWithDescriptionInHeadIn tag, detect if header doesn’t have <meta name=“descriptions” ... /> tag
detectMetaWithKeywordsInHeadIn tag, detect if header doesn’t have <meta name=“keywords” ... /> tag
detectStrongTagDetect if there’re more than 15 <strong> tag in HTML
detectH1TagDetect if a HTML have more than one <H1> tag

Customized Rules

There are three types of rule can be customized. 1. Detect if any specific tag without a defined attribute.

{
    rule: config.ruleCategory.TAG_WITH_ATTRIBUTE, 
    tag: 'a', 
    attr: 'rel'
}
  1. Detect if any specific child tag without a defined attribute and a defined value in a specific parent tag. (The keys parent, attr, and value are optional.)
{
    rule: config.ruleCategory.CHILD_TAG_WITH_ATTRIBUTE, 
    parent: 'head', 
    tag: 'meta', 
    attr: 'name', 
    value: 'keywords'
}
  1. Detect if the number of a specific tag in the whole contents is greater than a defined number.
{
    rule: config.ruleCategory.TAG_LIMIT_COUNT, 
    tag: 'strong', 
    count: 15
}

Test

The tests run the SEO detector with mocha for several sample HTMLs in test/ and output the results for each mode.

To run the tests:

npm test