npm.io
1.0.0 • Published 8 years ago

seo-rule-detector

Licence
MIT
Version
1.0.0
Deps
2
Size
43 kB
Vulns
0
Weekly
0

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 key Description
detectATagWithRelAttr Detect if any tag without alt attribute
detectImgTagWithAltAttr Detect if any tag without rel attribute
detectTitleInHead In tag, detect if header doesn’t have tag</td> </tr> <tr> <td><strong>detectMetaWithDescriptionInHead</strong></td> <td>In <head> tag, detect if header doesn’t have <meta name=“descriptions” ... /> tag</td> </tr> <tr> <td><strong>detectMetaWithKeywordsInHead</strong></td> <td>In <head> tag, detect if header doesn’t have <meta name=“keywords” ... /> tag</td> </tr> <tr> <td><strong>detectStrongTag</strong></td> <td>Detect if there’re more than 15 <strong> tag in HTML</td> </tr> <tr> <td><strong>detectH1Tag</strong></td> <td>Detect if a HTML have more than one <H1> tag</td> </tr> </tbody></table> <h6 id="user-content-customized-rules" data-level="4"><a href="#user-content-customized-rules">Customized Rules</a></h6> <p>There are three types of rule can be customized.</p> <ol> <li>Detect if any specific tag without a defined attribute.</li> </ol> <pre class="hljs"><code class="language-js">{ <span class="hljs-attr">rule</span>: config.<span class="hljs-property">ruleCategory</span>.<span class="hljs-property">TAG_WITH_ATTRIBUTE</span>, <span class="hljs-attr">tag</span>: <span class="hljs-string">'a'</span>, <span class="hljs-attr">attr</span>: <span class="hljs-string">'rel'</span> }</code></pre> <ol start="2"> <li>Detect if any specific child tag without a defined attribute and a defined value in a specific parent tag. (The keys <code>parent</code>, <code>attr</code>, and <code>value</code> are optional.)</li> </ol> <pre class="hljs"><code class="language-js">{ <span class="hljs-attr">rule</span>: config.<span class="hljs-property">ruleCategory</span>.<span class="hljs-property">CHILD_TAG_WITH_ATTRIBUTE</span>, <span class="hljs-attr">parent</span>: <span class="hljs-string">'head'</span>, <span class="hljs-attr">tag</span>: <span class="hljs-string">'meta'</span>, <span class="hljs-attr">attr</span>: <span class="hljs-string">'name'</span>, <span class="hljs-attr">value</span>: <span class="hljs-string">'keywords'</span> }</code></pre> <ol start="3"> <li>Detect if the number of a specific tag in the whole contents is greater than a defined number.</li> </ol> <pre class="hljs"><code class="language-js">{ <span class="hljs-attr">rule</span>: config.<span class="hljs-property">ruleCategory</span>.<span class="hljs-property">TAG_LIMIT_COUNT</span>, <span class="hljs-attr">tag</span>: <span class="hljs-string">'strong'</span>, <span class="hljs-attr">count</span>: <span class="hljs-number">15</span> }</code></pre> <h4 id="user-content-test" data-level="2"><a href="#user-content-test">Test</a></h4> <p>The tests run the SEO detector with mocha for several sample HTMLs in <code>test/</code> and output the results for each mode.</p> <p>To run the tests:</p> <pre class="hljs"><code class="language-bash">npm <span class="hljs-built_in">test</span></code></pre>