npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@klevn/seo-defects

v1.0.9

Published

A Node.js module to scan a HTML input and output all of the SEO defects

Downloads

5

Readme

SEO Defects Detector

Build Status

A module to scan a HTML input and output all of the SEO defects

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Installation

Install SEO defects globally using npm:

npm install -g @quanglight/seo-defects

Install on npm locally and save it in your package's package.json file:

npm install --save-dev @quanglight/seo-defects

Example

To get familiar with usage of the module, run an example after installation in terminal:

# cd to seo-defects installed folder inside node_modules folder
npm run-script example

Output should be printed to terminal like below:

This HTML has 3 <img> tag without alt attribute
This HTML has 1 <a> tag without rel attribute
This HTML has <head> tag without <title> tag
This HTML has <head> tag without <meta name='descriptions'/> tag
This HTML has <head> tag without <meta name='keywords'/> tag
This HTML has more than one <h1> tag

Pre-defined SEO rules

This module provides a list of pre-defined SEO rules with ability to override/add new rule easily:

1. Detect if there are any <img /> tags without alt attribute
2. Detect if there are any <a /> tags without rel attribute
3. In <head> tag
   i. Detect if there is any header that doesn’t have <title> tag
   ii. Detect if there is any header that doesn’t have <meta name=“descriptions” ... />
       tag
   iii. Detect if there is any header that doesn’t have <meta name=“keywords” ... /> tag
4. Detect if there are 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.

Usage

The input can be either:
  • A HTML file (User is able to config the input path)
  • A HTML string
  • Node Readable Stream
The output can be either:
  • A file (User is able to config the output destination)
  • Node Writable Stream
  • Console
  • A string as plain text
Basic usage
Input file, output to console
const Detector = require('@quanglight/seo-defects');
let exampleHtmlFile = "/an_absolute_path_to/file.html";

// Input file, output to console
let detector = new Detector();
detector.scanFromFile(exampleHtmlFile);
Input HTML string, output to string and print it to console
const Detector = require('@quanglight/seo-defects');
let exampleHtml = "<strong>Text</strong>";

let detector = new Detector("string");
detector.proceedScan(exampleHtml);
let stringResult = detector.outputResult();
console.log(stringResult);
Input file with override rule and add new rule, output to console
const Detector = require('@quanglight/seo-defects');
// Custom rule could be a json object, json string or even json file
let customRule = require('/an_absolute_path_to/custom-rules.json');
let exampleHtmlFile = "/an_absolute_path_to/file.html";

// Input file with override rule and add new rule, output to console
detector = new Detector("console", JSON.stringify(customRule));
detector.scanFromFile(exampleHtmlFile);
Input file, output to file with configurable path
const Detector = require('@quanglight/seo-defects');
let outputFilePath = "/an_absolute_file_path/";
let exampleHtmlFile = "/an_absolute_path_to/file.html";

// Input file, output to file with configurable path
detector = new Detector("file", {}, outputFilePath);
detector.scanFromFile(exampleHtmlFile);
Input readable stream, output to console
const Detector = require('@quanglight/seo-defects');
let exampleHtmlFile = "/an_absolute_path_to/file.html";

// Input readable stream, output to console
detector = new Detector();
detector.scanFromStream(require('fs').createReadStream(exampleHtmlFile));
Input file, output to writable stream with allow to access writable stream
const Detector = require('@quanglight/seo-defects');
let exampleHtmlFile = "/an_absolute_path_to/file.html";

// Input file, output to writable stream
detector = new Detector("stream");
let writeStream = detector.outputWriteStream;
writeStream.write = function(data) {
    console.log('Test override write() function');
    console.log(data);
};
detector.scanFromFile(exampleHtmlFile);

Rule structure

Rule is a JSON which can be an object, string or file. To define a rule, it must has structure as below:

With current structure, it support 3 types of rule for SEO defects detection:

  • Detect tag has property
  • Detect tag contains child tag
  • Compare number of tag with given positive number

Below are the way to define a rule for each type:

Detect tag has property
{
  "img": {
    "tag": "img",
    "conditions": [
      {
        "attribute": {
          "assertion": "to.have.property",
          "assertValue": "alt"
        },
        "defectMessage": "This HTML has %d <img> tag without alt attribute"
      }
    ]
  }
}
Detect tag contains child tag
{
  "head": {
      "tag": "head",
      "conditions": [
        {
          "children": {
            "assertion": "to.containSubset",
            "assertValue": [{
              "name": "title"
            }]
          },
          "defectMessage": "This HTML has <head> tag without <title> tag"
        },
        {
          "children": {
            "assertion": "to.containSubset",
            "assertValue": [{
              "name": "meta",
              "attribs": {
                "name": "description"
              }
            }]
          },
          "defectMessage": "This HTML has <head> tag without <meta name='descriptions'/> tag"
        }
      ]
    }
}
Compare number of tag with given positive number
{
  "h1": {
      "tag": "h1",
      "conditions": [
        {
          "itself": {
            "assertion": "length.to.be.below",
            "assertValue": 2
          },
          "defectMessage": "This HTML has more than one <h1> tag"
        }
      ]
    }
}

Tests

Run this command in your package after installation to get test result and its coverage information:

npm test

Credits

The main operation mechanism of this module was inspired by SEO Cop module created by Maulik Patel with all new source code structure intend to improve ability of maintenance and contribution.

License

This project is licensed under the MIT License - see the LICENSE file for details.